Jump to content
  • Advertisement
Sign in to follow this  
Dual Op Amp

DirectX mesh unhandled exception

This topic is 3412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I am trying to load a mesh with DirectX. Unfortunately, I'm getting an unhandled exception.
[source land = "cpp"]
bool Mesh::load(char* fileName)
	{
		HRESULT result;    // Used for error checking.

		// Load mesh from the specified file.
		result = D3DXLoadMeshFromX(L"airplane 2.x", D3DXMESH_SYSTEMMEM, engine->getDevice(), NULL, &this->materialsBuffer, NULL, 
									&this->materialCount, &this->mesh);

		if(result != D3D_OK)
		{
			return false;
		}

		// Extract the material properties and textures from material buffer.
		this->tempMaterials = (LPD3DXMATERIAL)this->materialsBuffer->GetBufferPointer();
		this->materials = new D3DMATERIAL9[this->materialCount];
		this->textures = new LPDIRECT3DTEXTURE9[this->materialCount];

		// Create the materials and textures.
		for(int i = 0; i < this->materialCount; i++)
		{
			this->materials = this->tempMaterials.MatD3D;    // Grab the material.
			
			// Set ambient color for material.
			this->materials.Ambient = this->materials.Diffuse;
			//this->materials.Emissive = this->
			this->materials.Power = 0.5f;
			//this->materials.Specular = materials.Diffuse;

			// Grab the texture, if there's one.
			this->textures = NULL;
			if(this->tempMaterials.pTextureFilename != NULL)
			{
				// Load texture file specified  in the .x file.
				result = D3DXCreateTextureFromFileA(engine->getDevice(), this->tempMaterials.pTextureFilename, &this->textures);
				
				if(result != D3D_OK)
				{
					return false;
				}
			}
		}
}

When I debug the program with a breakpoint in the D3DXLoadMeshFromX's error check, it hits. Meaning, that the D3DXLoadMeshFromX wasn't successful, but I don't know why. I also think that it has something to do with my initD3D function. Because, I often have problems with this.
[source lang = "cpp"]
int Engine::initD3D(int width, int height, int colorDepth, bool fullScreen)
	{

		// Initialize and check Direct3D.
		this->d3d = Direct3DCreate9(D3D_SDK_VERSION);
		if(this->d3d == NULL)
		{
			return 0;
		}

		// Get the color depth.
		this->d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3dDisplayMode);

		// Set configuration options for Direct3D.
		D3DPRESENT_PARAMETERS d3dpp;
		ZeroMemory(&d3dpp, sizeof(d3dpp));
		d3dpp.hDeviceWindow = getWindowHandle();
		d3dpp.Windowed = (!fullScreen);    // Windowed mode equals the opposite of fullScreen.
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.EnableAutoDepthStencil = true;
		d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
		d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
		d3dpp.BackBufferCount = 1;
		d3dpp.BackBufferFormat = d3dDisplayMode.Format;
		d3dpp.BackBufferWidth = width;
		d3dpp.BackBufferHeight = height;

		// Create Direct3D device.
		this->d3d->CreateDevice(D3DADAPTER_DEFAULT,
								D3DDEVTYPE_HAL,
								this->hWnd,
								D3DCREATE_HARDWARE_VERTEXPROCESSING,
								&d3dpp,
								&this->d3dDevice);
		if(this->d3dDevice == NULL)
		{ 
			this->message("D3D failed to initialise.", "Engine");
			return 0; 
		}

		// Clear the back buffer to black.
		this->clearScene(D3DCOLOR_XRGB(0, 0, 0));

		// Create a pointer to the back buffer.
		this->d3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &this->d3dBackBuffer);

		// Use ambient lighting and z-buffering.
		this->d3dDevice->SetRenderState(D3DRS_ZENABLE, true);
		this->d3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
		this->setAmbient(this->ambientColor);

		// Initialize renderer.
		HRESULT result = D3DXCreateSprite(this->d3dDevice, &this->d3dSpriteHandler);
		if(result != D3D_OK)
		{
			return 0;
		}

		// Call game initialization extern function.
		/**if(!game_init(this->getWindowHandle()))
		{
			return 0;
		}**/

		// Set the default material.
		this->setDefaultMaterial();
		return 1;
	}

