Title: Viewing and Projections
1Viewing and Projections
2Objective
- Viewing Transformations in 3D Graphics
- OpenGL functions to set up camera view
- Orthographic and Perspective projection
- Projective matrices
- OpenGL function for setting up projection
3Viewing Transformation
- Another change of coordinate systems
- Maps points from world space into eye space
- Viewing position is transformed to the origin
- Viewing direction is oriented along some axis
- A viewing volume is defined
- Combined with modeling transformation to form the
modelview matrix in OpenGL
4Motivation
- In the scene shown, the world space is convenient
for orienting the objects, for example floor,
walls, and the chair. - However, it is not convenient for describing the
camera view. - The goal of a viewing transformation is to
specify a coordinate system that is the most
convenient for the camera given the position and
orientation of our camera in the scene.
5Camera View
- Camera coordinate system
- the camera is located at the origin.
- The cameras optical axis is along one of the
coordinate axes (-z in OpenGL convention) - The up axis (y axis) is aligned with the camera's
up direction - we can greatly simplify the clipping and
projection steps in this frame. - The viewing transformation can be expressed using
the rigid body transformations discussed before.
6Viewing Transformation Steps
- Viewing transformation should align the world and
camera coordinate frames. - We can transform the world frame to the camera
frame with a rotation followed a translation.
Rotate
Translate
7Intuitive 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.
8Example
- void display()
- glClear(GL_COLOR_BUFFER)
- glColor3f(0.0f, 1.0f, 0.0f)
- glLoadIdentity()
- glLookAt(ex, ey, ez,
- 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()
-
9The Matrix for glLookAt
- (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
10Setup Camera
- Since viewing transformation is a rotation and
translation transformation. We can use
glRotatef() and glTranslatef() instead of
glLookAt(). - In the previous example (view a scene at origin
from (10, 0, 0) ), we can equivalently use - glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- glTranslatef(0, 0, -10)
- glRotatef(-90, 0, 1, 0)
- Since the viewing transformation is applied after
modeling transformations, it should be set before
modeling transformations.
11Set Viewing Transformation
- Furthermore, glTranslatef() and glRotatef() can
be used to define custom viewing control. - 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)
-
-
12Projection Transformations
- The projection transformation maps all of our 3-D
coordinates onto our desired viewing plane. - Greatly simplified by using the camera (viewing)
frame. - projection matrices do not transform points from
our affine space back into the same space. - Projection transformations are not affine and we
should expect projection matrices to be less than
full rank
13Orthographic Projection
- The simplest form of projection
- simply project all points along lines parallel to
the z-axis (x, y, z)-gt(x, y, 0) - Here is an example of an parallel projection of
our scene. Notice that the parallel lines of the
tiled floor remain parallel after orthographic
projection
14 Orthographic Projection
- The projection matrix for orthographic projection
is simple - Notice the units of the transformed points are
still the same as the model units. We need to
further transform them to the screen space. - OpenGL functions for orthographic projection
gluOrtho2D(left, right, bottom, top),
glOrtho(left, right, bottom, top, near, far)
15Mapping to Pixel Coordinates
- This process is often called the viewport
transformation. OpenGL function - glViewport(x, y, width, height)
- The variables, left, right, top, bottom, near and
far in glOrtho(left, right, bottom, top, near,
far) refer to the extents of the viewing frustum
in modeling units. - The values width and height are in unit of
pixels. This transformation is little more than a
scale and a translation.
16Perspective Projection
- Perspective projection is important for making
images appear realistic. - causes objects nearer to the viewer to appear
larger than the same object would appear farther
away - Note how parallel lines in 3D space may appear to
converge to a single point when viewed in
perspective.
17Perspective projection
- Also called central projection
- All points on projection lines passing through
the center (eye point) are projected to the same
point on the image plane - Not an affine transformation
- Projection transformation properties
- Angles are not preserved (not preserved under
Affine Transformation). - Distances are not preserved (not preserved under
Affine Transformation). - Ratios of distances are not preserved.
- Affine combinations are not preserved.
- Straight lines are mapped to straight lines.
18Perspective mapping in eye coordinate system
- Given a point S, we want to find its projection
P. - Similar triangles P(xn/z, n)
- In 3D,
- Have identified all points on a line through the
origin with a point in the projection plane. - If we have solids or colored lines, then we need
to know which one is in front''. - This map loses all z information, so it is
inadequate.
19Why Map Z?
- 3D-gt2D projections map all z to same value.
- Need z to determine occlusion, so a 3D to 2D
projective transformation doesn't work. - Further, we want 3D lines to map to 3D lines
(this is useful in hidden surface removal) - The mapping maps lines to lines, but
loses all depth information. - We could use
-
- Thus, if we map the endpoints of a line segment,
these end points will have the same relative
depths after this mapping. - BUT It fails to map lines to lines
20Bad mapping
- In this figure, P,Q,R map to P',Q',R' under a
pure projective projection. - With the mapping
P,Q,R actually map to , which fail
to lie on a straight line.
21Viewing 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
22OpenGL 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.
23The OpenGL Perspective Matrix
Matrix M maps the viewing frustum to a NDC
(canonical cube)
We are looking down the -z direction
24Projective space
The fourth coordinate is not 1. What is the
meaning of this coordinates? Homogenous
coordinates (x, y, z, w) -gt (x/w, y/w, z/w, 1)
25Verifications
glFrustrum(l, r, b, t, n, f)
26Mapping z
- The perspective transformation maps a line to a
line - P(f) 1 and P(n) -1.
- What maps to 0?
so
Note that
27Z mapping formula
- What happens as f and n move away from each
other. - Look at size of the regions n,2fn/(fn) and
2fn/(fn),f. - When f is much larger compared to n, we have
-
-
- So
-
- and
-
- But both intervals are mapped to a region of size
1. - Thus, as we move the clipping planes away from
one another, the far interval is compressed more
than the near one. With floating point
arithmetic, this means we'll lose precision. - In the extreme case, think about what happens as
we move f to infinity we compress an infinite
region to a finite one.
28Near/Far and Depth Resolution
- It may seem sensible to specify a very near
clipping plane and a very far clipping plane - Sure to contain entire scene
- But, a bad idea
- OpenGL only has a finite number of bits to store
screen depth - Too large a range reduces resolution in depth -
wrong thing may be considered in front - Always place the near plane as far from the
viewer as possible, and the far plane as close as
possible
29Perspective 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).
30Perspective Projection
- Suppose we have transformed from the viewing
frustum from Eye coordinate space to Canonical
viewing volume - How do we project onto image plane?
- Simply ignore z coordinate.
- Then why do we map z coordinate?
- Hidden-surface removal!
313D Clipping
- We could clip to all 6 sides of the truncated
viewing pyramid. But the plane equations are
simpler if we clip after projection, because all
sides of volume are parallel to coordinate plane.
- Clipping to a plane in 3D is identical to
clipping to a line in 2D. - We can also clip in homogenous coordinates.
32Summary
- Modeling transformations transform objects into
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)