Sign in to follow this  
yuppies

what is glPushMatrix?

Recommended Posts

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


Link to post
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 objects
glPopMatrix(); //back to identity




Share this post


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