Hi everyone,
I'm following some Direct3D tutorials and I'm at the point where I want to load in multiple .x files and display them. I'm trying to create a class to do this for me, and this is what I have so far:
class MeshObject { //Class for holding meshes
private:
LPD3DXBUFFER materialBuffer; //Holds the materials in the mesh
DWORD numMaterials; //No. of materials in the mesh
LPD3DXMESH texturedMesh; //Our mesh object
D3DMATERIAL9 *meshMaterials; //Create an array of materials for the object
LPDIRECT3DTEXTURE9 *meshTextures; //Create an array of textures for the object
public:
std::string filename; LPDIRECT3DDEVICE9 renderdev;
MeshObject(std::string name, LPDIRECT3DDEVICE9 dev){filename = name; renderdev = dev;};
bool MeshObjectLoad(){ //Load the mesh
if (D3DXLoadMeshFromX(filename.c_str(), D3DXMESH_SYSTEMMEM, renderdev, NULL, &materialBuffer, NULL, &numMaterials, &texturedMesh)!=D3D_OK){return false;}
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer(); //Get pointer to materialBuffer
meshMaterials = new D3DMATERIAL9[numMaterials]; //Create an array of materials for the mesh
meshTextures = new LPDIRECT3DTEXTURE9[numMaterials]; //Create an array of textures for the mesh
for (DWORD i=0; i<numMaterials; i++){ //For all the materials in the .x file
meshMaterials[i] = d3dxMaterials[i].MatD3D; // Copy the material
meshMaterials[i].Ambient = meshMaterials[i].Diffuse; //Set the ambient color for the material
meshTextures[i] = NULL; //Assume no texture exists
if (d3dxMaterials[i].pTextureFilename) // Create the texture if it exists
{D3DXCreateTextureFromFile(renderdev, d3dxMaterials[i].pTextureFilename, &meshTextures[i]);}}
if (materialBuffer!=NULL){materialBuffer->Release();}
return true;}
void MeshObjectRender(){ //Render the mesh
for (DWORD i=0; i<numMaterials; i++){ //For all the differently textured sections of the .x file
renderdev->SetMaterial(&meshMaterials[i]); // Set the material and texture for this subset
renderdev->SetTexture(0,meshTextures[i]);
texturedMesh->DrawSubset(i);}} // Draw the mesh subset
void MeshObjectClean(){ //Clean up after the mesh is used
if (meshMaterials!=NULL){delete[] meshMaterials;}
if (texturedMesh!=NULL){texturedMesh->Release();}
if (meshTextures!=NULL){delete[] meshTextures;}}
};
Whenever I try to use the class it seems to have trouble with the MeshObjectLoad() function. Here are the problems I'm having:
- The D3DXLoadMeshFromX() function fails in its current state - it only succeeds in loading the specified mesh if I exchange 'renderdev' for the name of my actual D3D device (meaning that it won't take a pointer to a D3DDEV from the user.
- When I get the D3DXLoadMeshFromX() working by specifying the global d3d device, the program crashes as soon as it gets to this part:
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer();
This code works fine if I take it out of the class and put it into the main body of the program, but I want to be able to create a class to load more than one .x file without having to re-write all of the code for each new object. What would be the best way to implement this class?
Thanks a lot!