The problem with SceneNode parent

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • The problem with SceneNode parent

      Hi!

      Just want to share some problem i've been having with Scene Nodes and Parent hierarchy, as i wanted Nodes to have knowledge their parents, i have rearranged the code this way and use the boost:weak_ptr to solve a double deletion of the nodes, as weak_ptr wont delete the node if there are no references:

      Source Code

      1. class ISceneNode
      2. {
      3. ...
      4. virtual void SetParent(boost::weak_ptr<ISceneNode> parent)=0;
      5. virtual bool HasParent()=0;
      6. virtual shared_ptr<ISceneNode> getParent() const =0;
      7. ...
      8. }
      9. class SceneNode : public ISceneNode
      10. {
      11. boost::weak_ptr<ISceneNode> m_weakParentNode;
      12. ...
      13. virtual void SetParent(boost::weak_ptr<ISceneNode> parent){m_weakParentNode = parent;}
      14. virtual bool HasParent(){return (!m_weakParentNode.expired();}
      15. shared_ptr<ISceneNode> getParent() const {return m_weakParentNode.lock();}
      16. };
      17. bool SceneNode::VAddChild(shared_ptr<ISceneNode> kid)
      18. {
      19. bool ret = false;
      20. //Did the node ever had a parent or was it misleaded???
      21. //:(
      22. shared_ptr<ISceneNode> pRelativeParent(kid->HasParent()? kid->getParent() : m_weakParentNode.lock() );
      23. if(!pRelativeParent)
      24. {
      25. assert(pRelativeParent);
      26. return false;
      27. }
      28. if(pRelativeParent.get() == this)
      29. {
      30. //FORCE THAT IM REALLY THE PARENT
      31. kid->SetParent(m_weakParentNode);
      32. m_Children.push_back(kid);
      33. ret = true;
      34. }
      35. else
      36. {
      37. pRelativeParent->VAddChild(kid);
      38. }
      39. ...
      40. }
      Display All

      To avoid possible deadlock while adding the node i am not sure if Root node and Nodes Groups (Static, Actor,...) should point to themselves ?

      Source Code

      1. RootNode::RootNode()
      2. {
      3. shared_ptr<SceneNode> staticGroup(GCC_NEW ...;
      4. m_Children.push_back(staticGroup);
      5. staticGroup->SetParent(staticGroup);
      6. ...
      7. actorGroup->SetParent(actorGroup);
      8. ...
      9. skyGroup->SetParent(skyGroup);
      10. }


      For now im using this code and im not having troubles on deletion, but im also not sure if this would be a correct architecture for a scene graph...

      Regards,

      @B^)>
    • RE: The problem with SceneNode parent

      I think you should never have nodes point to themselves - if they don't have a parent the weak pointer should be null - this is a pretty typical design of tree structures where children know of their parents.
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • RE: The problem with SceneNode parent

      Inside of your VAddChild function, you seem to be setting the new child node's parent pointer to point to its grandparent (in the case where its parent is already set) or adding the kid node as a child to its grandparent if its parent was not already set.

      I would look at inheriting SceneNode from <boost/enable_shared_from_this.hpp> as well as ISceneNode, and only setting the parent pointer within the VAddChild function (simply reset if moving a node between parents).

      VAddChild in this case would be:

      Source Code

      1. bool SceneNode::VAddChild(shared_ptr<ISceneNode> kid)
      2. {
      3. bool ret = false;
      4. // Set the kid's parent to be this.
      5. kid->SetParent(shared_from_this());
      6. m_Children.push_back(kid);
      7. ...
      8. }
    • Hi all!

      I'll check the code and follow your suggestions...

      Mike, i agree with you, i did it that way because of the relation of the three first children in the tree (Static, actors and sky) which was a bit unclear to me.... Right now, i am planning to extend the scene graph with an octree implementation for the level scene mesh, so i think they will be added as children of the Static node...

      BTW, i had some troubles including the boost weak_ptr and enable_shared_ptr from the Gamecode headers, and for now i have them included in SceneNode.h, and also i am having some troubles understanding the "enable_shared_from_this" concept... Ill take an deeper look...

      The idea im trying to achieve is to add "relative" Camera Scene Nodes, so it will follow and point always to the parent in relative coordinates... Maybe i could also achieve the same result with a Camera Controller, so im experimenting in both ways

      Regards,

      @B^)>
    • RE: in closing

      Oh I think I see. The children of the root node are just like children of any other node, but I make a small excecption in their construction to specifically call out the order in which they get processed and rendered. Really any node could do the same thing - so the slightly different nature of the root node probably is more confusing than useful.
      Mr.Mike
      Author, Programmer, Brewer, Patriot