glOrtho, glFrustum, gluOrtho2D, gluPerspective works in the same way.
These function generate a projection matrix and then post-multiply this matrix with the current selected matrix.
Multiply two projection matrix together don’t make a lot of sense, so the correct way to make this function works is.
// Specify that we want to modify the projection matrix
glMatrixMode(GL_PROJECTION);
// Reset the current matrix (we have selected projection so load identity will reset the projection matrix)
glLoadIdentity();
// setup the projection matrix.
one of [glOrtho, glFrustum, gluOrtho2D, gluPerspective]
Ordering of these functions is important. glMatrixMode tells OpenGL which matrix you’re about to work on. If you do glMatrixMode on GL_PROJECTION, calls like glOrtho, glTranslate, etc. will be working on the projection matrix.
You seem to know what glOrtho and gluOrtho2D do.
glLoadIdentity sets the identity matrix to the matrix mode you’re working on. Most OpenGL matrix operations like glTranslate and even glOrtho multiply against the current matrix (as opposed to just replacing it). Two exceptions are glLoadIdentity and glLoadMatrix where you specify your own matrix.
Typical workflow is this:
glMatrixMode(…) - tell OpenGL which matrix you’d like to work with
glLoadIdentity() - clear out anything there (usually at the start of a display function to clear out the previous frames matrix calls), operates on the matrix of whatever mode you just set in step 1
gl* - do operations that are multiplied against whatever matrix mode you’re in
Almost always you want to clear the projection matrix before using projection commands so they’ll look like this:
this is the initial value when the context is created. Then if somewhere in the code you make
glMatrixMode(GL_TEXTURE);
the matrix operation will modify this matrix until you say otherwise. And remember the state don’t reset automatically every frame.