Thursday, November 20, 2008

glMatrixMode Cheat Sheet

void FunctionRunWhenProgramStarts()
{
glMatrixMode(GL_PROJECTION); // signal that I want to work with the projection stack
glLoadIdentity(); // make sure that the projection stack doesn't already have anything on it
gluPerspective(whatever I want); // set up a normal perspective projection

glMatrixMode(GL_MODELVIEW); // the rest of my app will only change MODELVIEW
glLoadIdentity(); // make sure that the modelview stack doesn't already have anything on it
}

void FunctionToDrawAFrame()
{
// modelview is still the active stack, because I never that setting
glPushMatrix(); // push current matrix up one, because I'm about to do a bunch of stuff that shouldn't remain active after the end of this function

/* position camera - whatever code you want, this is just an example */
glTranslatef(cameraX, cameraY, cameraZ);
glRotatef(cameraAngle, 0, 1, 0);

/* draw all objects */
for(all objects)
{
glPushMatrix(); //we don't want each object to move the camera...

glTranslatef(objectX, objectY, obectZ);
glRotatef(objectAngle, 0, 1, 0);

DrawObject(); // whatever method you use to draw your object

glPopMatrix(); //restore unaffected camera for next object
}

glPopMatrix(); // restore view prior to movement by this camera
}


From: http://www.allegro.cc/forums/thread/595000

No comments: