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?
glMatrixMode sets the current matrix mode. mode can assume one of four values: GL_MODELVIEW. Applies subsequent matrix operations to the modelview matrix stack.
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.
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.
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.
OpenGL uses several matrices to transform geometry and associated data. Those matrices are:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With