Title: Viewing Transformation
1Viewing Transformation
- Position and aim the camera
- gluLookAt(eyex, eyey, eyez, centerx, centery,
centerz, upx, upy, upz) - Default location at origin looking down the
negative z-axis
2Viewing Transformation (Cont)
gluLookAt(4.0, 2.0, 1.0, 2.0, 4.0, -3.0, 2.0,
2.0, -1.0)
Default location
3Projection Transformation
- Define a viewing volume
- Orthographic projection
- Perspective projection
4Projection Transformation (Orthographic)
- glOrtho(left, right, bottom, top, near, far)
5Projection Transformation (Orthographic) Cont
- gluOrtho2D(left, right, bottom, top)
- A special case of glOrtho() withnear -1 and
far 1 - Vertices with z-coordinate smaller than 1 or
larger than 1 will be clipped away
6Projection Transformation (Perspective)
- glFrustum(left, right, bottom, top, near, far)
7Projection Transformation (Perspective) Cont
- gluPerspective(fovy, aspect, near, far), a
special case of glFrustum() with symmetric
perspective view
8Projection Transformation
- Orthographic projections
- w component is always 1
- Perspective projections,
- w component may not be 1
- Perspective division has to be performed by
dividing all coordinates values by w to get back
the normalized device coordinates
9Viewing Volume Clipping
- Six clipping planes
- Clip away the polygons outside the viewing volume
10Viewport Transformation
- Map the OpenGL image to the window system
- glViewport(x, y, width, height)
- Generally, the aspet ratio of viewport should
equal the aspect ratio of the image on the
projection plane to avoid distortion
11Accumulating Rotations
- Computer graphics application requires user to
spin a model with mouse - Incremental rotations must be accumulated for
accurate orientation - Two naive ways and quaternion way
12Accumulating Rotations (Cont)
- Naïve 1 Perform glRotate() calls for each
incremental rotation - floating-point errors produce non-orthogonal
matrices - model may shrink or grow
- Requires saving and restoring multiple modelview
matrices
X
X
X
13 Accumulating Rotations (Cont)
- Naïve 2 Represent each model orientation as
three angles, each representing rotation around
the x, y, z axis - incorrect result because rotations are not
commutative
X
14Accumulating Rotations (Cont)
- Quaternion Way quaternion represents rotation as
four floating-point values
15Trackball Interface
- trackball.c implements quaternion math
- contains routines for
- converting a rotation matrices to a quaternion
- accumulating quaternions
- converting a quaternion representation to a
rotation matrix
16Trackball Interface (Cont)
- Converting rotations to quaternions
- Call trackball() with the beginning and ending
window coordinates which are normalized to
between 1 and 1. - Example rotation around x-axis
- float deltaQuat4
- trackball(deltaQuat,
- 0.0, -0.1, / starting x, y /
- 0.0, 0.3) / ending x, y /
17Trackball Interface (Cont)
- Accumulating quarternions
- Call add_quarts() to accumulate one quaternion
with another - Example accumulate the first two quaternions and
store in the third one - float currentQuat4
- add_quats(deltaQuat, currentQuat, currentQuat)
18Trackball Interface (Cont)
- Converting a quarternion to a rotation matrix
- Call build_rotmatrix() to convert an accumulated
quaternion to a rotation matrix for modelview
matrix multiplication - Example
- GLfloat rotationMatrix44
- build_rotmatrix(rotationMatrix, currentQuat)
- glPushMatrix()
-
- glMultMatrix(rotationMatrix00)
- ...
- glPopMatrix()
19Trackball UI
- ui.c implements the trackball rotation, panning,
zooming effected by mouse buttons movements - The modelview stack is updated in the following
order TRM - Translation (panX, panY, zoom)x Rotation about
center of scenex Original Viewing Transformation
20Trackball UI (Cont)
- The original viewing transformation is first
applied - Rotation, panning and zooming are applied with
respect to the eye coordinate frame - First store the original viewing transformation
matrix - GLfloat originalViewMatrix44
- glGetFloatv (GL_MODELVIEW_MATRIX,
originalViewMatrix00)
21Trackball UI (Cont)
- Current modelview matrix is updated as follow
- glPushMatrix ()
- glLoadIdentity()glTranslatef (panX, panY,
Zoom) - glTranslatef (0.0, 0.0, -lengthOfView)
- build_rotmatrix (rotationMatrix, quat)
- glMultMatrixf (rotationMatrix00)
- glTranslatef (0.0, 0.0, lengthOfView)
- glMultMatrixf (originalViewMatrix00)
- drawScene()
- glPopMatrix ()