Introduction to OpenGL Transformations - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Introduction to OpenGL Transformations

Description:

Modeling transformation: change shapes and locations of objects to position them ... Start with basic set of 3D primitives: Cubes, spheres, prisms ... – PowerPoint PPT presentation

Number of Views:145
Avg rating:3.0/5.0
Slides: 33
Provided by: xiaoyu1
Category:

less

Transcript and Presenter's Notes

Title: Introduction to OpenGL Transformations


1
Introduction to OpenGL Transformations
2
Compositions of Transformations
Order is important ! The transformations are
applied from right to left.
3
OpenGL Transformations
  • Transformations are essential in OpenGL rendering
    pipeline
  • Model Transformation
  • Viewing Transformation
  • Projection
  • Viewport Transformation

4
OpenGL Transformations
  • Modeling transformation change shapes and
    locations of objects to position them correctly
    in the scene.
  • Viewing transformation specify camera position
    and orientation make camera the origin of
    coordinate system.
  • Modelview transformation the combination of
    modeling and viewing transformation.
  • Projection define viewing frustum and project
    the scene from 3D to 2D.
  • Viewport transformation Map the projected scene
    to the actual display window.

5
OpenGL Transformation Matrices
  • Transformations are represented as matrices in
    the OpenGL
  • Modelview matrix combining the modeling and
    viewing transformation.
  • Used for moving objects in the scene and setting
    up camera position and orientation.
  • Projection matrix representing projection
    transformation
  • Set up the viewing frustum
  • You can modify either matrix. Use
    glMatrixMode(mode) to switch current matrix to be
    modified
  • Mode GL_MODELVIEW or GL_PROJECTION

6
OpenGL Transformation Functions
  • Functions to change current matrix
  • Translation
  • glTranslatef(dx, dy, dz)
  • Rotation
  • glRotatef(angle, x, y, z)
  • Rotate about an axis through the origin in a
    direction (x, y,z).
  • Scaling
  • glScalef(sx, sy, sz)
  • Scaling about origin
  • When one transformation function is called, its
    corresponding matrix is multiplied to the right
    of the current matrix.
  • Transformation most recently defined is applied
    first. -- OpenGLs backwards transformation rule

7
OpenGL Transformation Matrix
  • glLoadIdentity() Set current matrix to Identity
    Matrix
  • Loading your own matrix to replace current
    matrix. Note the column-order of matrix elements.
  • GLfloat m16 a0, a1, a2, , a15
  • glLoadMatrixf(m)
  • Multiplying a custom matrix to the right of
    current matrix
  • glMultMatrixf(m)

8
Viewing Transformation
  • Since viewing transformation is applied after
    modeling transformation, you should define
    viewing transformation first in the modelview
    matrix
  • Viewing transformation sets up the camera
    position and orientation and maps coordinates
    from the world coordinate system to the viewing
    coordinate system.
  • The camera is located at the origin of the
    viewing coordinate system and faces the z
    direction.

9
Intuitive Camera Specification
  • How to specify a camera
  • gluLookAt(eyex, eyey, eyez, centerx, centery,
  • centerz, upx, upy, upz).
  • (eyex, eyey, eyez) Coordinates of the camera
    (eye) location in the world coordinate system
  • (centerx, centery, centerz ) the look-at point,
    which should appear in the center of the camera
    image, specifies the viewing direction
  • (upx, upy, upz) an up-vector specifies the
    camera orientation by defining a world space
    vector that should be oriented upwards in the
    final image.
  • This intuitive specification allows us to specify
    an arbitrary camera path by changing only the eye
    point and leaving the look-at and up vectors
    untouched.
  • Or we could pan the camera from object to object
    by leaving the eye-point and up-vector fixed and
    changing only the look-at point.

10
Example
  • void display()
  • glClear(GL_COLOR_BUFFER)
  • glColor3f(0.0f, 1.0f, 0.0f)
  • glLoadIdentity()
  • gluLookAt(10.0, 0.0, 0.0,
  • 0.0, 0.0, 0.0,
  • 0.0, 1.0, 0.0)
  • glBegin(GL_TRIANGLES)
  • glVertex3f(2.0f, 0.0f, 0.0f)
  • glVertex3f(0.0f, 2.0f, 0.0f)
  • glVertex3f(0.0f, 0.0f, 2.0f)
  • glEnd()
  • glFlush()

