Clip Planes
Here’s a torus with two-sided lighting and a custom clipping plane:
The code for this OpenGL demo is on github:
In olden times of yore, we used fixed-function commands like glClipPlane
. Nowadays a plane equation is simply an arbitrary vec4 uniform
, and you perform a dot-product test explictly in your vertex shader, sending the result to a special output.
You still need a small amount of setup on the C/C++ side of things:
One word of caution: I’ve heard that some drivers ignore the enable/disable state of GL_CLIP_DISTANCE0
.
As a reminder, here’s the implicit equation for a plane:
Ax + By + Cz + D = 0
I define my half-space using a unitized normal vector in (A,B,C) and a distance-from-origin in D.
Clip planes are usually defined in world space, so your vertex shader should simply dot
your vertex position in world space with the plane equation, then push it to the built-in gl_ClipDistance
array. Here’s an example vertex shader:
If you’ve got a geometry shader enabled, you’ll need to pass it through there too:
Enjoy!