3D Viewing & Clipping: Angel Chapter 5
3D Viewing & Clipping: Angel Chapter 5
Where
Where do
do geometries
geometries come
come from?
from?
Pin-hole camera
Pin-hole camera
Perspective
Perspective projection
projection
Viewing
Viewing transformation
transformation
Clipping
Clipping lines
lines &
& polygons
polygons
Angel Chapter 5
1
Vertex Transformation Pipeline
Vertex
Eye coordinates
ModelView
matrix
Projection Image plane coordinates
matrix and
perspective
division Viewport
transformation
Window coordinates
Eye coordinates
ModelView
matrix glMatrixMode(GL_PROJECTION)
Window coordinates
2
OpenGL Transformation Overview
glMatrixMode(GL_MODELVIEW)
gluLookAt(…)
glMatrixMode(GL_PROJECTION)
glFrustrum(…)
gluPerspective(…)
glOrtho(…)
glViewport(x,y,width,height)
3
Pinhole Optics
• Stand at point P, and look through the hole - anything within the
cone is visible, and nothing else is
• Reduce the hole to a point - the cone becomes a ray
• Pin hole is the focal point, eye point or center of projection.
Image
W
F
I
World
4
Image Formation
Image
F
World
• Projecting a shape
– project each point onto the image
plane
– lines are projected by projecting end
points only
Image W
F I
World Note:
Note: Since
Since wewe don’t
don’t want
want the
the
image
image toto be
be inverted,
inverted, from
from now
now on
on
we’ll
we’ll put
put FF behind
behind the
the image
image plane.
plane.
Orthographic Projection
• when the focal point is at infinity the rays are parallel
and orthogonal to the image plane
• good model for telephoto lens. No perspective effects.
• when xy-plane is the image plane (x,y,z) -> (x,y,0)
front orthographic view
World
Image
F
5
A Simple Perspective Camera
• Canonical case:
–camera looks along the z-axis
–focal point is the origin
–image plane is parallel to the xy-plane at distance d
– (We call d the focal length, mainly for historical reasons)
y
Image
Plane
x
z
F=[0,0,0]
[0,0,d]
Similar Triangles
Y
[Y, Z]
[(d/Z)Y, d]
Z
[0, 0] [0, d]
6
A Perspective Projection Matrix
•Projection using homogeneous coordinates:
– transform [x, y, z] to [(d/z)x, (d/z)y, d]
ªd 0ºªxº
« »« »
0 0
«0 0»«y» ªd º
«0 «
» » >dx dy dz z @ « x y d»
d 0 d
¬z ¼
« 0 d 0»«z » z
«
¬0 0 1 ¬1»
¼«
0» ¼ th
Divide by 4 coordinate
(the “w” coordinate)
7
The View Volume
• Pyramid in space defined by focal point and window in
the image plane (assume window mapped to viewport)
• Defines visible region of space
• Pyramid edges are clipping planes
• Frustum = truncated pyramid with near and far clipping
planes
– Why near plane? Prevent points behind the camera being seen
– Why far plane? Allows z to be scaled to a limited fixed-point
value (z-buffering)
But wait...
8
Camera Control Values
• All we need is a single translation and angle-axis
rotation (orientation), but...
9
Implementation
Implementing the lookat/lookfrom/vup viewing scheme
(1) Translate by -lookfrom, bring focal point to origin
(2) Rotate lookat-lookfrom to the z-axis with matrix R:
» v = (lookat-lookfrom) (normalized) and z = [0,0,1]
» rotation axis: a = (vxz)/|vxz|
» rotation angle: cosT = v•z and sinT = |vxz|
10
y
It's not so complicated…
x
vup
z y
lookfrom x y
z x
START HERE z
lookat
x
Multiply by the projection matrix
z
and everything will be in the
canonical camera position
Rotate about z to bring vup to y-axis
11
Clipping
• There is something missing between projection and
viewing...
• Before projecting, we need to eliminate the portion of
scene that is outside the viewing frustum
y
clipped line
x
z
image plane
near far
y y
clipped line
clipped line
x 1 x
1
near far
z 0 1 z
image plane
near far
12
Clipping to a Cube
• Determine which parts of the scene lie within
cube
• We will consider the 2D version: clip to
rectangle
• This has its own uses (viewport clipping)
• Two approaches:
–clip during scan conversion (rasterization) - check per
pixel or end-point
–clip before scan conversion
• We will cover
– clip to rectangular viewport before scan conversion
Line Clipping
• Modify endpoints of lines to lie in rectangle
• How to define “interior” of rectangle?
• Convenient definition: intersection of 4 half-planes
–Nice way to decompose the problem
–Generalizes easily to 3D (intersection of 6 half-planes)
ymax
interior
=
ymin
x > xmin x < xmax
xmin xmax
13
Line Clipping
• Modify end points of lines to lie in rectangle
• Method:
–Is end-point inside the clip region? - half-plane tests
–If outside, calculate intersection between the line and
the clipping rectangle and make this the new end
point
Cohen-Sutherland Algorithm
• Uses outcodes to encode the half-plane tests results
1001 1000 1010
bit 1: y>ymax
ymax bit 2: y<ymin
0001 0000 0010
bit 3: x>xmax
bit 4: x<xmin
ymin
• Rules:
– Trivial accept: outcode(end1) and outcode(end2) both zero
– Trivial reject: outcode(end1) & (bitwise and) outcode(end2)
nonzero
– Else subdivide
14
Cohen-Sutherland Algorithm: Subdivision
• If neither trivial accept nor reject:
–Pick an outside endpoint (with nonzero outcode)
–Pick an edge that is crossed (nonzero bit of outcode)
–Find line's intersection with that edge
–Replace outside endpoint with intersection point
–Repeat until trivial accept or reject
1001 1000 1010
bit 1: y>ymax
ymax bit 2: y<ymin
bit 3: x>xmax
0001 0000 0010
bit 4: x<xmin
ymin
0101 0100 0110
xmin xmax
Polygon Clipping
Convert a polygon into one or more polygons that
form the intersection of the original with the clip
window
15
Sutherland-Hodgman
Polygon Clipping Algorithm
• Subproblem:
–clip a polygon (vertex list) against a single clip plane
–output the vertex list(s) for the resulting clipped
polygon(s)
Sutherland-Hodgman
Polygon Clipping Algorithm (Cont.)
16
Cleaning Up
• Post-processing is required when clipping creates
multiple polygons
• As external vertices are clipped away, one is left with
edges running along the boundary of the clip region.
• Sometimes those edges dead-end, hitting a vertex on
the boundary and doubling back
–Need to prune back those edges
• Sometimes the edges form infinitely-thin bridges
between polygons
–Need to cut those polygons apart
Ivan Sutherland
17
Beyond Linear Perspective...
Announcements
18
Virtual Trackballs
19
Problems with Pinholes
• Correct optics requires infinitely small pinhole
– No light gets through
– Diffraction
• Solution: Lens with finite aperture
image plane lens W
f
v u
1 1 1
Lens Law:
u v f
20