Sign in to follow this  
darren_mfuk

When to Push & Pop ?

Recommended Posts

Hey guys, I have been trying to tidy up code and stuff lately, And going through it, and several demos and tutorials, I find some of it very different? When & how often should I use glPushMatrix() and glPopMatrix() Should I use it ONCE ? :
void DrawScene()
{
  glPushMatrix()

    // draw cube

    // draw sphere

    // draw something else

  glPopMatrix()
}


OR around every object ? :
void DrawScene()
{
  glPushMatrix()
    // draw cube
  glPopMatrix()

  glPushMatrix()
    // draw sphere
  glPopMatrix()

  glPushMatrix()
    // draw something else
  glPopMatrix()
}


Thanks ;-)

Share this post


Link to post
Share on other sites
As far as I know and I'm sure someone will come along and say differently

But in my experience you should do it after each object you create where you want to use different transformations on that object

Share this post


Link to post
Share on other sites
The second one, basically. You push when you need a new coordinate set, and pop when you're done with it. You're cube is (probably) defined about the origin and translated to it's position. This means that you need a new coordinate set (one where the origin is the center of the desired location of the cube), so you should push before it and pop after it. The same goes for everything else.

Share this post


Link to post
Share on other sites
Hey guys.

So really lets say I actualy create cube in DrawCube()

void DrawCube()
{
glBegin(GL_QUADS);
// blah blah make my cube
glEnd();
}


and call it from DrawScene()

void DrawScene()
{

DrawCube()

}


I should really add the push & pop, to the DrawCube like so

void DrawCube()
{
glPushMatrix();
glBegin(GL_QUADS);
// blah blah make my cube
glEnd();
glPopMatrix()
}

Share this post


Link to post
Share on other sites
In general, you'll push right before a glTranslate(), glRotate(), or glLoadMatrix() call. You'll pop right after drawing of whatever is at that position. So this depends on wherever you translate and/or rotate the cube.

Share this post


Link to post
Share on other sites
You really only need to push/pop matrices if you're going to be applying any transformations. As it is, your cube drawing is void of any transformations, so the pushing/popping will just slow you down. If, however, it looked something like this:
void DrawCube()
{
glPushMatrix();
glTranslatef(m_fX, m_fY, m_fZ);
glRotate(/*etc*/);
glBegin(GL_QUADS);
// blah blah make my cube
glEnd();
glPopMatrix()
}
Then that'd be the correct usage of pushing and popping. Just make sure that your pushes and pops match up, both in number and in which matrix is getting push/popped -- projection or modelview. You'll get strange errors that can be tricky to track down if you push while in modelview, do some things, then pop while in projection.

Share this post


Link to post
Share on other sites
you push when you want to retain/save the current matrix state.
and pop when you want to restore it

When you call glpushmatrix it saves the matrix state onto a stack

when you pop it, it restores the last matrix entered onto the stack

think of the stack as a big hole. you push the matrix in the hole,lets call it A.
you then push another matrix into the hole,lets call it B

when you pop you get the last matrix on the stack "B"
when you pop again you get A.

hople this helps

Share this post


Link to post
Share on other sites
Just seems to get a little confusing after a while.

Pretty much every object gets translated or rotated in some way or another.

And switching projection modes back and forth (to display fonts and crosshairs etc)

It just seems an awful lot of code !!

Is there something I could do, to get the 'engine' to manage it for me?

Just seems so much pushing and popping going on in the DrawScene();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this