Project 2

Due date: see Schedule
(100 points)

Overview

For this program you will write the software necessary to render 3D objects, using a Z-buffer algorithm for visible surface determination.

This assignment first entails generating images of 3D objects. The first thing you will need to do for this assignment is to be able to read in 3D objects. There are many different polygon file formats out there. We will work specifically with one format. You are welcome to implement other formats, but you will need to pass off the program rendering an object using the format given below.

Once you have read an object into your system, you need to extend your scan-converter from Program 1 to handle 3D objects. You will also need to implement the Z-buffer algorithm so that the object is drawn correctly. Only the portions of polygons that are closest to the viewer should appear. As you are scan-converting a polygon, you must compare the depth of the object at the current point with the depth of the object stored in the Z-buffer. You should only draw the object if it is closer to the viewer than the object in the Z-buffer.

You will need to demonstrate your code on two separate objects. One is the biplane model, which you can get from the class web page. The second is any polygonal model of your choice. For the second model, choose a complicated model that shows the capabilities of your code.

File Format

The format that you will use for this program (at least for the biplane model) is defined as follows:

  • The first line contains the character 'c', signifying that this is file format 'c'.
  • The second line is the number of groups of polygons in the file.
  • Starting with the third line, there is a repeating pattern for each group of polygons:
    • First, the number of polygons in the group.
    • Next, the number of vertices of a polygon of the group.
    • There will be a line for each vertex of the polygon, matching the number on the previous line. The first three values on the line are the x, y, z values of the vertex; the other three values are the normal vector of the vertex. (You won't need the normal vector for Program 2, but you will for Program 3.)
    • The previous 2 bullet points are repeated for each polygon in the group.
    • The next line (after the last polygon of the group) will be the color of the group of polygons, with 3 real numbers between 0.0 and 1.0.
  • This pattern repeats for each group of polygons, matching the second line of the file.
input meaning
c format of this file
2 number of groups of polygons
2 number of polygons (group 1)
3 number of vertices (group 1, polygon 1)
1 2 3 4 5 6 vertex 1 (group 1, polygon 1)
2 3 4 5 6 1 vertex 2 (group 1, polygon 1)
3 4 5 6 1 2 vertex 3 (group 1, polygon 1)
4 number of vertices (group 1, polygon 2)
1 2 3 4 5 6 vertex 1 (group 1, polygon 2)
. more vertices (group 1, polygon 2)
. and so forth
6 5 4 3 2 1 vertex 4 (group 1, polygon 2)
0.7 0.2 0.1 color of all polygons in group 1
3 number of polygons (group 2)
5 number of vertices (group 2, polygon 1)
6 5 4 3 2 1 vertex 1 (group 2, polygon 1)
. hopefully that's enough

Notes on Projection

Transformation to device coordinates is simply a matter of changing the values in the .dat input file so that those values look good on your display. This amounts to doing a translation and scale. Translate to put the xmin,ymin of the model to the xmin,ymin of the display and then scale so that object to roughly fill the display. For example, if foo.dat contains coordinates which span the range from -5.5334 to 3.334 then you'd want to translate -5.5334 to 0 and multiply the translated coordinates by screenWidth/(3.334 + 5.5334).

Parallel projection when the film plane and the XY planes are also parallel is called orthographic projection. Orthographic projection is a fine choice for project 2 because it's simple. In orthographic projection, xp = x, yp = y and zp = 0 where xp means "x after projection" and "yp means y after projection". (For a discussion simpler than what you might find on page 162 of the text, see these Notes on projection)

Z-Buffer and Extension Considerations

To get the Z-buffer working you will need to interpolate depths across each polygon. In the next project you will also interpolate several other numbers/vectors to get phong shading to work, so make sure your interpolation code is easily extensible and correct. Also, it can be hard to tell if Z-interpolation is working right; you might find it useful to draw the depths you compute as the color of the objects (make sure you scale them into an appropriate range for the glColor3*(…) command you are using first) and make sure they they are smoothly interpolated over each polygon.

Related Files

  • Hints for program 2
    • please note an error in the notes; "Z at Zmin" should read "Z at Ymin"
    • it is possible to set up your program so that you either only keep Znew < Zold or Zold < ZNew; it is likely the notes are incorrect for your setup in that respect
    • The notes assume an 800x800 window. You are welcome to assume the same, or to make the window resizable; resizability should only require a few lines of code, and you can significantly speed up the execution by using a smaller window. You may NOT fix the window size smaller than 800x800: either make it 800x800 or make it resizable.
  • Biplane input file
  • Biplane output image
  • More input shapes for you to use
page_revision: 10, last_edited: 1210185410|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.