CMSC 435/634: Introduction to Computer Graphics

Assignment 5
Enhanced Ray Tracing and Realism
Due May 8, 2012
No late projects accepted

The Assignment

Extend your ray tracer from Project 2 to implement lighting and shading. First, implement shadows, diffuse shading, Phong shading, and reflection. Then, add support for at least one type of procedural texture that includes noise. As there is a large variety of topics to cover, before beginning the project, read the following sections in the textbook and class notes.



As you cast rays into the scene, when an intersection is found, cast a shadow ray from the point of intersection to each light source, in turn. If a shadow ray hits another object before reaching a light source, then that light source is blocked. If a light source is behind a surface, i.e., in a direction opposite to the surface normal, then that light source is blocked. Each light source that is not blocked should contribute to the local surface illumination.

Local surface illumination (diffuse and Phong shading)

The surface illumination includes diffuse (aka Lambertian) and specular (aka Phong) components. The NFF format does not allow for an ambient component. Each light that is not blocked should contribute color according to:

localColor = lightColor * (Kd * surfaceColor * diffuse + (Ks * specular)^Shine),
where lightColor, Kd, surfaceColor, and Ks are determine from the values in the NFF file, diffuse is calculated according to Lambert's law, and specular is calculated according to the Blinn-Phong shading model (use the half vector in place of the reflection vector). Where there are multiple lights, you should add the results for each light together. Since the NFF format does not define intensities for light sources, you may find it useful to scale each light's contribution by 1 / sqrt(# of lights).


You should cast reflection rays from each intersection point into the scene whenever Ks is greater than zero. The total color of a point in the scene (i.e., what you should write to your output) is:

totalColor = localColor + Ks * reflection
This is a recursive process, since the color of the reflection rays is the color of whatever they hit. To avoid infininite recursion, limit the ray tree depth (i.e. number of bounces) to 5. You may skip the reflection ray if the contribution to the final color would be less than 1/255, but that optimization is not necessary.

Procedural texture
Add procedural texturing to the ray tracer. You will be given two different materials in class. Select one object and make your ray traced objects look as much like this materials as possible. A portion of the grading for this project will be subjective, with points awarded for realism. You should try to match the appearance of the material as closely as possible.

Re-Using Assignment 2 Code

You are expected to complete assignment 5 by modifying your code for assignment 2. If you did not complete assignment 2, or if you think you will be unable to modify your code, you can request a working implementation from the TA.

Sample Input Files

There are a few sample input files located at:

The files balls1.nff, balls2.nff, and balls3.nff are identical to the sample files from project 2, except that they include lighting commands. As before, the files correspond to different levels of recursion for fractally defined spheres.

Input File Format

As in project 2, the input file will use a subset of the NFF file format. You should support the commands below, in addition to the ones used in project 2 ("f" has been repeated since this assignment uses additional features of it). You will need to modify the format to add support for selecting a texture. You are free to modify the input format in any way you see fit, but you should document your changes in your readme file. An example of how you might specify a texture is given below.
Positional light.  A light is defined by XYZ position.  Description:
    "l" X Y Z [R G B]

    l %g %g %g [%g %g %g]

    All light entities must be defined before any objects are defined (this
    requirement is so that NFF files can be used by hidden surface machines).
    Lights have a non-zero intensity of no particular value, if not specified
    (i.e. the program can determine a useful intensity as desired); the
    red/green/blue color of the light can optionally be specified.
Fill color and shading parameters.  Description:
    "f" red green blue Kd Ks Shine T index_of_refraction

    f %g %g %g %g %g %g %g %g

    RGB is in terms of 0.0 to 1.0.

    Kd is the diffuse component, Ks the specular, Shine is the Phong cosine
    power for highlights, T is transmittance (fraction of contribution of the
    transmitting ray).  Usually, 0 <= Kd <= 1 and 0 <= Ks <= 1, though it is
    not required that Kd + Ks == 1.  Note that transmitting objects ( T > 0 )
    are considered to have two sides for algorithms that need these (normally
    objects have one side).

    The fill color is used to color the objects following it until a new color
    is assigned.
Procedural texture.  Description:
    "t" N [scale]

    t %d [%g]

    The parameter N specifies which procedural texture should be used.  Valid
    choices are 0 to 3.  The optional scale parameter influences the calculation
    of texture coordinates (default is 1.0).

    Like the fill color, the texture is applied to the objects following it until
    a new texture is assigned.

Extra Credit (some mandatory for 634)

Standard Procedural Database

More sample scenes in NFF format can be created using a set of programs called the "Standard Procedural Database", which is freely downloadable. A copy of the SPD is also located at:

The sample files listed above were created using the SPD program 'balls', which writes an NFF file to standard output. For example:
    balls -s 1 > balls1.nff
The -s option controls the level of recursion. Note that the output of the SPD programs typically includes NFF commands other than the ones required for this assignment (balls outputs a single polygon which was removed from the sample files). Therefore, if you want to test your ray tracer using the SPD programs, you should be able to handle the presence of such commands in your input file (you should simply ignore any commands you do not implement).

The program 'shells' outputs many spheres, similarly to balls; 'lattice' and 'jacks' output cylinders and cones, 'tetra' and 'teapot' output primarily convex polygons, and 'gears' includes concave polygons.

What To Turn In

Submit your assignment as 'Proj5' using cvs. Include your source code and makefile. Please do not submit PPM files, as they can be very large. If you must submit images, convert them to a compressed format such as .jpeg. The 'convert' program is available on the GL servers, so for example you can do:

    convert image.ppm image.jpg

Also include a readme file with a description of what hardware / software environment you used to develop your project, and a description of any help you received or outside resources you used. (If you received no help beyond the text and course staff, state as much.) Your readme should also include any instructions necessary for using your program.

Working At Home

If possible, don't. We test things out on the university computers and may or may not be able to help you if things don't work right for you at home. If you do work at home, your final submitted version must be able to run on the gl machines and must be electronically submitted there.