Jump to content
  • Advertisement
Sign in to follow this  
SnowMoo

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.

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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: 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!