Page 1 of 1

Similar, but different, objects being picked

Posted: Sun Jun 11, 2017 3:02 am
by LunaRebirth
Hello,

I'm having an issue where when I do something for an object that is the same mesh as another object, it does that thing for both.
So for example, I'm using the code:

Code: Select all

IAnimatedMeshSceneNode* Mouse::getMesh()
{
    core::line3d<f32> ray = Game::smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(Game::device->getCursorControl()->getPosition());
    core::vector3df intersection;
    core::triangle3df hitTriangle;
    scene::IAnimatedMeshSceneNode * selectedSceneNode =
        (IAnimatedMeshSceneNode*)Game::smgr->getSceneCollisionManager()->getSceneNodeAndCollisionPointFromRay(
                ray,
                intersection, // This will be the position of the collision
                hitTriangle, // This will be the triangle hit in the collision
                0, // This ensures that only nodes that we have
                        // set up to be pickable are considered
                0); // Check the entire scene (this is actually the implicit default)
 
    return selectedSceneNode;
}
to get the node at click position.

Then if I use code

Code: Select all

Game::smgr->getMeshManipulator()->setVertexColors(Mouse::getMesh(), SColor(255,255,255,255));
to set the color to white, it will set all objects that are similar, but not the same object, to white.

Similarly, if I update the bone position of one mesh, the bones of all the other meshes will also update.
So if I set the animation loop on one object to be a running motion, all other similar models will also do the run loop when they shouldn't.

Take this picture for example,
Only one head mesh should be green, not both.

If one starts walking, the other also starts walking in place.

Re: Similar, but different, objects being picked

Posted: Sun Jun 11, 2017 10:13 am
by CuteAlien
OK, I have to admit I'm somewhat shocked now. I always thought that was only complicated - didn't realize Irrlicht didn't allow copying of animated meshes at all. You can have different animations (you have to set setFrameLoop for each node to it's own values - if you set one to (0,0) it will not be animated even if other nodes are animated).

But creating real copies of animated meshes doesn't seem to be possible -simply a big missing feature in Irrlicht :-(
I had split my meshes in the past in a base-mesh and a mesh containing the skeleton animations - so I kinda automatically worked around that and didn't even notice this.

One workaround - kick the mesh out of the meshcache, but grab() it first so you keep it in memory. Then you can load it again and get a real copy. Second workaround - check-out new svn trunk of Irrlicht (will merge into ogl-es soon, probably later today). I just modified getMesh function -you can now pass an alternative cacheName. So you can create copies on loading. It's not perfect (it loads the mesh again each time), but at least somewhat better than first workaround.

And well - I guess creating copies of all the animated mesh formats with mesh-manipulator (or clone() functions for animated meshes) is a todo now. But sounds like a big task. Anyone else interested in doing that maybe?

edit: Oh my - I think your problem will still not be solved. Because you don't get the base-mesh - only the current copy of the animated mesh. Which would mean you would have to set colors each frame (very expensive...).
You probably have a skinned mesh - so I think (not tested) you have to cast to ISkinnedMesh and then use getMeshBuffers() to get the non-animated base-mesh. And that's the one you probably want to change.

Re: Similar, but different, objects being picked

Posted: Sun Jun 11, 2017 4:53 pm
by LunaRebirth
Hmm, that's too bad.

Unfortunately setting the animation for other objects did not solve the issue for all dudes walking in place.
It might be because I'm animating their bones with setJointMode(EJUOR_CONTROL) and controlling them each frame (setting bones a child of another bone crashes irrlicht) like so:

Code: Select all

for (int x = 0; x < this->children.size(); x++) {
      IAnimatedMeshSceneNode* m = this->children[x];
      for (int i = 0; i < this->b->getJointCount(); i++) {
          IBoneSceneNode* thisB = this->b->getJointNode(i);
          if (!thisB)
              continue;
          char* name = (char*)thisB->getName();
 
          IBoneSceneNode* mB = m->getJointNode(name);
          if (!mB)
              continue;
 
          mB->setPosition(thisB->getPosition());
          mB->setRotation(thisB->getRotation());
      }
}

Re: Similar, but different, objects being picked

Posted: Sun Jun 11, 2017 6:08 pm
by CuteAlien
Hm, could be. But if you want to change thiings you have to work with mesh-copies anyway. So better to use the new function (loading a new mesh for each copy you need). Can later (one day...) be optimized by copying meshes in memory after loading.

Re: Similar, but different, objects being picked

Posted: Sun Jun 11, 2017 9:58 pm
by LunaRebirth
Thanks, removing it from mesh cache seemed to work.

Re: Similar, but different, objects being picked

Posted: Wed Aug 16, 2017 4:42 am
by LunaRebirth
I hadn't have this issue continue to happen until last night.
Looks like it started to happen after I ran in release mode instead of debug.

If I Sleep(100) after setting a mesh, the issue is solved. Otherwise, it seems Irrlicht still can't tell two IAnimatedMeshSceneNode's apart when they use the same mesh, even after removing it from cache.

Re: Similar, but different, objects being picked

Posted: Wed Aug 16, 2017 9:30 am
by CuteAlien
Mesh-cache doesn't have any timers. Only timer I know about in regards to meshbuffers are hardware buffers on the graphic card. But you shouldn't have those yet directly after loading a mesh.

I also noticed btw that you can rename meshes in the mesh-cache, so no need to remove them.

Re: Similar, but different, objects being picked

Posted: Wed Aug 16, 2017 4:59 pm
by hendu
You really should use shaders to set such per-object colors and other visible attributes. No mesh duplication or costly memory operations.

Re: Similar, but different, objects being picked

Posted: Wed Aug 16, 2017 11:34 pm
by LunaRebirth
CuteAlien wrote:I also noticed btw that you can rename meshes in the mesh-cache, so no need to remove them.
Cool that worked. Removing only did after Sleep()'ing

Re: Similar, but different, objects being picked

Posted: Wed Aug 16, 2017 11:35 pm
by LunaRebirth
hendu wrote:You really should use shaders to set such per-object colors and other visible attributes. No mesh duplication or costly memory operations.
I'll definitely look into that. Thanks