Alpha Testing

[Previous] [Next]

Alpha testing provides the ability to control whether pixels are written to the render-target surface. It is often used to create nonrectangular bitmaps as well as to implement bitmap transparency. Alpha testing allows you to improve your application's performance when nearly transparent objects are being rasterized.

To enable alpha testing, set the D3DRENDERSTATE_ALPHATESTENABLE render state to TRUE. With alpha testing, if the color being rasterized at a pixel is more opaque than the color that is already at the pixel, the pixel is written. You determine the state of the color being rasterized at the pixel by using, for example, D3DPCMPCAPS_GREATEREQUAL. If the pixel being rasterized is less opaque than the color already at the pixel, Direct3D skips it completely, saving the time that would have been required to blend the two colors together. In addition, the color buffer, and more important, the z-buffer, aren't updated, avoiding an "invisible" portion of a polygon that obscures a polygon behind it that is rendered afterward. (You can also avoid this problem by rendering the transparent polygons back to front after rendering all the opaque polygons. However, this rendering requires that you sort the polygons in a scene before rendering them.) You can make Direct3D compare the incoming alpha value with a reference alpha value (which you supply) by specifying a comparison function via the D3DRENDERSTATE_ALPHAFUNC render state. When you enable alpha testing, alpha blending will occur only when the test you specify succeeds.

The D3DCMPFUNC enumerated type that defines the possible tests this render state uses is defined as follows:

 typedef enum _D3DCMPFUNC {      D3DCMP_NEVER        = 1,      D3DCMP_LESS         = 2,      D3DCMP_EQUAL        = 3,      D3DCMP_LESSEQUAL    = 4,      D3DCMP_GREATER      = 5,      D3DCMP_NOTEQUAL     = 6,      D3DCMP_GREATEREQUAL = 7,      D3DCMP_ALWAYS       = 8,      D3DCMP_FORCE_DWORD   = 0x7fffffff,  } D3DCMPFUNC; 

You supply the reference alpha value for alpha testing via the D3DRENDERSTATE_ALPHAREF render state. In the code below, the call to SetRenderState with D3DRENDERSTATE_ALPHAREF sets a value that specifies the reference alpha value that Direct3D tests incoming pixels against when alpha test is enabled. You can specify a reference alpha value between 0x00 and 0xFF; the default value is 0.

The following code checks whether a specific comparison function is supported, and if so, improves rendering performance by causing Direct3D to use that comparison function:

 LPD3DDEVICEDESC7 lpD3DHWDevDesc;     device->GetCaps(lpD3DHWDevDesc); // // lpD3DHWDevDesc is a hardware device D3DDEVICEDESC7 structure  // filled by a call to IDirect3DDevice7::GetCaps. // if (lpD3DHWDevDesc->dpcTriCaps.dwAlphaCmpCaps &      D3DPCMPCAPS_GREATEREQUAL) {     device->SetRenderState(D3DRENDERSTATE_ALPHAREF,                             (DWORD)0x00000a0);     device->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, TRUE);     device->SetRenderState(D3DRENDERSTATE_ALPHAFUNC,                             D3DCMP_GREATEREQUAL); } 

If the target system doesn't support the comparison you want to use, just render the primitive as usual. Unfortunately, you won't get the performance gain you hoped for. The IDirect3DDevice7::GetCaps method call in the preceding code acquires the capabilities of the current device. By checking the dwAlphaCmpCaps member of the D3DPRIMCAPS structure, which is contained in the D3DDEVICEDESC7 structure associated with the GetCaps call, you can determine whether the comparison function you want is available. If this member contains only the D3DPCMPCAPS_ALWAYS or only the D3DPCMPCAPS_NEVER capabilities, the current driver doesn't support alpha testing. Otherwise, dwAlphaCmpCaps contains flags that identify the individual comparisons that the driver supports. If the driver doesn't support the method you want to use, select a comparison function that is available.

If you intend to use alpha in texture maps, you need to make sure that the textures you use are in an ARGB:4444, ARGB:1555, or ARGB:8888 format so that they support alpha channels.



Inside Direct3D
Inside Direct3D (Dv-Mps Inside)
ISBN: 0735606137
EAN: 2147483647
Year: 1999
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net