CMSC 435/634: Introduction to Computer Graphics

Assignment 4

Ray Casting

Due April 8, 2004

The Assignment

For this assignment, you must implement a program that will render spheres using ray casting. Your input will be a text file (provided on stdin) containing information about the view and objects in the scene. Your output should be an image file in PPM format.

Input file

A sample input file is provided in ~olano/public/435/assn4/spheres.sph. The input is in a simple text format, easily parsed with fscanf or fstream. Empty lines or lines beginning with # should be ignored. The remaining lines of the input file appear in two sections. First is a view specification, consisting of lines giving the camera/eye position, center of interest, the view up vector, and image plane distance. Assume an image covering from (-1,-1) to (1,1) on the image plane.

eyeX eyeY eyeZ
coiX coiY coiZ
upX upY upZ

The second section gives all of the geometry. For the base assignment, this consists only of spheres. Each sphere is described by one line starting with the word 'sphere', then giving a series of up to 13 numeric parameters to define the sphere. These parameters are the sphere center, sphere radius, color, diffuse/ambient coefficient, specular coefficient and specular exponent (those three are unusued in this assignment), reflection coefficient, transmission/refraction coefficient and index of refraction.

sphere x y z radius r g b kd ks e kr kt ir

Parameters left out of any sphere line should be assumed to be zero (you can do this by setting the value to zero before you parse the line with either fscanf or fstream >>.

PPM image files

We are using PPM because it is an exceedingly simple format to write. PPM files can be viewed directly or converted to other image formats for viewing. See the man page for 'ppm' for details. The following outlines the code to write a simple PPM file:

unsigned char pixels[HEIGHT][WIDTH][3];
/* fill in pixel array */
/* to set the red component of pixel x,y to .5, use pixels[y][x][0] = 255*.5 */
FILE *f = fopen("file.ppm","wb");
fprintf(f, "P6\n%d %d\n%d\n", WIDTH, HEIGHT, 255);
fwrite(pixels, 1, HEIGHT*WIDTH*3, f);

On the GL systems, you can use "display" to view these files or "convert" convert them into most other image formats.

Extra credit / 634

For extra credit, implement one or more of: reflection, refraction, regular or jittered super-sampled anti-aliasing, or triangle primitives. Each of these features would be worth an additional 5 points. Students taking the class as CMSC 634 are required to implement at least two extra features. If you implement antialiasing, you should control it with a command line argument: for regular super-sampled anti-aliasing use "-a #samples", for jittered supersampling use "-j #samples". In both cases, the default number of samples should be one. For polygons, use the polygon format below (with all appearance parameters after the tri keyword optional):

tri r g b kd ks n kr kt ri
x1 y1 z1
x2 y2 z2
x3 y3 z3

If you do add features not demonstrated in the simple input file, you should create your own or modify the sample to demonstrate the new features and submit it with your project.


This is a big assignment. Start early, or you will probably not finish.

As before, I recommend laying out a plan of attack before coding. It is also worthwhile to get image output working early. Outputting values other than colors at each pixel can be a valuable debugging tool. To assist in your planning, here is an outline of the steps your ray tracing program will need to do:

  1. Read new file format
  2. Calculate ray from eye point through each pixel and into the scene
  3. Calculate ray-sphere intersections, choose smallest/closest
  4. If you have time, add one or more extras

What to turn in

Turn in this assignment electronically as 'cs435 Proj4' using the submit mechanism:

    submit cs435 Proj4 [files...]

The usual assignment boilerplate applies as always -- submit all source files; comments are your friend and ours, they're a good idea and increase your chances of partial credit since they help us understand what you were trying to do. All work must be your own and developed from scratch for this assignment. You may discuss concepts but not code with your classmates. Use of any "borrowed" code is considered cheating and will be treated accordingly. We must be able to build and run on the GL systems to grade. You should use C or C++ for your code, but since no external libraries are used for this assignment, developing on another OS and porting back to GL/Linux should not be too difficult (but do leave some time to do that port).