Homework 4

Write a polygon scan-converter. Your scan-converter should be able to handle the following problem:

Given two triangles with the following coordinates:

(-1,-1,-8) (0,1,-10) (1,0,-9)
(0,0,-7) (0.5,1,-7) (0.5,-1,-7)
and a camera located at the origin and facing into the negative z direction, with a "focal length" F = -3.0,

compute at each pixel the z-buffer value for these triangles.

Recall from our class discussion that a perspective transformation is given by

(x,y,z) -> (F*x/z,F*y/z,1/z)
and that a z-buffer stores perspective z values (ie: 1/z) at each pixel, and that the z-buffer is initialized to 0 (which is the "background" 1/z value corresponding to a z coordinate of negative infinity).

Your job is to create an image of the z-buffered triangles. When displaying the result, scale the values in the z-buffer to create clearly visible gray values.

To scan convert each triangle, you will need to loop through the image. Conceptually, the algorithm is as follows:

	FIND THE TOP AND BOTTOM ROW OF THE IMAGE WHICH IS ON THE TRIANGLE

	for (int row = topRow ; row <= botRow ; row++) {

	   COMPUTE LEFT AND RIGHT COLUMN FOR THIS ROW

	   for (int col = leftCol ; col <= rightCol ; col++) {

	      LINEARLY INTERPOLATE TO GET perspZ AT THIS PIXEL

	      if (zBuffer[row][col] > perspZ) {
		 zBuffer[row][col] = perspZ;
              }	

           }
        }

Remember, for this algorithm you need to use the perspective coordinates of the triangle vertices. You will also first need to convert the perspective x and y coordinates of each vertex to pixel values, (ie: an x coordinate of 0 must map into the center column of the image, -0.5 maps into the leftmost image column, +0.5 maps into the rightmost image column, etc).

You can find the top and bottom rows of the triangle in the image just by taking the respective min and max of the y coordinates of the vertices, after they have been converted to pixel coordinates.

You'll need to linearly interpolate the x coordinates along the edges in each row to find the left and right column in each row. To do this in each row, you will need to determine, for that row, which triangle edge bounds the triangle on the left, and which triangle edge bounds the triangle on the right.