Loading a .x file into your program (在程序中载入.x文件)
这一章我们会增加一个新的类CMesh,它的功能是渲染从.x文件中读取的网格(Mesh)。下面有CMesh类的源代码,它的构造函数将展示出怎样从.x文件中读取网格(Mesh)并且将它们保存至内存,它的析构函数将展示出怎样释放网格所占有的内存。它的渲染模块会告诉你怎样渲染网格(Mesh)。
//Create two arrays. One to hold the materials and only to hold the textures
m_pMeshMaterials = new D3DMATERIAL8[m_dwNumMaterials];
m_pMeshTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
for(DWORD i = 0; i < m_dwNumMaterials; i++)
{
//Copy the material
m_pMeshMaterials[i] = matMaterials[i].MatD3D;
//Set the ambient color for the material (D3DX does not do this)
m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;
//We've finished with the material buffer, so release it
SafeRelease(pMaterialsBuffer);
//Make sure that the normals are setup for our mesh
pMesh->CloneMeshFVF(D3DXMESH_MANAGED, MESH_D3DFVF_CUSTOMVERTEX,
m_pD3DDevice, &m_pMesh);
SafeRelease(pMesh);
D3DXComputeNormals(m_pMesh);
LogInfo("<li>Mesh '%s' loaded OK", pFilename);
}
CMesh::~CMesh()
{
SafeDelete(m_pMeshMaterials);
if(m_pMeshTextures != NULL)
{
for(DWORD i = 0; i < m_dwNumMaterials; i++)
{
if(m_pMeshTextures[i])
{
SafeRelease(m_pMeshTextures[i]);
}
}
}
//Create two arrays. One to hold the materials and only to hold the textures
m_pMeshMaterials = new D3DMATERIAL8[m_dwNumMaterials];
m_pMeshTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
for(DWORD i = 0; i < m_dwNumMaterials; i++)
{
// Copy the material
m_pMeshMaterials[i] = matMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;
//We've finished with the material buffer, so release it
SafeRelease(pMaterialsBuffer);
//Make sure that the normals are setup for our mesh
pMesh->CloneMeshFVF(D3DXMESH_MANAGED, MESH_D3DFVF_CUSTOMVERTEX, m_pD3DDevice, &m_pMesh);
SafeRelease(pMesh);
D3DXComputeNormals(m_pMesh);
LogInfo("<li>Mesh '%s' loaded OK", pFilename);
}
CMesh::~CMesh()
{
SafeDelete(m_pMeshMaterials);
if(m_pMeshTextures != NULL)
{
for(DWORD i = 0; i < m_dwNumMaterials; i++)
{
if(m_pMeshTextures[i])
{
SafeRelease(m_pMeshTextures[i]);
}
}
}