GL_ARB_depth_texture

init()

The initialization is fairly simple, check for the extension, create a texture then initialize it. The trick lies with the parameters that need to be set. GL_DEPTH_TEXTURE_MODE_ARB needs to be set to GL_LUMINANCE, GL_INTENSITY or GL_ALPHA. Internal format must be set to one of GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT16_ARB, GL_DEPTH_COMPONENT24_ARB, or GL_DEPTH_COMPONENT32_ARB. And format must be GL_DEPTH_COMPONENT.

bool init()
{
    // Check that extension is supported
    if(!glexExtensionsSupported(“GL_ARB_depth_texture”))
    {
        return false;
    }

    glEnable(GL_DEPTH_TEST);

    // Create texture
    glGenTextures(1, &texture);

    // Make texture active
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);

    // Fiddle with texture parameters
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

    // Set DEPTH_TEXTURE_MODE to LUMINANCE
    glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);

    // Initialize texture memory using DEPTH_TEXTURE for internal format and format
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SIZE, SIZE, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);

    return true;
}

draw()

Scene to take the depth of

Using depth textures requires three stages, drawing the scene that we want the depth of, capturing the depth buffer, using the the depth buffer. Sometimes we can combine the first two steps. The scene that we are taking the depth of is a very simple. It consists of two spheres, one close and one far away. We then copy the depth buffer using glCopyTexImage2D, then apply it to a quad to see the result.

void draw()
{
    // What to take the depth of
    glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
       
    // clear the depth and color buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    gluLookAt(
        0.5, 0.5, 7.0,
        0.5, 0.5, 0.0,
        0.0, 1.0, 0.0);
       
    glPushMatrix();
    {
        glColor3f(1.0f, 1.0f, 0.0f);
        glTranslatef(1.0f, 1.0f, 5.0f);
        auxSolidSphere(1.0f);
    }
    glPopMatrix();
       
    glPushMatrix();
    {
        glColor3f(0.0f, 1.0f, 1.0f);
        glTranslatef(-1.0f, -1.0f, 0.0f);
        auxSolidSphere(1.0f);
    }
    glPopMatrix();

    // Copy the current depth buffer to the texture
    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, SIZE, SIZE, 0);

    glClearColor(0.5f, 0.0f, 0.5f, 1.0f);
       
    // clear the depth and color buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    gluLookAt(
        1.2, 1.2, 1.2,
        0.5, 0.5, 0.0,
        0.0, 1.0, 0.0);

    // Draw a quad with the texture attached
    glBegin(GL_QUADS);
    {
        glColor3f(1.0, 1.0 ,1.0);

        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(0.0, 0.0, 0.0f);

        glTexCoord2f(1.0f, 0.0f);
        glVertex3f(1.0, 0.0, 0.0f);

        glTexCoord2f(1.0f, 1.0f);
        glVertex3f(1.0, 1.0, 0.0f);

        glTexCoord2f(0.0f, 1.0f);
        glVertex3f(0.0, 1.0, 0.0f);
    }
    glEnd();
}
Final scene with depth texture

And here is the resulting texture pasted to a quad. Dark values mean closer to the camera while light values mean farther from the camera