Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems loading .x file

Started by -pete- Aug 24, 2006 at 6:29 AM 2 replies 950+ views
Original Post
-pete-
-pete-
Hey all, I was wondering if you could help me with this, I am trying to load an .x file as a terrain, however whenever i try to load it i receive an "unhandled exception" error, I've based the code from directtutorial.com so it looks like it should work to me. The .x file is here if anyone wants to try loading it in their own program to see if its valid (I've tested it in the SDK viewer and it looks ok to me) anyway the model doesn't have any textures and it should look like this when its loaded... so thats what its supposed to look like, the code to load my file looks like:
[source lang = "cpp"]
void CRendering::LoadFromXFile(LPDIRECT3DDEVICE9 gD3dDevice)
{

HRESULT hr=D3DXLoadMeshFromX ("maze.X", D3DXMESH_SYSTEMMEM, gD3dDevice, NULL, &materialBuffer, NULL, &numMaterials, &maze);

D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer();
D3DMATERIAL9 *meshMaterials = new D3DMATERIAL9[numMaterials];

for (DWORD i=0; i<numMaterials; i++)
{
	// Copy the material
 meshMaterials = d3dxMaterials.MatD3D;

 // Set the ambient color for the material (D3DX does not do this)
 meshMaterials.Ambient = meshMaterials.Diffuse;
}

materialBuffer->Release ();
}

Thats it to load, I know its pretty much ripped from the tutorial but i have to hack this together very quickly. Anyway once its loaded this is the source to render it:
[source lang= "cpp"]

void CRendering::RenderXFile(LPDIRECT3DDEVICE9 gD3dDevice)
{
for (DWORD i=0; i<numMaterials; i++)
{
   // Set the material and texture for this subset
  gD3dDevice->SetMaterial(&meshMaterials);

    // Draw the mesh subset
  maze->DrawSubset( i );
}
}

And thats it... Am i missing something? If you want to look at the rest of the source, the most recent build is here feel try to test it.
webjeff
webjeff
pete,

I checked your code and here is the stuff I found:

Main function, I had to comment out the gTriangle, cause I see you were making changes to the cRendering class, not the cTriangle.
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ){...//gTriangle.initGeometry (gD3dDevice, gVertexBuffer, -10.0f, +1.0f, +5.0f, +3.0f, +7.0f, +3.0f);gCube.LoadFromXFile (gD3dDevice);...}VOID Render(){...	//gTriangle.drawGeometry();	gCube.RenderXFile (gD3dDevice);...}


Here is an important one. In LoadFromXFile, you are redefining meshMaterials so your class member would never be created, only your local copy (in the function).
void CRendering::LoadFromXFile(LPDIRECT3DDEVICE9 gD3dDevice){...//D3DMATERIAL9 *meshMaterials = new D3DMATERIAL9[numMaterials];  //REDEFINED!meshMaterials = new D3DMATERIAL9[numMaterials]; //correct...}


Lastly I had to modify your camera to sit in a position to see the model we are loading :)
void CCamera::setupCamera(LPDIRECT3DDEVICE9 gD3dDevice){	D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 5000.0f );	gD3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );	D3DXMATRIX matView;		D3DXVECTOR3 vEyePt(0.0f,10.0f,-2000.0f );	D3DXVECTOR3 vLookatPt(0.0f,0.0f,0.0f );	D3DXVECTOR3 vUpVec(0.0f,1.0f,0.0f );}	


I have the code if you want me to email you, but these are the only fixes I did. I think your unhandled exception was due to the fact you had init'd gCube but rendered gTriangle... I could be wrong. Try my changes and let me know. Or email me at jeff (at) guerrillagames.net and I can send you the code.

Jeff.
-pete-
-pete-
Hi Jeff,

Thanks for your reply, I've done the changes you said and the model is now loading, thanks for that!

I thought it wasn't displaying properly at first but if you change the -2000f to +2000f I can see the blue floor, just a couple more questions though, as I am thinking right now the model is standing vertically facing the camera, I was supposed to put it down into a first person view inside the maze (kind of like a mouse in a maze if you will) is there any way that i can rotate the model so that it is flat? Also is the scale right? Is the level going to be too small for my camera to move through?

Thanks again

Pete

[Edited by - -pete- on August 24, 2006 2:42:06 PM]
webjeff
webjeff
Pete,

I looked at your code again, I realized there was no depth buffer. So any perspective would look really weird, so add this:

HRESULT InitD3D( HWND hWnd ){...    //this sets up a depth buffer for you    d3dpp.EnableAutoDepthStencil = TRUE;    d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;...}


I would also leave the camera at -2000. Adjusting that to positive will invert the camera and could cause problems later on.

Since you want to view the other side of your model, in the camera class, add this:
void CCamera::setupCamera(LPDIRECT3DDEVICE9 gD3dDevice){...	D3DXMATRIX matWorld;	D3DXMatrixIdentity(&matWorld);	D3DXMatrixRotationY( &matWorld, D3DX_PI );	D3DXMatrixRotationX( &matWorld, -D3DX_PI/2.0f );	gD3dDevice->SetTransform(D3DTS_WORLD, &matWorld ); ...}


I'm guessing you will need to move the camera around like a FPS. This is going to get complicated very fast the way you have things setup. I would suggest creating smaller pieces that you can work on seperately. But this should give you a base app to play around with now that it properly shows the model.

Jeff.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.