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 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



4.) In your original ray-sphere interection calculation you simply colored the pixel white if you detected an intersection, now it is time to make your ray tracer more interesting. For this next step you will implement the phong shading model.





Where kd ks ka are the diffuse, specular and ambient coefficients for the sphere. Equivalently ld ls la are the diffuse, specular, and ambient coefficients for the light source. Recall from lecture that this equation is re-written in the following form.



L is a vector in the direction of the light source, N is the normal vector of the surface, V a vector in the viewing direction and R is the light reflectance vector. Note that R is calculated using



To do this you must do a few things.

When you are done you should have a sphere that looks something like the following. The color, light and specular point will vary depending on where you place your light source and the values you choose for k,l, and alpha.