Homework 4, due Wednesday, Oct 2.

I'm going to try to make this one easier for you, by providing most of the framework. As usual, when you have finished the assignment, post the working applet onto the web. I think we have everyone's URL now.

The files I've linked to at the bottom of this page provide a simple framework for a ray tracer, as well as a simple example scene. You should copy these five files to a folder on your computer before getting to work on them.

As I discussed at the beginning of the semester, this code is built from a PixApplet class (rather than the GenericApplet class). This provides a more efficient way to set individual pixels in the frame buffer.

Your job is to complete the implementation of a Sphere class. If you do this properly, then you'll be able to run the ray tracer to visualize the scene, which consists only of spheres.

You'll recall that any shape that wants to be rendered by a ray tracer needs to implement a traceRay method and a computeNormal method. In the provided code, abstract class Shape consists of just the stubs for these two methods:

public abstract class Shape {
   public abstract int traceRay(double v[], double w[], double t[]);
   public abstract void computeNormal(double p[], double n[]);
}
You need to round out these methods in Sphere, using the algorithms we discussed in class.

Note that traceRay expects to get a ray as input, encoded in the v (ray origin) and w (ray direction vector) arguments. It returns the number of times the ray intersects the shape. Your method should always return either zero or two. If you return two (ie: the ray hits the sphere), then you should place the two respective values of parameter t into t[0] and t[1].

For extra credit:

There are many things you can do for extra credit in this assignment. For one thing, i've put only a minimal place-holder for shading. If you want, you can get a jump on things by implementing the complete Phong shading model that we covered in class.

Below are some ideas for various things you might want to try for extra credit. Some of them are simple, and some are wildly ambitious, but I wouldn't want to hold you back. :-)

Content ideas:

make interesting shapes
try to make a caffeine molecule, etc.
choose interesting colors

Technology ideas:

allow different colors
complete phong shader
reflections (secondary rays bounced off surface)
refraction (secondary rays bent through surface)
other shapes (eg: box, ellipsoid)
boolean intersection/difference
some sort of surface texture effect
allow user to interactively vary:

Code base:

PixApplet.java
Shape.java
SimpleRayTracer.html
SimpleRayTracer.java
Sphere.java