[ACCEPTED]-OpenGL: What is MatrixMode?-opengl

Accepted answer
Score: 16

OpenGL uses several matrices to transform 15 geometry and associated data. Those matrices 14 are:

  • Modelview – places object geometry in the global, unprojected space
  • Projection – projects global coordinates into clip space; you may think of it as kind of a lens
  • Texture – adjusts texture coordinates before; mostly used to implement texture projection (i.e. projecting a texture as if it was a slide in a projector)
  • Color – adjusts the vertex colors. Seldomly touched at all

All these matrices are used all the 13 time. Since they follow all the same rules 12 OpenGL has only one set of matrix manipulation 11 functions: glPushMatrix, glPopMatrix, glLoadIdentity, glLoadMatrix, glMultMatrix, glTranslate, glRotate, glScale, glOrtho, glFrustum.

glMatrixMode selects on 10 which matrix those operations act upon. Say 9 you wanted to write some C++ namespacing 8 wrapper, it could look like this:

namespace OpenGL {
  // A single template class for easy OpenGL matrix mode association
  template<GLenum mat> class Matrix
  {
  public:
    void LoadIdentity() const 
        { glMatrixMode(mat); glLoadIdentity(); }

    void Translate(GLfloat x, GLfloat y, GLfloat z) const
        { glMatrixMode(mat); glTranslatef(x,y,z); }
    void Translate(GLdouble x, GLdouble y, GLdouble z) const
        { glMatrixMode(mat); glTranslated(x,y,z); }

    void Rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) const
        { glMatrixMode(mat); glRotatef(angle, x, y, z); }
    void Rotate(GLdouble angle, GLdouble x, GLdouble y, GLdouble z) const
        { glMatrixMode(mat); glRotated(angle, x, y, z); }

    // And all the other matrix manipulation functions
    // using overloading to select proper OpenGL variant depending on
    // function parameters, and all the other C++ whiz.
    // ...
  };

  // 
  const Matrix<GL_MODELVIEW>  Modelview;
  const Matrix<GL_PROJECTION> Projection;
  const Matrix<GL_TEXTURE>    Texture;
  const Matrix<GL_COLOR>      Color;
}

Later on 7 in a C++ program you could write then

void draw_something()
{
    OpenGL::Projection::LoadIdentity();
    OpenGL::Projection::Frustum(...);

    OpenGL::Modelview::LoadIdentity();
    OpenGL::Modelview::Translate(...);

    // drawing commands
}

Unfortunately 6 C++ can't template namespaces, or apply 5 using (or with) on instances (other languages have 4 this), otherwise I'd had written something 3 like (invalid C++)

void draw_something_else()
{
    using namespace OpenGL;

    with(Projection) {    // glMatrixMode(GL_PROJECTION);
        LoadIdentity();   // glLoadIdentity();
        Frustum(...);     // glFrustum(...);
    }

    with(Modelview) {     // glMatrixMode(GL_MODELVIEW);
        LoadIdentity();   // glLoadIdentity();
        Translate(...);   // glTranslatef(...);
    }

}

I think this last snipped 2 of (pseudo-)code makes it clear: glMatrixMode is kind 1 of a with statement of OpenGL.

Score: 4

As a sidenote, matrix modes (along with 2 the rest of the matrix stack functionality) are 1 deprecated in OpenGL 3.3 and up.

Score: 2

All of them are used internally by OpenGL, but 12 whether you need to change them depends 11 on your application.

You will always want 10 to set the Projection matrix to determine 9 your field of view and the extents of the 8 space you are viewing. Usually you will 7 set the Modelview matrix to choose your 6 "camera" orientation, and to position objects 5 in the scene.

The Texture and Color matrices 4 are less commonly used. In my current project 3 I use the Texture matrix to flip the Y in 2 my bitmaps. I have never used the Color 1 matrix personally.

Score: 0

You can find your answers here http://www.opengl.org/sdk/docs/man/xhtml/glMatrixMode.xml

modelview 4 is for modeling. Projection is for projecting 3 like 3d stuffs. Texture for texturing. Color 2 for coloring. But there is more than that. Just 1 read on the link I give you. Cheers.

More Related questions