ECS175
ECS 175 Introduction to Computer Graphics
Spring Quarter 2000
Assignment 3: Illumination and Shading
Due Date: Friday, May 26, 2000
This assignment extends the 3D rasterization library implemented in the first two projects to handle lighting and shading of polygons. The required algorithms are Phong lighting of vertices and Goraud shading of polygons. Your libraries have to support up to eight light sources at arbitrary positions and with arbitrary parameters. Furthermore, this assignment includes changing the user interfaces of your programs to allow interactive camera movements and changing of lighting parameters.
Input to the Geometry Pipeline
To allow evaluation of the lighting equations, the graphics library has to know a normal vector for each point of each specified polygon. The way normals are handled in OpenGL is very similar to the way colors are handled: OpenGL has a notion of a current normal vector that is set using a call to glNormal3f; whenever a vertex is specified (using one of the glVertex... calls), that vertex is assigned the current normal vector at that time.
In other words, the calculation and specification of normal vectors is left to the user. (OpenGL does support surfaces with automatic normal calculation, but that is beyond the scope of this assignment.)
OpenGL Functions to Implement
The following is a brief list of functions you have to implement to enable lighting for your graphics libraries:
- glEnable(GL_LIGHTING)
glDisable(GL_LIGHTING)
- This pair of calls to the glEnable function enables / disables lighting. With lighting disabled, all points are colored in their respective diffuse colors (set with glMaterialfv or a combination of glColorMaterial and glColor3f).
- glEnable(light)
glDisable(light)
- This pair of functions, called with one of the parameters GL_LIGHT0, GL_LIGHT1, ..., GL_LIGHT8 enables / disables the specified light source.
- glLightfv(light, parameter, values)
- This function is used to set the parameters defining a light source. The light parameter is one of the eight light source constants (GL_LIGHTn). Depending on the value of parameter, the values array is interpreted differently:
- GL_AMBIENT
GL_DIFFUSE
GL_SPECULAR
- The values array contains four values specifying the light source's ambient, diffuse and specular colors, respectively. The values are (in order) red, green, blue and alpha. The alpha value can be ignored by your libraries at this point.
- GL_POSITION
- The values array contains four numbers specifying the light source's position in homogeneous model coordinates. The position is transformed to and stored in eye coordinates at the time glLight is called. If the w component of the light position is zero, the light source is treated as a directional light source (disabling attenuation).
- GL_SPOT_DIRECTION
- The values array contains four numbers specifying the light source's spotlight direction in homogeneous model coordinates. The position is transformed to and stored in eye coordinates at the time glLight is called. The spotlight direction is ignored unless GL_SPOT_CUTOFF is not equal to 180 degrees.
- GL_SPOT_EXPONENT
- The values array contains a single number specifying a spot light source's dropoff exponent. The amount of light reaching a vertex from a spotlight source is multiplied by the cosine of the angle between the spotlight direction and the direction vector to the vertex, taken to the power of the dropoff exponent. The higher the exponent, the more focused the light source.
- GL_SPOT_CUTOFF
- The values array contains a single number specifying a spot light source's cutoff angle. A vertex is only lit by a spot light source, if the angle between the spotlight direction and the direction vector to the vertex is smaller than the cutoff angle.
- GL_CONSTANT_ATTENUATION
GL_LINEAR_ATTENUATION
GL_QUADRATIC_ATTENUATION
- The values array contains a single number specifying the constant, linear and quadratic attenuation factors for a point light source, respectively. The amount of light reaching a vertex from a point light source is multiplied by the following factor (d is the distance between the point light source's position and the vertex being lit): f = (constant_att + linear_att * d + quadratic_att * d * d)-1.
glMaterialfv(face, parameter, value)
This function is used to set the material properties of vertices being lit. The material properties define how a surface interacts with light, that is, they define of what material a surface appears to be made of. The face parameter can be assumed to be GL_FRONT_AND_BACK, since your libraries do not have to distinguish between front- and backfaces at this point. Depending on the value of parameter, the values array is interpreted differently:
- GL_AMBIENT
GL_DIFFUSE
GL_AMBIENT_AND_DIFFUSE
GL_SPECULAR
GL_EMISSION
- The values array contains four numbers specifying the material's ambient, diffuse, ambient and diffuse, specular and emissive colors, respectively. The values are (in order) red, green, blue and alpha. The alpha value can be ignored by your libraries at this point. Since the ambient and diffuse colors of objects are usually the same, the parameter value GL_AMBIENT_AND_DIFFUSE can be used to specify both at the same time.
- GL_SHININESS
- The values array contains a single number specifying the shininess of the material, i.e., the exponent used in the specular part of the Phong lighting equation.
glColorMaterial(face, mode)
This function sets which parts of the current material are affected by calls to the glColor function. The face parameter can again be assumed to be GL_FRONT_AND_BACK. The values for mode are:
- GL_AMBIENT
- Associates the material's ambient color with the current color.
- GL_DIFFUSE
- Associates the material's diffuse color with the current color.
- GL_AMBIENT_AND_DIFFUSE
- Associates the material's ambient and diffuse colors with the current color.
- GL_SPECULAR
- Associates the material's specular color with the current color.
- GL_EMISSION
- Associates the material's emissive color with the current color.
For this function to have any effect, color material association must be enabled with the following call:
glEnable(GL_COLOR_MATERIAL)
glDisable(GL_COLOR_MATERIAL)
Enables association of color values and parts of the current material. Which part(s) of the current material is/are associated is determined by subsequent calls to glColorMaterial.
glNormal3f(nx, ny, nz)
This call sets the current normal vector to the given (nx, ny, nz) components. The length of the vector (sqrt(nx*nx+ny*ny+nz*nz)) can not be assumed to be one.
glShadeModel(mode)
Sets the OpenGL shading mode to either GL_SMOOTH (Goraud shading) or GL_FLAT (constant-color shading, where the color of the whole polygon is determined by the color of the first vertex). The default value is GL_SMOOTH.
Changes to the User Interface
For this assignment, your user interfaces have to allow the user to interactively change the camera position in the scene. For this purpose, you should assume that the specified scene files do not contain camera movement and projection matrix commands any more. These should be set by the user interface alone.
Furthermore, your user interfaces have to allow the user to change the lighting parameters of the scene. This means, the user has to be able to move light sources in the scene, change their intensities, their types (directional, point, spotlight) etc.
Changes to the Scanner
To accomplish illumination and shading, you have to extend your scanners. These are the new commands the scanner has to understand:
- materialColor enable
materialColor disable
- Enables / disables associating colors with parts of the material definition.
- materialColor ambient
materialColor diffuse
materialColor ambientAndDiffuse
materialColor specular
materialColor emission
- Set the material color mode to ambient, diffuse, ambient and diffuse, specular and emissive, respectively.
- materialAmbient r, g, b
materialDiffuse r, g, b
materialAmbientAndDiffuse r, g, b
materialSpecular r, g, b
materialEmission r, g, b
- Set the ambient, diffuse, ambient and diffuse, specular and emissive material properties to the given color values.
- materialShininess shininess
- Sets the shininess material property to the given value.
- normal x, y, z
- Sets the current normal vector.
The materialColor commands can only appear outside a primitive bracket; the others can appear anywhere in the scene file. Again, for those who have taken 142 or 120 or are otherwise fond of context-free grammars, here it comes:
; Material mode definition:
materialModeCommand: "materialColor" "enable"
| "materialColor" "disable"
| "materialColor" "ambient"
| "materialColor" "diffuse"
| "materialColor" "ambientAndDiffuse"
| "materialColor" "specular"
| "materialColor" "emission"
; Material property specification:
material: "materialAmbient" float "," float "," float
| "materialDiffuse" float "," float "," float
| "materialAmbientAndDiffuse" float "," float "," float
| "materialSpecular" float "," float "," float
| "materialEmission" float "," float "," float
| "materialShininess" float
; Normal vector definition:
normal: "normal" float "," float "," float
; New definition for outer commands:
outerCommand: matrixCommand
| materialModeCommand
| material
| color
| normal
| polygon
; New definition for bracket commands:
bracketCommand: material
| color
| normal
| vertex