Lab 7: Introduction to Raytracing

Objective: This lab is designed to be a primer for the raytracer assignment. Today you will learn how to calculate intersections between a ray and sphere.

Conceptually, raytracing is a very simple idea. We have a grid which represents the pixels on our screen and the location of the eye. For each pixel, we shoot out a ray from the eye that passes through the center of that pixel. If that ray intersects a piece of geometry after passing through the grid the color of that pixel becomes the result of the shading calculation at that point on the surface of the object; else, the color of the pixel is that of the background. In the case when the ray intersects multiple pieces of geometry, we choose the closest intersection along the ray and perform the shading calculation at that location. Since we will not deal with shading today, if the ray intersects a piece of geometry set the pixel to white; otherwise, set the pixel to black.

1.) Download the starter code here.

2.) Implement ray-sphere intersections. To do this, we will be using a parametric representation for the ray. In vector form the equation for ray is given by



for t > 0, where d and represents the direction and s is the starting point of the ray. We will be working in 3D space so our previous equation corresponds to the following three equations



Given the following equation for a sphere centered at C



we can determine the time of intersection t by plugging in each of the three ray equations into our sphere equation and solving for t. The resulting equation is the following



As you will notice, the preceeding equation is a quadratic which can be solved using the quadratic formula.



We can rearrange our intersection equation to the get the following for A, B, and C



The number of intersections can be determined by the term



in the quadratic formula. If this term is greater than zero we have two intersections. If the term is zero then we have exactly one intersection. Finally, if the term is negative there is no solution to the quadratic and an intersection does not exist.

3.) Raytrace a sphere of radius length 200 at (0,0,0). The result should look like the following