How do I load 3D meshes from a .x file, and extract the material data from it? Also, how do I apply a texture to the loaded mesh?

This is code for DirectX 9:

void LoadModel(Model* model, LPCTSTR File){

D3DXCreateBuffer(NumBytes, &bufMaterial);
LPD3DXBUFFER bufMaterial;

D3DXLoadMeshFromX(File, D3DXMESH_SYSTEMMEM, d3ddev, NULL, &bufMaterial, NULL,
                  &model->numMaterials, &model->Mesh);
OutputDebugString("LOAD MESH \n");

D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMaterial->GetBufferPointer();
OutputDebugString("GET BUFFER\n");

model->Material = new D3DMATERIAL9[model->numMaterials];
model->Texture = new LPDIRECT3DTEXTURE9[model->numMaterials];

OutputDebugString("LOAD MESH \n");

for(DWORD index = 0; index < model->numMaterials; index++)
{
    model->Material[index] = tempMaterials[index].MatD3D;
    model->Material[index].Ambient = model->Material[index].Diffuse;

        // if there is a texture to load, load it
        if(FAILED(D3DXCreateTextureFromFileA(d3ddev,
                                     tempMaterials[index].pTextureFilename,
                                     &model->Texture[index])))
                model->Texture[index] = NULL;    // if there is no texture, set the texture to NULL
}   

return;}

Your code give these errors when I try to compile:

------ Build started: Project: DirectX9_Engine, Configuration: Debug Win32 ------
Compiling...
baseentity.cpp
object.cpp
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.h(70) : error C2065: 'Model' : undeclared identifier
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.h(70) : error C2065: 'model' : undeclared identifier
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.h(70) : error C2275: 'LPCTSTR' : illegal use of this type as an expression
        c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(375) : see declaration of 'LPCTSTR'
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.h(70) : error C2146: syntax error : missing ')' before identifier 'File'
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.h(70) : error C2182: 'LoadModel' : illegal use of type 'void'
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.h(70) : error C2078: too many initializers
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.h(70) : error C2275: 'LPCTSTR' : illegal use of this type as an expression
        c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(375) : see declaration of 'LPCTSTR'
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.h(70) : error C2059: syntax error : ')'
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.h(70) : error C2143: syntax error : missing ';' before '{'
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.h(70) : error C2447: '{' : missing function header (old-style formal list?)
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.cpp(23) : error C2511: 'Object::Object(LPSTR,D3DXVECTOR3,D3DXVECTOR3)' : overloaded member function not found in 'Object'
        c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.h(8) : see declaration of 'Object'
c:\documents and settings\tom\my documents\visual studio 2008\projects\directx9_engine\directx9_engine\object.cpp(68) : fatal error C1004: unexpected end-of-file found
Generating Code...
Build log was saved at "file://c:\Documents and Settings\tom\My Documents\Visual Studio 2008\Projects\DirectX9_Engine\DirectX9_Engine\Debug\BuildLog.htm"
DirectX9_Engine - 12 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What is Model? I have never seen it defined or used in any DirectX code before.
Why do you declare bufMaterial after it is used?

All of those errors are coming from the fact that Model isn't defined(on my computer anyway).

Edited 7 Years Ago by tomtetlaw: n/a

The code that I used was from directxtutorial.com and model refers to the object model that will hold all of the mesh information. I have not tested the code, but do see the problem with bufMaterial that you mentioned.

No one replied to the thread so I gave it a try to give him some help. Sorry if the solution did not work.

Edited 7 Years Ago by zortec: n/a

I have been doing that tutorial, the free part anyway haha.
Model probably is a class that you get to write in the 3D Meshes tutorial in that, which you have to pay for :(

I think they should make that class available to people free of charge. That is a lot to pay for just a small bit of source code.

Nah, if you look at the site, the paid part teaches you all this:

Direct3D Textures
Direct3D Meshes
Game Display
Game Input
Game Development
Advanced Effects
Particle Engines
Multiplayer

I am actually going to buy it when I get the money.

I know you get a lot for the price, but it's hard to justify having to pay any money for code. I use a lot of open source.

I agree, I think that most if not all software should be open-source, it would create a more bug-free world haha.

This article has been dead for over six months. Start a new discussion instead.