Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL: What is MatrixMode?

Tags:

opengl

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?

like image 986
user469652 Avatar asked Mar 20 '11 07:03

user469652


People also ask

What is Glmatrix mode?

glMatrixMode sets the current matrix mode. mode can assume one of four values: GL_MODELVIEW. Applies subsequent matrix operations to the modelview matrix stack.

What is matrix mode in OpenGL?

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.

What is Gl_projection in OpenGL?

GL_PROJECTION - Applies subsequent matrix operations to the projection matrix stack. What they are means? If you set current matrix mode as projection (e.g glMatrixMode(GL_PROJECTION) ), you are expected to change your projection matrix.

Why do we need glLoadIdentity?

glLoadIdentity() function ensures that each time when we enter the projection mode, the matrix will be reset to identity matrix, so that the new viewing parameters are not combined with the previous one.


1 Answers

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.

like image 127
datenwolf Avatar answered Sep 17 '22 18:09

datenwolf