[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
GL3.2 Version 4.1 for AIX: Programming Concepts

Z-Buffering

The z-buffer determines which of the things being drawn (polygons, lines, or text) is closest to the viewer. The distance to the figure being drawn is compared to the distance of the shapes already drawn in the scene. If the current figure is more distant (and therefore hidden), it is not drawn.

The z-buffer is a set of 24-bit integers, one associated with each pixel of the screen. You start by clearing the z-buffer: that is, by setting the z value of each pixel to the most distant (most positive) value possible. Then as each polygon, line, point, or character is rendered, its x and y screen coordinates are calculated in the usual way. Before the pixel is colored, however, the z coordinate is also calculated. The z coordinate is effectively the distance to the eye.

This incoming z value is compared to the z-buffer value already stored for that pixel. If the z value is smaller than the value in the z-buffer, the pixel is colored, and the pixel's z-buffer value is set to the new z value. At any point in the drawing, the values in the z-buffer represent the distance to the item that is currently closest to the eye. The color value stored in the bitplanes represents the color of that item. The z comparison is unsigned.

Near and far values in the call to perspective have a profound effect on the resolution of the z-buffer's comparison facility. The z-buffer contains a fixed and finite number of integer values that can be used to compare against the z value of the object in the scene. With this capability, you can control the resolution of the z-buffer by setting the near and far values. The more closely you bracket the objects between the near and far clipping planes, the better z compare resolution you achieve.

The zbuffer2.c example program draws three cubical objects (they are all originally perfect cubes, but scale stretches them along their axes). The objects tumble through each other and the whole scene is also rotating. While the left mouse button is up, the scene is drawn without z-buffering. When it is down, z-buffering is enabled. If the program is called with an argument, there is a short delay between drawing each of the polygons. In this mode, the left mouse button still controls the z-buffering.

The key part of the program that turns on the z-buffering is the pair of subroutines:

zbuffer(TRUE);
zclear();

The first subroutine enables z-buffer comparisons to be made before each write, and the second sets all the z values to the largest possible value for pixels in the viewport. In this example, zbuffer(TRUE) is called for every frame, but this is not necessary. The zbuffer(TRUE) call in a typical program is called only at the beginning. In the zbuffer2.c example program, the code is written as it is because the left mouse button can come up at any time, in which case z-buffering should be turned off.

The getzbuffer subroutine returns True or False, depending on whether z-buffering is enabled or not. By default, z-buffering is turned off; most applications that require 3-D hidden surface removal should probably turn on z-buffering.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]