Help with Push and Pop basics

Started by
3 comments, last by SnowMoo 16 years, 3 months ago
Hello, I'm a OpenGL beginner. Just to ask... What is the correct way of using the push and pop matrix. I am confused, because on the net it says: glPushMatrix(); <--starting point --Things you want to store in the matrix stack-- glPopMatrix(); <-- ending point ***************************************************** And my lecturer says: --Things you want to store in the matrix stack(a)-- glPushMatrix(); Some transformation here... glPopMatrix(); <--- is called (a) will be popped out... Which one is the correct way of doing it? Thanks to anyone who can help and explain to me...
Advertisement
I could be wrong here, it's been ages since I did OpenGL stuff, and I didn't do that much.

glPushMatrix() pushes an empty matrix onto the top of the matrix stack. Any time you modify transformations from then on, it's affecting the matrix on the top of the stack.
The end transform is the result of multiplying all of the matrices on the stack together.

So, in the first case, that will push an empty matrix onto the stack (I assume it starts with one on the stack already), then modifies it, then pops it off, and in the second case it sets up an initial transform, then adds to it by pushing an additional matrix on and modifying that.

Like I said, I could be wrong here...
These calls are used to preserve the state of the matrix stack. Let's say that the topmost matrix on the stack is A. When you call glPushMatrix(), it makes a copy of A and pushes it on the stack. When you call glPopMatrix(), it pops the topmost matrix from the stack. If you've paired these calls correctly, it should pop the copy of A (that was presumably modified between the two calls) and then the topmost matrix will be the original A. This matrix is of course unchanged, because all transformations that were performed between the two calls were applied to the copy of A.

For example, lets say you want to draw two models, one at (-1, 0, 0) and one at (1, 0, 0). You would do this:
glPushMatrix();glTranslatef(-1, 0, 0);drawModel1();glPopMatrix();glPushMatrix();glTranslatef(1, 0, 0);drawModel2()glPopMatrix();


Without the calls to push/pop, the second glTranslatef() would have "canceled" the first and the second model would be drawn at (0, 0, 0).

EDIT: To learn more, go here and read the section titled "Manipulating the Matrix Stacks".

Edit 2:

Quote:Original post by Evil Steve
glPushMatrix() pushes an empty matrix onto the top of the matrix stack.


No, it pushes a copy of the topmost matrix.
Also see this post for another explanation.
thanks for the replies... i understand it now

This topic is closed to new replies.

Advertisement