Title: OpenGL Matrix Stack
1OpenGL Matrix Stack
2OpenGL 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
3OpenGL 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.
4OpenGL 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_array16 where the
elements of the 4x4 matrix are listed in
column-major order
5OpenGL Matrix Operations
- You can also multiply the current matrix
- glMultMatrix(m_array2)
- The current matrix is post-multiplied by the
specified matrix
float m_array16 float m_array216 set up
the arrays glMatrixMode(GL_MODELVIEW) glLoadI
dentity() // sets the current matrix glMultMatrix
f(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
m_array m_array2
current matrix
v
v
Note This means that you are transforming by
m_array2 first.
6OpenGL Matrix Stacks
- For nested transformations
- Levels of modeling Table, room, building, campus
- Various transformations matrices
Room A
Room B
Room C
Room D
Room F
Room E
Building 1
Building 2
Building 3
Campus
7OpenGL 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.
8OpenGL Matrix Stack Example
glMatrixMode(GL_MODELVIEW) glLoadIdentity() glT
ranslatef(100.0,100.0,0.0) glPushMatrix() glT
ranslatef(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() glPushMatrix() gl
Translatef(200.0,200.0,0.0) glBegin(GL_POINTS)
glVertex3f(0.0,0.0,0.0) glEnd() glPushMatrix
() glTranslatef(200.0,200.0,0.0) glBegin(GL_PO
INTS) glVertex3f(0.0,0.0,0.0) glEnd() glPopMa
trix() glPopMatrix()
Transform Building A to desired location on campus
Transform Room A to desired location wrt.
Building A
Model Room A
Transform Room B to desired location wrt.
Building A
Model Room B
Transform Chair B to desired location wrt. Room B
Model Chair B
9Another Example
float M16 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) glP
ushMatrix() 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)
1
2
3
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?
10Example 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()
11Example 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()
z
20 10 0
x
0 10 20 30 40 50 60