Scene Graph

Filed under: Engine — yasunobu13 at 9:13 pm on Thursday, October 13, 2005

Gamedev.net has a good article on how to create a very simple scene graph with example code.

Now, I think that a proper scene graph should have drawables (triangles, lines, …) as leaf nodes. But the basic way to use a scene graph will be to create a certain type of node then add children. So, we create a transform node and add a bunch of children nodes (drawables, materials, other transforms, …). Who’s to say that we can’t create a drawable node and try to add a bunch of children. Using the above example code, this is perfectly legitimate. In fact, the examples on the first page say this is what should happen. But I still see a problem as each node should affect it’s children, and drawable nodes do not affect anything.

Is there anything wrong with drawables having children? Most likely not, but I think that it ruins the semantics of it all. What if, instead of a seperate node for drawables, each node will have a list of drawables that it is responsible for. This way, the drawable nodes are not part of the structure of the tree, but are actually an addition to each node. It should work, I think.

If we modify GameDev’s sample code, then each node has the following properties

  • Create
  • Destroy
  • Update
  • AddChild
  • AddDrawable
  • protected children list
  • protected drawable list

Now, each drawable is considered to be “under” the node it is attached to. There are no more drawable nodes. My plan for an engine is different from the article in that nodes only contain data. They know nothing about how to draw themselves. The root node of the scene graph is given to a renderer which interprets the node and applies what needs to be applied. In this context, the renderer will update a node, apply the transform, material, whatever, draw the drawable list, then do the same on the children. (In this sense, Update() is used to create animations, color changes, and so on. Not to actually apply the node as in the article.)

My first attempt at this was to create a single node type that held a void pointer and type info. Each type of node was added through it’s appropriate add<type> method (addDrawable, addTransform, addMaterial, …). Then, depending on the type, the tree structure was rearranged. If you tried to add a transform to a drawable, then a new Unknown type node was created with both the drawable and transform as its children. This is not a good way to do things.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>