Skip to main content
gamedev.net
🔒 Locked

glPush()/glPopMatrix?

Started by isiahil Jan 18, 2004 at 5:25 PM 4 replies 650+ views
Original Post
isiahil
isiahil
I don''t get the concept behind glPushMatrix and glPopMatrix. I don''t have to do nothing but live and die, everything else i choose to do- I don''t know, but my dad says it alot
I don't have to do nothing but live and die, everything else i choose to do- I don't know, but my dad says it alot
isiahil
isiahil
anyone know?

I don''t have to do nothing but live and die, everything else i choose to do- I don''t know, but my dad says it alot
I don't have to do nothing but live and die, everything else i choose to do- I don't know, but my dad says it alot
benjamin bunny
benjamin bunny
glPushMatrix pushes the topmost matrix onto the stack and creates a copy. glPopMatrix removes the topmost matrix from the stack. The topmost matrix on the stack is the one that is currently in use. That's pretty much all there is to it.

[edited by - benjamin bunny on January 18, 2004 8:42:11 PM]
Trienco
Trienco
if you just wonder what the heck its supposed to be good for, trust me, you will be using it a lot. once you set your matrix (for example with glulookat) you dont want to just overwrite it when placing an object and then start all over for the next object. instead you just push/save it and pop/load it when youre done. and as its a stack you can push as much as you want (more or less) and pop them back in reverse order (just what you need in a hierachy, for example a complex object consisting of smaller objects, consisting of even smaller... and so on)
f@dzhttp://festini.device-zero.de
m_wherrett
m_wherrett
Ok, when I first encountered OpenGL I was completely flumoxxed by glPushMatrix and glPopMatrix. People attempted to explain the concept of pushing and popping the Matrix stack but that only lead to more confusion. I finally got my head around the basics when it was explained as follows...

glPushMatrix = remember where I am in the scene and how I'm oriented.

glPopMatrix = put me back the way I was when I called glPushMatrix.

This means we can perform glTranslate, glRotate and glScale commands in between calling push and pop to ensure that those commands will not affect any other drawing we do later on in that frame.

So let's say I was going to draw a display list called 'spacecraft' I could do this....


// move to ships location

glTranslatef(location_x, location_y, location_z);

// scale to our crafts size

glScalef(size, size, size);

//draw the model

glCallList(spacecraft);



That would draw it in its specified position at its specified size. Unfortunatly, any subsequent movement/drawing will be done in relation to where that ship was drawn. After all we used the translate and rotate commands which affects the matrix we are drawing our scene in.

We could simply ensure we call the opposite commands to ensure the matrix is left the way it was before we started drawing but that would be stupid. All we need to do is ensure that glPushMatrix is called before the aforementioned code, and glPopMatrix afterwards.

   
glPushMatrix();

// move to ships location

glTranslatef(location_x, location_y, location_z);

// scale to our crafts size

glScalef(size, size, size);

//draw the model

glCallList(spacecraft);

glPopMatrix();


That way, drawing our spacecraft will not affect anything drawn afterwards. Admittedly, that is really dumbing it down but when you're starting out you don't need to remember the use of matrices and a stack, just think of it as "remember my current state" and "put me back that way, now I'm done drawing"

Hope that helps.

[edited by - m_wherrett on January 19, 2004 11:28:47 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.