# what is glPushMatrix?

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

## Recommended Posts

I only knw it pushes the current matrix to the stack. wats the use of pushing a matrix to a stack? pls enlighten me

##### Share on other sites
It has many uses for different matrix modes, but here's an example for the modelview matrix:

SomeFunction
{
//code to draw a sphere

glPushMatrix();
glTranslatef(5.0f, 0.0f, 0.0f);

//code to draw a cube

glPopMatrix();

//code to draw a cylinder
}

The sphere and the cylinder will be drawn at the world origin, but the cube will be drawn 5 units in the positive x direction. Pushing the matrix stores the current matrix values to the stack, popping retrieves the values from the last push. So if the Push and Pop weren't in the above code, both the cube and the cylinder would be at position 5 on the x axis. Pushing and Popping is very useful for applying specific transformations to specific sections of code.

[Edited by - smonahan on September 22, 2004 11:01:38 PM]

##### Share on other sites
More usefully, it's an easy way of rendering scenes with a transformation hierarchy if you don't want to manage your own matrices. So for example, if you were rendering a robot arm, you could do it like this:

glLoadIdentity(); glPushMatrix();  ...camera transform  glPushMatrix();    ...upper arm translate/rotate    glPushMatrix();      ...lower arm rotation      glPushMatrix();         ...hand rotation      glPopMatrix(); //pop back to lower arm transform    glPopMatrix(); //pop back to upper arm transform  glPopMatrix(); //back to cam transform  ... render other objectsglPopMatrix();  //back to identity

##### Share on other sites
Well, im new to opengl, but i like to think of the "matrix stack" like just a pack of paper,

when i say pushmatrix, i take a new paper, and start to "scratch" on it, (things like glrotatef, and gltranslatef) then i put my stuff (like spheres etc) on the screen.

When im done, i use popmatrix to throw away my latest piece of paper from the stack. This way i can continue from where i started.

(one thing to keep in mind is that earlier pieces of paper you scribbled still apply to the things you are drawing with a new piece of paper)

Hmm. does this make any sence? :D lol

btw, hope i am not talking sh*te :D

##### Share on other sites
(crap dubblepost :(