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?
glMatrixMode sets the current matrix mode. mode can assume one of four values: GL_MODELVIEW. Applies subsequent matrix operations to the modelview matrix stack.
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.
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.
glOrtho describes a transformation that produces a parallel projection.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With