View Full Version : Projection Matrix

04-04-2010, 03:22 AM
Hello everyone!
Im new to OpenGL..Can anyone please explain the following..


Now this should not work right?? Because im defining the Viewing volume before going to the projection mode.


does not work properly..ie i need to put it as

and the viewing volume will be defined..
Can anyone explain how its working for gluOrtho2D()??
It should not right??

Thanks in advance.

Rosario Leonardi
04-04-2010, 08:42 AM
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
// Reset the current matrix (we have selected projection so load identity will reset the projection matrix)
// setup the projection matrix.
one of [glOrtho, glFrustum, gluOrtho2D, gluPerspective]

04-04-2010, 08:50 AM
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.

Hello everyone!
gluOrtho2D(0,500,0,500); // make a matrix and multiply it by whatever the mode is set to
glMatrixMode(GL_PROJECTION); // set mode to projection
glLoadIdentity(); // blow away anything on the projection stack and put the identity there

Typical workflow is this:
1) glMatrixMode(...) - tell OpenGL which matrix you'd like to work with
2) 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
3) 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:


04-04-2010, 10:28 AM
Thanks a lot for the support Rosario and Strattonbrazil. :)

One more thing..Which is the default matrix set by OpenGL??
ie if i just give


which matrix is multiplied??

Rosario Leonardi
04-04-2010, 01:07 PM
the GL_MODELVIEW matrix

openGL 2.1 Specifications

this is the initial value when the context is created. Then if somewhere in the code you make
the matrix operation will modify this matrix until you say otherwise. And remember the state don't reset automatically every frame.

04-04-2010, 01:43 PM
Yeah i checked it out..Thanks Rosario..Im very new to OpenGL.
and your words:

"I tell you, realtime 3D is made of blood, sweat and screams!"

makes a lot of sense to me..