The purpose of this assignment was to add a Z-Buffer rendering algorithm to graphics program. The purpose of Z-Buffer rendering is to create an efficient way of properly drawing 3D models. In particular the Z-Buffer algorithm addresses the problem of determining what objects are "visible" in a particular scene. It does this by keeping track of a Z buffer at each point of the scene, which represents its position in 3D space. The algorithm uses these points to determine what point is closest to the view point, and therefore what needs to be drawn.
Description of the Task
Hidden surface removal is an important problem in computer graphics. While the simple "Painters Algorithm", which simply builds an image by drawing from the background forward, drawing over previously rendered polygons, works in many cases, it is not complex enough to deal with many problems that arise. For example, consider the following image of two intersecting polygons:
Following the painters algorithm, these two polygons would not be able to intersect, instead, one of them would have to be rendered "on top" of the other, giving the following result:
How the Task Was Solved
The idea behind the Z-Buffer rendering algorithm is that each pixel in the image has a Z-Buffer value. This represents the location of that pixel relative to the origin in the Z direction. This collection of Z-Buffer values is called the image's "Depth Map".
Following the painters algorithm, each point of any polygon is drawn on the corresponding point on the image. The Z-Buffer algorithm has an associated Z-Buffer value for each point on the polygon, which is compared to the corresponding Z-Buffer value in the depth map before it is drawn. If the point on the polygon is "closer" than the value in the depth map, then the point on the polygon is drawn, and the z-buffer value is updated.
In order to implement this algorithm, a modification to the scanfill algorithm was made, which interpolates the Z-Buffer value across the polygon based on the values at each vertex. As the algorithm traverses each scanline it makes the proper comparison of the polygons Z-Buffer at a point to the corresponding Z-Buffer value of the image.
In order to properly draw polygons, the line_draw() function also had to be updated to take into account the Z-Buffer. This was done in the same way as the scanline algorithm (Z-Buffer values were interpolated across the line, then used to determine which points need to be drawn).
The Z-Buffer algorithm helps to show the utility of the scanfill algorithm. When we first coded it, it simply colored solid 2D polygons. Through modifications, we can change this simple algorithm to take care of alpha blending, gradients, and even hidden surface removal. It goes to show relatively basic algorithms can be expanded to accomplish difficult tasks.
Spinning Cubes. In the beginning, the direction of rotation was incorrect, it turned out that the matrix parameters for transformations were simply swapped in order, making the rotation counterclockwise.
The zBuffer representation of the image, one can notice "a sort of" shading relying on the depth of any object in the scene.