PS2 Linux Programming

 

Z-Buffer

 

Introduction

 

This is a very short tutorial which illustrates the use and operation of the z-buffer within the PS2.

 

 

What is a Z-Buffer

 

A z-buffer is an array of data elements that represent the depth values of pixels on a screen. If each pixel on the screen has a known depth value, then new pixels (also with a know depth) can be drawn onto the screen without having to worry about whether they are in front of or behind the existing pixels. This process is known as z sorting.

 

To explain this further, when it comes time to blit pixels into the off screen buffer, each pixels z depth value is compared to the current value at that pixel location in the z buffer. If the depth value of the pixel to be drawn is closer than the z value already in the z buffer, then the pixel is drawn to the screen and the z buffer value for that pixel is updated with the newly drawn pixels z depth. If the pixel to be drawn is farther away than the depth value in the z buffer, then the pixel is not drawn and the z buffer value is not updated.

 

Without some form of Z-sorting, 3D games would be impossible to program and even 2D games would create some difficult sorting problems. It’s worth mentioning that not all consoles have a z-buffer – the PlayStation 1 never had a z-buffer and all of the depth sorting was done using “Painters Algorithm” and an ordering table. If you take a close look at some PlayStation 1 titles it can often be seen (depending upon the view) that the ordering of the polygons is not quite right (Tomb Raider is a typical example to mention one of many).

 

 

The PS2 Z-Buffer Under SPS2

 

When using SPS2, the dimensions of the z-buffer are set to the same resolution as the screen buffers and the z-buffer has a resolution of 24 bits. That means that the z-buffer values range from 0 to 16,777,215 (or 0xFFFFFF). The value of 0xFFFFFF represents closest to the viewer and the value of 0 represents furthest away from the viewer. When a polygon is to be drawn into the off screen buffer, the hardware works out the z depth of each pixel to be drawn by interpolating over the surface of the polygon using the (x, y, z) coordinates of the three vertices. Once the z value of each pixel is know, it can properly be z-sorted into the screen buffer.

 

 

Some Code

 

The program in the tutorial “Using the DMAC” has been modified to  illustrate the sorting process in practice. The vertex data for the polygons to be drawn has been modified to the values given below.

 

// Define triangle vertices relative to centre of screen

int VertexData[][3] =

{

        {-200, -220,  10}, {-200,  -20,  10}, { 180, -120, 300}, // Triangle 1

        { 200, -220, 200}, { 200,  -20, 200}, {-180, -120, 200}, // Triangle 2

        {-200,  220, 100}, {-200,   20, 100}, { -50,  120, 100}, // Triangle 3

        { 200,  220, 100}, { 200,   20, 100}, {  50,  120, 100}, // Triangle 4

};

 

Triangles 1 and 2 have been defined in a manner which allows triangle 1 to partially penetrate through triangle 2 and the effect of the z sorting algorithm and hardware is evident. This is best illustrated by running the program and observing the effect.

 

 

Conclusions

 

Z sorting is fundamental to the creation of 3D computer games and is implemented in hardware on the PlayStation 2 using a Z-buffer. Even when creating 2D games, the use of the z-buffer is critical since it allows for the proper depth sorting of 2D sprites and other graphic images.

 

 

Dr Henry S Fortuna

University of Abertay Dundee

h.s.fortuna@abertay.ac.uk