Programming with DirectX : Additional Texture Mapping - Alpha Mapping
Creating Alpha Maps
There are two ways to specify per-pixel alpha values. The first is to use the alpha channel of a 32-bit RGBA image. This can be done in any image editor, and the alpha values typically range between 0 for transparent and 255 for visible. Anything between 0 and 255 represents a level of transparency between the two extremes. In fact, you can consider 0 to be 0% and 255 to be 100% visible. An example of an alpha channel from a texture created in Adobe Photoshop is shown in Figure 1. If this alpha map was used, there would be many semi-transparent areas (any gray area in the alpha map), only a few fully visible areas, and even fewer fully transparent areas.
Figure 1. The alpha map channel in Adobe Photoshop.
Another way to create an alpha map is to create a separate texture image. If you use an RGB image, these images are typically grayscale images (black and white). More specifically, it means the red, green, and blue components have equal values (e.g., R:200 G:200 B:200, R:123 G:123 B:123, etc.). When you use a grayscale image, it does not matter which color channel you use for the alpha value since it is the same value across the channels. If the image is not in black and white, it would be difficult to determine how the alpha map would affect the rendered object unless you were looking solely at one component at a time.
Creating an alpha map using a 24-bit RGB image is wasteful memory-wise if you are only using one component because the alpha channel is one byte; having RGBA images just to use that one channel means that the other channels (the RGB channels) are not used, which results in three wasted bytes per pixel. Therefore, it is better to use either a one-component image or the alpha channel of an RGBA image. That way each pixel is 1 byte instead of 3. If semitransparent pixels are not needed, you can replace that 1 byte per pixel with 1 bit, where 0 represents invisible and 1 represents visible (i.e., true or false).
Alpha Mapping Demo
The demo places the alpha values in the alpha channel of the image to avoid having to load a separate image, and the alpha map used is the one from Figure 1. The image in Figure 1 was copied into the alpha channel of the decal image in Photoshop and saved as an RGBA DXT5 texture.
Alpha mapping is done by using alpha blending. In Direct3D 10 this can be set on the application side or through HLSL. Throughout this article we will set states in HLSL, but if you were to do it on the application side, you would create a D3D10_BLEND_DESC object, set each of its member states (variables), and send it to Direct3D by calling OMSetBlendState().
In HLSL a blend state can be created by creating a BlendState descriptor and naming it whatever you like. In the Alpha Mapping demo for this article, the BlendState descriptor is called AlphaBlending. Inside the descriptor we can set a host of related states that include the following.
AlphaToCoverageEnable
BlendEnable
SrcBlend
DstBlend
BlendOp
SrcBlendAlpha
DstBlendAlpha
BlendOpAlpha
RenderTargetWriteMask[n]
Alpha to coverage is a term in computer graphics that deals with multisampling and refers to the way alpha-mapped surfaces are rendered in scenes that have many overlapping polygons. Alpha to coverage can be used even if the application is not using multisampling, in which case it is used to draw overlapping polygons that have transparency without the need for you to render them in a specific order to get the correct results.
The keyword BlendEnable enables or disables the blending feature, while SrcBlend, SrcBlendAlpha, DstBlend, and DstBlendAlpha set the blend options of the source 1 (SRC) and source 2 (DST) colors and alphas. The blend options can be one of the following. Note that the HLSL keywords are the same minus the D3D10_BLEND_ part of each option.
D3D10_BLEND_ZERO: The data source is black.
D3D10_BLEND_ONE: The data source is white.
D3D10_BLEND_SRC_COLOR: The data source is the color from the pixel shader.
D3D10_BLEND_INV_SRC_COLOR: The data source is 1 minus the color from the pixel shader.
D3D10_BLEND_SRC_ALPHA: The data source is the alpha value from the pixel shader.
D3D10_BLEND_INV_SRC_ALPHA: The data source is 1 minus the alpha value from the pixel shader.
D3D10_BLEND_DEST_ALPHA: The data source is the alpha value from the rendering target.
D3D10_BLEND_INV_DEST_ALPHA: The data source is 1 minus the alpha value from the rendering target.
D3D10_BLEND_DEST_COLOR: The data source is the color value that is already stored in the rendering target.
D3D10_BLEND_INV_DEST_COLOR: The data source is 1 minus the render target’s color value.
D3D10_BLEND_SRC_ALPHA_SAT: The data source is the clamped alpha value from the pixel shader.
D3D10_BLEND_BLEND_FACTOR: The data source is the blend factor that was set by the Direct3D function OMSetBlendState() in the second parameter (the first parameter is the blend descriptor, and the last is the blend mask).
D3D10_BLEND_INV_BLEND_FACTOR: The data source is 1 minus the blend factor set by the Direct3D function OMSetBlendState().
D3D10_BLEND_SRC1_COLOR: The data source is both color values outputted by the pixel shader (used in dual-source color blending).
D3D10_BLEND_INV_SRC1_COLOR: The data source is 1 minus the color values from the pixel shader.
D3D10_BLEND_SRC1_ALPHA: The same as D3D10_BLEND_SRC1_COLOR but using the alpha values from the pixel shader.
D3D10_BLEND_INV_SRC1_ALPHA: One minus D3D10_BLEND_SRC1_ALPHA.
BlendOp and BlendOpAlpha from the blend state are used to set the blend operation. This means the two sources can be added using D3D10_BLEND_OP_ADD, subtracted using D3D10_BLEND_OP_SUBTRACT (or D3D10_BLEND_OP_REV_SUB-TRACT, which subtracts source 2 from source 1), set to the minimum of the two sources using D3D10_BLEND_OP_MIN, or set to the maximum of the two sources using D3D10_BLEND_OP_MAX.
The last member of the blend description, RenderTargetWriteMask[n], is used to set the per-pixel write mask that is used to determine which components of the rendering target can be written to during rendering. A value of 0×0F can be used to specify that all components are to be written to. The value 0×0F is essentially the logical OR result of D3D10_COLOR_WRITE_ENABLE_RED | D3D10_COLOR_WRITE_ENABLE_GREEN | D3D10_COLOR_WRITE_ENABLE_BLUE | D3D10_COLOR_WRITE_ENABLE_ALPHA.
The shader from the Alpha Mapping demo is shown in Listing 1. This object is set by calling the HLSL function SetBlendState(), which is the same as calling the Direct3D function OMSetBlendState() on the application side. The function takes the blend state description, a blend factor, and a blend mask. The mask is set to 0xFFFFFF to allow for all components. Note that you can set the blend state either in HLSL, which is what we are doing in this demo, or on the application side using OMSetBlendState(). You don’t have to do both.
Listing 1. The Alpha Mapping Demo’s Shader
In the Alpha Mapping demo the only difference is in the HLSL effect shader. The main source file from the demo is the same except for a few object name changes. The relevant code form the Alpha Mapping demo’s main source file is shown in Listing 2. Figure 2 shows a screenshot from the demo.
Listing 2. The Relevant Code in the Alpha Mapping Demo’s Source File