Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple curiosity about scene graph conception

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

enter image description here

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 :

enter image description here

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 ?

like image 220
user1364743 Avatar asked Nov 11 '22 09:11

user1364743


1 Answers

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:

  • what to draw, vs.
  • how to draw it.

Then you could even use double dispatch (like an evolved Visitor Pattern) to make things more generic.

like image 192
Shadocko Avatar answered Jan 03 '23 05:01

Shadocko