Code to load and draw a DirectX .X model
The following code is from book source MDX Kick Start to load a .X mesh file:
======================================
private void LoadMesh(string file)
{
ExtendedMaterial[] mtrl;
// Load our mesh
mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);
// If we have any materials, store them
if ((mtrl != null) && (mtrl.Length > 0))
{
meshMaterials = new Material[mtrl.Length];
meshTextures = new Texture[mtrl.Length];
// Store each material and texture
for (int i = 0; i < mtrl.Length; i++)
{
meshMaterials = mtrl.Material3D;
if ((mtrl.TextureFilename != null) && (mtrl.TextureFilename != string.Empty))
{
// We have a texture, try to load it
meshTextures = TextureLoader.FromFile(device, @"..\..\" + mtrl.TextureFilename);
}
}
}
}
============================================
You also need to have declared the following:
Mesh mesh = null;
Material[] meshMaterials;
Texture[] meshTextures;
The mesh can be drawn using the following code:
============================================
for (int i = 0; i < meshMaterials.Length; i++)
{
device.Material = meshMaterials;
device.SetTexture(0, meshTextures);
mesh.DrawSubset(i);
}