Jump to content

  • Log In with Google      Sign In   
  • Create Account


Creating a class to load .x files


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 george7378   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 24 January 2013 - 04:45 AM

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!



Sponsor:

#2 Dan Mayor   Crossbones+   -  Reputation: 1712

Like
1Likes
Like

Posted 24 January 2013 - 07:25 AM

Sorry I can't give you a straight answer, but I can ask some questions that can help someone get to the answer.  When you say that D3DXLoadMeshFromX doesn't succeed, do you mean it returns a failure value but does not affect the program (eg no crash?).  If so what does it return?  Is there a method to poll the last known error?

 

When crashing getting the pointer to the material buffer, is this a silent crash or is it throwing an exception?  Have you tried stepping through the code in your debugger and evaluating the pointer / variable values leading up to the erroneous sections?  Can you notice or announce what values of materialBuffer, numMaterials and meshTextures are just after D3DXLoadMeshFromX and prior to GetBufferPointer are?

 

It's a bit more of standard debugging practices than anything particular to DirectX but it will start arming you with more information that helps lead to an understanding of the issue.  Adding in answers to these questions on the thread might help someone else to have an answer for you.


Digivance Game Studios Founder:

Dan Mayor - Dan@Digivance.com
 www.Digivance.com


#3 george7378   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 24 January 2013 - 11:42 AM

Thanks for the reply, it's OK.

 

At the moment, D3DXLoadFromMeshX is returning D3DERR_INVALIDCALL, which means that my MeshObjectLoad() function returns false and the program halts/closes. The function succeeds though if I exchange 'renderdev' for 'd3ddev' which is my globally declared direct3d device.

 

For the buffer pointer thing I just get a '<program>.exe has stopped working...' message. I'm not sure if it's throwing an exception - would I use debug mode to check for this?

 

meshMaterials and meshTextures are both NULL after D3DXLoadFromMeshX, and I know that the others aren't NULL. I'm doing a bit more debugging now to see what I can get to.



#4 gdunbar   Crossbones+   -  Reputation: 901

Like
0Likes
Like

Posted 24 January 2013 - 01:19 PM

At the moment, D3DXLoadFromMeshX is returning D3DERR_INVALIDCALL, which means that my MeshObjectLoad() function returns false and the program halts/closes. The function succeeds though if I exchange 'renderdev' for 'd3ddev' which is my globally declared direct3d device.

 

Presumably you only have one DIRECT3DDEVICE9, correct? So the contents of renderdev and d3ddev should be the same. It is therefore suspicious that the function succeeds with one, but fails with the other. Look at the pointer values in the debugger. Are they the same? If not, why not?

 

If, on the other hand, you do have multiple DIRECT3DDEVICE9 objects, that probably isn't a good idea. Try using the same one everywhere. I would still pass it to your MeshObject constructor to avoid relying on global pointers, but don't create the device more than once.

 

Good luck!

Geoff



#5 george7378   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 24 January 2013 - 01:38 PM

Yeah, I've only got the one, and it's my global one. The way I define it is like this:

DIRECT3DDEVICE9 d3ddev;
//Class here, see code above
MeshObject meshTorus("Torus.x", d3ddev);

So basically I just say that renderdev=d3ddev when I create my meshTorus object. It seems a bit strange to me that the function D3DXLoadFromMeshX() doesn't take a pointer - that would make more sense...

Edited by george7378, 24 January 2013 - 01:39 PM.


#6 george7378   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 24 January 2013 - 01:47 PM

I think I might have cracked it - the function MeshObjectLoad() only works when I create my MeshObject AFTER the CreateDevice() has been called. I changed the MeshObjectLoad() to this:

bool MeshObjectLoad(LPDIRECT3DDEVICE9 dev){ renderdev = dev;//rest of function

 

Here's a random screenshot of the program working - both the torus and skybox were loaded using this class

 

DirectX Use Skybox 2013-01-24 23-32-00-77.jpg


Edited by george7378, 24 January 2013 - 05:36 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS