René Nyffenegger
René Nyffenegger

Reputation: 40603

Why is there a glMatrixMode in 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?

Upvotes: 18

Views: 32975

Answers (4)

Kos
Kos

Reputation: 72299

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.

Upvotes: 24

Bahbar
Bahbar

Reputation: 18015

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).

Upvotes: 4

Nils Pipenbrinck
Nils Pipenbrinck

Reputation: 86393

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.

Upvotes: 4

Ben Voigt
Ben Voigt

Reputation: 283803

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.)

Upvotes: 2

Related Questions