Skip to main content
GameDev.net gamedev.net
🔒 Locked

When to save modelview matrix?

Started by SelethD Oct 13, 2005 at 12:28 PM 3 replies 800+ views
Original Post
SelethD
SelethD
I have been looking at several tutorial, and all of them draw several objects with only a LoadIdentity, like this... glLoadIdentity(); **Draw Object** repeat... So when do you ever push the modelview matrix onto the stack, and pop it, if just calling glLoadIdentity() resets everything for the next object? I was under the impression that you needed to glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); **Draw Object** glMatrixMode(GL_MODELVIEW); glPopMatrix(); repeat... Which way is correct?
Kalidor
Kalidor
Quote:
Original post by SelethD
Which way is correct?
It depends on what you need to do, but in most cases I would say neither is correct. That is because loading the identity on the modelview matrix will destroy your viewing transformation as well (if you're using it correctly, as in not adding your viewing transformation to the projection matrix). So it should really be something more like this...
glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(...);  //Or whatever you use to apply your viewing transformationforeach(object){     glPushMatrix();     //translate, scale, and/or rotate to apply model transformation     //draw object     glPopMatrix();}
SelethD
SelethD
That is soooo helpful.

It seems there are no tutorials or source that use the push/pop so one is left to try and figure it out on their own.

Ok, so...

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(...); //Or whatever you use to apply your viewing transformation

goes like at the begining of the Draw() function, that is called every frame?

and then inside the draw function, one would look through objects and apply...

foreach(object)
{
glPushMatrix();
//translate, scale, and/or rotate to apply model transformation
//draw object
glPopMatrix();
}


Do I need to do a LoadIdentity for each object or not?
SelethD
SelethD
Ok, now I see, when you pus and pop, unless you do a laod identity, your just adding onto the current matrix, so unless your rendering objects that are relative to other objects, its really not necessary.

I totally get it, thanks so much.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.