user469652
user469652

Reputation: 51241

OpenGL: What is MatrixMode?

There are a few modes available:

Modelview
Projection
Texture
Color

What do they mean? What one is most commonly used? Any easy readings you know for the beginners?

Upvotes: 14

Views: 19240

Answers (4)

datenwolf
datenwolf

Reputation: 162164

OpenGL uses several matrices to transform geometry and associated data. Those matrices 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 time. Since they follow all the same rules OpenGL has only one set of matrix manipulation functions: glPushMatrix, glPopMatrix, glLoadIdentity, glLoadMatrix, glMultMatrix, glTranslate, glRotate, glScale, glOrtho, glFrustum.

glMatrixMode selects on which matrix those operations act upon. Say you wanted to write some C++ namespacing 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 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 C++ can't template namespaces, or apply using (or with) on instances (other languages have this), otherwise I'd had written something 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 of (pseudo-)code makes it clear: glMatrixMode is kind of a with statement of OpenGL.

Upvotes: 17

Nicolas Lefebvre
Nicolas Lefebvre

Reputation: 4282

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

Upvotes: 4

Martin Stone
Martin Stone

Reputation: 13007

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

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

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

Upvotes: 2

Neigyl R. Noval
Neigyl R. Noval

Reputation: 6038

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

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

Upvotes: 0

Related Questions