Modifying the Projection Matrix to Perform Oblique Near-Plane Clipping
Note: This topic is covered in much greater detail in Foundations of Game Engine Development, Volume 2: Rendering.
The following code modifies the OpenGL projection matrix so that the near plane of the standard view frustum is moved so that
it coincides with a given arbitrary plane. The far plane is adjusted so that the resulting view frustum has the best shape possible.
This code assumes that the original projection matrix is a perspective projection (standard or infinite). The
must be in camera-space coordinates, and its w-coordinate must be negative (corresponding to the camera being on the negative side of the plane).
For algorithmic details, see “Oblique View Frustum Depth Projection and Clipping”, by Eric Lengyel.
This technique is also described in Game Programming Gems 5, Section 2.6.
inline float sgn(float a)
if (a > 0.0F) return (1.0F);
if (a < 0.0F) return (-1.0F);
void ModifyProjectionMatrix(const Vector4D& clipPlane)
// Grab the current projection matrix from OpenGL
// Calculate the clip-space corner point opposite the clipping plane
// as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
// transform it into camera space by multiplying it
// by the inverse of the projection matrix
q.x = (sgn(clipPlane.x) + matrix) / matrix;
q.y = (sgn(clipPlane.y) + matrix) / matrix;
q.z = -1.0F;
q.w = (1.0F + matrix) / matrix;
// Calculate the scaled plane vector
Vector4D c = clipPlane * (2.0F / Dot(clipPlane, q));
// Replace the third row of the projection matrix
matrix = c.x;
matrix = c.y;
matrix = c.z + 1.0F;
matrix = c.w;
// Load it back into OpenGL
How to cite this article
Please cite the original journal paper when referring to the oblique near-plane clipping technique:
Lengyel, Eric. “Oblique View Frustum Depth Projection and Clipping”. Journal of Game Development, Vol. 1, No. 2 (2005), Charles River Media, pp. 5–16.