Swapping the model of a mesh?

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
Dragfang
Posts: 21
Joined: Tue Mar 02, 2010 4:08 pm

Swapping the model of a mesh?

Post by Dragfang »

Hi, I'm having some troubles with an RTS-game that I'm making.

I'm using getMesh to load a model to a mesh, but I'm not able to then change that model to another model using the same mesh.

Code: Select all

//Declaration
IAnimatedMesh* objectMesh;
ISceneNode* objectNode;
ITriangleSelector* objectSelector;
ISceneNodeAnimator* objectAnim;

// Loading model and stuff
objectMesh = smgr->getMesh("media/townhall.obj");

if (objectMesh)
{
	objectNode = smgr->addOctreeSceneNode(objectMesh->getMesh(0));
}

if (objectNode)
{
    objectNode->setPosition(vector3df(350,1,100));
    objectSelector = smgr->createOctreeTriangleSelector(objectMesh->getMesh(0), objectNode, 128);
    objectNode->setTriangleSelector(objectSelector);
}

if (objectSelector)
{
	objectAnim = smgr->createCollisionResponseAnimator(objectSelector, camera, vector3df(30,50,30),vector3df(0,0,0),vector3df(0,10,0));
	camera->addAnimator(objectAnim);
	objectAnim->drop();
}

// Trying to change the model, fails
objectMesh = smgr->getMesh("media/townhall_unbuildable.obj");

It's something like that but a bit more complicated that I'm trying. In the example above my "objectMesh" gets the model townhall.obj, and not townhall_unbuildable.obj. Is it just me failing or is there any other function for swapping to another model?

Update: I tried setting the node to position (500,-5000,500) and then running addOctreeSceneNode again and that worked, but I got horrific FPS quite fast since I was stacking old models at position (500,-5000,500).
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

I don't really remember how exactly is this done but I know that you can find your answer in Mesh Viewer example :wink:
Working on game: Marrbles (Currently stopped).
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I guess first thing to learn is C++. Then you'll notice that assigning a new object to an existing pointer won't do anything to the previously stored oject. So first think about what should happen with the old object. And do that, e.g. call remove on it. Then get the new mesh, and add it to the sceen manager. Either as octree or as usual object.
Dragfang
Posts: 21
Joined: Tue Mar 02, 2010 4:08 pm

Post by Dragfang »

hybrid wrote:I guess first thing to learn is C++. Then you'll notice that assigning a new object to an existing pointer won't do anything to the previously stored oject. So first think about what should happen with the old object. And do that, e.g. call remove on it. Then get the new mesh, and add it to the sceen manager. Either as octree or as usual object.
Exactly, I wanna call a remove on it, I searched the API.net for such function but couldn't find one. So I'm not quite sure on how to do it.

Should this work or am I doing something wrong?

Code: Select all


IAnimatedMesh* objectMesh;
ISceneNode* objectNode;
ITriangleSelector* objectSelector;
ISceneNodeAnimator* objectAnim;

// Loading model and stuff
objectMesh = smgr->getMesh("media/townhall.obj");

if (objectMesh)
{
   objectNode = smgr->addOctreeSceneNode(objectMesh->getMesh(0));
}

if (objectNode)
{
    objectNode->setPosition(vector3df(350,1,100));
    objectSelector = smgr->createOctreeTriangleSelector(objectMesh->getMesh(0), objectNode, 128);
    objectNode->setTriangleSelector(objectSelector);
}

if (objectSelector)
{
   objectAnim = smgr->createCollisionResponseAnimator(objectSelector, camera, vector3df(30,50,30),vector3df(0,0,0),vector3df(0,10,0));
   camera->addAnimator(objectAnim);
   objectAnim->drop();
}

objectNode->remove();
objectMesh = smgr->getMesh("media/townhall_unbuildable.obj"); 
objectNode = smgr->addOctreeSceneNode(objectMesh->getMesh(0));

I've only used the remove(); for GUI-related stuff before, but would it work for a node like this? And is it the node that I should use remove(); on?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Yes, this would change the scene node (actually remove the old one and create the new one). Usage is correct. You might want to remove the mesh from the mesh cache as well, in case you don't ever use it again.
Dragfang
Posts: 21
Joined: Tue Mar 02, 2010 4:08 pm

Post by Dragfang »

hybrid wrote:Yes, this would change the scene node (actually remove the old one and create the new one). Usage is correct. You might want to remove the mesh from the mesh cache as well, in case you don't ever use it again.
Allright :) Thanks a lot!
Post Reply