Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a glMatrixMode in OpenGL?

Tags:

opengl

I just don't understand what OpenGL's glMatrixMode is for.

As far as I can see, when glMatrixMode(GL_MODELVIEW) is called, it is followed by glVertex, glTranslate, glRotate and the like, that is, OpenGL commands that place some objects somewhere in the space. On the other hand, if glOrtho or glFrustum or gluProjection is called (ie how the placed objects are rendered), it has a preceeding call of glMatrixMode(GL_PROJECTION).

I guess what I have written so far is an assumption on which someone will prove me wrong, but is not the point of using different Matrix Modes exactly because there are different kinds of gl-functions: those concerned with placing objects and those with how the objects are rendered?

like image 552
René Nyffenegger Avatar asked Dec 30 '10 21:12

René Nyffenegger


People also ask

What is glMatrixMode in OpenGL?

glMatrixMode sets the current matrix mode. mode can assume one of four values: GL_MODELVIEW. Applies subsequent matrix operations to the modelview matrix stack.

What is the use of glMatrixMode Gl_modelview );?

glMatrixMode() specifies which matrix is the current matrix. GL_MODELVIEW - Applies subsequent matrix operations to the modelview matrix stack. GL_PROJECTION - Applies subsequent matrix operations to the projection matrix stack.

Why is glLoadIdentity used?

glLoadIdentity() function ensures that each time when we enter the projection mode, the matrix will be reset to identity matrix, so that the new viewing parameters are not combined with the previous one.

What is glOrtho OpenGL?

glOrtho describes a transformation that produces a parallel projection.


2 Answers

This is simple and can be answered very briefly:

  • Rendering vertices (as in glVertex ) depends on the current state of matrices called "model-view matrix" and "projection matrix";

  • The commands glTranslatef, glPushMatrix, glLoadIdentity, glLoadMatrix, glOrtho, gluPerspective and the whole family affect the current matrix (which is either of the above);

  • The command glMatrixMode selects the matrix (model-view or projection) which is affected by the forementioned commands.

(There's also the texture matrix used for texture coordinates, but it's seldomly used.)

So the common use case is:

  • Have the model-view matrix active most of the time;
  • Whenever you have to initialize the projection matrix (usually at the beginning or when the window is resized, perhaps), switch the active to projection, set up a perspective, and revert back to model-view.
like image 117
Kos Avatar answered Oct 19 '22 12:10

Kos


You can use glRotate and glTranslate for projection matrices as well.

Also: OpenGL supports transforms of textures and colors. If you active this feature you can for example modify the texture coordinates of an object without rewriting the texture coordinates each frame (slow).

This is a very useful feature if you want to scroll a texture across an object. All you have to do for this is to draw the textured object, set the matrix mode to GL_TEXTURE and call glTranslate to set the offset into the texture.

like image 40
Nils Pipenbrinck Avatar answered Oct 19 '22 11:10

Nils Pipenbrinck