Upload
victor-harmon
View
220
Download
1
Tags:
Embed Size (px)
Citation preview
CS 3388Rendering Pipelines: Ray
Casting vs Rasterization
[Hill §12.1—12.3,7.4,3.2.2]
2
“Rendering”
• Generate visual representation of computer’s internal model
• Inverse problem: . Computer Vision
output: bitmap of rendered scene
input: computer model of 3D scene
computer model of camera
rendering
?input: bitmap
output: model
3
“Rendering”
• Problem: determine colour of every pixel
• Two major approaches:– ray tracing (“trace pixel’s path to the objects”)
– rasterization (“project object onto image plane”)
? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?
4
• Important facts about light:– travels along straight lines (geodesics, in general)– can reflect off surface, but usually scatters
• Ray Casting: “what surface point would send light into eye through this pixel?”
• Ray Tracing: “what is total amount of light that would really arrive at that surface point?”
Ray Casting vs Ray Tracing
forward propagation of photons (only tiny fraction enter eye)
ray casting
5
Light Isn’t Obvious
• Pythagoras (582—500BC) thought eyes sent out ‘feeler rays’ to sense objects
a
b
c
“light consists of rays that, acting like feelers, travel in straight lines from the eye to the object, and the sensation of sight is obtained when these rays touch the object, much like the sense of touch.”
6
Aside: “Helmholtz Reciprocity”
• Can interchange light and camera, yet camera sees identical shading at fixed 3D point
http://www.cs.virginia.edu/~mjh7v/vision/final/index.php
Ray Casting
• Photon reaching eye through pixel (i,j) traveled on straight path from final surface
• Find closest intersection on eye-pixel ray!
• Called primary rays
pixel grid² ² ² ² ² ²² ² ² ² ² ²² ² ² ² ² ²² ² ² ² ² ²² ² ² ² ² ²² ² ² ² ² ²
path photon would take
wd£ht pixels
Assuming canonical view frustum:
8
• Ray casting makes very local assumptions:– that photons arrive directly from a light source– that photons are never ‘blocked’ (no hard shadows)
• Hack: angle of light-ray, primary-ray, and normal determine colour
Ray Casting
path photon would take
We cover ray tracing for assignment 4!
9
Ray Casting Loop
• Really simple to implement
• Trivially parallelizable
// pseudocode for ray casting (wd,ht = framebuffer dimensions)raycast(scene,eye) { for i=0..wd-1 for j=0..ht-1 { define ray r_ij relative to eye for each surface in scene { hit = intersection of r_ij with surface (if any) if hit.z is closest to eye so far, keep it } compute colour at hit point store colour at pixel (i,j) }}
10
We’ll Work in Flatland (x-z Plane)
you live here
A2 lives here
11
We’ll Work in Flatland (x-z Plane)
12
We’ll Work in Flatland (x-z Plane)
13
3D from 2D Ray Casting (“2.5D”)
• Define world as 2D lines (walls of height h)• Hit at means 3D points
and perspective projection
x
z
horizon
n
assumes eye at height
14
2D Ray Casting EnginesWolfenstein 3D DOOM
Duke Nukem 3D
a mirror!!
John Carmack
Ken Silverman (when he was 18!!!)
15
Ray Casting vs Rasterization
• Rendering needs final colour for every pixel
• Ray casting “samples” a colour at every pixel independently (perfect!)
• Rasterization does not “sample”…– apply perspective projection only at
sparse 3D points (not surfaces!)– draw lines/surfaces by
interpolating in screen-space
? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?
16
Rasterization Pipeline
• The “graphics pipeline” of a 3D triangle…
(modelview & perspective transformation)
(clipping)
(perspective divide)
(viewport transformation)
model coordinates
clip coordinates
normalized devicecoordinates (NDC)
window coordinates
(Z-culling, rasterization)
(homogeneous)
(cartesian)
17
Model View & Perspective (3D)(modelview & perspective transformation)
clip coordinates(homogeneous)
x
z
x
z
model coordinates
x0
pseudodepth
world coordinates
eye coordinates
clip coordinates
homogenous!!
18
The Projection Matrix
• Derive perspective transformation that distorts view frustum before projection
• Matrix P maps canonical view frustum to canonical view volume
xz
y
¡zx
y
P
(NDC before clipping)
19
• Assume point p in x-z plane
• Orthographic needs
The Projection Matrix
what do we want here?
P
20
• Assume point p in x-z plane
• Perspective is tricky
The Projection Matrix
what do we want here?
P
whaaaa?how does this work?
Deriving the Projection Matrix
• Assuming l = ¡1, r = 1, want
• #1 means must have
#1. need perspective divide #2. must scale x
#3. need pseudodepth(n) = ¡1 pseudodepth(f) = 1
x not relevant to depth so set b1=0
General Projection Matrices
• General perspective matrix in x-z plane
• Full 3D projection matricesorthographic perspective
reminder: we assume near plane z=n for n < 0; textbook assumes z=-n for n>0
orthographic!
what we wanted!
23
Clipping
• Must clip in homogeneous coordinates, before perspective divide!
• Clip to canonical view volume– axis-aligned faces ) easy to clip!
clip coordinates(homogeneous) (clipping)
Excellent research paper explains how it all works: http://research.microsoft.com/pubs/73937/p245-blinn.pdf
Jim Blinn
SIGGRAPH 1978
24
Clipping in x-z Plane
• Canonical view ‘area’ = four boundaries
• For boundary i, three cases:1. both a and b outside i (trivial reject)2. both a and b inside i (don’t clip to i)3. one inside and one outside i (may need to clip)
¡1 · x · 1
¡1 · z · 1
(¡1,1) (1,1)
(¡1,¡1) (1,¡1)
HAVE WANT
three lines two clipped lines
ab
25
Clipping in x-z Plane
• Apply “Cohen-Sutherland” clipping to homogeneous coordinates
// return false if line was clipped away entirely, true otherwisebool clip(a,b) { test a against every boundary test b against every boundary
if a and b both outside same boundary return false // trivial reject
if a and b inside all boundaries return true // trivial accept
for each boundary clip a or b against this boundary (if applicable)
return true if line not empty}
[Hill p.106]
26
Clipping in x-z Plane
• How to test if a=(x,z,w) inside boundary?
• What if w · 0? Means original point was behind near plane! (its z > n)
• Such points automatically fail above tests
x ¸ ¡1 x · 1
z ¸ ¡1
z · 1 Assuming w > 0 we have...
if all are true, then a inside all boundaries!
[Hill p.357]
27
Clipping in x-z Plane
• How to clip line (a,b) to x = ¡1?• Write line parametrically
as• Solve for t such that
• Where have we seen these “boundary coordinate” quantities before?
ab
x ¸ ¡1
a(t)
“boundary coordinate of a on x = ¡1”
“boundary coordinate of b on x = ¡1”
28
Clipping in x-z Planebool clip(a,b) { abc[0..3] = boundary coordinates of point a bbc[0..3] = boundary coordinates of point b
if any abc[i] and bbc[i] both negative return false // trivial reject (outside same boundary)
if all abc and bbc non-negative return true // trivial accept (inside all boundaries)
for i = 0..3 if abc[i] < 0 tmin[i] = abc[i] / (abc[i] – bbc[i]) else if bbc[i] < 0 tmax[i] = abc[i] / (abc[i] – bbc[i])
if max(tmin) > min(tmax) return false // line was clipped away entirely
set a based on max(tmin) // careful not to invalidate tmin/tmax set b based on min(tmax) // by changing a and b before finished! return true}
[Hill. p359]
29
Device Coordinates
• After clipping, all w > 0 so safe to divide
• Must map NDC to viewport coordinates
• V is viewport transformation matrix
(perspective divide)
(viewport transformation)
normalized devicecoordinates (NDC)
window coordinates
(cartesian)
3D Viewport Transformation
30
¡z x
y
NDC window coordinates
viewport
¡z x
y
xy
V
A2 warning: bitmap coordinates have flipped y axis!!
maps pseudodepth to [0,1]
glViewport(...)
31
Raster Interpolation
• Final stage in 3D rasterization pipeline• Take (x,y) window coordinates and draw
2D primitives (points, lines, polygons)– next topic!!
• Also use pseudodepth to figure out which 2D pixels are ‘in front’ of others
convex non-convex always convex
32
The z-Buffer Test
• For every pixel in framebuffer, remember its final pseudodepth (the z-buffer)
• Overwrite colour at (x,y ) if corresponding pseudodepth < zbuffer[x,y] (‘in front’)
• Can draw opaque objects in any order!!
framebuffer (BGR 2[0..255])
z-buffer (pseudodepth 2[0,1])
pseudodepth ¼ 0
pseudodepth z = 1