Before you start

This assignment builds on the previous one. If your assignment 1 worked, I strongly recommend using your own code as a starting point for this assignment. If you were not able to get yours working, I will provide access to sample code after the assn1 late submission deadline. You are free to either use this code to figure out what was wrong with your assignment, or use it as a base for this one.

balls3.ray without polygons, using reflection, shading and shadows

The Assignment

For this assignment, you will add mirror reflection, and direct illumination with shadows to your ray tracer (shown at right from balls3.ray without the ground-plane polygon).

File Keywords

To support these features, you will need to find the surface normal and intersection location at the closest intersection along each ray, and handle these additional ray file parameters:

maxdepth bounces
cutoff threshold
light intensity point Lx Ly Lz
surface name
    ambient Ar Ag Ab
    diffuse Dr Dg Db
    specular Sr Sg Sb
    specpow e
    reflect Kr

Code Organization

You will want two ray tracing functions. The first, commonly called probe or anyhit, is used for shadow testing. It should return true if there is any intersection between the start and end of the ray, and does not need to necessarily find the closest intersection or continue intersection testing once it has found a hit.

The second, commonly called trace, should return the overall color from recursively tracing into the scene along a ray. That is, it should find the closest intersection along the ray, and return a color that is the sum of the direct illumination and the recursive reflected color scaled by the surface reflection coefficient. The direct illumination is the sum for each unshadowed light source of the Lambertian diffuse and Blinn-Phong specular. The reflected color is the result of a recursive call to the trace function for a ray starting at the surface intersection point pointing in the reflection direction. The recursion should stop when the number of bounces reaches the maxdepth (default 15), or the product of the reflection coefficients along the ray is less than the cutoff (default 0.002).


Use floating point for all color values until it is time for conversion to the final 8-bit pixel values. Your floating point to byte conversion should include: clamping out-of range values to avoid overflow, gamma correction with a factor of 1/2.2, scaling by 255, then type conversion from float to unsigned char.

Sample Images

To help with your development and debugging, here are the effects of each components (shown from balls3.ray, with the ground-plane polygon)

Diffuse Diffuse + Specular Diffuse + Specular + Reflection
balls3.ray, diffuse balls3.ray, diffuse+specular balls3.ray, diffuse+specular+mirror reflection

Extra credit

For up to 5 points of extra credit, make your ray tracer run faster by parallelizing it.

For 10 points of extra credit, make it run faster with an acceleration structure, either BVH or spatial partitioning.

You can do either parallelization or acceleration structure, or both for a possible total of 15 points extra credit.

100-sample balls3 with antialiasing and depth of field aperture of 0.1

634 only

Also implement antialiasing and depth of field. You will need to add support for two additional ray file keywords:

aperture radius
samples count jitter

For depth of field, choose random ray start positions in a disk in the u/v plane, centered around the eye. For antialiasing, choose random positions within the pixel for each ray. For both, the samples line will tell you how many samples to use. Ignore the jitter or nojitter options for sample, and always randomize the sample locations.

Commit one or more new or modified ray files with aperture and samples settlings that will show off your depth of field effects.

What to turn in

Turn in this assignment electronically by pushing your source code to your class git repository by 11:59 PM on the day of the deadline and tagging the commit assn2. Do your development in the trace directory so we can find it (we'll be able to use the assn2 tag to find that version for grading, so you can just continue development in the same directory).

Commit one or a few sample ppm images in your trace directory so we can see your results before running.

Also include an assn2.txt file at the top level of your repository telling us about your assignment. Tell us what works, and what does not. Describe your sample image files by name so we can know what they are. Also tell us what (if any) help you received from books, web sites, or people other than the instructor and TA.

You must make multiple commits along the way with useful checkin messages. We will be looking at your development process, so a complete and perfectly working ray tracer submitted in a single checkin one minute before the deadline will NOT get credit. Individual checkins of complete files one at a time will not count as incremental commits. Do be sure to check in all of your source code, but no build files, log files, zip files, libraries, or other non-code content.