3D Viewing & Clipping: Announcements Where Do Geometries Come From?
3D Viewing & Clipping: Announcements Where Do Geometries Come From?
Getting Geometry on the Screen Given geometry in the world coordinate system, how do we get it to the display?
Transform to camera coordinate system Transform (warp) into canonical view volume Clip Project to display coordinates (Rasterize)
Viewing and Projection Our eyes collapse 3-D world to 2-D retinal image (brain then has to reconstruct 3D) In CG, this process occurs by projection Projection has two parts:
Viewing transformations: camera position and direction Perspective/orthographic transformation: reduces 3-D to 2-D
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.
Use homogeneous transformations As you learned in Assignment 1, camera can be animated by changing these transformations the root of the hierarchy
Computer Graphics 15-462 5
Image Formation
Image F World
W
scene point
Projecting a shape
project each point onto the image plane lines are projected by projecting end points only
Image W
View plane or image plane - a plane behind the pinhole on which the image is formed
point I sees anything on the line (ray) through the pinhole F a point W projects along the ray through F to appear at I (intersection of WF with image plane)
Computer Graphics 15-462 7
f
v u
I
World
Lens Law:
Computer Graphics 15-462 8
1 1 1 + = u v f
Computer Graphics 15-462
Note: Since we don't want the Note: Since we don't want the image to be inverted, from now on image to be inverted, from now on we'll put F behind the image plane. we'll put F behind the image plane.
9
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
Similar Triangles
Y [Y, Z]
[(d/Z)Y, d]
Z [0, 0] [0, d]
F=[0,0,0]
vup: a vector that is pointing straight up in the image usually want world up direction
10
11
12
0 d 0 0
But wait... What if we want the camera somewhere other than the canonical location? Alternative #1: derive a general projection matrix. (hard) Alternative #2: transform the world so that the camera is in canonical position and orientation (much simpler) These transformations are viewing transformations They can be specified in many ways - some more sensible than others (beware of Foley, Angel and Watt are ok)
Computer Graphics 15-462 16
17
18
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: cos = vz and sin = |vxz|
y x z y x z Translate LOOKFROM to the origin Rotate the view vector (lookat -lookfrom) onto the z-axis.
START HERE
lookat
glRotate(, ax, ay, az) (3) Rotate about z-axis to get vup parallel to the y-axis
LOOKFROM: LOOKAT:
Where the camera is A point that should be centered in the image VUP: A vector that will be pointing straight up in the image FOV: Field-of-view angle. d: focal length WORLD COORDINATES
Computer Graphics 15-462 20
y x
Multiply by the projection matrix and everything will be in the canonical camera position
19
21
Virtual Trackballs
Imagine world contained in crystal ball, rotates about center Spin the ball (and the world) with the mouse Given old and new mouse positions
project screen points onto the sphere surface rotation axis is normal to plane of points and sphere center angle is the angle between the radii
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 x
clipped line
y x
clipped line
z
image plane near far
z
image plane near far
Need to clip objects to the frustum (truncated pyramid) Now in a canonical position but it still seems kind of tricky...
Computer Graphics 15-462 22 Computer Graphics 15-462 23
Converts perspective frustum to orthographic frustum This is yet another homogeneous transform!
24
The Normalized Frustum OpenGL uses -1<=x<=1, -1<=y<=1, -1<=z<=1 But it doesnt really matter we can clip against any such cube.
Or, we can translate normalizing transformations by applying the appropriate trans.
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
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)
Cohen-Sutherland Algorithm
Uses outcodes to encode the half-plane tests results
1001 ymax 0001 0000 0010 1000 1010
ymin
ymax
interior
ymin xmin xmax
28
y < ymax
y > ymin
x > xmin
x < xmax
Both endpoints inside: trivial accept One inside: find intersection and clip Both outside: either clip or reject (tricky case)
0101 xmin
0100 xmax
0110
Rules:
Trivial accept: outcode(end1) and outcode(end2) both zero Trivial reject: outcode(end1) & (bitwise and) outcode(end2) nonzero Else subdivide
Computer Graphics 15-462 30
29
Cohen-Sutherland Algorithm
Uses outcodes to encode the half-plane tests results
1001 ymax 0001 0000 0010 1000 1010
Polygon Clipping Convert a polygon into one or more polygons that form the intersection of the original with the clip window
Rules:
Trivial accept: outcode(end1) and outcode(end2) both zero Trivial reject: outcode(end1) & (bitwise and) outcode(end2) nonzero Else subdivide
Computer Graphics 15-462 31 Computer Graphics 15-462 ymin 0101 xmin 0100 xmax 0110
32
33
Sutherland-Hodgman Polygon Clipping Algorithm (Cont.) To clip vertex list against one half-plane:
if first vertex is inside - output it loop through list testing inside/outside transition - output depends on transition:
> in-to-in: > out-to-out: > in-to-out: > out-to-in: output vertex no output output intersection output intersection and vertex
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
34
35
36