OpenGL Help with Push and Pop basics

This topic is 3908 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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...

Share on other sites
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...

Share on other sites
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 2:

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

No, it pushes a copy of the topmost matrix.

Share on other sites
Also see this post for another explanation.

Share on other sites
thanks for the replies... i understand it now

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 9
• 23
• 9
• 32
• Forum Statistics

• Total Topics
632625
• Total Posts
3007508
• Who's Online (See full list)

There are no registered users currently online

×