Before you start

This assignment builds on the previous one. If yours worked, I strongly recommend using your own code as a starting point for this assignment. If you were not able to get the first part of your ray tracer working, I will provide access to sample code in ~olano/public/trace (after the late submission deadline for assignment 1). You could either use this code to figure out what was wrong with your assignment, or use it as a base for this one.

balls -s 3 with reflection, shading and shadows

The Assignment

For this assignment, you will add mirror reflection and diffuse and Blinn-Phong specular lighting with shadows to your ray tracer.

You will need to use additional parameters from the 'f' lines in the nff file. The full set of 'f' parameters is

f r g b Kd Ks e Kt ir

Where r,g,b is the base surface color, Kd is a coefficient to scale the overall diffuse contribution, Ks is the coefficient for both reflection rays and a Blinn-Phong glossy specular component with exponent e, Kt is the coefficient for refracted rays, and ir is the index of refraction.

Scale the light color by 1/sqrt(NumLights), so lights that list only a position and no color would have r=g=b=1/sqrt(NumLight). This is a convention of the spd generator programs, as can be seen at line 183 of balls.c as well as in other spd generating programs. If you do not include this scaling, everything will appear quite washed out!

In assignment 1, you needed to track the color at the closest intersection. For this assignment, you will also need to track the surface normal, intersection location, and additional surface parameters. Once you find that intersection, you will need to compute the surface shading. Given unit normal, N; unit vector from the surface intersection point toward the light, L; unit vector halfway between the light and ray, H; and light intensity of 1/sqrt(numLights), sum the contribution from each light. For each light, cast a shadow ray from the surface to the light. If that light is not shadowed, the diffuse and specular contribution from that one light would be computed as:

diffuse = max(0, dot(N,L))
specular = pow(max(0, dot(N,H)), e)
localColor.r += (Kd*r + Ks*specular) * diffuse * lightIntensity
localColor.g += (Kd*g + Ks*specular) * diffuse * lightIntensity
localColor.b += (Kd*b + Ks*specular) * diffuse * lightIntensity

Since the color of each reflection ray is the total color of whatever it hits, including its own diffuse, specular and reflection, compute the color in a recursive process:

Color trace(ray) {
  totalColor = localColor;
  if (Ks > 0 && !recursionLimit)
    totalColor += Ks*trace(reflectionRay)
  return totalColor

Stop the recursion if the total contribution to the final color would be less than 1/255 (one bit in the final color) or if the number of bounces would be greater than 5. The easiest way to do this is to add data to each ray to track the number of bounces left and the total contribution so far. For example, a ray that is the result of one reflection with coefficient Ks1, then one with coefficient Ks2 would have three bounces left and a maximum possible contribution of Ks1*Ks2

balls with depth of field

634 only

Implement depth of field by shooting multiple rays per pixel. You should allow the user to specify the number of rays and aperture size. The focal plane should pass through the 'at' point specified in the nff file.

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. Be sure the Makefile will build your project when we run 'make' (or edit it so it will).

Also include an assn2.txt file telling us about your assignment. Include your name and campus ID at the top of this file. Do not forget to tell us what (if any) help you received from books, web sites or people other than the instructor and TA.

Check in 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 full credit. Individual checkins of complete files one at a time just before the deadline will also NOT get full credit. Do be sure to check in all of your source code, Makefile, README, and updated .gitignore file, but no build files, log files, generated images, zip files, libraries, or other non-code content.