11
The Matrix for gluLookAt
  • (u, v, w, eye) forms the viewing coordinate
    system.
  • w eye look
  • u up w
  • v w u
  • The matrix that transforms coordinates from world
    frame to viewing frame.
  • dx - eye u
  • dy - eye v
  • dz - eye w

12
gluLookAt Matrix Example
  • gluLookAt(10.0, 0.0, 0.0,
  • 0.0, 0.0, 0.0,
  • 0.0, 1.0, 0.0)
  • Matrix

13
Setup Camera
  • Since viewing transformation is a rotation and
    translation transformation. We can use
    glRotatef() and glTranslatef() instead of
    gluLookAt().
  • In the previous example (view a scene at origin
    from (10, 0, 0) ), we can equivalently use
  • glLoadIdentity()
  • glTranslatef(0, 0, -10)
  • glRotatef(-90, 0, 1, 0)
  • Furthermore, glTranslatef() and glRotatef() can
    be used to define custom viewing control.

14
Set Viewing Transformation
  • Example Plane View -- display the world from the
    point of view of a plane pilot.
  • Void PlaneView(GLfloat planex, GLfloat planey,
  • GLfloat planez, GLfloat roll,
  • GLfloat pitch, GLfloat yaw)
  • glRotatef(roll, 0, 0, 1)
  • glRotatef(yaw, 0, 1, 0)
  • glRotatef(pitch, 1, 0, 0)
  • glTranslatef(-planex, -planey, -planez)

15
Modeling transformations
  • We start with 3-D models defined in their own
    model spaceThink they are all at the origin
  • Modeling transformations create the 3D scene by
    moving the objects to appropriate positions and
    shapes in the world space
  • Obviously different objects would have different
    modeling transformations.

16
OpenGL Matrix Stack
  • For each transformation matrix, OpenGL maintains
    a stack of matrices. The top matrices are the one
    that applied at any given time.
  • glPushMatrix()
  • Make a copy of the current matrices and push it
    to the matrix stack
  • glPopMatrix()
  • Pop out the top matrices from the stack and
    replace current top matrices
  • Those functions will be useful when you want to
    apply modeling transformations to multiple
    objects.
  • Modeling transformations are often applied in a
    hierarchical way.

17
Example
  • Suppose we want to draw a scene of houses and we
    have a cube and a prism primitive.
  • House()
  • PushMatrix()
  • MultMatrix(M)
  • Prism()
  • Popmatrix()
  • PushMatrix()
  • MultMatrix(N)
  • Cube()
  • Popmatrix()

18
Example (II)
  • DrawScene()
  • PushMatrix()
  • MultMatrix(B)
  • House()
  • PopMatrix()
  • PushMatrix()
  • MultMatrix(D)
  • House()
  • PopMatrix()

19
Hierarchical Transformations and Modeling
  • How do we model complex objects and scenes?
  • Describing everything as a single complex model
    is a Bad Idea.
  • Use hierarchical modeling instead...
  • Start with basic set of 3D primitives
  • Cubes, spheres, prisms ...
  • Each is defined in a nice'' way in its own
    modeling space.
  • To put primitives together, use transformations.
  • Each transformation embeds one space in another.
  • Use a whole hierarchy of spaces to build up
    complex models...
  • Not just one Model space'', but one for each
    model and part of a model.

B
D
20
Robot Example
  • Textbook robot example
  • One drawcube() function to draw a cube in its
    local coordinate system
  • Use modeling transformations to construct a robot
    from cubes.

