Texture Based Clipping Demo

Home | Up | Search | X-Zone News | Services | Book Support | Links | Feedback | Smalltalk MT | The Scrapyard | FAQ | Technical Articles

 

Written by Robert Dunlop
Microsoft DirectX MVP

Download Demo
clipcube.zip
271KB

Requires DirectX 8.0 Runtimes or Later

Description

This sample 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:

  1. 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.
  2. Pixels can be tested against the clipping planes and clipped pixels be excluded from rasterization.

Our Alternative

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 components:

bulletA 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.
bulletAutomatic 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.
bulletTexture 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.
bulletA texture stage modulates the pixel's alpha by that of the mapped alpha mask.
bulletAlpha testing is set to exclude rendering of pixels with fully transparent alpha.

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.

Notes

Here are some miscellaneous notes and suggestions regarding this technique:

bulletRelease Notes for the sample:
bulletThe 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.
bulletThe 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.
bulletExtending the number of planes:
bulletThe 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.
bulletUsing 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.
bulletA programmable vertex shader could extend the number of planes clipped in a single stage indefinitely.
bulletSome ideas:
 
bulletTo 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.
bulletMasks 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.
bulletPossible uses of projected texture coordinates using D3DTTFF_PROJECTED and the fourth column of the texture transform matrix?

This site, created by DirectX MVP Robert Dunlop and aided by the work of other volunteers, provides a free on-line resource for DirectX programmers.

Special thanks to WWW.MVPS.ORG, for providing a permanent home for this site.

Visitors Since 1/1/2000: Hit Counter
Last updated: 07/26/05.