Hi everyone,
I'm doing some Direct3D programming and I'm at the point where I can load .x files and display them in the environment using the techniques described on this page:
Load X Simply
I'm also cross-referencing with the example given in the DirectX code samples. In an attempt to make the process of loading and rendering a .x file easier, I've created a class to do it for me. I'm just going to go ahead and post my class here:
Code:
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;};
void MeshObjectLoad(){ //Load the mesh
D3DXLoadMeshFromX(filename.c_str(), D3DXMESH_SYSTEMMEM, renderdev, NULL, &materialBuffer,NULL, &numMaterials, &texturedMesh);
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]);}}}
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(){ //Cleanup for mesh
materialBuffer->Release();}
};
As you can see, there are 3 functions - I call MeshObject.MeshObjectLoad() when I initialise Direct3D, I call MeshObject.MeshObjectRender() when I render my frame, and I call MeshObject.MeshObjectClean() when I cleanup after closing my program. When I try and create an instance of this class (just underneath the class definition itself):
Code:
MeshObject meshTorus("Torus.x", d3ddev);
... it compiles fine but causes a program crash. I've narrowed this down to the following line in the MeshObjectLoad() function:
Code:
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer(); //Get pointer to materialBuffer
If I remove this line and everything after it and use the class, the program runs fine (apart from the fact that I don't have my Torus.x object loaded into it). So my question is:
What is wrong with this line to cause the program to crash? Can anyone tell me of a better way to define my class to load .x files? I'd like to know if I'm doing something horribly wrong, but as far as I can tell, this should work. It's the same basic structure as the DirectX mesh tutorial code in the samples directory, but rather I've put the mesh loading/rendering code into a class so I don't have to repeat it all when I want to load more than one object.
Thanks a lot!