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 command to set up your directory with the viewer software:

/usr/class/cs248/assignments/assignment4/setup

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. For now, use the sample configuration file, sample.cfg.

See the web page on configuration files for more information on the format and content of these files.

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

Button Function
Left Rotates the object
Middle Scales the object
Right Accesses the menu
Using the menu, you can select the lights and rotate those independently of the object.

What we provide for you

Aside from the viewer itself, we provide a number of routines that you will almost certainly want to take advantage of:

Configuration file routines

As described earlier, the configuration file mechanism allows you to specify paramaters to your shading functions. We provide several routines for accessing the contents of the configuration files, and we strongly suggest that you make use of them. The routines will make it easy for you to change the parameters of your shading functions without recompiling. The file param.h contains descriptions of functions that you can use to retrieve parameters from these configuration files, and our example setup function shows how you might go about using a few of these routines. The shader.c file we provide to you also gives a few examples of how you might go about using these routines.

Don't forget to see the web page on configuration files, which includes a list of pre-defined variable names that can appear in the these files.

Global variables needed by your shaders

In order for your shaders to compute the color of each vertex, you'll need some information about the object at each of those vertices, as well as some information about the environment surrounding the object. This information is computed for you and stored into a set of global variables that are documented in shader.h.

Helper code for vector operations

We provide 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.

A noise generating function

Finally, to assist you with generating your wood texture, we provide you with an implementation of Perlin's noise function, FBm(). The FBm() function is described in the file mnoise.h.

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. In addition to setting up the shading functions and getting parameters from the configuration file, shaderSetup specifies the function that is called to shade each of the vertices of the object being rendered by setting the pointer theShader to point to the function to be called. Your shaderSetup must set this pointer!

An example shaderSetup().

Your shading function has access to the information about the scene specified in shader.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

CS248: Introduction to Computer Graphics, Pat Hanrahan, Fall 1998