Comp 360 Lab 3: Hidden Surface Removal and Shading Models
Due Thursday, December 1, at 11:59 PM
Overview
In this project you will write a program to load and display 3-dimensional polygonal models. You will be provided with a viewer that performs rendering with the OpenGL library. You will implement two other renderers for hidden surface removal: one using Z-buffers, and one using binary space-partition (BSP) trees. This project is worth 100 points.
Specification
Your program will take a single command-line
argument: the name of a text file that defines a scene to render.
The format of the text file is given in the section "Scene File
Format," below. Under the Windows OS, the path to a file is passed
as an argument to a program if the program is opened by dragging the
file onto the program's icon.
The user can interact with the program by pressing certain keys as
specified below. Pressing keys allows the user to switch renderers,
switch shading models, or move the camera.
Polygonal Model Viewer using OpenGL
The first renderer using OpenGL has been provided for you. You can use this renderer as a comparison for the other renderers.
The OpenGL renderer should respond to the following keys.
'o': Enable the OpenGL renderer (disabling whatever renderer was active previously).
'w': Toggle wireframe mode. In wireframe mode, only the edges of polygons are displayed.
'f': Enable flat shading, overriding the previous setting.
'g': Enable smooth (Gouraud) shading, overriding the previous setting.
Additionally, the user should be able to move the camera within the OpenGL renderer.
Free Camera Movement (10 Points)
The user should be able to move the camera (that is,
the viewing point or eye point) in a way similar to a 3D
first-person shooter game- for example, Quake or Unreal Tournament.
The camera movement controls are given below.
The camera has a local
coordinate system defined by three vectors and a point (see figure
1). The eye is the center point for the coordinate system. The
up vector points in an upward direction from the eye. The
right vector points towards the right of the camera. The
forward vector is the cross product of up and right. The forward vector
also corresponds with the direction that the camera is facing. The
initial settings for this local coordinate system is given below:
- Right ( 1 0 0 0 )
- Up ( 0 1 0 0 )
- Forward ( 0 0 -1 0 )
- Eye : ( 0 0 0 1 )

