15-462 Computer Graphics I
Assignment 4 - Ray Tracing
100 points

Due: December 5


In this assignment, you will be building a ray tracer. Your ray tracer will be able to handle opaque surfaces with lighting, shadows, reflections and texture mapping. Provided for you will be starter code that will load scene data from a file.

The first step is to uniformly send out rays from the camera location. Since the camera does not have to move, you can assume this is (0,0,0). You should use backwards ray tracing where rays are sent from the camera, one ray per pixel. The final images should be 640x480, but for debugging you would see faster rendering times for fewer rays. For example, if you halve each dimension, you would send out 1/4th of the number of rays.

There are a number of ways to store the data generated from ray tracing. One way is to directly output each pixel to the screen as it's generated. If you do a flush and swap buffers, you can watch the scene being rendered. The disadvantage is that this may be a slow way to render; a speed up may be to write the data to the display every line, instead of every pixel. Another way to render is to store the data in an off-screen buffer, and send the whole data to the video buffer. This may be faster, as accessing the video buffer directly may be slow. To write to the video buffer, you should use the function glWritePixels. The PIC_PIXELS macro can be used for jpeg writing. You could use the PIC_PIXELS macro to not deal with the OpenGL at all. Just allocate memory for a jpeg file, and use the PIC_PIXELS macro. The source code gives you two methods of plotting a pixel. Using the provided function plot_pixel(), if you provide a second command line argument, it will go into JPEG mode and output the JPEG to the filename of the third agument. If you do not provide a second argumnet, it will plot only to the screen.

The next step would be to write the intersection and texture mapping code. The mathematical solutions for the intersection and texture mapping code are provided in the lecture notes and chapter 12 of Watt.

The third step is to implement the illumination equations. The illumination equation is provided below.

The fourth step is to do a still image showing off your ray tracer

The final step is to implement any extra credit features. You may pick features that interest you from a provided list, or suggest others to the instructor for approval.

Intersection Testing

For intersection testing, the lecture notes and textbook provide you with the equations.

For Phong shading, the normals are required. For triangles, you can interpolate the x,y,z coordinates of the normals at each vertex, and then normalize the length. Use barycentric coordinates for interpolation of triangles. For spheres, the normal is simple to calculate from the center of the sphere.

You will also need to implement texture mapping. For triangles, this is very similar to interpolating normals. For spheres, you should use spherical coordinates for texture mapping.


We suggest using a slightly modified version of the formula provided on page 176 of your textbook. The slight modification is as follows:

  • ka = kd (the ambient material term is the same as the diffuse material term).

The illumination equation for a ray is as follows:

I = kd * Ia + Ii * (kd * (L dot N) + ks * (R dot V) ^ n)

For rays with a nonzero specular component, you should recurse (up to a max depth of at least 3).

To add in the value of the recursive ray, use the following formula:

Ifinal = (1 - ks) * I + ks * Ir

Functionality Requirements

This is the list of requirements for this assignment.

  • Triangle Intersection (15 points)
  • Sphere Intersection (15 points)
  • Triangle texture mapping (15 points)
  • Sphere texture mapping (15 points)
  • Triangle Phong shading (10 points)
  • Sphere Phong shading (10 points)
  • Shadows rays (10 points)
  • Animation or Still (5 points)
  • Recursive reflection (5 points)

Extra Credit Features (Up to 10 points)

  • Recursive refraction (note: recursive reflection is required)(10 points)
  • Good antialiasing (10 points)
  • Motion blur (10 points)
  • Soft Shadows (10 points)
  • Animation (5 points)

Scene Description Format

The first line is the number of objects in the file. There are three types of objects supported: lights, triangles, and spheres. Color values range from 0-1.

The format is as follows:
Number of Objects (1 integer)
Ambient Light (3 floats).
Then you can have lights, spheres or triangles.

  • sphere
    • texture name (up to 40 chars, or null)
    • position (3 floats)
    • diffuse color (3 floats)
    • specular color (3 floats)
    • shininess (1 float)
    • radius (1 float)
  • triangle
    • texture name (or null)
    • then 3 of the following
      • position (3 floats)
      • diffuse color (3 floats)
      • specular color (3 floats)
      • normal (3 floats)
      • shininess (1 float)
      • s,t texture coordinates (2 floats)
  • light
    • position (3 floats)
    • color (3 floats)

Following is an example of a scene description file. It sets a gray sphere of radius 1 at (0,0,-3), with no texture. It sets a white light source at the origin.

		  amb: .3 .3 .3
		  tex: null
		  pos: 0.0 0.0 -3.0
		  dif: .3 .3 .3
		  spe: .5 .5 .5
		  shi: 1
		  rad: 1
		  pos: 0 0 0
		  col: 1 1 1

Animation or Still

In addition to your program, you are also required to hand in a still picture in the form of JPEG files. See the sample code for how to write these files.

  • The animation or still should be something which shows off the features of your program.
  • For example, if you implement extra credit, you should show that off in your still.
  • If you choose to do an animation, it should consist of a series of JPEGs stored in a subdirectory called movie/.
  • If you do a still, please create a directory called movie/ and put 60 copies of your still (000.jpg-059.jpg)
  • The JPEGs should be numbered consecutively starting from 000.jpg.


Grading is based on the above functionality of your ray tracer. You may get up to 10 points of extra credit.

Handin Instructions

In your graphics handin directory, /afs/andrew/scs/cs/15-462/students/user/, create a subdirectory called asst4/. In that directory submit all your files (source code, Makefiles, texture maps, scene descriptions, ...). MAKE SURE YOU INCLUDE A MAKEFILE.

Include a readme file that describes the functionality of your ray tracer. In the readme, list the scene description file(s) that show off your features.

Place your JPEG files in a subdirectory named movie/ Number them consecutively starting from 000.jpg.

Starter Code

The starter code takes a file at the prompt which contains a scene description. It fills global structures containing triangles, spheres, and lights.

As usual, we require that your code compile and run in the graphics cluster.