Written by Robert
Microsoft DirectX MVP
demonstrates an alternative method for implementing user-defined clipping planes
in DirectX. Clipping occurs during rasterization, and does not rely upon
Direct3D support for clipping planes. This technique does not require the
use of programmable shaders, using only standard render states and texture stage
states to achieve this effect.
Performance of this technique with large geometries is markedly better than
using clip planes with software vertex processing to clip vertices. On
cards that do not provide the needed support for user-defined clipping planes,
this allows hardware vertex processing to be used, rather than having to drop
back to the used of software vertex processing.
Clipping Planes in Direct3D
User-defined clipping planes allow the application to define one or more
planes, in world space, to which primitives will be clipped.
Up to six clipping planes can be defined simultaneously when using software
vertex processing, so support of this feature through emulation is guaranteed.
When using hardware vertex processing, the number of clipping planes that can be
simultaneously defined varies. One major manufacturer of video cards,
which previously implemented clipping planes at a cost of one texture stage per
plane, recently removed support from their drivers entirely.
There are basically two ways that clipping can be implemented:
- Primitives can be clipped prior to rasterization, modifying vertices and
adding extra triangles as needed. This is how clipping planes are
implemented in software vertex processing.
- Pixels can be tested against the clipping planes and clipped pixels be
excluded from rasterization.
The demo performs clipping in pixel space, by using a texture as an alpha
mask and mapping the texture so that areas to be clipped fall within a
transparent region of the texture. This involves several functional
|A small texture is created that acts as an alpha mask. In its
simplest form, used for clipping to a single plane, a 2x1 texture is used,
consisting of one opaque and one transparent pixel.|
|Automatic texture coordinates are generated by Direct3D using the
D3DTSS_TCI_CAMERASPACEPOSITION texture coordinate index, so that texture
coordinates are generated based on the position in camera space.|
|Texture coordinate transformation is then applied, using a matrix that
transforms the camera space coordinates to texture coordinates, such that
pixels that are to be clipped map to transparent texels, while those not
clipped map to opaque pixels.|
|A texture stage modulates the pixel's alpha by that of the mapped alpha
|Alpha testing is set to exclude rendering of pixels with fully transparent
Multiple planes can be clipped in a single texture stage using this
technique. In the demo, four clipping planes are simultaneously clipped,
using a single texture stage.
Here are some miscellaneous notes and suggestions regarding this technique:
|Release Notes for the sample:|
|The sample does not set min and mag texture filters, as the default
point filtering is what is required. If you extend this sample, be
sure that point filtering is set for the mask texture's stage.|
|The sample doesn't use any textures other than the mask, so it just
selects the alpha from the texture. To use with texture mapping, use
the mask in the last stage and set the alpha operation to modulate between
the texture and the current alpha.|
|Extending the number of planes:|
|The number of arbitrary planes that can be clipped using a generalized
application of this technique is two planes per stage, using a 2x2 mask.
That corresponds to one plane per texture coordinate component (u and v).
The texture coordinate transformation matrix is offset so that the planes
lie on u=0.5 and v=0.5.|
|Using a 4x4 mask, as in the sample, two pairs of parallel planes can be
clipped against. The transformation is scaled and offset such the area
between the planes falls in the range of 0.25 to 0.75. Note that to
use two pairs together in this manner, the normals of both pairs must have
the same relation, either the normals of both pairs point away from each
other, or they both point towards each other.|
|A programmable vertex shader could extend the number of planes clipped
in a single stage indefinitely.|
|To achieve blending of the clipped edges, linear filtering could be
applied to the mask. The size of the blended area would be dependent
on the resolution of the mask texture.|
|Masks could be possibly be created to perform non-planar clipping, for
example, clipping to a curve. This would most likely work best with
texture filtering of the mask set to linear or better , and an alpha test
reference that is just shy of opaque.|
|Possible uses of projected texture coordinates using D3DTTFF_PROJECTED
and the fourth column of the texture transform matrix?|