I'm writing a simple 3D engine using OpenGL. I've already implemented a simple scene graph with the following pattern :
ISceneNode
IMeshSceneNode
StaticMeshSceneNode
ICameraSceneNode
StaticCameraSceneNode
TrackBallCameraSceneNode
ILightSceneNode
PointLightSceneNode
But I wonder if a 'Renderer' (the class which implement the shader program) could be a scene node too (extract the rendering code from MeshSceneNode to the RenderSceneNode). For me it could be a right choice because if I must render several meshes (for example 42 meshes) using the same vertex and fragment shaders it should be usefull to bind and unbind the shader program just one time and not 42 times!
So what do you think about the following schemas :
The first one represent my current conception (for a sake of simplicity I don't represent the 'Light' and 'Camera' scene nodes).
So, here, if I want to render my 3 meshes (with 3 shader programs using the sames shaders) I will bind and unbind 3 times my shader programs for each frame (in the 'render' method of each Mesh node).
Here's the other conception :
As you can see above, this time I'll bind a unique shader program in the render node for all the children nodes. So it could be faster.
What do you think about my idea ?
Another way to say what Anton said: if you want to optimize state changes, you don't want nodes in your scene graph to make any draw calls directly. Delegate that to your renderer, which will then be able to build an intermediate representation based on which it can reorder OpenGL calls for optimization.
Defining a clean API for your renderer will also allow you to separate your concerns:
Then you could even use double dispatch (like an evolved Visitor Pattern) to make things more generic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With