JavaScript EditorFree JavaScript Editor     Ajax Editor 

Main Page
  Previous Section Next Section

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:


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:


and the corresponding structure:

    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.

      Previous Section Next Section

    JavaScript EditorAjax Editor     JavaScript Editor