PDA

View Full Version : pushmatrix and popmatrix



fox
09-04-2000, 03:12 AM
Any one explain how this works? and what effect it in different situations?

NewROmancer
09-04-2000, 03:37 AM
PushMatrix saves the matrix in a stack and PopMatrix gets the matrix from the stack. So with PushMatrix you can save the state of the frameBuffer at one moment and restore it with PopMatrix.

NewROmancer

fox
09-04-2000, 06:40 AM
Does push & popmatrix affect memory? and can their use cause memory leak?

Diego
09-04-2000, 07:30 AM
Matrix information is stored on a sack. PushMatrix pushes a matrix that you define on the stack, thus making it the "active" matrix (becuase its at the top). So any transformations you do, affect the active matrix. Once your done with all the transformation, you "Pop" the matrix off the stack and now the one that was underneath becomes the active matrix.

fox
09-05-2000, 05:08 AM
What are the uses or benefits of using push & popmatrices ? (Please give example - I've tried using them but can't see any point

glTranslated(0.0, 0.0, -25.0);
glRotated(m_xRotate, 1.0, 0.0, 0.0);
glRotated(m_yRotate, 0.0, 1.0, 0.0);
glScalef(2.0f,2.0f, 2.0f);

glMaterialfv(GL_FRONT , GL_AMBIENT_AND_DIFFUSE , RedSurface);
glPushMatrix();
glBegin(GL_POLYGON);
glNormal3d( 0.0, -1.0, 0.0);
glVertex3d( -5.0, -5.0, 5.0);
glVertex3d( 5.0, -5.0, 5.0);
glVertex3d( 5.0, -5.0, -5.0);
glVertex3d( -5.0, -5.0, -5.0);
glEnd();
glPopMatrix();
//Cube 1
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslated(2.0, 1.0, -10.0);
glRotated(25.0, 1.0, 1.0, 0.0);

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.3, 0.0, 0.0);
glVertex3d( 0.3, 0.3, 0.3);
glVertex3d( 0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, -0.3);
glVertex3d( 0.3, 0.3, -0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);

glBegin(GL_POLYGON);
glNormal3d( -0.3, 0.0, 0.0);
glVertex3d( -0.3, -0.3, 0.3);
glVertex3d( -0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, -0.3);
glVertex3d( -0.3, -0.3, -0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.3, 0.0);
glVertex3d( 0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, -0.3);
glVertex3d( 0.3, 0.3, -0.3);
glEnd();


glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);

glBegin(GL_POLYGON);
glNormal3d( 0.0, -0.3, 0.0);
glVertex3d( -0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, -0.3);
glVertex3d( -0.3, -0.3, -0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.0, 0.3);
glVertex3d( 0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, 0.3);
glVertex3d( -0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, 0.3);
glEnd();


glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.0, -0.3);
glVertex3d( -0.3, 0.3, -0.3);
glVertex3d( 0.3, 0.3, -0.3);
glVertex3d( 0.3, -0.3, -0.3);
glVertex3d( -0.3, -0.3, -0.3);
glEnd();


//Cube 2
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslated(4.0, 5.0, -10.0);
glRotated(50.0, 6.0, 1.0, 0.0);

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.3, 0.0, 0.0);
glVertex3d( 0.3, 0.3, 0.3);
glVertex3d( 0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, -0.3);
glVertex3d( 0.3, 0.3, -0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( -0.3, 0.0, 0.0);
glVertex3d( -0.3, -0.3, 0.3);
glVertex3d( -0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, -0.3);
glVertex3d( -0.3, -0.3, -0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.3, 0.0);
glVertex3d( 0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, -0.3);
glVertex3d( 0.3, 0.3, -0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, -0.3, 0.0);
glVertex3d( -0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, -0.3);
glVertex3d( -0.3, -0.3, -0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.0, 0.3);
glVertex3d( 0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, 0.3);
glVertex3d( -0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, 0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.0, -0.3);
glVertex3d( -0.3, 0.3, -0.3);
glVertex3d( 0.3, 0.3, -0.3);
glVertex3d( 0.3, -0.3, -0.3);
glVertex3d( -0.3, -0.3, -0.3);
glEnd();

//CUBE 3
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslated(0.0, 2.0, -10.0);
glRotated(40.0, 1.5, 1.3, 0.0);

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.3, 0.0, 0.0);
glVertex3d( 0.3, 0.3, 0.3);
glVertex3d( 0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, -0.3);
glVertex3d( 0.3, 0.3, -0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( -0.3, 0.0, 0.0);
glVertex3d( -0.3, -0.3, 0.3);
glVertex3d( -0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, -0.3);
glVertex3d( -0.3, -0.3, -0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.3, 0.0);
glVertex3d( 0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, -0.3);
glVertex3d( 0.3, 0.3, -0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, -0.3, 0.0);
glVertex3d( -0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, -0.3);
glVertex3d( -0.3, -0.3, -0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.0, 0.3);
glVertex3d( 0.3, 0.3, 0.3);
glVertex3d( -0.3, 0.3, 0.3);
glVertex3d( -0.3, -0.3, 0.3);
glVertex3d( 0.3, -0.3, 0.3);
glEnd();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.0, -0.3);
glVertex3d( -0.3, 0.3, -0.3);
glVertex3d( 0.3, 0.3, -0.3);
glVertex3d( 0.3, -0.3, -0.3);
glVertex3d( -0.3, -0.3, -0.3);
glEnd();
glPopMatrix();



[This message has been edited by fox (edited 09-05-2000).]

Bob
09-05-2000, 10:51 AM
SetupMyOwnNiftyCameraMatrix();
// oop, I need to make one more translation, and I don't want to recalculate the entire camera matrix after translating because it's too dang slow!
glPushMatrix();
// GREAT, store the matrix on stack!
glTranslatef(car1_position);
DrawCarNumberOne();
// so, now we need to get that cameramatrix back, and recalcilating the matrix will take too long...
glPopMatrix();
// now we got that old matrix back *phew*
// now we want to draw the other car
glPushMatrix();
glTranslatef(car2_position)
DrawCarNumberTwo();
glPopMatrix();


Push and Pop is great when you want to make temporarly translations, like positioning an object, and then get back to the old matrix.

If you can't see the point now, I don't know what to say.