You are on page 1of 11

OpenGL Matrix Stack

Soon Tee Teoh CS 116A

OpenGL Transformations
glTranslatef(0.0,1.0,32.5); glTranslatei(3,1,88); glRotatef(90.0,1.0,0.0,0.0); // rotate about x axis by 90 degrees glRotatei glScalef(5.0,2.0,-1.0); // performs stretch, shrink, reflect

OpenGL transformation example


glMatrixMode (GL_MODELVIEW); glTranslatef(5.0,0.0,2.0); // third transformation glScalef(2.0,1.0,3.8); // second transformation glRotatef(32.0,0.0,0.0,1.0); // first transformation glBegin(GL_QUADS);

Our intention is to first rotate the quad, then scale it, then translate it. Why reverse order? Because every time OpenGL sees a command, it will post-multiply the current matrix by the transformation.

OpenGL maintains some matrices


We have seen before glMatrixMode(GL_PROJETION) and glMatrixMode(GL_MODELVIEW) This sets the current matrix that will be modified. Here are some modification options:
glLoadIdentity(); // set the current matrix to be identity matrix glLoadMatrixf(m_array); // set the current matrix to be m_array* glTranslatef(1.0,0.3,0.5); // multiply the current matrix by a translate matrix
*m_array is: float m_array[16]; where the elements of the 4x4 matrix are listed in column-major order

OpenGL Matrix Operations


You can also multiply the current matrix:
glMultMatrix(m_array2);

The current matrix is post-multiplied by the specified matrix:


float m_array[16]; float m_array2[16]; set up the arrays glMatrixMode(GL_MODELVIEW);

current matrix

m_array

m_array2

glLoadIdentity(); // sets the current matrix glMultMatrixf(m_array); // adjusts the current matrix glMultMatrixf(m_array2); // adjusts the current matrix glBegin(GL_QUADS); glVertex3f(1.0,0.0,0.0);

current matrix

Note: This means that you are transforming by m_array2 first.

OpenGL Matrix Stacks


For nested transformations Levels of modeling: Table, room, building, campus Various transformations matrices:

Room A

Room B

Room C

Room D

Room E

Room F

Building 1

Building 2

Building 3

Campus

OpenGL Matrix Stack


OpenGL is able to maintain a matrix stack Use operations: glPushMatrix() and glPopMatrix() With glPushMatrix(), OpenGL copies the current matrix to the top of the stack and stores the copy in the second stack position. With glPopMatrix(), OpenGL destroys the top (current) matrix on the stack, and the next matrix on the stack becomes the current matrix.

OpenGL Matrix Stack Example


glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(100.0,100.0,0.0);

Transform Building A to desired location on campus Transform Room A to desired location wrt. Building A Model Room A

glPushMatrix(); glTranslatef(100.0,100.0,0.0); glRotatef(90.0,0.0,0.0,1.0);


glBegin(GL_POINTS); glVertex3f(0.0,0.0,0.0); glEnd(); glPopMatrix();

Transform Room B to desired location wrt. Building A Model Room B

glPushMatrix(); glTranslatef(200.0,200.0,0.0); glBegin(GL_POINTS); glVertex3f(0.0,0.0,0.0); glEnd();

Transform Chair B to desired location wrt. Room B


Model Chair B

glPushMatrix(); glTranslatef(200.0,200.0,0.0); glBegin(GL_POINTS); glVertex3f(0.0,0.0,0.0); glEnd(); glPopMatrix();


glPopMatrix();

Another Example
float M[16] = { 1.0,0.0,0.0,0.0, 0.0,1.0,0.0,0.0, 0.0,0.0,1.0,0.0, 2.0,5.0,1.0,1.0 }; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(1.0,0.0,0.0); glMultMatrixf(M);

1 2 3

glPushMatrix(); glScalef(3.0,2.0,1.0); glTranslatef(1.0,1.0,0.0); glLoadMatrixf(M); geometry glPopMatrix(); glTranslatef(1.0,2.0,3.0);

What is the matrix at the top of the OpenGL matrix stack at point 1? At point 2? At point 3? What is the effective model view transformation that the geometry at point 2 is subjected to?

Example: Modeling a Chair


// draws a cuboid with the lower left front corner at (0,0,0) and with dimensions (length,height,width) void DrawCuboid(float length, float height, float width); // draw a chair with legs of height 10, length and width 1, with corner at (0,0,0) void DrawChair() { DrawCuboid(1,10,1); // draw leg glPushMatrix(); glTranslatef(9,0,0); DrawCuboid(1,10,1); // draw leg glPopMatrix(); glPushMatrix(); glTranslatef(9,0,9); DrawCuboid(1,10,1); // draw leg glPopMatrix(); glPushMatrix(); glTranslatef(0,0,9); DrawCuboid(1,10,1); // draw leg glPopMatrix(); glPushMatrix(); glTranslatef(0,9,0); DrawCuboid(10,1,10); // draw seat glPopMatrix();

Example: Put three chairs in the room


void DrawRoom() { glPushMatrix(); DrawChair(); glPopMatrix(); glPushMatrix(); glTranslatef(10,0,10); DrawChair(); glPopMatrix(); glPushMatrix(); glTranslatef(40,0,0); glRotatef(45.0,0,1,0); DrawChair(); glPopMatrix(); }

20 10 0 0 10 20 30 40 50 60

You might also like