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.
This project is required for those taking COMP560 but optional for those taking COMP 360 (as extra credits).
You may work with a partner on this lab. Each pair only needs to submit one completed lab.
Important: If you find any bug in the project that you think was already there in the starter code and for which you are not responsible, report the issue to the TA ASAP.
We will provide you with a framework upon which you can build your project. By default, the program will load in a scene file called default.txt
and render a polygonal representation of the model.
The user can interact with the program by pressing certain keys as specified below. Pressing keys allows the user to switch renderers or switch shading models. The user can also rotate the model or zoom in on the model by using the mouse.
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:
The second renderer is based on BSP trees. Whenever you load a polygonal model (including when you start the program), you should calculate the BSP-tree representation of the model. 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 two-dimensions using the GL_TRIANGLES
primitive and glVertex2d()
. 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:
In the Z-buffer renderer, you will duplicate in software what OpenGL does in hardware. You will also implement the 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 a buffer of pixels (with glDrawPixels()
).
The Z-buffer renderer should respond to the following keys:
The following table summarizes the shading models and features that must be supported by each renderer:
Renderer | Shading Model | ||
---|---|---|---|
Flat | Gouraud | Phong | |
OpenGL | ✔ | ✔ | - |
BSP Trees | ✔ | ✔ | - |
Z-Buffer | ✔ | ✔ | ✔ |
_view->getProjection()
) that is OpenGL's equivalent to pseudo-perspective projection. For a 3D point x
and the projection matrix P
, you just need to multiply x*P
and divide by the mass to get the projected point x'
. The third component of the x'
(the z-component: x'[2]
) is the pseudo-depth. Additionally, OpenGL keeps a modelviewmatrix (
M
) for modeling purposes. So the actual multiplication is x'= x*M*P
and remember to divide by the mass of x'
. In the projected space, the viewing window corresponds to the square with two opposite corners at (-1,-1) and (1,1).Release Modeis faster than in
Debug Mode.
Use the given lab_hidden_surface
directory. This directory should contain your source code and all files needed to build your program in Visual C++. You need not submit any image file as in the previous labs. Also include a README file containing:
Zip up this folder and follow the instruction on the course web page for submission.
The code for reading the model has been completed for you. The following is for your reference in case you would like to create your own test model file. Input files are ASCII text files containing a series of numbers separated by spaces. The input file has three parts:
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 (RGB) components.
After the description of material properties, the input file describes the definition of at least one light source. The scene's lighting attributes are given in the format identical to the lighting information in the ray tracing lab's input files.
Polygon data is given in two sections. The first section enumerates the coordinates for all the vertices that will be used in the polygons. Coordinates are given as floating-point triples (x,y,z). 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 start from zero. In class this data was called the topology of the model.
Formally, the polygon data are given as follows:
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 4 // Number of triangles 0 2 1 // First polygon, consisting of 0th, 2nd, and 1st vertices 0 1 3 // Second polygon, consisting of 0th, 1st, and 3rd vertices 0 3 2 // Third polygon, consisting of 0th, 3rd, and 2nd vertices 1 2 3 // Fourth polygon, consisting of 1st, 2nd, and 3rd vertices
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.
The files
folder in the given code contains several test models. You may add more models as desired.