Scene Graph Flattening
← OpenGL Matrix Stack | ● | Scene Graph To OpenGL →
As an alternative to using the matrix stack to compute the respective transformation matrices, we ca compute the MVP matrix directly from its components using a linear algebra library. Such a library is glslmath.
Supposed we have a view matrix V and a transformation matrix M for an object to be rendered. Supposed the object has also child geometry and we have the relative transformation matrices Mi for each part as well. The we compute the respective matrices for the object and its parts as follows:
void drawObject(mat4 M, mat4 V, mat4 P)
{
// set projection matrix once
lglProjection(P);
// compute actual model-view matrix
mat4 MV = V*M;
// set actual model-view matrix
lglModeView(MV);
// render actual object
lglBegin();
...
lglEnd();
// render transformed child geometry #1
mat4 M1MV = MV * M1;
lglModelView(M1MV);
// render actual object
lglBegin();
...
lglEnd();
// render transformed child geometry #2
mat4 M2MV = MV * M2;
lglModeView(M2MV);
// render actual object
lglBegin();
...
lglEnd();
and so on ...
}
In case the object geometry is encapsulated into a vertex buffer object (VBO):
void drawObject(mat4 M, mat4 V, mat4 P)
{
lglProjection(P);
mat4 MV = V*M;
lglModeView(MV);
lglRender(vbo);
mat4 M1MV = MV*M1;
lglModelView(M1MV);
lglRender(vbo1);
mat4 M2MV = MV*M2;
lglModelView(M2MV);
lglRender(vbo2);
}
That’s how rendering is done in the core profile. Basically, just VBOs and matrices are allowed, since matrix stack operations have been deprecated as legacy functions.