PushMatrix + PopMatrix
I still have not a glue what is the practical use of pushing and popping the matrix when drawing objects. Can you make an example ?
The purpose of glPushMatrix and glPopMatrix is to save all current transformations, do a seperate transformation for a particular object, and then reload the previously saved matrix.
For example, say you wanted to draw a square at (5,0,1), you would use the command glTranslatef(5,0,1), and after this transformation, say you wanted to draw the same square at (0,0,0), OpenGL will assume (5,0,1) to be the origin, and draw the same square over the top of the old one, because the matrix has been altered by the glTranslatef command.
glTranslatef(5,0,1);
glBegin(GL_QUADS);
..glVertex3f(0,0,0);
..glVertex3f(1,0,0);
..glVertex3f(1,1,0);
..glVertex3f(0,1,0);
glEnd;
glTranslatef(0,0,0);
glBegin(GL_QUADS);
..glVertex3f(0,0,0);
..glVertex3f(1,0,0);
..glVertex3f(1,1,0);
..glVertex3f(0,1,0);
glEnd;
In the above, the second square is drawn ontop of the first, whereas in:
glPushMatrix;
glTranslatef(5,0,1);
glBegin(GL_QUADS);
..glVertex3f(0,0,0);
..glVertex3f(1,0,0);
..glVertex3f(1,1,0);
..glVertex3f(0,1,0);
glEnd;
glPopMatrix;
glBegin(GL_QUADS);
..glVertex3f(0,0,0);
..glVertex3f(1,0,0);
..glVertex3f(1,1,0);
..glVertex3f(0,1,0);
glEnd;
The 1st quad is drawn at 5,0,1 and then the matrix is restored to draw the second quad at 0,0,0.
Why not try drawing multiple objects with different transformations to see what ugly happenings occur for yourself?
Edited by - wAVaRiaN on January 22, 2001 5:10:24 AM
For example, say you wanted to draw a square at (5,0,1), you would use the command glTranslatef(5,0,1), and after this transformation, say you wanted to draw the same square at (0,0,0), OpenGL will assume (5,0,1) to be the origin, and draw the same square over the top of the old one, because the matrix has been altered by the glTranslatef command.
glTranslatef(5,0,1);
glBegin(GL_QUADS);
..glVertex3f(0,0,0);
..glVertex3f(1,0,0);
..glVertex3f(1,1,0);
..glVertex3f(0,1,0);
glEnd;
glTranslatef(0,0,0);
glBegin(GL_QUADS);
..glVertex3f(0,0,0);
..glVertex3f(1,0,0);
..glVertex3f(1,1,0);
..glVertex3f(0,1,0);
glEnd;
In the above, the second square is drawn ontop of the first, whereas in:
glPushMatrix;
glTranslatef(5,0,1);
glBegin(GL_QUADS);
..glVertex3f(0,0,0);
..glVertex3f(1,0,0);
..glVertex3f(1,1,0);
..glVertex3f(0,1,0);
glEnd;
glPopMatrix;
glBegin(GL_QUADS);
..glVertex3f(0,0,0);
..glVertex3f(1,0,0);
..glVertex3f(1,1,0);
..glVertex3f(0,1,0);
glEnd;
The 1st quad is drawn at 5,0,1 and then the matrix is restored to draw the second quad at 0,0,0.
Why not try drawing multiple objects with different transformations to see what ugly happenings occur for yourself?
Edited by - wAVaRiaN on January 22, 2001 5:10:24 AM
Just adding to wAVaRiaN''s post... I thought it''d be helpful to point out that glPushMatrix() saves the matrix and glPopMatrix() loads the matrix.
The Matrices are saved onto what is known as a "matrix stack". Basically, you can imagine this as being a pile of paper. Each piece of paper representing a matrix.
Matrices are always saved an loaded on the top of this pile/stack. Theres also a limit as to how many matrices can be on the stack... I think its 5 but I''m not sure.
The Matrices are saved onto what is known as a "matrix stack". Basically, you can imagine this as being a pile of paper. Each piece of paper representing a matrix.
Matrices are always saved an loaded on the top of this pile/stack. Theres also a limit as to how many matrices can be on the stack... I think its 5 but I''m not sure.
Protozone
thanks! I suppose I could use this concept also for rotations. Could it be the right solution to the ''external cockpit views 3d math !'' trouble I posted in this forum ?
Actually the OpenGL standard specifies that the modleview matrix stack have "at least" 32 slots but there may be more. the projection view and texture matrix''s are only required to have 2 slots. You can find the number of slots available on the system by calling glGet with GL_MODLEVIER_STACK_DEPTH, GL_PROJECTIONVIEW_STACK_DEPTH, or GL_TEXTURE_STACK_DEPTH.
Another use of the stack is in a hirarcial object structure where one object may be made of several primitives (spheres, cubes, cylinders, etc...) for example.
//draw a cube
DrawCube();
//draw a sphere below the cube
glPushMatrix();
glTranslated(0,-1,0);
DrawSphere();
glPopMatrix();
//draw a sphere above the cube
glPushMatrix();
glTranslated(0,1,0);
DrawSphere();
glPopMatrix();
In this case you have two spheres attached to a cube (one above and one below) you have one routine to draw the sphere and with glPush/PopMatrix you don''t have to worry about any changes that are made to the transform matrix when you move to draw the two spheres.
Just remember that every call to push must have a corosponding pop.
And yes this pushes the entire transform matrix i.e. the move, rotation, and scale information!
http://tannara.2y.net/
Another use of the stack is in a hirarcial object structure where one object may be made of several primitives (spheres, cubes, cylinders, etc...) for example.
//draw a cube
DrawCube();
//draw a sphere below the cube
glPushMatrix();
glTranslated(0,-1,0);
DrawSphere();
glPopMatrix();
//draw a sphere above the cube
glPushMatrix();
glTranslated(0,1,0);
DrawSphere();
glPopMatrix();
In this case you have two spheres attached to a cube (one above and one below) you have one routine to draw the sphere and with glPush/PopMatrix you don''t have to worry about any changes that are made to the transform matrix when you move to draw the two spheres.
Just remember that every call to push must have a corosponding pop.
And yes this pushes the entire transform matrix i.e. the move, rotation, and scale information!
http://tannara.2y.net/
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
no
what it does is
im looking at hendrixs band of gypies album cover
glPushMatrix();
bush becomes persident
US dollar dives
israel is invaded by egypt
aliens invade the earth
bush decides to save earth by blowing up the earth
AAAAAAAAAAAAAAAAHHHHHHHH!
bollux bollux my main man
glPopMatrix()
im looking at hendrixs band of gypies album cover
http://members.xoom.com/myBollux
what it does is
im looking at hendrixs band of gypies album cover
glPushMatrix();
bush becomes persident
US dollar dives
israel is invaded by egypt
aliens invade the earth
bush decides to save earth by blowing up the earth
AAAAAAAAAAAAAAAAHHHHHHHH!
bollux bollux my main man
glPopMatrix()
im looking at hendrixs band of gypies album cover
http://members.xoom.com/myBollux
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement