The alpha value of a color controls its transparency. Enabling alpha blending allows colors, materials, and textures on a surface to be blended with transparency onto another surface. For more information, see Alpha Texture Blending and Multipass Texture Blending.
Applications must use the D3DRENDERSTATE_ALPHABLENDENABLE enumerated value to enable alpha transparency blending. The Direct3D API allows many types of alpha blending. However, it is important to note the user's 3-D hardware may not support all of the blending states allowed by Direct3D.
The type of alpha blending that is done depends on the D3DRENDERSTATE_SRCBLEND and D3DRENDERSTATE_DESTBLEND render states. Source and destination blend states are used in pairs. The following code fragment demonstrates how the source blend state is set to D3DBLEND_SRCCOLOR and the destination blend state is set to D3DBLEND_INVSRCCOLOR.
// This code fragment assumes that lpD3DDevice3 is a valid pointer to
// an IDirect3DDevice3 interface.
// Set the source blend state.
lpD3DDevice3->SetRenderState(D3DRENDERSTATE_SRCBLEND,
D3DBLEND_SRCCOLOR);
// Set the destination blend state.
lpD3DDevice3->SetRenderState(D3DRENDERSTATE_DESTBLEND,
D3DBLEND_INVSRCCOLOR);
As a result of the calls in the preceding code fragment, Direct3D performs a linear blend between the source color (the color of the primitive being rendered at the current location) and the destination color (the color at the current location in the frame buffer). This gives an appearance similar to tinted glass. Some of the color of the destination object seems to be transmitted through the source object. The rest of it appears to be absorbed.
Altering the source and destination blend states can give the appearance of emissive objects in a foggy or dusty atmosphere. For instance, if your application models flames, force fields, plasma beams, or similarly radiant objects in a foggy environment, set the source and destination blend states to D3DBLEND_ONE.
Another application of alpha blending is controlling the lighting in a 3-D scene, also called light mapping. Setting the source blend state to D3DBLEND_ZERO and the destination blend state to D3DBLEND_SRCALPHA darkens a scene according to the source alpha information. The source primitive is used as a light map that scales the contents of the frame buffer to darken it when appropriate. This produces monochrome light mapping.
Color light mapping can be achieved by setting the source alpha blending state to D3DBLEND_ZERO and the destination blend state to D3DBLEND_SRCCOLOR.
Direct3D devices provide alpha value stippling if it is supported by the display hardware. See D3DRENDERSTATE_STIPPLEDALPHA. If your application creates an RGB or ramp software emulation device, Direct3D ignores this enumerated value.
Applications can use alpha testing to control when pixels are written to the render target surface. By using the D3DRENDERSTATE_ALPHATESTENABLE enumerated value, your application sets the current Direct3D device so that it tests each pixel according to an alpha test function. If the test succeeds, the pixel is written to the surface. If it doesn't, Direct3D ignores it. Select the alpha test function with the D3DRENDERSTATE_ALPHAFUNC enumerated value. Your application can set a reference alpha value for all pixels to be compared against by using the D3DRENDERSTATE_ALPHAREF render state.
The most common use for alpha testing is to improve performance when rasterizing objects that are nearly transparent. If the color data being rasterized is more opaque than the color already at a given pixel (D3DPCMPCAPS_GREATEREQUAL) then the pixel is written, otherwise the rasterizer ignores the pixel altogether, saving the processing required to blend the two colors. The following example checks to see if a given comparison function is supported and, if so, it sets the comparison function parameters required to improve performance during rendering.
// This example assumes that pd3dDeviceDesc is a
// D3DDEVICEDESC structure that was filled with a
// previous call to IDirect3DDevice3::GetCaps.
if (pd3dDeviceDesc->dpcTriCaps.dwAlphaCmpCaps & D3DPCMPCAPS_GREATEREQUAL)
{
dev->SetRenderState( D3DRENDERSTATE_ALPHAREF, (DWORD)0x00000001);
dev->SetRenderState( D3dRENDERSTATE_ALPHATESTENABLE, TRUE );
dev->SetRenderState( D3DRS_ALPHACMP, D3DCMP_GREATEREQUAL );
}
// If the comparison isn't supported, render anyway.
// The only drawback is no performance gain.
Not all hardware supports all alpha testing features. You can check the device capabilities by calling the IDirect3DDevice3::GetCaps method. After retrieving the device capabilities, check the dwAlphaCmpCaps member of the D3DPRIMCAPS structure (contained by the associated D3DDEVICEDESC structure) for the desired comparison function. If the dwAlphaCmpCaps member contains only the D3DPCMPCAPS_ALWAYS capability or only the D3DPCMPCAPS_NEVER capability, the driver does not support alpha tests at all.