🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
Tutorial to explain Push and Pop Matrix?
Just use them to "save" the location at where you are
You are at 20,20,40
glPushMatrix();
do stuff here
move to 100,20,40
do more stuff
gpPopMatrix() --> you are back to 20,20,40
Bruno
You are at 20,20,40
glPushMatrix();
do stuff here
move to 100,20,40
do more stuff
gpPopMatrix() --> you are back to 20,20,40
Bruno
Thanks. Someone said you can use them to make a camera....
So do you glPushMatrix() on your object, then translate to move the camera, then when you''re done translating you do glPopMatrix() and it''ll be like you moved the camera because the object will stay in the same place?
So do you glPushMatrix() on your object, then translate to move the camera, then when you''re done translating you do glPopMatrix() and it''ll be like you moved the camera because the object will stay in the same place?
Push and Pop work off a stack... so when you Push a variable its placed on the stack when you call Pop the last value saved is called up and removed from the stack so you have to be careful when you call a lot of them.
glPushMatrix(); // 1
glPushMatrix(); // 2
glPopMatrix(); // Call 2
glPushMatrix(); // 3
glPopMatrix(); // Call 3
glPopMatrix(); // Call 1
hope i cleared it up
glPushMatrix(); // 1
glPushMatrix(); // 2
glPopMatrix(); // Call 2
glPushMatrix(); // 3
glPopMatrix(); // Call 3
glPopMatrix(); // Call 1
hope i cleared it up
If you want to keep the same camera angle you have to Push every time (or else you loose it when you Trans/Rotate/etc)you Pop except when you dont need the matrix anymore.
I believe it was me that said you can use them to *save* a camera, not to make one.
You must still calculate the camera matrix yourself (i.e. with glTranslate and glRotate funcs) but the purpose of the matrix stack was to push them onto the video board cache (where the stack is I think - in hardware at least) so that you can get your camera transformation ultra-quick.
Just remember that if you don''t take all your stack vars off or push too many on then your prog may crash.![](sad.gif)
Here''s an example of that camera idea:
You get the idea now?
========
Smidge
www.smidge-tech.co.uk
========
You must still calculate the camera matrix yourself (i.e. with glTranslate and glRotate funcs) but the purpose of the matrix stack was to push them onto the video board cache (where the stack is I think - in hardware at least) so that you can get your camera transformation ultra-quick.
Just remember that if you don''t take all your stack vars off or push too many on then your prog may crash.
![](sad.gif)
Here''s an example of that camera idea:
glRotatef(-Cam.Rotation.z, 1, 0, 0);glRotatef(-Cam.Rotation.y, 0, 1, 0);glRotatef(-Cam.Rotation.z, 0, 0, 1);glTranslatef(-Cam.Pos.x, -Cam.Pos.y, -Cam.Pos.z);// Push matrix onto stack...glPushMatrix();...inline void ApplyCamera(){ // Pop the matrix to make it the current one, then push it // back onto the stack (note it is just as before, but we now // have the camera matrix that we made earlier. glPopMatrix(); glPushMatrix();}...for (all polygons in level){ if (whatever algorithm you''re using) { // Apply camera and draw at obj''s coords... ApplyCamera(); glTranslatef(Obj.x, Obj.y, Obj.z); DrawPoly(); }}
You get the idea now?
========
Smidge
www.smidge-tech.co.uk
========
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement