问题
what is the difference between placing glRotatef() after glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glRotatef(red, green, blue);
and placing glRotatef() after glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(red, green, blue);
回答1:
From documentation:
glMatrixMode()
specifies which matrix is the current matrix.
GL_MODELVIEW - Applies subsequent matrix operations to the modelview matrix stack.
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. Naturally, one of them are expected to be next line :
For orthographic projection:
glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);
gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);
For perspective projection:
void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);
void gluPerspective(GLdouble fov, GLdouble aspect, GLdouble near, GLdouble far);
If you set current matrix mode as modelView(e.g glMatrixMode(GL_MODELVIEW)
), you are saying that I am in the modelview matrix and I can apply basic operations to transform my objects like :
glRotatef();
glTranslatef();
glScalef();
In your question if you use rotatef after gl_projection instead of gl_modelview, you rotate your projection matrix which would corrupt your projection matrix.
`
回答2:
OpenGL stores the Projection and ModelView matrixes separately, with the call to glMatrixMode() you specify which matrix you want to manipulate with the following calls. So in the first example, you applying a rotation to the projection matrix, and in the second you apply it to the ModelView Matrix (which would be more common)
Also look at this answer for clarification Difference between glMatrixMode(GL_PROJECTION); and glMatrixMode(GL_MODELVIEW);
来源:https://stackoverflow.com/questions/29804087/opengl-glmatrixmodegl-projection-vs-glmatrixmodegl-modelview