Professional Documents
Culture Documents
05 Raycast
05 Raycast
The color of each pixel on the view plane depends on the radiance emanating from visible surfaces
Ray Casting
Tom Funkhouser Princeton University COS 426, Spring 2006
Rays through view plane Simplest method is ray casting Eye position
View plane
Ray Casting
For each sample
Construct ray from eye position through view plane Find first surface intersected by ray through pixel Compute color sample based on surface radiance
Ray Casting
For each sample
Construct ray from eye position through view plane Find first surface intersected by ray through pixel Compute color sample based on surface radiance Rays through view plane
Eye position
Ray Casting
Simple implementation:
Image Image RayCast(Camera RayCast(Camera camera, camera, Scene Scene scene, scene, int int width, width, int int height) height) { { Image image = new Image(width, height); Image image = new Image(width, height); for for (int (int ii = = 0; 0; ii < < width; width; i++) i++) { { for for (int (int jj = = 0; 0; jj < < height; height; j++) j++) { { Ray Ray ray ray = = ConstructRayThroughPixel(camera, ConstructRayThroughPixel(camera, i, i, j); j); Intersection Intersection hit hit = = FindIntersection(ray, FindIntersection(ray, scene); scene); image[i][j] image[i][j] = = GetColor(hit); GetColor(hit); } } } } return return image; image; } }
Ray Casting
Simple implementation:
Image Image RayCast(Camera RayCast(Camera camera, camera, Scene Scene scene, scene, int int width, width, int int height) height) { { Image image = new Image(width, height); Image image = new Image(width, height); for for (int (int ii = = 0; 0; ii < < width; width; i++) i++) { { for for (int (int jj = = 0; 0; jj < < height; height; j++) j++) { { Ray Ray ray ray = = ConstructRayThroughPixel ConstructRayThroughPixel(camera, (camera, i, i, j); j); Intersection Intersection hit hit = = FindIntersection(ray, FindIntersection(ray, scene); scene); image[i][j] image[i][j] = = GetColor(hit); GetColor(hit); } } } } return return image; image; } }
View Plane
2*d*tan()
back
P0
right
towards
towards
P0
right
V P
P1 = P0 + d*towards d*tan()*right P2 = P0 + d*towards + d*tan()*right P = P1 + ((i + 0.5) / width) * (P2 - P1) V = (P - P 0) / ||P - P0 ||
P P2
Ray: P = P0 + tV 0
Ray: P = P0 + tV 0
Ray Casting
Simple implementation:
Image Image RayCast(Camera RayCast(Camera camera, camera, Scene Scene scene, scene, int int width, width, int int height) height) { { Image Image image image = = new new Image(width, Image(width, height); height); for for (int (int ii = = 0; 0; ii < < width; width; i++) i++) { { for for (int (int jj = = 0; 0; jj < < height; height; j++) j++) { { Ray Ray ray ray = = ConstructRayThroughPixel(camera, ConstructRayThroughPixel(camera, i, i, j); j); Intersection Intersection hit hit = = FindIntersection FindIntersection(ray, (ray, scene); scene); image[i][j] image[i][j] = = GetColor(hit); GetColor(hit); } } } } return return image; image; } }
Ray-Scene Intersection
Intersections with geometric primitives
Sphere Triangle Groups of primitives (scene)
Acceleration techniques
Bounding volume hierarchies Spatial partitions Uniform grids Octrees BSP trees
Ray-Sphere Intersection
Ray: P = P0 + tV Sphere: |P - O|2 - r 2 = 0 P P
V r
Ray-Sphere Intersection I
Ray: P = P0 + tV Sphere: |P - O|2 - r 2 = 0 Substituting for P, we get: |P0 + tV - O|2 - r 2 = 0 Solve quadratic equation: at2 + bt + c = 0 where: a=1 b = 2 V (P0 - O) c = |P0 - C|2 - r 2 = 0 P = P0 + tV P P
V r
Algebraic Method
P0
P0
Ray-Sphere Intersection II
Ray: P = P0 + tV Sphere: |P - O|2 - r 2 = 0 L = O - P0 tca = L V if (tca < 0) return 0 d2 = L L - tca if (d2 > r2) return 0
2
Ray-Sphere Intersection
Need normal vector at intersection for lighting calculations N = (P - O) / ||P - O||
Geometric Method
tca V
P P
thc d r r V
P0
P0
Ray-Scene Intersection
Intersections with geometric primitives
Sphere Triangle Groups of primitives (scene)
Ray-Triangle Intersection
First, intersect ray with plane Then, check if point is inside triangle
Acceleration techniques
Bounding volume hierarchies Spatial partitions Uniform grids Octrees BSP trees P0 P
Ray-Plane Intersection
Ray: P = P0 + tV Plane: P N + d = 0 Substituting for P, we get: (P0 + tV) N + d = 0 Solution: t = -(P0 N + d) / (V N) P = P0 + tV V P0 P N Algebraic Method
Ray-Triangle Intersection I
Check if point is inside triangle algebraically
For each side of triangle V1 = T1 - P V2 = T2 - P N1 = V2 x V1 Normalize N1 if ((P - P0 ) N1 < 0) return FALSE; end V1 P0 T3
P T1 N1 V2 T2
Ray-Triangle Intersection II
Check if point is inside triangle parametrically
Compute barycentric coordinates , : = Area(T1T2P) / Area(T1T2 T3) = Area(T1PT3) / Area(T1T2 T3) Area(T1T2 T3) =
1/ 2
Box
Intersect 3 front-facing planes, return closest P 1 T2
(T2-T1) x (T3-T1)
Convex polygon
Same as triangle (check point-in-polygon algebraically)
Concave polygon
Same plane intersection More complex point-in-polygon test
Ray-Scene Intersection
Find intersection with front-most primitive in group
Intersection FindIntersection(Ray ray, Scene scene) { min_t = infinity min_primitive = NULL For each primitive in scene { t = Intersect(ray, primitive); D if (t > 0 && t < min_t) then min_primitive = primitive min_t = t } } A return Intersection(min_t, min_primitive) }
Ray-Scene Intersection
Intersections with geometric primitives
Sphere Triangle Groups of primitives (scene) E F
Acceleration techniques
Bounding volume hierarchies Spatial partitions Uniform grids Octrees BSP trees
Bounding Volumes
Check for intersection with simple shape first
Bounding Volumes
Check for intersection with simple shape first
Bounding Volumes
Check for intersection with simple shape first
If ray doesnt intersect bounding volume, then it doesnt intersect its contents
Bounding Volumes
Check for intersection with simple shape first
If ray doesnt intersect bounding volume, then it doesnt intersect its contents If found another hit closer than hit with bounding box, then can skip checking contents of bounding box
Bounding Volumes
Sort hits & detect early termination
FindIntersection(Ray FindIntersection(Ray ray, ray, Scene Scene scene) scene) { { // Find intersections with bounding // Find intersections with bounding volumes volumes ... ... // // Sort Sort intersections intersections front front to to back back ... ... // // Process Process intersections intersections (checking (checking for for early early termination) termination) min_t min_t = = infinity; infinity; for for each each intersected intersected bounding bounding volume volume ii { { if if (min_t (min_t < < bv_t[i]) bv_t[i]) break; break; shape_t shape_t = = FindIntersection(ray, FindIntersection(ray, bounding bounding volume volume contents); contents); if (shape_t < min_t) if (shape_t < min_t) { { min_t min_t = = shape_t;} shape_t;} } } return return min_t; min_t; } }
Ray-Scene Intersection
Intersections with geometric primitives
Sphere Triangle Groups of primitives (scene)
Uniform Grid
Construct uniform grid over scene
Index primitives according to overlaps with grid cells
Acceleration techniques
Bounding volume hierarchies Spatial partitions Uniform grids Octrees BSP trees D
E F
C A B
Uniform Grid
Trace rays through grid cells
Fast Incremental E Only check primitives in intersected grid cells D F
Uniform Grid
Potential problem:
How choose suitable grid resolution?
Too little benefit if grid is too coarse Too much cost if grid is too fine
E D F
C A B
C A B
Ray-Scene Intersection
Intersections with geometric primitives
Sphere Triangle Groups of primitives (scene)
Octree
Construct adaptive grid over scene
Recursively subdivide box-shaped cells into 8 octants Index primitives by overlaps with cells E Generally fewer cells D F
Acceleration techniques
Bounding volume hierarchies Spatial partitions Uniform grids Octrees BSP trees
C A B
Octree
Trace rays through neighbor cells
Fewer cells More complex neighbor finding E Trade-off fewer cells for more expensive traversal D F
Ray-Scene Intersection
Intersections with geometric primitives
Sphere Triangle Groups of primitives (scene)
Acceleration techniques
Bounding volume hierarchies Spatial partitions Uniform grids Octrees BSP trees
C A B
1 2 4 3
1
E D F 2 C A
4 2
1 3
P D
E F
C A
2
1 2 4 3
P D
E F
C A
4 2
Other Accelerations
Screen space coherence
Check last hit first Beam tracing Pencil tracing Cone tracing
Acceleration
Intersection acceleration techniques are important
Bounding volume hierarchies Spatial partitions
General concepts
Sort objects spatially Make trivial rejections quick Utilize coherence when possible Expected time is sub-linear in number of primitives
Memory coherence
Large scenes
Parallelism
Ray casting is embarassingly parallelizable
etc.
Summary
Writing a simple ray casting renderer is easy
Generate rays Intersection tests Lighting calculations
Image Image RayCast(Camera RayCast(Camera camera, camera, Scene Scene scene, scene, int int width, width, int int height) height) { { Image Image image image = = new new Image(width, Image(width, height); height); for for (int (int ii = = 0; 0; ii < < width; width; i++) i++) { { for for (int (int jj = = 0; 0; jj < < height; height; j++) j++) { { Ray Ray ray ray = = ConstructRayThroughPixel(camera, ConstructRayThroughPixel(camera, i, i, j); j); Intersection Intersection hit hit = = FindIntersection(ray, FindIntersection(ray, scene); scene); image[i][j] image[i][j] = = GetColor(hit); GetColor(hit); } } } } return return image; image; } }
Without Illumination
With Illumination