Direct3D Textures

Loading A Texture

Loading textures in Direct3D is very easy using the D3DX functions.

Note: to use the D3DX functions you must include the header d3dx9.h and link with d3dx9d.lib (in debug build) or d3dx9.lib (in release mode).

You need to obtain a pointer from Direct3D to a set of texture functions. This pointer can be declared like this:

LPDIRECT3DTEXTURE9 gTexture=NULL;

This creates an instance of a pointer to a Direct3D texture. The type is a synonym and resolves to IDirect3DTexture9*. This pointer is returned from the following function and set to point to an instance of the texture.

To load a texture from a file and allow Direct3D to handle memory location and formatting for your hardware device you can use the D3DXCreateTextureFromFile function, its definition is:

HRESULT D3DXCreateTextureFromFile(LPDIRECT3DDEVICE9 pDevice,LPCTSTR pSrcFile, LPDIRECT3DTEXTURE9 *ppTexture );

  • pDevice - your device pointer
  • pSrcFile - the full path and filename of the texture e.g. "earth.bmp", "fire.tga" etc.
  • ppTexture - the address of your texture pointer, this is the returned pointer.

D3DXCreateTextureFromFile automatically converts the texture into a correct format for your device (the video card when in hardware mode). The following file formats are supported: bmp, .dds, .dib, .jpg, .png, and .tga. Mipmaps are loaded (in .dds files) and alpha if present (supported by .tga and .dds files amongst others). The texture memory allocation is left to Direct3D to handle – it will place it in the best place possible. Remember to check the return code for success. As with all DirectX objects you must release the texture once you have finished with it e.g.

texture->Release()

Further Reading: the Microsoft help for this function can be found here: D3DXCreateTextureFromFile

Sampler States

Some states can be set that control the displaying of a texture, these are called sampler states.

HRESULT SetSamplerState(DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value );

  • Sampler -  the sampler stage. For one texture this is 0.
  • Type - the state you wish to set
  • Value - the value you wish to set for the state

There are a number of states, some are:

- D3DSAMP_MINFILTER allows you to determine how a texture will be displayed when it is rendered smaller than its actual size. Choices include point, linear and anisotropic
- D3DSAMP_MAGFILTER as above for when a texture is rendered larger than its actual size.
- D3DSAMP_ADDRESSU and V, allows you to specify if the edge of a texture should wrap around or be clamped when rendered.

e.g. to turn on bilinear filtering for texturing:

gD3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
gD3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
gD3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );

Note: these states stay set until you change them. More information on Sampler States can be found here: Sampler States.

Transparent Textures

To use transparent textures in Direct3D is fairly straightforward. Simply create a texture in a format that supports alpha, e.g. tga and load as described above. You then need to turn on transparency by enabling alpha blending before rendering. You should then get transparent textures.

SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);

Advanced Note: if you are rendering many semitransparent graphics on top of each other (like in a particle system) you are going to need to sort them back to front before rendering so the blending works in the correct way.

More information on Render States can be found here: Render States

Combining Textures / Multitexturing

Textures can be combine with each other or with colour values when rendering primitives. How they are combined depends on the texture stage states.

Further Reading

On this site: Render States, Sampler States, Texture States, Sprites, Reading from a texture
Other: MSDN D3DXCreateTextureFromFile



© 2004-2016 Keith Ditchburn