Graph scene using W3C DOM

Hi guys, I was thinking about to use W3C DOM implementation to manage the graph scene. Then I decided to test, and check if it's a good approach. I don't think I've imagined all possibilities, problems, etc. So I want your opinion… pros, cons, etc.

I'm not suggesting to change the current JME graph scene.



The following code can explain the idea:



public class TestScene {

   public static void main(String[] args) {
      new TestScene();
   }
   
   public TestScene() {
      // create the nodes (meshes, geometries, etc) -- it's just a test
      SceneNode node1 = new SceneNode();
      SceneNode node2 = new SceneNode();
      SceneNode node3 = new SceneNode();
      SceneNode node4 = new SceneNode();
      SceneNode node5 = new SceneNode();
      SceneNode node6 = new SceneNode();
      SceneNode node7 = new SceneNode();
      SceneNode node8 = new SceneNode();
      SceneNode node9 = new SceneNode();
      
      // change the localPosition in node5, it will be used later for debugging ;p
      node5.getLocalPosition().set(-5f, 10f, 1.333f);
      
      SceneNodeManager nodeMgr = new SceneNodeManager();
      // insert them into the node manager, giving a name
      nodeMgr.insert("node1", node1);
      nodeMgr.insert("node2", node2);
      nodeMgr.insert("node3", node3);
      nodeMgr.insert("node4", node4);
      nodeMgr.insert("node5", node5);
      nodeMgr.insert("node6", node6);
      nodeMgr.insert("node7", node7);
      nodeMgr.insert("node8", node8);
      nodeMgr.insert("node9", node9);
      
      // create the graph scene passing the node manager (node *database*)
      SceneGraph graph = new SceneGraph(nodeMgr);

      // build the scene's hierarchy using the created nodes
      graph.attachNode(null, "node1", AttachMethod.ROOT);
      graph.attachNode("node1", "node2", AttachMethod.CHILD);
      graph.attachNode("node2", "node3", AttachMethod.CHILD);
      graph.attachNode("node3", "node4", AttachMethod.BEFORE);
      graph.attachNode("node1", "node5", AttachMethod.CHILD);
      graph.attachNode("node4", "node6", AttachMethod.CHILD);
      graph.attachNode("node5", "node7", AttachMethod.CHILD);
      graph.attachNode("node3", "node8", AttachMethod.CHILD);
      graph.attachNode("node2", "node9", AttachMethod.CHILD);
      
      // create the scene debugger and dump the scene
      SceneDebugger debugger = new SceneDebugger(graph);
      debugger.dumpSceneTree();

      // remove the node2, and dump the scene again
      graph.detachNode("node2");
      debugger.dumpSceneTree();

      // retrieve the node5 from the scene, and print out it's localPosition
      SceneNode gotNode = graph.getNode("node5");
      System.out.println("node5.localPosition = "+gotNode.getLocalPosition().toString());
         
   }

}



The output is:


*** scene dump begin ***
-- node1
|  -- node2
|  |  -- node4
|  |  |  -- node6
|  |  -- node3
|  |  |  -- node8
|  |  -- node9
|  -- node5
|  |  -- node7
*** scene dump end ***
*** scene dump begin ***
-- node1
|  -- node5
|  |  -- node7
*** scene dump end ***
node5.localPosition = -5.000000, 10.000000, 1.333000



Basically:
- SceneNodeManager has a Hashtable, used to store every SceneNode created. They're tied to a given name, that works as key;
- SceneNode doesn't store the name inside itself;
- SceneGraph has a org.w3c.dom.Document, where SceneNodes can be attached/inserted/whatever, and organized hierarchically.
- SceneDebugger has the methods to dump the whole tree;

First, requiring a unique name for each node in the entire tree is a poor choice. For example, for characters, you want mount points ("hand" or "back" for example) to have a well-known name you can find. If you have more than one character, it means you have more than one instance of a node with the same name.



Second, the W3C DOM provides hierarchy, that's true, but does it provide anything else that's useful to graphics? For example, automatically calculated inherited position/orientation, or automatically calculated bounding volumes? And have you measured the performance of the W3C DOM to make sure that it's not getting in the way of efficient rendering of the scene?



I guess it would make more sense if you could answer the question: what is the benefit you're trying to get by using W3C DOM to organize your scene hierarchy?

Thank you for replying jwatte.



Good point, unique names really su**. Maybe I could change the algorithm to avoid it. I'll keep thinking on it. – thanks!



Inherited positions/rotations/etc calculations can be done using the GL matrix functions, it isn't necessary to implement every calc since OpenGL can do it for us, we only need to store the resultant matrices. At some point, the same applies to bounding volumes, but it can be more complex, depending exclusively on what kind of bounding algorithm you choose.



I didn't measure the performance because the algorithm isn't done/complete, and I know other problems that will possibly arrive using that scene-graph. Being that the main reason I asked for opinions, pros, cons, etc. But I definitively agree with your point - performance means efficient rendering.



The benefit? To separate scene data from objects. So, the scene-graph algorithm could be used by any programmer, with minor or no changes to his objects. And I chose DOM because it's widely known, and easy to work with.



Any opinion, suggestion or critic is always welcome.

Thanks again jwatte.

Having a ListArray on top of the hash or tree is a common choice for allowing repetitions in data structures.