Recursive Ray Tracing Ron Goldman Department of Computer Science Rice University
Setup 1. Eye Point 2. Viewing Screen 3. Light Sources 4. Objects in Scene a. Reflectivity b. Transparency c. Index of Refraction 5. Bounding Boxes {Optional}
Algorithm For each pixel Find all intersections of ray from eye to pixel with every object in the scene. {Line Surface Intersection) Keep the intersection closest to the eye. {Smallest positive parameter value along the line.} Compute the color and intensity of the light at this intersection point. {Recursion -- see below.} Display the scene.
Color and Intensity at a Point Binary Light Tree I = I direct + k s I reflected + k t I refracted I direct = I ambiant + I diffuse + I specular I direct k s k t I reflected I refracted k s k t k s k t I reflected I refracted I reflected I refracted M Tree truncated at specified depth to avoid infinite recursion.
Reflections, Refractions, and Shadows For each object in the scene Decide whether it is reflecting, transparent, semi-transparent, or opaque. Assign values for k s = reflectivity k t = transparency {for shadowing and refraction} c i = index of refraction opaque = 0 air = 1 Note: if transparent, then I direct = 0
Shadows Algorithm For each visible point cast a virtual ray to each light source. If the virtual ray hits an opaque object before it hits the light source {0<t<1}, then omit the contribution of this light source. I diffuse = I specular = 0 Note: an object may be self shadowed -- lie in its own shadow (e.g. sphere) -- so we must compute intersections even with the object containing the point. If the virtual ray hits a transparent or semi transparent object before it hits the light source, then scale the contribution of this light source and continue to look for further intersections.
Shadows (continued) Shadow Coherence To speed up the algorithm Store current shadowing object. Test it first for next ray. Problem Shadow rays not refracted (if they were they would not hit the light source!). Solution Trace rays starting from light sources. Very expensive. Not generally done.
Reflection For each visible point on a reflecting object: Treat the point as a virtual eye Use the law of mirrors: angle of incidence = angle of reflection to calculate a reflected secondary ray. {See calculation of R(t) below.} Find all intersections of the reflected ray with every object in the scene. Keep the intersection closest to the virtual eye. {Smallest positive parameter value along the line.} Compute the color and intensity of the light at this intersection recursively and add a scaled version of this contribution to the color and intensity of the original point as k s I reflected. {Note: some algorithms also scale by distance.}
Reflection N Eye V (V N)N (V N)N V V (V N)N θ θ W P Surface R(t) = P + tw W = 2(V N)N V {virtual (secondary) ray}
Refraction For each visible point on a transparent object: Treat the point as a virtual eye. c Use Snell s Law: 2 = sin(θ 2) c 1 sin(θ 1 ) to find the refracted secondary ray {See calculation of R(t) below.} Find all intersections of the refracted ray with every object in the scene Keep the intersection closest to the virtual eye. {Smallest positive parameter value along the line.} Compute the color and intensity of the light at this intersection recursively and add a scaled contribution to the color and intensity of the original point as k t I refracted. {Note again some algorithms also scale by distance.}
Refraction Setup N = unit vector normal to surface {known} V = unit vector from surface to eye {known} W = refracted ray {to be computed} R(t) = P + tw {virtual (refracted) ray}
Refraction N Eye V θ 1 P Surface θ 2 N W
Refraction Made Simple Snell s Law c 2 = sin(θ 2) c 1 sin(θ 1 ) Rotation Matrix rot(θ 2,u) = cos(θ 2 )I + ( 1 cos(θ 2 ))(u u)+sin(θ 2 )(u _) sin(θ 2 ) = c 2 c 1 sin(θ 1 ) cos(θ 2 ) = 1 sin 2 θ 2 u = N V N V (unit vector normal to the plane of N and V) Refraction Vector W = N rot(θ 2,u)
Refraction Made Simple (continued) Refraction Vector W = N rot(θ 2,u) = cos(θ 2 )N ( 1 cos(θ 2 ))(N {)u u sin(θ 2 )(u N) =0 = cos(θ 2 )N + sin(θ 2 )(N u) Further Simplification u = N V N V = N V { N {sin(θ V 1 ) = N V sin(θ 1 ) =1 =1 W = cos(θ 2 )N + sin(θ 2) sin(θ 1 ) N (N V) = cos(θ 2 )N + c 2 c 1 N (N V )
Refraction Made Simple (continued) Further Simplification A (B C) = (A C)B (A B)C N (N V) = (N V)N (N1 2 N 3 )V = (N V )N V =1 Refraction Vector W = cos(θ 2 )N + c 2 ((N V)N V) c 1 cos(θ 2 ) = 1 sin 2 θ 2 = 1 c 2 2 2 c sin2 θ 1 = 1 c 2 2 2 1 c (1- cos2 θ 1 ) 1 cos(θ 2 ) = 1 c 2 c 1 2 ( 1-(N V) 2 )
Total Reflection May occur when c 2 c 1 > 1. c 2 c 1 sin(θ 1 ) >1 sin(θ 2 ) cannot exist. Test for total reflection ( 1-(N V)2) >1 1-(N V)2 > c 1 2 c 2 2 c 1 c 2 2 Must check for this case. Be careful!
Observations 1. Refraction is caused by different speeds of light in different mediums. 2. Note that for each color, we use the same index of refraction and therefore the same refracted ray. Technically this is incorrect, but it saves tremendously on computation.
Extents 1. Ray Surface intersections are expensive to compute. 2. Lots of rays are caste -- m(2 n 1) m = number of light sources n = depth of ray tree Even more rays for antialiasing! 3. To speed computations, bound the objects in the scene with simple objects. Each object comes equiped (after some preprocessing) with a. a bounding box in 3-space; b. a bounding rectangle in the viewing plane. 4. Quick reject if ray fails to intersect the bound.
Bounding Boxes and Bounding Rectangles Bounding Boxes -- 3-Space For secondary and virtual rays Sides parallel to axes for faster computation -- coordinate dependent Sides in arbitrary orientations for tighter fit -- view independent Bounding Rectangles -- Viewing Plane Rectangle surrounding projection of bounding box in 3-space View dependent and coordinate dependent -- speed If pixel not within rectangle bound, do not bother to intersect ray with object
Parallel Computations 1. Independent rays can be cast in parallel. 2. Reflection, Refraction, and Shadow rays can be caste in parallel. 3. Ray Surface intersection can be calculated independently for each surface.
Antialiasing Caste rays through corners of pixel instead of center. If 4 corner values are close, take their average. Otherwise Subdivide the pixel into 4 virtual pixels. Recursively compute the intensity of each virtual pixel. Take the average intensity of the 4 virtual pixels. {Equivalently take the average of all the subpixels weighted by area.} Stop subdiving when the 4 coner values are close or the vitual pixel is small. Aliasing is caused by discrete and regular sampling of points Distrributed ray tracing randomly jitters the location of the pixels from their centers