Model
声明变量
ID3DXMesh* mesh = 0; vector<D3DMATERIAL9> Mtrls(0); vector<IDirect3DTexture9*> Textures(0); DWORD numMtrl = 0;
|
加载模型
HRESULT hr = 0; ID3DXBuffer* adjbuffer = 0; ID3DXBuffer* mtrlbuffer = 0;
hr = D3DXLoadMeshFromX("bigship1.x", D3DXMESH_MANAGED, g_pd3dDevice, &adjbuffer, &mtrlbuffer, 0, &numMtrl, &mesh); if (FAILED(hr)) { ::MessageBox(0, "D3DXLoadMeshFromX-lost", 0, 0); return false; }
if (mtrlbuffer != 0 && numMtrl != 0) { D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlbuffer->GetBufferPointer(); for (int i = 0; i < numMtrl; i++) { mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse; Mtrls.push_back(mtrls[i].MatD3D); if (mtrls[i].pTextureFilename != 0) { IDirect3DTexture9* tex = 0; D3DXCreateTextureFromFile(g_pd3dDevice, mtrls[i].pTextureFilename, &tex); Textures.push_back(tex); } else { Textures.push_back(0); } } }
mtrlbuffer->Release();
hr = mesh->OptimizeInplace( D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE, (DWORD*)adjbuffer->GetBufferPointer(), 0, 0, 0 ); adjbuffer->Release();
if (FAILED(hr)) { ::MessageBox(0, "OptimizeInplace-lost", 0, 0); return false; }
|
绘制模型
for (int i = 0; i < numMtrl; i++) { g_pd3dDevice->SetMaterial(&Mtrls[i]); g_pd3dDevice->SetTexture(0, Textures[i]); mesh->DrawSubset(i); }
|
效果