Microsoft DirectX 8.1 (Visual Basic)

Step 1: Loading a Mesh Object

A Microsoft® Direct3D® application must first load a mesh before using it. The Meshes sample project loads the tiger mesh by calling InitGeometry, an application-defined function, after loading the required Direct3D objects.

A mesh needs a material buffer that will store information about all the materials and textures that will be used. The function starts by declaring a material buffer.

Dim MtrlBuffer As D3DXBuffer

The Meshes sample project uses the D3DX8.LoadMeshFromX method to load the mesh, as shown in the code below.

Set g_Mesh = g_D3DX.LoadMeshFromX(App.Path + "\Tiger.x", D3DXMESH_MANAGED, _
                               g_D3DDevice, Nothing, MtrlBuffer, g_NumMaterials)

If g_Mesh Is Nothing Then Exit Function

The first parameter that D3DXLoadMeshFromX accepts is a string that tells the name of the Microsoft DirectX® file to load. This sample loads the tiger mesh from Tiger.x. The second parameter tells Direct3D how to create the mesh. The sample uses the D3DXMESH_MANAGED flag, which tells Direct3D to use the D3DPOOL_MANAGED memory class for the index buffers. The third parameter is the Direct3D device that will be used to render the mesh. The fourth parameter takes a D3DXBuffer object. This object will be filled with information about neighbors for each face. This information is not required for this sample, so this parameter is set to Nothing. The fifth parameter also takes a pointer to a D3DXBuffer object. After this method is finished, this object will be filled with D3DXMATERIAL structures for the mesh. The sixth parameter takes a variable of type Long that will tell the number of D3DXMATERIAL structures placed into the RetMaterials parameter after this method returns.

After loading the mesh object and material information, you need to extract the material properties and texture names from the material buffer.

Before getting this information from the material buffer, the material and texture arrays must be resized to fit all the materials and textures for this mesh.

ReDim g_MeshMaterials(g_NumMaterials)
ReDim g_MeshTextures(g_NumMaterials)

For each material in the mesh the following steps occur.

The first step is to use D3DX8.BufferGetMaterial to copy the material, as shown in the code fragment below.

g_D3DX.BufferGetMaterial MtrlBuffer, i, g_MeshMaterials(i)

The first parameter that BufferGetMaterial accepts is the material object, which in this case is MtrlBuffer. The second parameter is the index of the material to retrieve. The third parameter is the location to place the material.

The second step is to set the ambient color for the material, as shown in the following code fragment.

g_MeshMaterials(i).Ambient = g_MeshMaterials(i).diffuse

The final step is to create the texture for the material, as shown in the following code fragment.

strTexName = g_D3DX.BufferGetTextureName(MtrlBuffer, i)
If strTexName <> "" Then
    Set g_MeshTextures(i) = g_D3DX.CreateTextureFromFile(g_D3DDevice, App.Path + "\" + strTexName)
End If

After loading all the materials, you need to let Direct3D know that you are finished with the material buffer by setting it to Nothing.

Set MtrlBuffer = Nothing

The mesh, along with the corresponding materials and textures are loaded. The mesh is ready to be rendered to the display, as described in Step 2: Rendering a Mesh Object.