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
As Nils pointed out, you do have more to matrices than just what you mentioned.
I'll add a couple thoughts:
OpenGL core (from 3.1 onwards) does away with all the matrix stuff completely, so does GL ES 2.0. This is simply due to the fact that shader programs removed much of the requirement of having them exposed at the GL level (it's still a convenience, though). You then only have uniforms, and you have to compute their value completely on the client side.
There are more matrix manipulation entrypoints than the ones you mention. Some of them apply equally well to projection/modelview (glLoadIdentity/glLoadMatrix/glMultMatrix, Push/Pop), They are very useful if you want to perform the matrix computation yourself (say because you need them somewhere else in your application).
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.
(Also, there's also the texture matrix used for texture coordinates, but it's seldomly used.)
So the common use case is:
All geometry coordinates undergo several linear transformations in sequence. While any linear transformation can be expressed by a single matrix, often you want to think of a sequence of transformations and edit the sequence, and if you have only a single matrix you could only change the ends of that sequence. By providing several transformation steps, OpenGL gives you several places in the middle where you can change the transformation as well.
Calling glMatrixMode
before emitting geometry has no effect at all. You call glMatrixMode
before editing the transform matrix, to determine where in the overall sequence those edits appear.
(NB: Looking at the sequence makes a lot more sense if you remember that translation and rotation are not commutative, because translation changes the center of rotation. Similarly translation and scaling are not commutative.)
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.