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.
The framebuffer stuff is handled with
a PixApplet class,
which is just
a variant of the MISApplet you've already been using.
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.
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 have 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 to make your ray tracer fancy and cool. For one thing, i've put only a minimal place-holder for shading. If you want, you can implement the Phong shading model that we covered in class.
Below are some ideas for various things you might want to try for fun, learning and 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:
- object size, shape
- object color, shininess
- camera view point
- camera view direction
- camera focal length
Code base:
PixApplet.java
Shape.java
SimpleRayTracer.html
SimpleRayTracer.java
Sphere.java