Sign in to follow this  
AsOne

OpenGL glPushMatrix, glPopMatrix, glFlush ...

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


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


Link to post
Share on other sites
Quote:
Original post by AsOne
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?


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


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

gluLookAt() 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 draw
glPopMatrix();//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 this post


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

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  

  • Forum Statistics

    • Total Topics
      627759
    • Total Posts
      2978958
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now