Hide a mesh buffer

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
carlosgc
Posts: 5
Joined: Wed Nov 14, 2007 5:26 pm

Hide a mesh buffer

Post by carlosgc »

Hi to all

I have a mesh with 8 mesh buffer and I need not to render (disable or hide) one of them at some point.

Is it possible?

Thanks in advance.
scippie
Posts: 26
Joined: Sat Oct 13, 2007 3:35 pm
Location: Belgium
Contact:

Post by scippie »

I don't know if it's possible, but I would probably take another path for this.

I would create 8 different meshes (with one mesh buffer) and put them in a SceneNode. Then I would create one empty SceneNode and put those 8 nodes in it as children.

You can then control the visibility, but also you can for example independantly transform/rotate/scale them.

Of course, this is slower because you need a little bit more overhead between the different mesh-renders, but I think that is probably the reason why it might not be possible to do what you ask.
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

You could set the alpha of all the meshbuffer's vertices to 0, of course you'll have to use the right material (with type EMT_TRANSPARENT_VERTEX_ALPHA).
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Make a new subclass of IMesh, one that has an extra function for setting the visibility of each buffer, then add it to the scene with addMeshSceneNode as usual. You don't have to touch the buffers themselves.

Here's some code that I just threw together, I didn't compile it so it probably has bugs, but it does show how easy it is to make your own custom static meshes-

Code: Select all

#include <IMesh.h>

namespace irr
{
namespace scene
{

	class CCustomMesh: public IMesh
	{
	public:

		//! constructor
		virtual CCustomMesh(IMesh* source) 
		{
			// grab references to mesh buffers, so they can't be deleted
			for (u32 i=0; i< source->getMeshBufferCount(); ++i)
			{
				Buffers.push_back(source->getMeshBuffer(i));
				VisibleBuffers.push_back(Buffers[i]);
				BufferVisibility.push_back(true);
				Buffers[i]->grab();
			}

			BoundingBox = source->getBoundingBox();

		}

		// destructor
		virtual ~CCustomMesh()
		{
			// release references to buffers
			for (u32 i=0; i < Buffers.size(); ++i)
				Buffers[i]->drop();
		}

		//! Returns the amount of (visible) mesh buffers.
		virtual u32 getMeshBufferCount() const 
		{
			return VisibleBuffers.size();
		}

		//! Returns pointer to a mesh buffer.
		virtual IMeshBuffer* getMeshBuffer(u32 nr) const
		{
			if (nr < VisibleBuffers.size())
				return VisibleBuffers[nr];
			else
				return 0;
		}

		//! Returns pointer to a mesh buffer which fits a material
		virtual IMeshBuffer* getMeshBuffer( const video::SMaterial &material) const
		{
			for (u32 i=0; i< VisibleBuffers.size(); ++i)
				if (VisibleBuffers.[i]->getMaterial() == material)
					return VisibleBuffers[i];

			return 0;
		}

		//! Returns an axis aligned bounding box of the mesh.
		/** \return A bounding box of this mesh is returned. */
		virtual const core::aabbox3d<f32>& getBoundingBox() const
		{
			return BoundingBox;
		}

		//! set user axis aligned bounding box
		virtual void setBoundingBox( const core::aabbox3df& box)
		{
			BoundingBox = box;
		}

		//! Sets a flag of all contained materials to a new value.
		virtual void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
		{
			for (u32 i=0; i< Buffers.size(); ++i)
				Buffers.[i]->getMaterial().Flags[flag] = newvalue;
		}


		// get real number of mesh buffers
		u32 getRealMeshBufferCount() const
		{
			return Buffers.size();
		}

		// hide or show a buffer
		void setVisible(u32 bufferNo, bool visible)
		{
			if (bufferNo < Buffers.size())
			{
				BufferVisibility[bufferNo] = visible;
				reset();
			}
		}

		// is this buffer visible?
		bool isVisible(u32 bufferNo)
		{
			if (bufferNo < Buffers.size())
				return BufferVisibility[bufferNo];
			else
				return false;
		}


	private:

		void reset()
		{
			VisibleBuffers.set_used(0);
			for (u32 i=0; i < Buffers.size(); ++i)
				if (BufferVisibility[i])
					VisibleBuffers.push_back(Buffers[i]);
		}

		core::aabbox3df BoundingBox;
		core::array<IMeshBuffer*> Buffers;
		core::array<IMeshBuffer*> VisibleBuffers;
		core::array<bool> BufferVisibility;

	};

} // end namespace scene
} // end namespace irr
you'd use it something like this-

Code: Select all

	IMesh *original = smgr->getMesh("mesh.obj")->getMesh(0);
	CCustomMesh *mesh = new CCustomMesh(original);
	smgr->addMeshSceneNode((IMesh*)(mesh));

	// main loop:
	// use mesh->getRealMeshBufferCount() and mesh->setVisible()

	// remember to drop it before dropping the device
	mesh->drop();
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Post Reply