Why is there a glMatrixMode in OpenGL?

后端 未结 4 493
攒了一身酷
攒了一身酷 2021-02-05 13:10

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

相关标签:
4条回答
  • 2021-02-05 13:16

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

    0 讨论(0)
  • 2021-02-05 13:23

    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:

    • 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.
    0 讨论(0)
  • 2021-02-05 13:28

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

    0 讨论(0)
  • 2021-02-05 13:30

    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.

    0 讨论(0)
提交回复
热议问题