ID3DXMesh** objMesh;
And for loading materials and textures, I made a vector like this:
std::vector<D3DMATERIAL9> *objMtrls;std::vector<IDirect3DTexture9*> *objTexture;
Now after deciding the number of mesh that needs to be loaded (this is decided in runtime), let's suppose for this case 2 meshes are to be loaded, I create mesh, material, and texture array like this:
objMesh = new ID3DXMesh* [2];objMtrls = new std::vector<D3DMATERIAL9> [2];objTex = new std::vector<IDirect3DTexture9*> [2];
Now, I have created a namespace called "d3dLoadGraphics" and in it a function called "loadXObjects" for loading x files. Here it is:
bool d3dLoadGraphics::loadXObjects(LPCSTR lpXFileName, ID3DXMesh **meshOut, IDirect3DDevice9 *d3dDev, std::vector<D3DMATERIAL9> *mtrls, std::vector<IDirect3DTexture9*> *textures) { ID3DXBuffer* mtrlsBuff; ID3DXBuffer* adajBuff; DWORD numMtrls; HRESULT retVal; retVal = D3DXLoadMeshFromX(lpXFileName, D3DXMESH_MANAGED, d3dDev, &adajBuff, &mtrlsBuff, NULL, &numMtrls, meshOut); if (retVal != D3D_OK) { MessageBox(NULL, lpXFileName, "LOADING MESH FAILED", MB_ICONERROR); return false; } if (mtrlsBuff != 0 && numMtrls != 0) { D3DXMATERIAL* xMtrls = (D3DXMATERIAL*) mtrlsBuff->GetBufferPointer(); for (unsigned int i = 0; i < numMtrls; i++) { xMtrls.MatD3D.Ambient = xMtrls.MatD3D.Diffuse; mtrls->push_back(xMtrls.MatD3D); if (xMtrls.pTextureFilename != NULL) { IDirect3DTexture9 *tex = 0; retVal = D3DXCreateTextureFromFile(d3dDev, xMtrls.pTextureFilename, &tex); if (retVal != D3D_OK) { MessageBox(NULL, xMtrls.pTextureFilename, "LOADING TEXTURE FAILED", MB_ICONERROR); return false; } textures->push_back(tex); } else textures->push_back(0); } release<ID3DXBuffer*>(mtrlsBuff); } retVal = meshOut[0]->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE, (DWORD*)adajBuff->GetBufferPointer(), NULL, NULL, NULL); if (retVal != D3D_OK) { MessageBox(NULL, lpXFileName, "OPTIMIZING MESH FAILED", MB_ICONERROR); return false; } return true;}
I think there is a bug in above function. Here is how I load a mesh file:
d3dLoadGraphics::loadXObjects("test1.x", &objMesh[0], d3dDevice, &objMtrls[0], &objTex[0]);d3dLoadGraphics::loadXObjects("test2.x", &objMesh[1], d3dDevice, &objMtrls[1], &objTex[1]);
After loading it, here is how I render it:
for(unsigned int i = 0; i < objMtrls[0].size(); i++) { if (objTex[0]) d3dDevice->SetMaterial(&WhiteMtrl); else d3dDevice->SetMaterial(&objMtrls[0]); d3dDevice->SetTexture(0, objTex[0]); objMesh[0]->DrawSubset(0); } D3DXMatrixTranslation(&W, 3.0f, 0, 0); d3dDevice->SetTransform(D3DTS_WORLD, &W); for(unsigned int i = 0; i < objMtrls[1].size(); i++) { if (objTex[1]) d3dDevice->SetMaterial(&WhiteMtrl); else d3dDevice->SetMaterial(&objMtrls[1]); d3dDevice->SetTexture(0, objTex[1]); objMesh[1]->DrawSubset(0); }
Ok now the problem is, object get loaded but without texture. Can you check the function loadXObjects and tell me if I wrote it correctly or not? Also, this line:
retVal = meshOut[0]->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE, (DWORD*)adajBuff->GetBufferPointer(), NULL, NULL, NULL);
Is this line correct? Please help me!!!