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...