dynamic MeshBuffer, Mesh, and MeshSceneNode?

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
phixuannhat
Posts: 15
Joined: Tue Jun 29, 2010 9:52 am

dynamic MeshBuffer, Mesh, and MeshSceneNode?

Post by phixuannhat »

I have this problem where I need to create/edit/delete Meshbuffers in a Mesh based on some dynamic data.

The problem is this, after I create the MeshSceneNodefrom the intial Mesh, I can only edit/delete existing MeshBuffers but not add new buffers from new data, otherwise it crashes when smgr->drawAll(). I've tried set the Mesh to EHM_DYNAMIC and used setDirty() after changing buffers, but no good.

Below are the simplified proj to reproduce this problem

Code: Select all

class TMesh
{
public:
	SMesh *Mesh;

	TMesh()
	{
		Mesh = new SMesh();
	}

	~TMesh()
	{
		Mesh->drop();
	}

	void init(array<SColor> &colors)
	{
		int i;
		for (i=0; i<colors.size(); i++)
			addstrip(colors[i], i);

		if(i < Mesh->getMeshBufferCount())
		{
			// clear the rest
			for (int j=i; j < Mesh->getMeshBufferCount(); j++)
				Mesh->getMeshBuffer(j)->drop();
			Mesh->MeshBuffers.erase(i, Mesh->getMeshBufferCount()-i);
		}
		Mesh->recalculateBoundingBox();
	}

	void addstrip(SColor color, u32 bufNum)
	{
		SMeshBuffer *buf = 0;
		if (bufNum < Mesh->getMeshBufferCount())
			buf = (SMeshBuffer*)Mesh->getMeshBuffer(bufNum);
		else
		{
			buf = new SMeshBuffer();
			Mesh->addMeshBuffer(buf);
		}
		buf->Vertices.set_used(3);

		buf->Vertices[0] = S3DVertex(vector3df(0, bufNum*100, 0), vector3df(0,1,0), color, vector2df(0,0));
		buf->Vertices[1] = S3DVertex(vector3df(0, bufNum*100, 100), vector3df(0,1,0), color, vector2df(0,0));
		buf->Vertices[2] = S3DVertex(vector3df(100, bufNum*100, 0), vector3df(0,1,0), color, vector2df(0,0));

		buf->Indices.set_used(3);
		buf->Indices[0] = 0;
		buf->Indices[1] = 1;
		buf->Indices[2] = 2;

		buf->recalculateBoundingBox();
	}

};

class MyEventReceiver : public IEventReceiver
{
public:
	// This is the one method that we have to implement
	virtual bool OnEvent(const SEvent& event)
	{
		// Remember whether each key is down or up
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

		return false;
	}

	// This is used to check whether a key is being held down
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}

	MyEventReceiver()
	{
		for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;
	}

private:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};

int main()
{
	MyEventReceiver rv;
	IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2du(800, 600), 32, false, false, false, &rv);
	if(!device)
		return 1;

	IVideoDriver *driver = device->getVideoDriver();
	ISceneManager *smgr = device->getSceneManager();
	device->setWindowCaption(L"Irrlicht Example for SMesh usage");

	TMesh mesh;
	array<SColor> colors;
	colors.push_back(SColor(255, rand()%256, rand()%256, rand()%256));
	colors.push_back(SColor(255, rand()%256, rand()%256, rand()%256));
	mesh.init(colors);

	// Add mesh to the scene graph
	IMeshSceneNode *meshnode = smgr->addMeshSceneNode(mesh.Mesh);
	meshnode->setMaterialFlag(EMF_LIGHTING, false);

	ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
	camera->setPosition(vector3df(-20.f, 150.f, -20.f));
	camera->setTarget(vector3df(0,0,0));

	while(device->run())
	{

		if(rv.IsKeyDown(irr::KEY_KEY_1))
		{
			colors.push_back(SColor(255, rand()%256, rand()%256, rand()%256));
 			mesh.init(colors);
		}
		else if(rv.IsKeyDown(irr::KEY_KEY_2))
		{
			if (colors.size() != 0)
			{
				colors.erase(colors.size()-1);
				mesh.init(colors);
			}
		}

		driver->beginScene(true, true);
		smgr->drawAll();
		driver->endScene();
	}

	device->drop();

	return 0;
}
Anyone can help? Thanks
phixuannhat
Posts: 15
Joined: Tue Jun 29, 2010 9:52 am

Post by phixuannhat »

any help?
pippy3
Posts: 155
Joined: Tue Dec 15, 2009 7:32 am

Post by pippy3 »

Do you know how to work your debugger? what is its output?
phixuannhat
Posts: 15
Joined: Tue Jun 29, 2010 9:52 am

Post by phixuannhat »

This is what i get from debugger

Code: Select all

Unhandled exception at 0x100184ef in DynamicMesh.exe: 0xC0000005: Access violation reading location 0xbaadf00d.
and it breaks at line smgr->drawAll

So I think when I add new buffer to the mesh, somehow some pointers are lost therefore smgr can't access and draw
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Post by REDDemon »

you have not dropped buf in "addStrip"

when you get it from mesh you have not to drop it.
But when you are creating a new buf with "new" you must drop it.

I let you the choice on how to handle that.

but this way i think is better:

Code: Select all

    buf = new SMeshBuffer(); 
    Mesh->addMeshBuffer(buf); 
    buf->drop();

i'm not sure this is the only problem with your code.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Post Reply