Color-Key Transparency

[Previous] [Next]

When you enable color-key transparency by setting a color key, you tell Direct3D to treat a particular color as transparent. To apply a color key to a texture, you must create the texture by using the DDSD_CKSRCBLT flag. Whenever a texture created with the DDSD_CKSRCBLT flag is applied to a primitive, none of the texels that match the color key are rendered on the primitive.

Like alpha testing, enabling color-key transparency accelerates the rasterization of any invisible pixels because Direct3D performs only a texture read (as opposed to a texture write). Only a read operation is performed because the color-key test result tells Direct3D to skip the write operation for that pixel. Keep in mind, however, that visible pixels incur a slight performance degradation because of the color-key test. Also, unlike alpha blending, in which there are levels of transparency, with color-key transparency enabled, the color is always completely transparent and thus never rendered.

To set a color key, you use the IDirectDrawSurface7::SetColorKey method for the surface you want the color key to be used for. This method is defined as follows:

 HRESULT SetColorKey(     DWORD dwFlags,                LPDDCOLORKEY lpDDColorKey ); 

ParameterDescription
dwFlags Determines which color key is requested. The following flags are defined:

DDCKEY_COLORSPACE The structure contains a color space. Not set if the structure contains a single color key.

DDCKEY_DESTBLT A color key or color space to be used as a destination color key for blit operations.

DDCKEY_DESTOVERLAY A color key or color space to be used as a destination color key for overlay operations.

DDCKEY_SRCBLT A color key or color space to be used as a source color key for blit operations.

DDCKEY_SRCOVERLAY A color key or color space to be used as a source color key for overlay operations.

lpDDColorKey Address of the DDCOLORKEY structure that contains the new color-key values for the DirectDrawSurface object. This value can be NULL to remove a previously set color key.

The DDCOLORKEY structure can be used to set the parameters of a source or destination color key. This structure is defined as follows:

 typedef struct _DDCOLORKEY{      DWORD dwColorSpaceLowValue;      DWORD dwColorSpaceHighValue; } DDCOLORKEY, FAR* LPDDCOLORKEY; 

The DDCOLORKEY structure has two members:

  • dwColorSpaceLowValue Low value of the color range that is to be used as the color key
  • dwColorSpaceHighValue High value of the color range that is to be used as the color key

You can toggle color keying on and off by using the IDirect3DDevice7::SetRenderState method and setting the first parameter to D3DRENDERSTATE_COLORKEYENABLE and the second to TRUE (enabled) or FALSE (disabled).

This code shows how to set up color keying:

 // // Enable color keying. // m_pd3dDevice->SetRenderState(D3DRENDERSTATE_COLORKEYENABLE, TRUE); 

Direct3D allows you to use both alpha and color keying together. To combine these two features, you must enable alpha testing to make sure that the textures are rendered properly. To set up the color key, use the IDirectDrawSurface7::SetColorKey method and set the dwColorSpaceLowValue and dwColorSpaceHighValue variables. If you use color keying, be sure to set both color space values to the same color value.

NOTE
Remember that if you want to have complete transparency, you can use color keying or one-bit alpha. If you want levels of transparency, however, use alpha blending. The Microsoft DirectX team suggests that developers stop using color keying and use one-bit alpha transparency instead because it avoids certain filtering artifacts.



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