glPushMatrix and glPopMatrix question

Started by
1 comment, last by gharen2 16 years, 10 months ago
Well I've seen code that uses it with the drawing and some without.. I've read in the blue book what it does, but what's its purpose then? And is it needed and why?
Advertisement
I'll try to explain it's uses with an example. Let's say you have a very simple scene graph with two types of nodes, mesh nodes and transformation nodes. This one for example:
              t              |    ---------------------    |    |    |    |    |        t    t    m    m    t    |    |              |   ---  ---           -----   | |  | |           | | |   t m  m m           m m t   |                      |  ---                    ---  | |                    | |  m m                    m t                           |                           m

Now you want all children of a transformation node to be affected by the transformation which it holds, but nodes which are not children of this node should not be affected. And you also want to keep the effect the parents' transformations when applying a new one. Now as you traverse this tree recursively matrix stacks become very useful indeed.
At each transformation node you will first push your current matrix onto the stack, so that you can return it's state later. Then you multiply the current node's transformation matrix and process all the children, which will in turn to the same thing. Once your recursion pops back the next statement to be executed is to pop the old matrix back, since you don't want this transformation to be in effect when processing the other nodes.
Pseudo code
function UpdateNode()  PushMatrix  MultiplyMatrix(this.Matrix)  Foreach Child child in Children    child.Update  PopMatrix


Looking at our earlier graph ASCII image, let's say we take the path outlined by bold font.
              t              |    ---------------------    |    |    |    |    |        t    t    m    m    t    |    |              |   ---  ---           -----   | |  | |           | | |   t m  m m           m m t   |                      |  ---                    ---  | |                    | |  m m                    m t                           |                           m

The recursion will stop at the mesh node and will pop back up to the 3rd level. As you can see the transformation node their has a sibling, a mesh node. When processing this node, the effects of the sibling transformation node should not be in effect, and it won't since when we are done with the transformation node we pop the matrix back to the state it had when the transformation node in the 2nd level had been processed.

I hope you can make some sense out of this [smile]
Best regards, Omid
Omid's explanation is excellent, but a bit high level for a beginner.

A much simpler and more common use for the functions is simply to save the current state of the matrix, and restore it later. So in this case it's just used as a convenience.

glPushMatrix // Save the current matrix
// Do some stuff that modifies the matrix
glPopMatrix // Restore the matrix

There's other functions that do this with renderstates as well.

The use Omid describes is much more powerfull. It makes use of the fact that you can call the functions multiple times, and the matrices will be "pushed" into a stack. glPushMatrix will take the current matrix and put it at the start of the stack, and push the one that was there previously down. glPopMatrix takes the matrix at the top of the stack and makes it the current matrix, while the next one in the stack comes to the top. The fact that you can have multiple matrices in the stack is what allows the hierarchy structure Omid described.

As to whether or not to use it, that all depends on the structure of your graphics code. I tend not to use it much at all, because I have a scene graph setup, so calculate and store the matrices myself. Hence I don't use glRotate or anything. I just calculate them myself and apply them with glLoadMatrix. However, for many simple opengl applications where you do use the matrix functions like glRotate, using glPushMatrix and glPopMatrix is a good choice.

This topic is closed to new replies.

Advertisement