# 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 Z-axis

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

• 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 rays 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.
• color_buffer[x] = color from above
• Write color_buffer to screen or file

## Ray Intersection

For this lab, you need to calculate the intersection of a parametric semi-infinite 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= r2
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 = r2 or At2 + Bt + C = 0 with:
A = D.x2 + D.y2 + D.z2
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 - r2
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..

David S. Ebert