Figure 1: Camera coordinate system.
You should implement the following specific controls.
- Up/down arrow keys: The camera moves forward or backward along the forward vector.
- Left/right arrow keys: The camera moves to the left or right along the right vector. (In gaming this motion is sometimes called "strafing.")
- Pageup/pagedown keys: The camera moves up or down along the up vector.
- Click and drag mouse left/right: The right vector (and the forward vector with it) rotates left or right around the up vector.
- Click and drag mouse up/down: The forward vector rotates up or down around the right vector.
You should be able to use these controls to view a model from any
angle. Note that the viewpoint and viewing frustum should be shared between
all renderers. The user should see essentially the same thing as he
changes the rendering algorithm.
Your projection parameters should correspond to setting the viewing matrix using the call glFrustum(-1,1,-1,1,1,100000). For a description of glFrustum, please refer to the following reference.
Static scene renderer: Z-Bufffer Algorithm (45 Points)
In the Z-buffer renderer, you will duplicate in
software what OpenGL does in hardware. You will also implement a
third shading model, Phong shading. The algorithm and data
structures you need will be described in class.
Within the Z-buffer renderer, you may only use OpenGL to draw
individual pixels (with glBegin(GL_POINTS)) or buffers of
pixels (with glDrawPixels()).
The Z-buffer renderer should respond to the following keys.
'z': Enable the Z-buffer renderer (disabling whatever renderer was active previously).
'f': Enable flat shading, overriding the previous setting.
'g': Enable Gouraud shading, overriding the previous setting.
'n': Enable Naive Phong shading (as described in the textbook), overriding the previous setting.
's': Enable Phong Shading with spherical linear interpolation
(as described in the textbook).
While the Z-buffer renderer is enabled, the program should ignore camera movement input.
Dynamic scene renderer: BSP Trees Algorithm (45 Points)
The final renderer is based on BSP-trees. When you
load a polygonal model at the beginning of the program, you should
calculate the BSP-tree representation of the model. Once again, the
algorithm and data structures you need will be covered in class.
To render a BSP tree, you should project polygons onto the viewing
plane and draw them in 2 dimensions using the GL_TRIANGLES
primitive. This means all your OpenGL calls should be made within
an orthographic projection. The BSP tree should also handle
all depth issues between different polygons, so
GL_DEPTH_TEST should be disabled as well. In addition, your BSP
tree renderer must perform its own lighting calculations and set the
color of each vertex manually. Thus GL_LIGHTING should be
disabled while this renderer is active.
The BSP tree renderer should respond to the following keys.
'b': Enable the BSP tree renderer (disabling whatever renderer was active previously).
'f': Enable flat shading, overriding the previous setting.
'g': Enable Gouraud shading, overriding the previous setting.
The user should be able to move the camera within the BSP tree renderer.
Summary
The following table summarizes the shading models and features that must be supported by each renderer.
| Renderer | Shading Model | Features |
| Flat | Gouraud | Phong | Wireframe Rendering | Camera Movement |
| OpenGL | Yes | Yes | No | Yes | Yes |
| Z-Buffer | Yes | Yes | Yes | No | No |
| BSP Trees | Yes | Yes | No | No | Yes |
When started, the program should use the OpenGL renderer with Gouraud shading.
Implementation Notes
- You will need to compute normal vectors for each polygonal
face of the model, as well as normal vectors at each vertex. The
normal vector of a vertex is calculated as the average of the normals of all the polygons sharing that vertex.
- Remember that constructing a BSP-tree involves splitting
polygons, and that splitting a triangle may result in a four-sided
polygon. You should further split four-sided polygons into
triangles to maintain the invariant that your BSP-tree nodes
represent triangles.
- Once you have the program working correctly, try to optimize your code for efficiency. In particular, make sure your program does not leak memory over time.
- Your program may assume that input files are well-formed.
What To Turn In
Create a directory lab4. This directory should contain your source code and all
files needed to build your program in Visual Studio or CMake. Also include a README file containing:
- The names of you and your partners.
- A brief overview of how your code is organized.
- Any known bugs or incomplete features.
- Any other implementation details you think your grader should know.
Zip up the folder and follow the submission instruction on the class webpage to submit your zip file.
Scene File Format
File Format Overview
Input files are ASCII text files containing a series of numbers
separated by spaces (a space is any character that causes the C
library function isspace() to return true.) The input file
has three parts:
- Material Attributes
- Lighting Attributes
- Polygon Data
Material Attributes
The first seven values in the input file specify the material
properties of all polygons in the model. These properties appear in
the input as follows. As usual, colors are specified by their
red, green, and blue components.
- The object's ambient coefficient. (Floating point value between 0 and 1.)
- The object's diffuse coefficient. (Floating point value between 0 and 1.)
- The object's specular coefficient. (Floating point value between 0 and 1.)
- The object's specular exponent. (Floating point value.)
Lighting Attributes
After the definition of material properties, the input file will
contain definitions of at least one light source. The scene's
lighting attributes are given in a format identical to the lighting
information in lab 3 input files.
- The scene's ambient light color. (Three floating point values.)
- The number of lights in the scene - at least 1. (One integer.)
- For each light
- The light's position in space. (Three floating point values.)
- The light's color. (Three floating point values.)
Polygon Data
Polygon data is given in two sections. In the first section, we
enumerate the coordinates for all the vertices that will be used in
the polygons. Coordinates are given as floating point triples. In
class we called this data the geometry of the model.
In the second section, the polygons are given as sets of integers,
with three integers per polygon (our models consist only of
triangles). Each integer is an index into the array of vertices
defined in the geometry section. Note that indices are counted
beginning at zero. In class this data was called the topology of
the model.
Formally, the polygon data are given as follows.
- The number of unique vertices in the model. (One integer.)
- For each vertex
- The vertex's position in space. (Three floating point values.)
- The number of polygons in the model. (One integer.)
- For each polygon
- The indices into the vertex array of the vertices of the polygon. (Three integers.)
Below is an example of the polygon data for a small model. (Our input files do not contain comments.)
4 // Number of vertices
0 0 0 // Coordinates of 0th vertex
0 0.1 0.1 // Coordinates of 1st vertex
0.1 0 0.1 // Coordinates of 2nd vertex
0.1 0.1 0 // Coordinates of 3rd vertex
3 // Number of total triangles
0 1 2 // First polygon, consisting of 0th, 1st, and 2nd vertex
0 1 3 // First polygon, consisting of 0th, 1st, and 3rd vertex
0 2 3 // First polygon, consisting of 0th, 2nd, and 3rd vertex
Note that triangle vertices are ordered counterclockwise. That is,
a polygon's vertices are listed in counterclockwise order around the
outward-facing normal of the polygon. You will need to use this
information to determine the normals of each polygon.
Below are links to example input files of various sizes.
File translated from
TEX
by
TTH,
version 3.85.
On 05 Nov 2009, 03:09.