If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource
The purpose of this tutorial is to teach you, the reader what steps are necessary when drawing primitives in Direct 3D's Immediate Mode with textures containing transparency. What I mean in this is that each pixel/texel of the texture is either visible or not visible. In other words, the alpha value is either full or zero. This method has the same effect as using source color keys in Direct Draw, but isn't nearly as easy to do in Direct 3D.
This tutorial is NOT here to teach you about how to setup Direct 3D or how to render primitives. I make the assumption that you already know how to do these things. I have also removed most error checking code because I assume you have your own methods already. In a perfect world you should check the response of every function call and react accordingly.
There are five main steps involved in rendering textures in this way:
Just like all the other DirectX Enumeration functions, EnumTextureFormats' first parameter is a pointer to your callback function. The second parameter is a void pointer. In this case we will be sending the address of a DDPIXELFORMAT Variable. The EnumTextureFormats function is called using your LPDIRECT3DDEVICE3 pointer. A good place for this code would be during DirectX Initialization right after QueryInterface'ing for the D3DDevice pointer.
pD3DDevice->EnumTextureFormats(( LPD3DENUMPIXELFORMATSCALLBACK )EnumTextures, ( void* )&TexturePixelFormat );
// If a Texture Format was NOT found, use the Current Back Surface Format instead
if( TexturePixelFormat.dwSize != sizeof( DDPIXELFORMAT ))
pBackSurface->GetPixelFormat( &TexturePixelFormat );
The LPD3DENUMPIXELFORMATSCALLBACK type is a pointer to a function that passes two parameters. The first parameter is of type LPDDPIXELFORMAT. This parameter will have the pixel format information for the current enumeration. The following code is an example of how you might write this function. This version will only accept 16bit textures with a single bit of alpha. There are only two possible texture formats for this that I am aware of are: RGBA 5551 and alternatively ARGB 1555. If the desired texture format is found the enumeration is stopped and the supplied DDPIXELFORMAT value is filled. If the desired format is not found the format will be set using the current primary/back format as above.
Creating the texture involves creating a surface and QueryInterface'ing for the texture. For this you'll need a DDSURFACEDESC2 Variable initialized with the usual information. You will also need to supply the PixelFormat from the section above. You then create the surface using this information and Query for the texture.
Loading the Image, or setting up the surface, requires opening a bmp file and copying the information from it to the surface. The following code uses a DC for this purpose since BitBlt will convert from the 24-bit BMP's I use to the 16-bit format of the surface supplied. You could of course write all this yourself, or better yet load from a image file that already has an alpha value in it, but that's not the purpose of this tutorial. That and I haven't done it myself
Setting up the Transparency is a simple matter of locking the Texture's Surface, scanning through it one colour (as in 2 bytes for a 16-bit surface) at a time and setting the alpha value to 0 or 1. There is an example very similar to this in the Frame work files. The code below locks the surface then retrieves the Alpha Mask and RGB Masks. Since the two possible formats are 1555 or 5551, AlphaMask | RGBMask will always equal 0xFFFF. This code then steps through each pixel in the surface. If the Pixel is black, it is cleared(just in case it already contained alpha information). Otherwise the pixel is OR'd with the AlphaMask (to make it visible). This code ensures that any value in the texture that was black is now non-visible. If you want other colors to be non-visible you need only compare the pixel to your color. The problem with this however is that colors have different values in different modes. One way to get around this is to make the first or last pixel in any bitmap your transparent color then check each pixel against that color.
Rendering the texture is done in the same way you would any other texture, using one of the DrawPrimitive functions. Alpha blending does not need to be on, since this is not blending. However, unless your transparent textures are only going to be used as overlays(like cursors or menu interfaces) you do need to turn on Alpha Testing. As far as I know, what this does is set the Z-Buffer based on the Alpha value. When it is disabled and you draw a rectangle on the screen, the Z-Buffer is updated so that even though you might see through a section of the texture you might chop off part of an object. For a simple example, render a rectangle with a texture where the center is transparent and render a cube in the center of the transparent section of this rectangle. You will only see the front half of the cube. To fix this, just set the renderstates accordingly: