本篇是D3D中的Alpha颜色混合(2)的后续篇。
另一种实现实现背景透明显示的简便方法是直接应用渲染管道流水线的Alpha测试功能进行,D3D中的Alpha颜色混合(2)介绍的接口方法实际就是对Alpha测试的一个包装。Alpha测试是对需要写入绘图表面的像素颜色Alpha值进行测试,判断该Alpha值是否满足预先设定的条件,如果满足条件,则将该像素颜色值写入绘图表面,否则不写入。
如上图所示,瞄准器的背景色为标准绿色,为了使瞄准镜可以背景透明地显示在其他图象的上面,需要把瞄准镜的绿色部分镂空。为此,可调用 D3DXCreateTextureFromFileEx函数,相应的创建一个背景为黑色的纹理对象,这个黑色的Alpha值为 0,它的RGBA颜色值则为(0, 0, 0, 0)。然后,开启渲染管道流水线的Alpha测试,并使Alpha测试仅对Alpha值大于或等于某个值的像素颜色值进行写入,以使Alpha值为0的瞄准镜背景黑色不能写入绘图表面,从而使得瞄准镜图象贴在老虎背景画面上时,背景可透明地显示出来。
来看看 D3DXCreateTextureFromFileEx的具体使用说明: Creates a texture from a file. This is a more advanced function than D3DXCreateTextureFromFile. HRESULT D3DXCreateTextureFromFileEx(
LPDIRECT3DDEVICE9
pDevice,
LPCTSTR
pSrcFile,
UINT
Width,
UINT
Height,
UINT
MipLevels,
DWORD
Usage,
D3DFORMAT
Format,
D3DPOOL
Pool,
DWORD
Filter,
DWORD
MipFilter,
D3DCOLOR
ColorKey,
D3DXIMAGE_INFO *
pSrcInfo,
PALETTEENTRY *
pPalette,
LPDIRECT3DTEXTURE9 *
ppTexture ); Parameters- pDevice
- [in] Pointer to an IDirect3DDevice9 interface, representing the device to be associated with the texture.
- pSrcFile
- [in] Pointer to a string that specifies the filename. If the compiler settings require Unicode, the data type LPCTSTR resolves to LPCWSTR. Otherwise, the string data type resolves to LPCSTR. See Remarks.
- Width
- [in] Width in pixels. If this value is zero or D3DX_DEFAULT, the dimensions are taken from the file and rounded up to a power of two. If the device supports non-power of 2 textures and D3DX_DEFAULT_NONPOW2 is specified, the size will not be rounded.
- Height
- [in] Height, in pixels. If this value is zero or D3DX_DEFAULT, the dimensions are taken from the file and rounded up to a power of two. If the device supports non-power of 2 textures and D3DX_DEFAULT_NONPOW2 is sepcified, the size will not be rounded.
- MipLevels
- [in] Number of mip levels requested. If this value is zero or D3DX_DEFAULT, a complete mipmap chain is created. If D3DX_FROM_FILE, the size will be taken exactly as it is in the file, and the call will fail if this violates device capabilities.
- Usage
- [in] 0, D3DUSAGE_RENDERTARGET, or D3DUSAGE_DYNAMIC. Setting this flag to D3DUSAGE_RENDERTARGET indicates that the surface is to be used as a render target. The resource can then be passed to the pNewRenderTarget parameter of the IDirect3DDevice9::SetRenderTarget method. If either D3DUSAGE_RENDERTARGET or D3DUSAGE_DYNAMIC is specified, Pool must be set to D3DPOOL_DEFAULT, and the application should check that the device supports this operation by calling IDirect3D9::CheckDeviceFormat. D3DUSAGE_DYNAMIC indicates that the surface should be handled dynamically. See Using Dynamic Textures.
- Format
- [in] Member of the D3DFORMAT enumerated type, describing the requested pixel format for the texture. The returned texture might have a different format from that specified by Format. Applications should check the format of the returned texture. If D3DFMT_UNKNOWN, the format is taken from the file. If D3DFMT_FROM_FILE, the format is taken exactly as it is in the file, and the call will fail if this violates device capabilities.
- Pool
- [in] Member of the D3DPOOL enumerated type, describing the memory class into which the texture should be placed.
- Filter
- [in] A combination of one or more D3DX_FILTER constants controlling how the image is filtered. Specifying D3DX_DEFAULT for this parameter is the equivalent of specifying D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER.
- MipFilter
- [in] A combination of one or more D3DX_FILTER constants controlling how the image is filtered. Specifying D3DX_DEFAULT for this parameter is the equivalent of specifying D3DX_FILTER_BOX. In addition, use bits 27-31 to specify the number of mip levels to be skipped (from the top of the mipmap chain) when a .dds texture is loaded into memory; this allows you to skip up to 32 levels.
- ColorKey
- [in] D3DCOLOR value to replace with transparent black, or 0 to disable the color key. This is always a 32-bit ARGB color, independent of the source image format. Alpha is significant and should usually be set to FF for opaque color keys. Thus, for opaque black, the value would be equal to 0xFF000000.
- pSrcInfo
- [in, out] Pointer to a D3DXIMAGE_INFO structure to be filled in with a description of the data in the source image file, or NULL.
- pPalette
- [out] Pointer to a PALETTEENTRY structure, representing a 256-color palette to fill in, or NULL.
- ppTexture
- [out] Address of a pointer to an IDirect3DTexture9 interface, representing the created texture object.
Return ValuesIf the function succeeds, the return value is D3D_OK. If the function fails, the return value can be one of the following: D3DERR_INVALIDCALL. D3DERR_NOTAVAILABLED3DERR_OUTOFVIDEOMEMORYD3DXERR_INVALIDDATAE_OUTOFMEMORY RemarksThe compiler setting also determines the function version. If Unicode is defined, the function call resolves to D3DXCreateTextureFromFileExW. Otherwise, the function call resolves to D3DXCreateTextureFromFileExA because ANSI strings are being used. Use D3DXCheckTextureRequirements to determine if your device can support the texture given the current state. This function supports the following file formats: .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga. See D3DXIMAGE_FILEFORMAT. Mipmapped textures automatically have each level filled with the loaded texture. When loading images into mipmapped textures, some devices are unable to go to a 1x1 image and this function will fail. If this happens, then the images need to be loaded manually. For the best performance when using D3DXCreateTextureFromFileEx: - Doing image scaling and format conversion at load time can be slow. Store images in the format and resolution they will be used. If the target hardware requires power of 2 dimensions, then create and store images using power of 2 dimensions.
- For mipmap image creation at load time, filter using D3DX_FILTER_BOX. A box filter is much faster than other filter types such as D3DX_FILTER_TRIANGLE.
- Consider using DDS files. Since DDS files can be used to represent any Direct3D 9 texture format, they are very easy for D3DX to read. Also, they can store mipmaps, so any mipmap-generation algorithms can be used to author the images.
When skipping mipmap levels while loading a .dds file, use the D3DX_SKIP_DDS_MIP_LEVELS macro to generate the MipFilter value. This macro takes the number of levels to skip and the filter type and returns the filter value, which would then be passed into the MipFilter parameter.
|