模型加载类
#ifndef __MeshH__ #define __MeshH__ #include <vector> using namespace std; struct SMesh { ID3DXMesh* Mesh; vector<D3DMATERIAL9> Mtrls; vector<IDirect3DTexture9*> Textures; }; #include <d3d9.h> #include <d3dx9.h> void CreateMesh(IDirect3DDevice9* pDevice, char* sName, SMesh& Mesh) { Mesh.Mesh = NULL; Mesh.Mtrls.clear(); Mesh.Textures.clear(); ID3DXBuffer* adjBuffer = 0; ID3DXBuffer* mtrlBuffer = 0; DWORD numMtrls = 0; //Load FighterMan Mesh D3DXLoadMeshFromXA(sName, D3DXMESH_MANAGED, pDevice, &adjBuffer, &mtrlBuffer, 0, &numMtrls, &Mesh.Mesh); if(mtrlBuffer != 0 && numMtrls != 0) { D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer(); for(int i = 0; i < numMtrls; i++) { mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse; Mesh.Mtrls.push_back(mtrls[i].MatD3D); if(mtrls[i].pTextureFilename != 0) { IDirect3DTexture9* tex = 0; D3DXCreateTextureFromFileA(pDevice, mtrls[i].pTextureFilename, &tex); Mesh.Textures.push_back(tex); } else { Mesh.Textures.push_back(0); } } } if(mtrlBuffer) mtrlBuffer->Release(); if(Mesh.Mesh)) Mesh.Mesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE, (DWORD*)adjBuffer->GetBufferPointer(), 0, 0, 0); if(adjBuffer) adjBuffer->Release(); } void DrawMesh(IDirect3DDevice9* pDevice, SMesh& Mesh) { for(int i = 0; i < Mesh.Mtrls.size(); i++) { pDevice->SetMaterial(&Mesh.Mtrls[i]); pDevice->SetTexture(0, Mesh.Textures[i]); Mesh.Mesh->DrawSubset(i); } } #endif