21
Robot Example
glPushMatrix() glColor3f(1.0f, 0.0f, 0.0f) //
red glTranslatef(xPos, yPos, zPos) glScalef(1.0f,
4.0f, 1.0f) // arm is a 1x4x1
cube DrawCube(0.0f, 0.0f, 0.0f) glPopMatrix()
glPushMatrix() glColor3f(1.0f, 1.0f, 1.0f) //
white glTranslatef(1.0f, 2.0f, 0.0f) glScalef(2.0
f, 2.0f, 2.0f) // head is a 2x2x2
cube DrawCube(0.0f, 0.0f, 0.0f) glPopMatrix()
  • glTranslatef(0.0f, 0.0f, -30.0f)
  • glRotatef(angle, 0.0f, 1.0f, 0.0f) // global
    transformations
  • DrawHead()
  • DrawTorso()
  • DrawArms()
  • DrawLegs()

glPushMatrix() glColor3f(0.0f, 0.0f, 1.0f) //
blue glTranslatef(1.5f, 0.0f, 0.0f) glScalef(3.0f
, 5.0f, 2.0f) // torso is 3x5x2 DrawCube(0.0f,
0.0f, 0.0f) glPopMatrix()
glPushMatrix() Arm1Transformation
DrawArm() glPopMatrix() glPushMatrix()
Arm2Transformation DrawArm() glPopMatrix()
glPushMatrix() Leg1 Transformation
DrawLeg() glPopMatrix() glPushMatrix() Leg2
Transformation DrawLeg() glPopMatrix()
22
Projection
  • The projection transformation maps all of our 3D
    coordinates onto our 2D desired viewing plane.
  • Projection transformation set up the viewing
    volume that defines the clipping planes and
    corresponding projection matrix
  • Orthographic (parallel) projection
  • Perspective projection

23
Parallel (Orthographic) Projection
  • The projection matrix for orthographic projection
    is simple

24
Parallel (Orthographic) Projection
  • Set the viewing volume and matrix
  • glMatrixMode(GL_PROJECTION)
  • glLoadIdentity()
  • glOrtho(left, right, bottom, top, near, far)

25
Perspective Projection
  • All projection lines pass through the center of
    projection (eye point). Also called central
    projection
  • In 3D,
  • Have identified all points on a line through the
    origin with a point in the projection plane.

26
Viewing Frustum and Clipping
  • The right picture shows the view volume that is
    visible for a perspective projection window,
    called viewing frustum.
  • It is determined by a near and far cutting planes
    and four other planes
  • Anything outside of the frustum is not shown on
    the projected image, and doesnt need to be
    rendered
  • The process of remove invisible objects from
    rendering is called clipping

far
near
27
Perspective Projection
  • Set viewing frustum and perspective projection
    matrix
  • glFrustum(left,right,bottom,top,near,far)
  • left and right are coordinates of left and right
    window boundaries in the near plane
  • bottom and top are coordinates of bottom and top
    window boundaries in the near plane
  • near and far are positive distances from the eye
    along the viewing ray to the near and far planes
  • Projection actually maps the viewing frustum to a
    canonical cube the preserves depth information
    for visibility purpose.

28
The OpenGL Perspective Matrix
Matrix M maps the viewing frustum to a canonical
cube.
We are looking down the -z direction
29
Perspective Projection (2)
  • If the viewing frustum is symmetrical along the x
    and y axes. It can be set using gluPerspective().
  • gluPerspective(?,aspect,n,f)
  • ? the field of view angle
  • aspect the aspect ratio of the display window
    (width/height).

30
Canonical to Window Transform
(1,1)
(xmax,ymax)
(xmin,ymin)
(-1,-1)
  • After projection, the 3D scene is mapped to a
    canonical 2D region (as left).
  • It still needs to be mapped to the actual display
    region (viewport) in the window.

31
Viewport
  • Viewport The sub-window into which the current
    graphics are being drawn is called a viewport.
  • By default, viewport is the entire window. OpenGL
    allows to set viewport to be a sub-region of the
    window.
  • glViewport(x, y, width, height)
  • (x, y) is the lower-left corner of the viewport

32
Summary
  • Modeling transformations transform objects in
    the world coordinate system
  • Viewing transformations change from world
    coordinate system to eye coordinate system
  • Projection transformations map eye centered
    viewing frustum to a canonical viewing volume
    (NDC)

Local Coordinate Space
World Coordinate Space
View Space
Canonical View Volume
viewport
Write a Comment
User Comments (0)
About PowerShow.com