Reputation: 22270
When looking at the reshape examples in the redbook, I usually find something like:
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW);
}
I understand that calling glMatrixMode(GL_PROJECTION);
followed by glLoadIdentity();
resets the projection matrix but I don't understand why glMatrixMode(GL_MODELVIEW)
is usually called at the end of reshape.
In this particular example glFrustum
affects the projection matrix, right? Why is GL_MODELVIEW
called later? Would it make a difference if the last call to glMatrixMode(GL_MODELVIEW)
is omitted?
Upvotes: 2
Views: 1222
Reputation: 12126
Most of your rendering code is going to affect GL_MODELVIEW
, because it's what affects object translation and camera position. However, the resizing code works on GL_PROJECTION
. The programmer probably assumes the current matrix is the modelview one in most of his code, and when a different one needs to be affected, he/she would change the matrix, update it, and then change the target back to GL_MODELVIEW
so the rest of the code doesn't target the wrong matrix.
Generally, OpenGL functions affect whatever matrix is currently being targeted, which is why you change the matrix.
Upvotes: 3