View Full Version : glPushMatrix and GL_PROJECTION

ajbiv

02-16-2009, 08:42 AM

I am confused about which open gl calls are effected by glPushMatrix.

Can I setup multiple projection matrices using glPushMatrix.

or is it just the glTranslate etc that are effected.

I assume all glEnable etc calls go unaffected.

scratt

02-16-2009, 10:28 AM

glPushMatrix just puts the current matrix in use on it's stack.

glMatrixMode, sets which matrix and thus which stack we use.

You can push and pop independently off each stack.

Be aware that the OpenGL standard has a very low limit for the size of the Projection Matrix stack. 1 or 2 MAX I think. I have not found that to ever be that low on current hardware, but your mileage may vary from GPU to GPU.

marshats

02-16-2009, 11:12 AM

Most often you will see two matrix stacks used: glMatrixMode(GL_MODELVIEW) and glMatrixMode(GL_PROJECTION). Although there are two others relating to texture cooridinate and the color matrix -- I will not talk about those here since your question seems unrelated to those. Note a good reference is http://www.songho.ca/opengl/gl_transform.html for details of all these matrices. That HTML might be good background reading and has excellent tutorial executables helping to see the effects of different matrix operations. For the linux users, the sample windows exe files there run fine under "WINE".

Anyhow, once you choose a matrix stack to work with then you can do any of the matrix operations. However, you want to segregate which operations to use on specific stacks. openGL doesn't limit what operations to use but expects the coder to use the "correct" ones -- this makes it confusing sometimes. I find it best to realize the math behind what is going on is simple -- just a set of successive multiplications with a push/pop helper!

The modelview stack should start with a camera view then any of the translate, scale and rotate operations. For instance,

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt( ... ); //camera view

glPushMatrix();

glTranslate(...);

glRotate(...);

glScale(...);

draw_your_mesh_etc_here();

glPopMatrix();

// repeat for each of your meshes as needed ie push...pop

This in pseudo code means switch to the MODELVIEW matrix, lets call it M. Then

M = identity matrix

M *= camera view matrix defined by gluLookAt

M.push(M); copy the present matrix to the top of the stack

M *= translation matrix; // working on copy / top of stack

M *= rotation matrix;

M *= scale matrix;

draw mesh operations like vertex arrays, gluSphere, whatever you like

M.pop(); M now is identity * camera view again.

Note, The minimum MODELVIEW stack depth is 32. Which means you can do 32 glPushMatrix operstions before having to unwind the stack with glPopMatrix().

For the Projection mode, you don't want to put the model operations, but rather one of the projection matrices; glOrtho, glFrustum, or gluPerspective.

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

//glOrtho(...);

//glFrustum(...);

//gluPerspective(...);

Pseudo code now say lets switch to the projection Matrix, lets call it P.

P = identity matrix

P *= ortho view or frustrum view or perspective view

You will notice I don't mention the glPush/Pop stuff here with the PROJECTION matrix. Because in 100% of my programs I never need them with the PROJECTION matrix. Typically I set the projection matrix once and therefore don't need to modify it. However there is nothing in openGL stopping you from using push/pop with it, neither does openGL complain about doing the translate, rotate, scale, lookat operations with it -- it just doesn't make sense to do those operations with it in the typical application. Also note that the matrix stack is only two deep for the PROJECTION matrix stack. There are probably experts who find use for the push/pop operations with the PROJECTION matrix critical but for most of the times it is not necessary.

ajbiv

02-17-2009, 03:39 PM

thanks that helps, since i'm a beginner i'll stick with the normal use :)

Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.