# State propagation in modern OpenGL

When last I dabbled in game development, I managed my geometry in a tree. Every node would call the render method on it's children, prior to resetting it's transformations, thus allowing the transforms to propagate. (The typical illustration of how this works is a radar dish spinning, on top of a vehicle that moves. The radar is a child of a autorotator node, which is a child of the vehicle.)

Now I'm at it again, and I learn that transformations are supposed to be done with shaders. Fair enough. What I can't wrap my head around is how to inherit transformations between objects.

Perhaps I´m over thinking it. Is it just a matter of passing the M, V and P matrices down the tree yourself and multiplying them prior to sending them to the shader?

In any case I'd like to hear how other have dealt with this. There is not much out there on this topic.

## 2 Answers

Model, View and Projection matrices are passed as uniforms to the vertex shader, which uses them to transform vertex coordinates and normals. Typically projection matrix is constant between frames, view matrix is calculated once per frame and model matrix is unique for each object. Model matrix is in world space. This is of course not the only way to do transformations, but I think it's the simplest to understand.

For hierarchical scene graph, objects can have local transformation relative to their parent. As you guessed correctly, the matrices are multiplied on CPU prior passing them to the shader. It's possible to do it similar to the legacy OpenGL by having your own matrix stack and multiplying the matrices in the render function. However it's often better to calculate the world matrices in a separate (also recursive) function and store the results in the objects themself. After that you can render the objects in any order you want, e.g. group them by shader / textures to minimize switching or render all translucent objects after solid ones. Also if you render the scene multiple times per frame e.g. due to reflections, the matrices need to be calculated only once. In addition you can use the world matrices for other purposes such as for physics calculations.

Perhaps I´m over thinking it.

Yes, you are. Transformation being done in shaders is meant to be literal. "Transformation" in this case being the application of some transform to the various per-vertex attributes. Where that particular transformation comes from is generally irrelevant to the shader. It is given a transformation, and it applies it to the vertex data for the object.