Tutorials: ZBuffer Help


What is a ZBuffer?

A zbuffer is like a 256-color image in which each pixel corresponds to another pixel in the background image. Therefore, a zbuffer must have the exact same dimensions as the background image. But, unlike the background image, the zbuffer remains invisible to the user; it is there only to determine how to draw objects in front or behind trees, walls, parked spaceships, rocks, great gravy, or whatever else happens to be depicted in the backround image.

Objects and Priority Bands

There are 256 possible priorities (one for each color in the zbuffer), and they are numbered 0 to 255. Each entire set of pixels in the zbuffer with the same priority is called a priority band.

Each object has a priority property. The object is drawn in front of bands of lesser or equal priority, and in front of objects with lesser priority. (There is no reliable way to determine how objects of equal priority will overlap, but it will be consistent so that overlapping objects will not seem to flicker.)

If an object's priority is set to the special value PRIORITY_AUTO, the interpreter will calculate the priority automatically by dividing the object's positionY by 8. (PRIORITY_AUTO is defined in the standard script file "Constants.s".)

Why divide by 8?

In case you are wondering why 8 is the magic value, here is the explanation: 256 priorities is only enough for a screen with a vertical resolution of 256 pixels, but AGAST has a resolution of 480. Dividing by 2 (instead of 8) would allow for enough bands to reach the bottom of the screen, but it is not enough if the screen needs to scroll vertically. 8 was chosen to allow zbuffers to function with vertically scrolling backgrounds of up to 2048 pixels in height, which is 256 multiplied by 8. If this is a ever a limiting factor, a clever script programmer can always work around it.

Construction Techniques

Before drawing a zbuffer, you should have your background image ready. Make a second copy of it, on which you will draw the zbuffer.

1

In a paint program with layers, like Photoshop or Paint Shop Pro, create a transparent layer above the image. On the topmost layer, use the lasso tool to trace the edges of an area in a unique priority band. Make sure options such as feathering and anti-aliasing are disabled. Then color the area with any color you like; it doesn't matter, as long as the color is different for each separate band, and are not pure black (RGB: 0, 0, 0) or full-intensity white (RGB 255, 255, 255).

2

Once you have traced each area in a unique color, it's time to bring out the pure black. This is a special color which is always translated into band 0. Cover the bottom layer with pure black, and merge the visible layers.

3

The only thing left is to specify which priority band each color should represent. The 2zbuffer conversion utility has a pretty simple method for doing this.

For each unique color, a separate band is created. To specify the priority of a band, place one full-intensity white pixel inside that band, and the priority will become the y-coordinate of the white pixel, divided by 8. (The x-coordinate of this pixel is arbitrary, and the pixel becomes part of the band around it.) All areas of color that do not contain a white pixel become part of band 255.

Simply stated, objects are always drawn in front of black areas. They are only drawn in front of colored areas if they are standing below the white pixel in that area. They are never drawn in front of areas without a white pixel.

A finished zbuffer, complete with white priority marks, looks something like this:

4

See Also...