# OpenGL glPushMatrix, glPopMatrix, glFlush ...

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

## Recommended Posts

Hey, I'm pretty new to using openGL and have looked at quite a few tutorials just dealing with the basic stuff. I've noticed a few differences between some and can't quite figure out why one does something and the other doesn't. When drawing, some tutorials say you should use glPushMatrix to push the current matrix on top of the stack (not really sure what is meant by that, but that is the description given by what I read). Followed by that is all the translations and drawing, then at the end a glPopMatrix, the tutorial says that this moves the camera back to the original position being the origin (0, 0, 0). However, I can remove this code and everything works. OK, so I'm going through another tutorial on the exact same basic openGL stuff, just wanting to make sure I wasn't missing anything. So this tutorial did not use glPushMatrix, or glPopMatrix in the drawing code, but used glFlush before the buffers were swapped, I am under the impression that glFlush forces openGL to draw everything? But again, removing this line didn't change anything. Then the last thing is, is that sometimes glMatrixMode(GL_MODELVIEW) is called before drawing and then glIdentity is called. I'm not exactly sure why this is sometimes there either, is setting the matrix mode to GL_MODELVIEW used for rotating and translating objects and GL_PROJECTION used to rotate the camera? If that is not the reason why would this be called before drawing? Thanks for any help in advance.

##### Share on other sites
You push the matrix only if you need to save the current camera location.
If the default camera location is 0 (as it is in many tutorials) saving the matrix then restoring it, won't help so much, you can just reset the matrix with load identity or whatever it's called.

As for the flush command, it's not necesary, most of the times it can be ignored unless you are tryig to do fancy stuff, such as rendering over a network, or doing some tricky optimizations, etc.

##### Share on other sites
I recommend you the red book, basic for opengl.

##### Share on other sites
Quote:
 Original post by AsOneThen the last thing is, is that sometimes glMatrixMode(GL_MODELVIEW) is called before drawing and then glIdentity is called. I'm not exactly sure why this is sometimes there either, is setting the matrix mode to GL_MODELVIEW used for rotating and translating objects and GL_PROJECTION used to rotate the camera? If that is not the reason why would this be called before drawing?

The function glMatrixMode() sets a current matrix as 'active' this means that all the functions which adjust a matrix act on that matrix.

There are at least 3 matrix stacks;
GL_MODELVIEW, which is the matrix which constrols translation, rotation etc. Every vertex you submit is multipled by this matrix to get its 'true' position in "world space"

GL_PROJECTION, which is used to project the vertices from their world space position to screen space. It has no relation to the postioning or rotation of the 'camera'.

GL_TEXTURE, which is used to adjust how a texture is applied to polygon, REALLY dont worry about this at all as you've just started out, its only here for completeness.

this is a good explaination on why you use GL_MODELVIEW vs GL_PROJECTION.

As to why its called before rendering, its to ensure the correct matrix is adjusted, as at the end of rendering a frame you might have the modelview matrix 'current' (in simple programs you probably will but as things get more complex it might not be).

Edit: And LangFox is spot on, the Red Book is THE book to read when it comes to OpenGL.

##### Share on other sites
I'm going to assume you don't know much about matrix math, or at least how it's used with 3d graphics (I sure someone can provide some good links , or will by the time I finish writing this). Hopefully this will help more than confuse.

GL_MODELVIEW stores the position of the camera AND the position of the object (or the position of the camera relative to what you're about the draw, which makes a lot more sense to me).
glMatrixMode(GL_MODELVIEW), sets it so the OpenGL calls that adjust the matrix work on the MODELVIEW matrix, (as opposed to the PROJECTION matrix).
So
glMatrixMode(GL_MODELVIEW); //We want to move the objects and the camera.glLoadIdentity();// Reset the camera to it's starting placegluLookAt() or some combination of glTranslate and glRotate calls (or some other stuff).//Move the camera to where we want it.glPushMatrix(); //save the camera position because we're moving the object,or camera relative to the object, which ever makes more sense to you.glTranslate and glRotate calls; //position the object (or the camera relative to the object)DrawObject(); //whatever method you use to drawglPopMatrix();//go back to the real camera position, other wise the new object will be positioned relative to the last object, which can be useful.glPushMatrix();//save our camera since we're moving again.glTranslate and glRotate calls; //position the new object (or the camera relative to the object)DrawObject2();glPopMatrix();

Some of the matrix calls can be unneeded. For example you don't need glPushMatrix glPopMatrix for the last drawobject in the example above since when we redraw everything the loadidentity, glulookat will reset the camera for us. But if you decide to later draw something else you'll need to add the pushmatrix popmatrix stuff anyway. The example above is (as far as I know) the standard way of drawing basic 3d stuff (minus the glMatrixMode(GL_MODELVIEW) since most people try to keep that as the current matrix)

The camera position should never be set in the GL_PROJECTION matrix, it may look like it's working, but all the lighting will be messed up (among some other nasty problems). GL_PROJECTION is for setting some camera properties (like how much smaller objects far away are than closer objects, and where the clipping planes are).

Calling glFlush before swapping is redundant (since swaping should call glFlush), There are some times when you need it (apps without a backbuffer and the old slow way of rendering to a texture come to mind as possible cases).

##### Share on other sites
Thank you for the replies, that cleared all my questions up. As you said I don't know much about matrices and how they apply to 3d graphics, I have a vague idea, and only have some basic knowledge about identity matrices and how to do basic operations on matrices. I should probably do a bit of reading so I actually know what I'm doing when manipulating matrices within openGL.

I'll take a look into that book as well. Hopefully I can pick it up cheap somewhere.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5
gaxio
11

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631708
• Total Posts
3001837
×