PDA

View Full Version : Projection Matrix



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

gluOrtho2D(0,500,0,500);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

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

However,
glOrtho(0,500,0,500,0,500);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

does not work properly..ie i need to put it as
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,500,0,500,0,500);

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, 09: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
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]

strattonbrazil
04-04-2010, 09: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:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,500,0,500,0,500);

Mukund
04-04-2010, 11: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

glOrtho();

which matrix is multiplied??

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

openGL 2.1 Specifications
pag.289
http://www.opengl.org/registry/doc/glspec21.20061201.pdf

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.

Mukund
04-04-2010, 02: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..