Archived

This topic is now archived and is closed to further replies.

Infuscare

Matrix stack

Recommended Posts

Let me know if I got this right: when u use glpushmatrix, it sets the coordinates so that when u make a shape it draws there, and when you use glpopmatrix it resets to the coordinates you were at before??? I have this strong feeling thats not it, so can anyone explain that to me...? _________________________________________ "Why, why why does everyone ask ''''why'''' when ''''how'''' is so much more fun" -Spawn 1997

Share this post


Link to post
Share on other sites
When you render a polygon, all the vertices are transformed by the top matrix on the stack. When you call pushmatrix, a copy of the top matrix on the stack is put on the top of the stack. You can then manipulate that matrix with additional transformations, draw some polygons, and get back to the original state by calling popmatrix. Whenever you write a function that draws an object to the screen, it's usually a good idea to push the matrix before you draw anything and pop the matrix afterwards. This way, you can manipulate the matrix however you wish without messing up the calling routine. For example, say you are drawing a car, which consists of a chassis and four wheels. You might have some code that looks like this:


        
draw_car(){

draw_chassis();

// draw tire 1

glPushMatrix();
glTranslate(); // determine the tire's position relative to the chassis

draw_tire();
glPopMatrix();

// draw tire 2

glPushMatrix();
glTranslate();
draw_tire();
glPopMatrix();

// draw tires 3 and 4

...
}

draw_tire(){

glPushMatrix();
glRotate3f(); // animate the tire's rotation

/* code to draw tire */
glPopMatrix();

}


Now, your main draw program can draw several different cars at different locations, scales, and orientations very easily by setting up the matrix and calling draw_car.


        
display(){

// draw car 1 at a certain place and rotation

glPushMatrix(); // save current matrix

glTranslate3f();
glScale3f();
glRotate3f();
draw_car();
glPopMatrix(); // now the matrix is back to how it started


// draw car 2 at a different place and size

glPushMatrix();
glTranslate3f();
glScale3f();
glRotate3f();
draw_car();
glPopMatrix();

}


[edited by - kdogg on May 7, 2003 2:04:14 AM]

[edited by - kdogg on May 7, 2003 2:06:18 AM]

[edited by - kdogg on May 7, 2003 2:06:46 AM]

Share this post


Link to post
Share on other sites