View Full Version : Push Pop matrix VS Load identity??

LostInTheWoods

08-01-2002, 08:50 AM

I am a bit new, and a bit confussed. I am making a scene. In the scene I want a room, with about 10 independently moving objects.

I understand if the user moves around the scene I simply load the identity matrix Rotate or translate(in the negative direction), then draw my room. Now I want to draw the objects, after they have moved.

Should I Push the current matrix, move object one, pop the matrix, push the matrix, move object two, pop the matrix, and so on??

Is this how its done??

Liquid

08-01-2002, 10:12 AM

Simple answer: Yes :)

nexusone

08-01-2002, 10:17 AM

You have to remember anything in the current matrix is affected by any changes to it. example would be glrotate and gltranslate.

What happens is when you push the matrix, every thing that has been done before the push matrix is saved in a stack. Look at it like a save command, you have saved and closed the last items you worked on.

Now anything you do after the push matrix will not change the past saved matrix.

now you can translate and rotate a new object without it effecting the past matrix.

Then after you have complete changes to that object you pop the matrix back, which combines the past matrix with the current matrix.

example:

display()

{

glLoadIdenty(); // Lets start a new

glMatrixMode(GL_MODELVIEW);

glRotate(); //Rotate world

glTranslate() //Translater world

glPushMatrix()// Save past matrix to stack

Draw_cube(); Draw a cube some where

glPopMatrix(); Now add it to our past matrix.

with out using the glPush/pop we get a rotation of the whole world each time we call the glrotate command, but by using the glPush/pop we select what objects we want to rotate and translate.

example without using glpush/pop

glRotate(15,1,0,0);

drawcube(); drawn rotated 15 degrees

glRotate(15,1,0,0);

drawcube(); but this cube will be drawn with a rotation of 15 + 15 = 30 degrees

Hope this helps...

Originally posted by LostInTheWoods:

I am a bit new, and a bit confussed. I am making a scene. In the scene I want a room, with about 10 independently moving objects.

I understand if the user moves around the scene I simply load the identity matrix Rotate or translate(in the negative direction), then draw my room. Now I want to draw the objects, after they have moved.

Should I Push the current matrix, move object one, pop the matrix, push the matrix, move object two, pop the matrix, and so on??

Is this how its done??

pleopard

08-01-2002, 01:35 PM

Correct on all accounts ... If you didnt push and pop for every object, each successive object's position and rotation would have to be relative to the previous object.

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.