Sign in to follow this  
Enerjak

Help with loading meshes.

Recommended Posts

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[i] = d3dxMaterials[i].MatD3D;

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

       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, 
                &textures[i]);

			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?

Share this post


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

Share this post


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

Share this post


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

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