Open GL Matrix Stack
← MVP Rechenbeispiel | ● | Scene Graph Flattening →
Supposed we have a procedure drawObject() that renders a particular object, for example:
{
lglBegin();
...
lglEnd();
}
To render multiple objects of the same type with different transformations we use the matrix stack via glPushMatrix() and glPopMatrix() (resp. its lgl replacements):
lglTranslate(-10,0,0);
lglRotate(90,0,1,0);
drawObject();
lglPopMatrix();
lglPushMatrix();
lglTranslate(10,0,0);
lglRotate(-90,0,1,0);
drawObject();
lglPopMatrix();
The glPushMatrix command saves the actual transformation state on top of a matrix stack. Later on the state can be restored with glPopMatrix().
With the matrix stack we can construct objects hierarchically. This means that an object is made up from parts which in turn can be made up from sub-parts and so on.
A simple example of an object which has two sub-parts attached:
{
// parent geometry
lglBegin();
...
lglEnd();
// transformed child geometry #1
lglPushMatrix();
lglTranslate(-1,0,0);
drawPart();
lglPopMatrix();
// transformed child geometry #2
lglPushMatrix();
lglTranslate(1,0,0);
drawPart();
lglPopMatrix();
...
}
void drawPart()
{
// and so on...
}
This is called hierarchical modeling. Here, the scene has parent-child relationships between the objects and their respective sub-parts. These relationships correspond to a graph which is called the scene graph.