CS 248: Introduction to Computer Graphics

Pat Hanrahan


Assignment 4 - Getting Started

In this assignment, you will be writing a few functions that then will get linked into our interactive viewer. Our viewer will read in and display an object, using the shading functions that you have written to determine the color at each vertex of the model. First, make a directory in which you will be developing your functions. Next, execute the following commands to set up your directory with the viewer software:

If you will be developing in C, type:
/usr/class/cs248/assignments/assignment4/setup_c

If you will be developing in C++, type:
/usr/class/cs248/assignments/assignment4/setup_C

Please do not change any of the source files for the viewer. The submit script will not be submitting any of the viewer's source files, so we will not see any changes you make. The file shader.c contains stub functions that you should replace with your own functions, as described below.

Using the viewer

To start the viewer after making it, type:
./viewer [ -single | -double ] config-file
where config-file is the name of a configuration file. Specifying -single uses single buffering, and -double uses double buffering. On the machines in Sweet Hall, use -single if you plan to save the image you render, since your rendering will not appear in 24-bit color in double buffering mode.

The configuration file specifies the layout of the scene, including which object to display, the location of the viewer and the light sources, and any parameters you wish to specify to your shading functions. We suggest that you make use of this facility - it is very handy to be able to change parameters of your shading function without recompiling. The file param.h contains descriptions of functions that you can use to retrieve parameters from these configuration files.

A sample configuration file.

A list of pre-defined variable names that can appear in a configuration file.

Once the viewer has started, the mouse buttons do the following:

left button Rotates the object
middle button Scales the object
right button Access the menu

Helper code for vector operations

We have provided a very simple vector library for you to use in developing your shading functions. The file vec.h contains a list of the functions we have written for your use, as well as brief descriptions of what each function does. The functions only work for vectors of length 3, which should be adequate for this project.

What you need to write

You need to implement the function void shaderSetup(void), which is called once at the beginning of the program. It is called after the configuration file has been read but before the object is rendered for the first time. It is also called whenever the configuration file changes. This allows you to edit the configuration file and view the changes without exiting and restarting the viewer. Typically, you use shaderSetup to set up your shading functions, including getting parameters from the configuration file. This function must also specify which shading function is called by setting the pointer theShader to point to the function to be called.

An example shaderSetup().

Your shading function has access to the information about the scene specified in info.h. The various information is documented in that file. Your shading function takes as a parameter a floating-point array of length 3, into which it should put the RGB color value it computes (minimum intensity is zero maximum intensity is one).

Example shaders.

hanrahan@cs.stanford.edu
beers@graphics.stanford.edu

Copyright © 1997 Pat Hanrahan and Andrew C. Beers