Depth, blending & cullingGL 1.0

glDepthRange

Maps the depth after projection (−1 to 1) to the values stored in the depth buffer (0 to 1 by default).

Signature

void glDepthRange(GLdouble nearVal, GLdouble farVal);

How it works

After the perspective divide, z runs from −1 (near plane) to 1 (far plane). The viewport step turns that into the stored depth:

depth = nearVal + (z + 1) / 2 × (farVal − nearVal)

With the default 0 and 1, the near plane stores 0 and the far plane 1. A smaller range squeezes an object into part of the buffer: drawing a first-person weapon with glDepthRange(0, 0.1) puts it in front of everything else, so it never sinks into walls.

Parameters

nearValGLdouble

Stored depth for the near plane, 0 by default. Clamped to [0, 1].

farValGLdouble

Stored depth for the far plane, 1 by default. Clamped to [0, 1].

Tips & pitfalls

  • nearVal may be larger than farVal, which reverses the depth order; for a proper reversed-Z setup use glClipControl (GL 4.5) with a float depth buffer instead.

  • glDepthRangef is the same with GLfloat arguments (GL 4.1, and the only form in OpenGL ES).

Example

example.cpp
1drawWorld();
2
3glDepthRange(0.0, 0.1);   // weapon always in front
4drawWeapon();
5glDepthRange(0.0, 1.0);
Official Khronos documentation