Sign in to follow this  
directXtreme

Problem loading .X files in DirectX.

Recommended Posts

MODEL *LoadModel(char *filename) { MODEL *model = (MODEL*)malloc(sizeof(MODEL)); LPD3DXBUFFER matbuffer; HRESULT result; //load mesh from the specified file result = D3DXLoadMeshFromX( filename, //filename D3DXMESH_SYSTEMMEM, //mesh options d3ddev, //Direct3D device NULL, //adjacency buffer &matbuffer, //material buffer NULL, //special effects &model->material_count, //number of materials &model->mesh); //resulting mesh if (result != D3D_OK) { MessageBox(NULL, "Error loading model file", "Error", MB_OK); return NULL; } //extract material properties and texture names from material buffer LPD3DXMATERIAL d3dxMaterials = (LPD3DXMATERIAL)matbuffer->GetBufferPointer(); model->materials = new D3DMATERIAL9[model->material_count]; model->textures = new LPDIRECT3DTEXTURE9[model->material_count]; //create the materials and textures for(DWORD i=0; i<model->material_count; i++) { //grab the material model->materials[i] = d3dxMaterials[i].MatD3D; //set ambient color for material model->materials[i].Ambient = model->materials[i].Diffuse; model->textures[i] = NULL; if( d3dxMaterials[i].pTextureFilename != NULL && lstrlen(d3dxMaterials[i].pTextureFilename) > 0 ) { //load texture file specified in .x file result = D3DXCreateTextureFromFile(d3ddev, d3dxMaterials[i].pTextureFilename, &model->textures[i]); if (result != D3D_OK) { MessageBox(NULL, "Could not find texture file", "Error", MB_OK); return NULL; } } } //done using material buffer matbuffer->Release(); return model; } This is the code to load a Model that I got from the book "Begining game programming" by Jonathoan S. Harbour. Now, for some reason when I export an X file from 3DS max and try to load it into the framework from the book it doesn't load the textures, and if I open the .X file and insert the texture name into the file then the program says "Could not find texture file". The modelk appears white as a ghost. with out any textures

Share this post


Link to post
Share on other sites
what are you using to export the meshes from 3dsmax? Panda?! Dont forget to check the Copy Textures(or something like) option. Export it as text and take a look at the file. open it in the notepad and search for the filename of your texture. Are you setting the textures before rendering?!

Share this post


Link to post
Share on other sites
void DrawModel(MODEL *model)
{
//draw each mesh subset
for( DWORD i=0; i<model->material_count; i++ )
{
// Set the material and texture for this subset
d3ddev->SetMaterial( &model->materials[i] );
d3ddev->SetTexture( 0, model->textures[i] );

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

this function renders the model but for some reason, it wont render the texture.

Share this post


Link to post
Share on other sites
void DrawModel(MODEL *model)
{
//draw each mesh subset
for( DWORD i=0; i<model->material_count; i++ )
{
// Set the material and texture for this subset
d3ddev->SetMaterial( &model->materials[i] );
d3ddev->SetTexture( 0, model->textures[i] );

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

this function renders the model but for some reason, it wont render the texture.

Share this post


Link to post
Share on other sites
void DrawModel(MODEL *model)
{
//draw each mesh subset
for( DWORD i=0; i<model->material_count; i++ )
{
// Set the material and texture for this subset
d3ddev->SetMaterial( &model->materials[i] );
d3ddev->SetTexture( 0, model->textures[i] );

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

this function renders the model but for some reason, it wont render the texture.

P.S. Sorry for the thriple post. thought didn't go through case it was being slow.

[Edited by - directXtreme on May 9, 2007 9:11:12 PM]

Share this post


Link to post
Share on other sites
May be you need to set a light I've neaver rendered any textures with out a light, may not be it, but when you said it was all white.

And also when you export it with pandatsoft set the Materials on the 3ds max objets tab and also on the Textures & fx files tab don't have Use Full PathNames selected and put all the textures in the folder of your current app.

this info was for PandaSoft 4.9.63.0 and 3DSMax 9

Share this post


Link to post
Share on other sites
Quote:
Original post by ankhd
May be you need to set a light I've neaver rendered any textures with out a light, may not be it, but when you said it was all white.

And also when you export it with pandatsoft set the Materials on the 3ds max objets tab and also on the Textures & fx files tab don't have Use Full PathNames selected and put all the textures in the folder of your current app.

this info was for PandaSoft 4.9.63.0 and 3DSMax 9


I did what U said: The Material checkbox is always checked, everything is default and the Left_Handed_Axis is checked. I don't know whether it's something in the LoadModel function or the DrawModel function. I use folders to put the models in then I do mode = LoadModel("foldername/model.x") when I export the model from 3DS max, it exports the textures to the directory that model is going to. So I don't know. this is driving me nuts.

Share this post


Link to post
Share on other sites
Hi man,

Try :

pD3D->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
pD3D->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);

tell me if it worked.

Share this post


Link to post
Share on other sites
Hey ya,
Did you check the result from the D3DXCreateTextureFromFile was it loaded
if so then did you copy the files to the same directory as your application, They are not in the xfile them selfs anyway. and are the images powers of 2
eg 256 X 256, 128, 256.

Share this post


Link to post
Share on other sites
Yes, the texture files are in the app directory in their own files. I tried to put both the model and textures into the same directory as the EXE (which is with all the other files, not in DEBUG) for some reason, it wont load the textures but if I export a model with out textures, only materials or no materials it renders it. I don't know what it is. Maybe it's the code to load the model that's messed up. here's how it's set up:

C:\Documents and Settings\Dinokiller\Desktop\Hall\sources\chapter14\Framework

C:\Documents and Settings\Dinokiller\Desktop\Hall\sources\chapter14\Framework\smallmodel
i do claw = LoadModel("smallmodel/claws3.x")

and it doesn't load the textures.

Share this post


Link to post
Share on other sites
Hi Again here is some code I used see if you can see whats wrong with yours this works if the codes the same then its where the images are check the string you use to load them with may be a message box

HRESULT hr = 0;

//
// Load the XFile data.
//

ID3DXBuffer* adjBuffer = 0;
ID3DXBuffer* mtrlBuffer = 0;
DWORD numMtrls = 0;

hr = D3DXLoadMeshFromX(
"bigship1.x",
D3DXMESH_MANAGED,
Device,
&adjBuffer,
&mtrlBuffer,
0,
&numMtrls,
&Mesh);

if(FAILED(hr))
{
::MessageBox(0, "D3DXLoadMeshFromX() - FAILED", 0, 0);
return false;
}

//
// Extract the materials, and load textures.
//

if( mtrlBuffer != 0 && numMtrls != 0 )
{
D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();

for(int i = 0; i < numMtrls; i++)
{
// the MatD3D property doesn't have an ambient value set
// when its loaded, so set it now:
mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;

// save the ith material
Mtrls.push_back( mtrls[i].MatD3D );

// check if the ith material has an associative texture
if( mtrls[i].pTextureFilename != 0 )
{
// yes, load the texture for the ith subset
IDirect3DTexture9* tex = 0;
D3DXCreateTextureFromFile(
Device,
mtrls[i].pTextureFilename,
&tex);

// save the loaded texture
Textures.push_back( tex );
}
else
{
// no texture for the ith subset
Textures.push_back( 0 );
}
}
}
d3d::Release<ID3DXBuffer*>(mtrlBuffer); // done w/ buffer

//
// Optimize the mesh.
//

hr = Mesh->OptimizeInplace(
D3DXMESHOPT_ATTRSORT |
D3DXMESHOPT_COMPACT |
D3DXMESHOPT_VERTEXCACHE,
(DWORD*)adjBuffer->GetBufferPointer(),
0, 0, 0);

d3d::Release<ID3DXBuffer*>(adjBuffer); // done w/ buffer

if(FAILED(hr))
{
::MessageBox(0, "OptimizeInplace() - FAILED", 0, 0);
return false;
}

//
// Set texture filters.
//

Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);

//
// Set Lights.
//

D3DXVECTOR3 dir(1.0f, -1.0f, 1.0f);
D3DXCOLOR col(1.0f, 1.0f, 1.0f, 1.0f);
D3DLIGHT9 light = d3d::InitDirectionalLight(&dir, &col);

Device->SetLight(0, &light);
Device->LightEnable(0, true);
Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
Device->SetRenderState(D3DRS_SPECULARENABLE, true);

//
// Set camera.
//

D3DXVECTOR3 pos(4.0f, 4.0f, -13.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);

D3DXMATRIX V;
D3DXMatrixLookAtLH(
&V,
&pos,
&target,
&up);

Device->SetTransform(D3DTS_VIEW, &V);

//
// Set projection matrix.
//

D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)Width / (float)Height,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);

return true;
}


Share this post


Link to post
Share on other sites
........Ok........Apperantly it works now. I need to do a bunch of other tests to see. I made a 10x10x10 box in 3DSmax then exported it into the framework folder. OK, tried to load it from the same folder with the EXE it worked. tryed to load it from "box/box11.x", it works. So, I must of did something wrong when exporting or something. I'll see about other tests.

Share this post


Link to post
Share on other sites
.......Ok, I loaded a cube with a simple texture and it worked. I tryied to load other stuff like a claw doing all the same things I did with the cube, yet it doesn't seem to load the claw. The box worked like a charm.

Share this post


Link to post
Share on other sites
hey yes I think I Know whats up now, you have a mesh that is made of many objects and materials I think you need to load the mesh file with the D3DXLoadMeshHierarchyFromX look it up and do it this way may be. here is some links I used to cerat a meshloader using the D3DXLoadMeshHierarchyFromX function.

http://www.toymaker.info/Games/html/load_x_hierarchy.html#HierarchyConcept

http://www.gamedev.net/reference/articles/article1835.asp

http://www.gamedev.net/reference/programming/features/xfilepc/

http://www.toymaker.info/html/links.html

Share this post


Link to post
Share on other sites
Ok, after some tests, I've found that the code can't seem to load textures with numbers in it. I've tried to load a Model with a texture with numbers in it, then tried to load a model with a texture with no numbers in it. and wouldn't you know it? It loaded the one with out the numbers...........Strange....

Share this post


Link to post
Share on other sites
Hey, what do you mean with numbers in it, make sure the image is a power of 2 then no odd suface sizes, EG, 256 X 256, 128 X 68 all good, bad ones 123 X 123

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