Open GL Super Bible

Previous Table of Contents Next

The Depth Buffer

The depth buffer holds distance values for each pixel. Each value represents the pixel’s distance from the viewer and is scaled to fill the current near/far clipping volume. The software implementation of OpenGL under Windows supports both 16- and 32-bit depth values.

The depth buffer is normally used to perform hidden surface removal. Hidden surface removal is a process that occurs naturally in the real world; when one solid (opaque) object is placed in front of another, the nearer object will hide some or all of the one behind it.

In OpenGL, the depth buffer can also be used for some interesting effects, such as cutting away the front of objects to show the inner surfaces (see Figures 15-2a and 15-2b).

Figure 15-2a  Typical depth buffering with GL_LESS

Figure 15-2b  Typical depth buffering with GL_GREATER

Depth Comparisons

When you draw in a window using OpenGL, the Z position of each pixel is compared with the value in the depth buffer. If the result of the comparison is True, the pixel is stored in the color buffer along with its depth. OpenGL defines eight depth-comparison functions that can be used for depth buffering (Table 15-6).

Table 15-6 Depth Comparison Functions

Name Function

GL_NEVER Always False.
GL_LESS True if source Z < depth Z.
GL_EQUAL True if source Z = depth Z.
GL_LEQUAL True if source Z <= depth Z.
GL_GREATER True if source Z > depth Z.
GL_NOTEQUAL True if source Z != depth Z.
GL_GEQUAL True if source Z >= depth Z.
GL_ALWAYS Always True.

The default comparison function is GL_LESS. To change it, call glDepthFunc:


Using the GL_LESS function, pixels in a polygon are drawn if the depth value of the pixel is less than the depth value in the depth buffer.

Depth Values

When using the GL_EQUAL and GL_NOTEQUAL depth comparisons, it is sometimes necessary to alter the range of depth values used, in order to reduce the number of available values (keeping the number of values to a minimum). Use glDepth Range, as follows:

glDepthRange(near, far);

The near and far parameters are floating point numbers between 0.0 and 1.0, inclusive. The defaults are 0.0 for near and 1.0 for far. Normally, near is less than far, but you may also reverse the order to achieve special effects (or use the GL_GREATER and GL_GEQUAL functions). Reducing the range of values stored in the depth buffer does not affect clipping, but it will make the depth buffer less accurate and can lead to errors in hidden surface removal in the display.

Some depth comparisons need a different initial depth value. By default, the depth buffer is cleared to 1.0 with the glClear function. To specify a different value, use the glClearDepth function:


The depth parameter is a floating point number between 0.0 and 1.0, inclusive, unless you have defined a smaller range with glDepthRange. In general, use a value of 0.0 for GL_GREATER and GL_GEQUAL comparisons, and 1.0 for GL_LESS and GL_LEQUAL comparisons.

Applications of the Depth Buffer

The usual application of the depth buffer is to remove hidden surfaces. As noted earlier, the depth buffer can also be used to cut away the front parts of a scene. Listing 15-1 demonstrates this type of application. The key to this program is the use of glDepthFunc and glClearDepth:


Here we use a global variable to hold the current depth function. The depth_function variable is initialized to GL_LESS when the program starts. When the user presses the D key, the toggle_depth callback function switches this between GL_GREATER and GL_LESS.

if (depth_function == GL_LESS)

The glClearDepth call is needed to provide the correct initial depth value for the window, since the depth value is 1.0 by default. Nothing would be drawn when the depth function is set to GL_GREATER, because no pixel could possibly have a depth value greater than 1.0.

Previous Table of Contents Next