glPushMatrix()???

Hello guys!
I have a little question, for what do I need the function glPushMatrix()? I know that its like “remembereing the current position of the matrix”, but no matter if i execute my programme with the glPushMatrix() and glPopMatrix() functions, its absolutely the same. In example, I rotate a cube… with or without this function, its still the same.

Could someone please explain me the basic usage of that stuff and when exactly I need it and for what?

Thank you 1000 times,
Fabian

An example would if you want to cudes doing diferent things like one rotating and the other one translating… You would use the glPushMatrix() and glPopMatrix() to the save the state of each cube so that both cubes dont rotate and translate…

So,

glPushMatrix(); // Save the current working matrix.
glLoadIdentity(); // Makes the new working matrix the identity.
glTrans… // Translate you cude.
drawCUBE_1; // Draw your cube.
glPopMatrix(); // Restore you previous matrix.

The same goes for the second cube…

glPushMatrix();
glLoadIdentity();
glRotate…
drawCUBE_2;
glPopMatrix();

So, imagine that you have five cubes now, each one doing different things. When you apply a translation on the first and you dont push the matrix and make it identity, the translation will be also applied to the following cubes. So, if the second was suppossed to only have rotation, you will now end up with rotation + translation, ans will the remaining cubes…

I am not sure if it is any better now, but it is not the simplest concept.

I just put up on my site the red book and the super bible. They are both good books to start with… They will be up only for a short period of time…
http://141.217.220.20/miguel/

[This message has been edited by mancha (edited 08-03-2002).]

If you are only rotating or translating one object you will not see any effect from push/pop the matrix, depending on your code.
But if you have more then one object, then it is a must that you use them.

You must remember that any time you use a command that changes the matrix it effects every object in that matrix.

example:

glRotate(15, 1, 0, 0);
My_Object1(); this object is rotated 15 degrees

glRotate(15, 1, 0, 0);
My_Object2(); this object is rotated 15 degress plus the 15 degrees from the past rotate, a total of 30 degrees is what this object will be rotated.

example with glPush/pop

glPushMatrix(); // save past matrix, before next operation
glRotate(15, 1, 0, 0);
My_Object1(); this object is rotated 15 degrees.
glPopMatrix(); // Combine past matrix with current

glPushMatrix(); // save matrix
glRotate(15, 1, 0, 0);
My_Object2(); this object is rotate 15 degrees, is not effected by past matrix rotation before Push Matrix
glPopMatrix(); //Combine past matrix with current.

Originally posted by fabianspring:
[b]Hello guys!
I have a little question, for what do I need the function glPushMatrix()? I know that its like “remembereing the current position of the matrix”, but no matter if i execute my programme with the glPushMatrix() and glPopMatrix() functions, its absolutely the same. In example, I rotate a cube… with or without this function, its still the same.

Could someone please explain me the basic usage of that stuff and when exactly I need it and for what?

Thank you 1000 times,
Fabian[/b]

glPushMatrix and glPopMatrix all act on the matrix stack. If you’re not sure how a stack works, think of it like so…

Say you have a deck of playing cards. Define some location on the table to be a stack. “Pushing” a card onto the stack is like placing a playing card on that location, on top of any other cards that may already be there. “Popping” a card from the stack is basically like drawing the top card.

Now with OpenGL, when you do glPushMatrix, it is taking your current matrix (GL_PROJECTION, GL_MODELVIEW, or GL_TEXTURE depending on what your glMatrixMode is) and placing it on top of the stack. One little difference with this example is that you also still have a “copy” of that current matrix as your current matrix. Now when you do a glPopMatrix, you are essentially replacing your current matrix with the matrix that is on top of the stack and removing it from the stack.

Depending on how your code is setup, you don’t necessarily ever NEED to use it, but it can be useful for things such as the example already given of multiple objects. Even if you just have multiple objects, however you COULD do a glLoadIdentity and your “camera” transformations before each object.

Another example where I find glPush/Pop more useful is when you have hierarchical objects. Take a solar system as an example. You have planets that revolve around the sun, and moons that revolve around the planets. You can use glPushMatrix and glPopMatrix to help you out by doing something like so…

glPushMatrix();
glTranslatef(sun position);
for each planet
{
glPushMatrix()
glRotatef(planet’s orbital rotation);
glTranslatef(planet’s orbit distance from sun);
for each moon
{
glPushMatrix();
glRotatef(moon’s orbital rotation);
glTranslatef(moon’s orbit distance from planet);
glRotatef(moon’s axis rotation);
DrawMoon();
glPopMatrix();
}
glRotatef(planet’s axis rotation);
DrawPlanet();
glPopMatrix();
}
glRotatef(sun’s axis rotation);
DrawSun();
glPopMatrix();

Originally posted by mancha:
[b]
glPushMatrix(); // Save the current working matrix.
glLoadIdentity(); // Makes the new working matrix the identity.
glTrans… // Translate you cude.
drawCUBE_1; // Draw your cube.
glPopMatrix(); // Restore you previous matrix.

The same goes for the second cube…

glPushMatrix();
glLoadIdentity();
glRotate…
drawCUBE_2;
glPopMatrix();
[/b]

Not to be picky, but glPushMatrix and glPopMatrix really don’t do anything for you here since you are using glLoadIdentity before each object. The only reason for possibly using them in the above code is if you have “camera” transformations and other objects that use those. (The above ones wouldn’t be using any other “camera” transformations because of the glLoadIdentity)

To make the above example more valid, you could take out the glLoadIdentity and at the top of the code (before the first glPushMatrix) add something like…

glTranslate(0.0, 0.0, -10.0); // move the “camera” back

[This message has been edited by Deiussum (edited 08-04-2002).]

Yeah, now I understood the usage of the glPushMatrix()!!!

This board is really so great, thank you all so much for your fast and good explaining answers!

Thanx,
Fabian

Deiussum: You are picky…

No dude, it was only to illustrate how you can use the glPop and Push. There are a few other million scenarios that I could have presented here…
But it was to give him a quick example.


Oh man, I got bryce 5, and it rocks for making skyboxes and stuff like that…

[This message has been edited by mancha (edited 08-04-2002).]