Game Development Reference
In-Depth Information
To do this, we modify our vertex structure once again and add a pair of
texture coordinates that identifies a vertex on the texture.
struct Vertex
{
float _x, _y, _z;
float _nx, _ny, _nz;
float _u, _v; // texture coordinates
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
Observe that we have added D3DFVF_TEX1 to our vertex format
description, which says that our vertex structure contains one pair of
texture coordinates.
Now every triangle built from three Vertex objects also defines a
corresponding texture triangle from the texture coordinates.
Note: Although we specify a corresponding texture triangle to a 3D
triangle, the texture isn't mapped until the rasterization stage where
the 3D triangle has been transformed to screen space.
6.2 Creating and Enabling a Texture
Texture data is usually read from an image file stored on disk and
loaded into an IDirect3DTexture9 object. To do this, we can use the
following D3DX function:
HRESULT D3DXCreateTextureFromFile(
LPDIRECT3DDEVICE9 pDevice, // device to create the texture
LPCSTR pSrcFile, // filename of image to load
LPDIRECT3DTEXTURE9* ppTexture // ptr to receive the created texture
);
This function can load any of the following image formats: BMP, DDS,
DIB, JPG, PNG, and TGA.
For example, to create a texture from an image called stone-
wall.bmp, we would write the following:
IDirect3Dtexture9* _stonewall;
D3DXCreateTextureFromFile(_device, "stonewall.bmp", &_stonewall);
To set the current texture, we use the following method:
HRESULT IDirect3DDevice9::SetTexture(
DWORD Stage, // A value in the range 0-7 identifying the texture
// stage - see note on Texture Stages
IDirect3DBaseTexture9* pTexture // ptr to the texture to set
);
Search WWH ::




Custom Search