Create a duplicate of a IAnimatedMesh instance.

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
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Create a duplicate of a IAnimatedMesh instance.

Post by christianclavet »

Hi, I would like to create nodes that use the same base mesh (I mean loaded from the same file). But use them differently as:

- Change their materials for each model used
- Use the command "useAnimationFrom" for each model used

Here is the code I'm using right now.

Code: Select all

IMesh* mesh = smgr->getMesh(meshFile);
nodeAnim = smgr->addAnimatedMeshSceneNode((IAnimatedMesh*)mesh,0,0x0010);
So from what I understand, when Irrlicht see that the filename is the same will use the same meshbuffers, so it would save memory because all the instances of that mesh will use the same buffers. But the downside of this is that since it uses the same meshbuffers, if the buffers are to be modified it will affect all instances, and that is what I would not like to have for my current project.

My question is: Is there a way to duplicate the "mesh" pointer content so when each node will not use the same meshbuffers? If possible, how can it be done?

If this is possible, then it will fix all my problems. As of now, I have to put all the animations required inside the same object. And I would really much like to load separate files with their animations and apply them on the character when needed. This will also allow me to create variation by code by changing the textures only.

Edit: Seen something with google but not sure. Could it be something like this?:

Code: Select all

 
IMesh* mesh = smgr->getMesh(meshFile);
IMesh* duplicate = new IMesh*(mesh);
nodeAnim = smgr->addAnimatedMeshSceneNode((IAnimatedMesh*)duplicate,0,0x0010);
I assume that If I'm using new, I will have to delete it.
Another important note I almost forgot, I need to duplicate (clone) a animated mesh. I seen that this is possible with static meshes (IMeshManipulator), but not seen anything with animated meshes.

EDIT2: I think I have found a "dirty trick" to make a duplicate of the IAnimatedMesh. I am manipulating the meshcache and renaming all files that are matching the one that I want to load. Not 100% sure it will use different meshbuffers but seem to be working. I will have to check by changing the animation from an instance of a model to see if the others will be affected to be totally sure. Here is my current code to check and rename the mesh filename in the mesh cache:

Code: Select all

//Attempt to create different meshbuffer when getting instances from the meshbuffer. "realfile" is the filename name.
    bool result=smgr->getMeshCache()->isMeshLoaded(realFile);
    if (result)
    {
        IAnimatedMesh* oldmesh = smgr->getMeshCache()->getMeshByName(realFile);
        smgr->getMeshCache()->renameMesh(oldmesh,realFile+"1"); //Only extend the filename with a "1" at the end.
        printf("This mesh %s is already loaded and in the mesh cache\n",realFile.c_str());
    }
I still think the cleaner way would be to do this (This method exist in IAnimatedMeshSceneNode(), but it only clone the NODE not the mesh):

Code: Select all

IAnimatedMesh* mesh->clone()
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Create a duplicate of a IAnimatedMesh instance.

Post by CuteAlien »

Sorry, I only have very old code which I haven't tested in years with any newer engine version (someone trying it had some troubles...). But maybe it still is working somewhat similar or helps getting there: http://irrlicht.sourceforge.net/forum/v ... 1&start=15
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Re: Create a duplicate of a IAnimatedMesh instance.

Post by christianclavet »

Thanks for posting this, I'll keep that as reference if my current trick is not working adequately, I will try to see how I could patch Irrlicht to have the "clone" method using your code as reference.
Post Reply