Ray Tracing

By: Raymond Ching

For my final project I decided to create a ray tracer. Ray tracing is a technique used in computer graphics that models realistic reflections and shadows by bouncing light rays off of objects in the scene and tracing their path through the world. The basic algorithm for ray tracing is as follows:

for (every pixel)
if (ray intersects an object)
compute its normals
set pixel color based on lighting
else set pixel color to some background color

Here is a sample of what the scene will look like once this has been implemented.


Figure 1: Early Render

Shading

The next step is getting shading on the objects in the world. A data structure for a light must be implemented and they must be placed in the scene. Here is the basic algorithm for lighting/shading:

for (each light)
calculate distance from light to ray intersection the object
create a ray from the light to the object
if (light ray hits the sphere)
calculate diffuse lighting
calculate specular lighting

Figure 2: Diffuse Lighting


Figure 3: Specular Lighting

Shadows

The light ray that we use in this calculation can also be used to create shadows in our world. Basically, when we are determining how to color our objects based on our light we run another check to see our light ray has already collided with an object before reaching our current point. If that is the case, then no illumination from the that light is considered at this point, creating a shadow.

Figure 4: Lighting with Shadows

Reflection

Finaly reflection needs to implemented. This is done using a while loop that follows thw reflected ray as it bounces off of objects in our scene. The basic algorithm for that can be seen here:
while (reflections less than 10)
calculate reflection coefficient
set start of new ray to current intersection
calculate direction of new ray
reflections++

The constant 10 in this example is a hardcoded number for how many times you want to have your reflections bounce. This number can be changed to lower the time it takes to render the image or increased to have a more precise image. Here are my final renders with reflections, shadows, and lighting all implemented together.

Figure 5: Final Render 1


Figure 6: Final Render 2


Figure 7: Final Render 3

Sources:

  • Codeminder: What is ray tracing? - http://www.codermind.com/articles/Raytracer-in-C++-Introduction-What-is-ray-tracing.html

  • Fundamentals of Computer Graphics, Ch 10: Ray Tracing - http://www.cs.utah.edu/~shirley/books/fcg2/rt.pdf