Share this post


Link to post
Share on other sites
Advertisement
I'll bet $50 that the your not telling it to load the file where you think it is.

You just gave a file name, so I assume you think it is in the current working directory.

What do you have the current directory set to be in VS debug properties, if you are using VS?

What does a call to getcwd() return?

Is your file there?

At any rate, get in the habit of providing function calls with a full path instead of a relative path. It will save you lots of headaches. You can construct the full path to pass using getcwd if you wish.

Share this post


Link to post
Share on other sites
Well.... I put the whole path and I'm not getting an exception error now. Instead, I'm getting a blank screen with nothing on it. Also, it won't let me escape the program. I had to Cntr + Alt + Delete out. And that almost didn't work!!!

Share this post


Link to post
Share on other sites
Quote:
Original post by Dual Op Amp
Well.... I put the whole path and I'm not getting an exception error now. Instead, I'm getting a blank screen with nothing on it. Also, it won't let me escape the program. I had to Cntr + Alt + Delete out. And that almost didn't work!!!


That's usually the way it goes. Solve one bug and find several more.
Did you implement an exit key? It won't happen automagically.
Not rendering could be a plethora of things.

Share this post


Link to post
Share on other sites
Yes, I implemented an exit key. I've never had any problem with that.

Here's the draw method.


void Mesh::draw()
{
if(this->materialCount == 0)
{
mesh->DrawSubset(0);
}
else
{
// Draw each mesh subset.
for(int i = 0; i < this->materialCount; i++)
{
// Set the material and texture for this subset.
engine->getDevice()->SetMaterial(&this->materials);

if(this->textures)
{
if(this->textures->GetType() == D3DRTYPE_TEXTURE)
{
engine->getDevice()->SetTexture(0, this->textures);
}
}
// Draw the mesh subset.
this->mesh->DrawSubset(i);
}
}
}

Share this post


Link to post
Share on other sites
I'm having the unhandled exception again. I don't know what I did to get the blank screen earlier, but now I can't get that.


This is what I replaced as a result of your advice. I think I did it right.
L"C:\Users\Jeff\Documents\Visual Studio 2008\Projects\Cell Engine\Cell Engine\spaceship 2.x"

....

Share this post


Link to post
Share on other sites
Doubt the problem is in the draw method.

Try giving PIX a whirl and see what is actually making it into the vertex shader. I don't know if you can use it for a FFP or not. I haven't used dx9 in so very long.

I'd also check the data you made the mesh with, vertices in correct order and tex coords. Is it your .x file or did you get it from somewhere?

Share this post


Link to post
Share on other sites
Check if you have a null pointer somewhere using debug.

I cannot say what could be wrong, the only 2 things that come
to my mind are:

replace "airplane 2.x" with "airplane_2.x"

Use D3DXMESH_MANAGED instead for memory management.

Another thing, the HRESULT does have more states than D3D_OK,
check what result you have instead only check if its different
of D3D_OK.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dual Op Amp
I'm having the unhandled exception again. I don't know what I did to get the blank screen earlier, but now I can't get that.


This is what I replaced as a result of your advice. I think I did it right.
L"C:\Users\Jeff\Documents\Visual Studio 2008\Projects\Cell Engine\Cell Engine\spaceship 2.x"

....


You must use an escape character before every slash
L"C:\\Users\\Jeff\Documents\\Visual Studio 2008\\Projects\\Cell Engine\\Cell Engine\\spaceship 2.x"

Share this post


Link to post
Share on other sites
Okay, I replaced the string with the modified version, with the escape characters. I get a blue screen (Don't worry, that's my ambient) and it allows me to escape. But, no rendered mesh.

Also, I'm no longer getting the null pointers to the mesh, material, materialBuffer or textures like I was. That was the problem, because D3DXLoadMeshFromX wasn't working. But, debugging now tells me they're working.


But, still no mesh being drawn.

I used the manage code, no difference. I also tried putting the underscore
in the name. And that just brought back the exception error.

I'm not sure what you mean by HRESULT, how can I do that?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!