CS 435 Autumn 1997  Introduction to Interactive Graphics
Simple Ray Tracing Guide
Viewing
The simplest ray tracer does hidden surface removal, lighting, and shadows.
To simplify things further, you can trace the scene in a
restricted world (eye) space
by requiring the viewer to be at the
origin, looking down the negative Zaxis
To calculate the field of view,
assume that your rays go from
X: tan(theta) to tan(theta)
Y: ratio*tan(theta) to ratio*tan(theta), where
ratio = (Vyt  Vyb)/(Vxr  Vxl)
General Algorithm Outline
 Read in scene information and object information
 Initialize color table (using ct_gen)
 Calculate viewing parameters and area to raytrace
 For Y = ratio*tan(theta) to ratio*tan(theta)
 Initialize color_buffer to background color

 For X = tan(theta) to tan(theta)
 For each object in the scene
 Test to see if ray intersects each object and
save
t value of this intersection and object number
if this is the closest one so far.
 Shoot shadow ray for this pixel for the closest object
Shadow ray origin = Intersection point
Shadow ray direction =
light  Intersection point
 Shoot reflection ray for this pixel for the closest object
Reflection ray origin = Intersection point
Reflection ray direction =
2*N*(N.E)  E
 Calculate the illumination and color of this object based on
the
illumination model, shadow information, and reflection
color.
I suggest adding Ir*ks*.5 for the reflected color.
 color_buffer[x] = color from above
 Write color_buffer to screen or file (using write_buffer cmd)
Ray Intersection
For this lab, you need to calculate the intersection of a parametric semiinfinite ray with a sphere:
Let the ray be R(t) = O + D*t
Rx = O.x + t * D.x
Ry = O.y + t * D.y
Rx = O.z + t * D.z
where O is the origin of the ray,
D is the direction of the ray, and
t > 0 traces out the ray.
If we have a sphere centered at (cx,cy,cz) with radius r,
then the points on the sphere satisfy
(x cx)^{2} + (y cy)^{2} + (z cz)^{2}= r^{2}
To solve this, we put the ray's Rx, Ry, Rz into the sphere equation and solve
for t.
(O.X + D.x * t  cx)^{2} + (O.y + D.y * t  cy)^{2} + (O.z + D.z * t  cz)^{2} = r^{2}
or At^{2} + Bt + C = 0
with:
A = D.x^{2} + D.y^{2} + D.z^{2}
B = 2 * (D.x * (O.x  cx) + D.y * (O.y  cy) + D.z * (O.z  cz))
C = (O.x  cx)^{2} + (O.y cy)^{2} + (O.z  cz)^{2}  r^{2}
So, the quadratic equation can be used to solve for the t's. If no real roots
exist, then there is no intersection.
Choose the smallest t for the closest point..
Color Calculation:
Use the edge library calls ct_gen and write_buffer to create
the color table and to write the scanline information for the image. See the
sample program "scan_sample.c" in the class area for examples of how to use these functions.
David S. Ebert
December 1998