How not to do a scene graph
Numerous hobby 3D engines use a straightforward and naive class-based approach to implement a scene graph. It is always tempting to define a structure similar to the following code, but please do not do this:
struct SceneNode { Â Â SceneNode* parent_; Â Â vector<SceneNode*> children_; Â Â mat4 localTransform_; Â Â mat4 globalTransform_; Â Â Mesh* mesh_; Â Â Material* material_; Â Â void Render(); };
On top of this structure, you can define numerous recursive traversal methods, such as the dreaded render()
operation. Let's say we have the following root object:
SceneNode* root;
Here, rendering a scene graph can be as simple as doing the following:
root->render();
The rendering routine in this case does multiple things. Most importantly, the render()
method calculates the global transform for the current node. After that, depending on the rendering API being used, mesh...