Help with loading meshes.

Started by
2 comments, last by hughiecoles 16 years, 2 months ago
For some reason, my code from beginning game programming by jonathan Harboaur doesn't load the textures. here is the modified code from his book. I've mad a class to be able to load meshes and position them. here it is:


int MODEL::LoadModel(char *filename)
{
 
    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
		&material_count, //number of materials
		&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();
    materials = new D3DMATERIAL9[material_count];
    textures  = new LPDIRECT3DTEXTURE9[material_count];

    //create the materials and textures
    for(DWORD i=0; i<material_count; i++)
    {
        //grab the material
       materials = d3dxMaterials.MatD3D;

        //set ambient color for material 
        materials.Ambient = materials.Diffuse;

       textures = NULL;
        if( d3dxMaterials.pTextureFilename != NULL && 
            lstrlen(d3dxMaterials.pTextureFilename) > 0 )
        {
            //load texture file specified in .x file
            result = D3DXCreateTextureFromFile(d3ddev, 
                d3dxMaterials.pTextureFilename, 
                &textures);

			if(FAILED(result))
			{
				return 0;
			}
        }
    }

    //done using material buffer
    matbuffer->Release();

    return 1;
}


it doesn't seem to load the textures of the model, what should i do?
Advertisement
the problem may be that you have to have the mesh in the same folder as the .exe, as it loads the filepath as "mesh.x" , and not an absolute path.

I guess there are ways around it, like creating a string with the beginning of the absolute path in it, then combining the strings while running through the loop, but for just learning, try throwing a copy of the mesh in the folder with the project file, as well as in the debug folder.
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Ok, that worked. but it doesn't seem like a good idea to have all the meshes in the same folder as the EXE as that would be too consufibg, any way that i can make it work by putting the models in a folder and loading them and have them show up with the texture on?
add the beginning of the absolute path to the beginning of the string when leading hte texture, or go into the .x file and change the filepath to what it needs to be, those are 2 quick solutions i can think of but i don't know how the "pros" do it, as i just started 3D stuff a month ago
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried

This topic is closed to new replies.

Advertisement