Ray Tracing Tutorial
Ray Casting One type of visibility algorithm
Basic Idea RayCast(Camera, Scene, img_w, img_h) { for (row=0; row < img_h; row++) { for (col=0; col< img_h; col++) { ray = constructRay(camera, row, col); (hit,color)=getIntersection(ray, scene); image[row][col] = color; } return image }
Constructing Primary Rays CoP u n v X min, Y min
Constructing Primary Rays CoP
Constructing Primary Rays CoP
Constructing Primary Rays CoP
Gathering all the info from.dat file Camera specification Viewport Resolution Camera {perspective, orthographic} CoP point lookat point up vector
Visualizing the components CoP Look at point up vector Width (in pixels) Height (in pixels) x_res y_res focal length
Constructing the Orthogonal Basis CoP Look at point up vector u n v n = normalize lookat vector u = n x up v = u x n
Computing Starting Point CoP u n v X, Y Beware of units. Pixels versus world units focal length
Constructing a Primary Ray CoP
Cast Ray Current Pixel Color (0.2,0.8,0.2) due to ambient term
CoP Check for Intersections Current Pixel Color (0.2,0.8,0.2) due to ambient term Beware of near and far clipping planes as well
Some Tips Try the simplest test case you can think of (eg one sphere, and simple camera location) Break Down into steps –Ray Cast in BW with spheres (ensure generation of primary rays is correct) –Shade with Primary Ray contribution. –Try more complex surfaces Normalize, Normalize, Normalize Clamp color [0,1]
CoP Pick Nearest Current Pixel Color (0.2,0.8,0.2) due to ambient term
Computing the Surface Lighting Effect
CoP Connect Light Ray Current Pixel Color (0.2,0.8,0.2) due to ambient term
I R = I R,ambdiff + I R,diffused + I R,specular I G = I G,ambdiff + I G,diffused + I G,specular I B = I B,ambdiff + I B,diffused + I B,specular
I = I ambdiff + I diffused + I specular For example (0.2, 0.8, 0.2) for this material. A constant. Independent of lighting And viewing direction
I = I ambdiff + I diffused + I specular N L I diffused = k d I l (N.L) Compute for all 3 channels. For example (0.1, 0.1, 0.1). Independent of Viewpoint
I = I ambdiff + I diffused + I specular N L I specular = k s I l (V.R) ns Compute for all 3 channels. For example (0.2, 0.2, 0.2). V R
I = I ambdiff + I diffused + I specular = ( 0.2, 0.8, 0.2) + (0.1, 0.1, 0.1) + (0.2, 0.2, 0.2) = (0.5, 1.0, 0.5) Clamp to [0,1]
CoP Current Pixel Color (0.5,1.0,0.5) Connect Light Ray
What about areas in shadow?
CoP Current Pixel Color (0.2,0.8,0.2) due to Ambient term Connect Light Ray
Ray Tracing Physical World : –Photons shoot out from light sources, reflect off surfaces and into the eye. –Problem : Only small fraction reaches the eye (or image plane). Difficult to simulate. Alternative way to simulate : –Reverse the process! –Trace the path backwards i.e from the eye (or pixels) back to the light sources. –Ray Tracing!
We modify the Ray-Casting algorithm to trace rays bouncing off the surfaces.
Outer Loop Select center of projection and window on view plane; For (each scan line in image) { For (each pixel in scan line) { 1) Construct ray from center of projection through pixel 2) pixel = trace(ray,1); }
color trace(RT_ray ray, int depth) { Determine closest intersection of ray with an object if there is an intersection { 1) compute normal at intersection 2) return shade ( closest object hit, ray, intersection, normal, depth ) } Shade will return the cumulative color by recursively calling trace with depth counter decremented at each invocation.
CoP Contribution From Primary Ray (0.5,1.0,0.5)
CoP Contribution From Secondary Ray (0.5,1.0,0.5) V2V2 V1V1 L (0.8,0.8,0.0)
CoP Final Pixel Color (0.5,1.0,0.5) V2V2 V1V1 L (0.8,0.8,0.0) + = ksks
The End