OpenGL Matrix Stack - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

OpenGL Matrix Stack

Description:

Levels of modeling: Table, room, building, campus. Various ... draw a chair with legs of height 10, length and width 1, with corner at (0,0,0) void DrawChair ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 12
Provided by: csS1
Category:
Tags: opengl | legs | matrix | stack | table

less

Transcript and Presenter's Notes

Title: OpenGL Matrix Stack


1
OpenGL Matrix Stack
  • Soon Tee Teoh
  • CS 116A

2
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
3
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.
4
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_array16 where the
elements of the 4x4 matrix are listed in
column-major order
5
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_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.
6
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 F
Room E
Building 1
Building 2
Building 3
Campus
7
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.

8
OpenGL 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
9
Another 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?
10
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()
11
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()
z
20 10 0
x
0 10 20 30 40 50 60
Write a Comment
User Comments (0)
About PowerShow.com