• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
george7378

Creating a class to load .x files

5 posts in this topic

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!

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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

 

[attachment=13356:DirectX Use Skybox 2013-01-24 23-32-00-77.jpg]

Edited by george7378
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0