Step 2 - Rendering a Mesh Object

In step 1 the mesh was loaded and is now ready to be rendered. It is divided into a subset for each material that was loaded for the mesh. To render each subset, the mesh is rendered in a loop. The first step in the loop is to set the material for the subset, as shown in the following code fragment.

g_pd3dDevice->SetMaterial( &g_pMeshMaterials[i] );

The second step in the loop is to set the texture for the subset, as shown in the following code fragment.

g_pd3dDevice->SetTexture( 0, g_pMeshTextures[i] );

After setting the material and texture, the subset is drawn with the ID3DXBaseMesh::DrawSubset method, as shown in the following code fragment.

g_pMesh->DrawSubset( i );

The ID3DXBaseMesh::DrawSubset method takes a DWORD that specifies which subset of the mesh to draw. This sample uses a value that is incremented each time the loop runs.

After using a mesh, it is important to properly remove the mesh from memory, as described in Step 3 - Unloading a Mesh Object.