subscribe

Z-Buffer

When lines are to be drawn, or images positioned, on a computer screen, a consistent method of specifying screen locations has to be used. As computer displays are made up of columns and rows of pixels, the location of any pixel on the screen can be described by using two numbers: the number of the column and the number of the row in which the pixel is located. The top-left pixel of a screen is row one, column one and the bottom right pixel of an XGA screen is column 1024, row 768 (although, being perverse creatures, programmers usually start counting at zero, so they would say that these locations are “0, 0” and “1023, 767”, respectively). These are known as the “X” and “Y” co-ordinates of a screen pixel, in the same way as points on a graph are described.

Where three-dimensional graphics are involved, a method of describing how far into the scene a point lies must also be used. The letter used for this purpose is “Z”. If a point (let’s call it “A”) on an object has a Z value which is less than a point on another object (you guessed it, point “B”) which has the same XY co-ordinates, it is closer to the viewer than point B and, assuming that the object on which A lies isn’t transparent, will obscure point B.

When complex scenes are being assembled for display, a record of the Z value of the last point plotted at each XY screen co-ordinate must be kept to prevent objects appearing at the wrong depth in the scene. This record is held in the Z-buffer, an area in memory having one location which corresponds to each XY co-ordinate on the screen. Each time a screen point is to be plotted, the corresponding Z-buffer location is checked to see if the point has a lower Z value than the one last plotted at that XY location. If it has, the point is plotted; if it hasn’t, the point isn’t plotted.

The depth to which a scene can be represented depends on how large a number can be held in each of the Z-buffer’s memory locations. A 16-bit Z-buffer allows scenes with 65,536 levels to be represented. A 32-bit Z-buffer increases this to over four billion levels. In programs which allow movement within a scene, the larger the value which can be held in each Z-buffer location is, the less chance of “pop-up” (the effect often seen in racing and flight simulation programs where distant objects suddenly appear out of nowhere) occurring.

When transparent or translucent objects appear in a scene, the Z-buffer can be used to check whether an object is in front of or behind these objects and whether or not their colours need to be altered to reflect this. So if a sheet of tinted glass is close to the viewer, for example, points which lie on objects behind the glass will have their colour dimmed correspondingly. The Z-buffer can also be used to add the effect of mist in a scene: the further an object is from the viewer, the more washed-out its colours will become. Likewise, aerial-perspective, the tendency of very distant objects, such as mountains, to take on a bluish tint, can also be simulated using a Z-buffer, adding realism to 3D landscapes.

Z-buffering isn’t the only, or the fastest, way to keep track of which points in a scene can be seen by the viewer but it is one of the simplest and, consequently, the most widely used.