Using clipping planes with HLSLBy Rim van Wersch, January 19 2006 |
Clipping planes can be used to limit (clip) rendering to a certain potion of your (world) space by setting up planes that define those limitations. When using the fixed function pipeline these planes are defined in world space and can be easily used like this:
Plane clipPlane = Plane.FromPointNormal( Vector3.Empty, new Vector3(0, 1, 0)); device.ClipPlanes[0].Plane = clipPlane; device.ClipPlanes[0].Enabled = true; device.RenderState.Clipping = true;
When you are using the programmable pipeline (HLSL) however, the planes are assumed to be in clip space (which is typically just your eye/camera space). So to come up with the proper planes, we'll need to transform them from world space into clip space before we set them on the device. This isn't a difficult task in itself, but you'll need to supply the transposed inverse of the actual tranformation matrix you wish to set. This is also stated in the documentation (even in the MDX docs ), but it is often overlooked.
So, to transform our clipping plane properly to clip/view space, we'd need to use this code:
Plane clipPlane = Plane.FromPointNormal( Vector3.Empty, new Vector3(0, 1, 0)); clipPlane.Normalize(); Matrix tempMatrix = camera.ViewMatrix; tempMatrix.Invert(); tempMatrix.Transpose( tempMatrix ); clipPlane.Transform( tempMatrix ); tempMatrix = camera.ProjectionMatrix; tempMatrix.Invert(); tempMatrix.Transpose( tempMatrix ); clipPlane.Transform( tempMatrix ); device.ClipPlanes[0].Plane = clipPlane; device.ClipPlanes[0].Enabled = true; ? ? ? ? ? ? ? ? device.RenderState.Clipping = true;
Further reading
-
? -
Fixed function clipping sample
? -
HLSL clipping sample
?