Working with Texture Maps

It is simple to add texture mapping to our Direct3D code. All we have to do is learn how to select the right texture map from a file and how to specify mapping coordinates for the geometry.

To begin with, we can load a texture map from a file by using the code:

 LPDIRECT3DTEXTURE9 g_pTexture = NULL; D3DXCreateTextureFromFile(g_pd3dDevice,"image.bmp", &g_pTexture)); 

Textures can be loaded using the Direct3D utility library. We need to specify a previously initialized Direct3D device object, the file we want to load (.bmp, .dds, .dib, .jpg, .png, and .tga. are all supported), and the texture object that will encapsulate the map from that moment on.

Then, we need a call to specify which is the current texture, so the renderer applies that one to subsequent geometry. This is achieved with the call:

 g_pd3dDevice->SetTexture( 0, g_pTexture ); 

The first parameter is the stage that the texture will be assigned for. The first stage is 0, and so on. Texture stages are useful for multitexturing.

Then, all we have to do is specify an FVF that includes texture coordinates. This way our objects will appear fully textured. This is achieved by using a line like:

 #define D3DFVF_GEOMETRY (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 ) 

and the corresponding structure:

 struct GEOMETRY {     D3DXVECTOR3 p;     D3DXVECTOR3 n;     FLOAT tu, tv; }; 

A final observation is that texture mapping can be switched on and off depending on what you want to do. This is achieved with a call to SetRenderState, which is explained in the next section.



Core Techniques and Algorithms in Game Programming2003
Core Techniques and Algorithms in Game Programming2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 261

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net