Sign in to follow this  

problem loading mesh with trancparent texture

Recommended Posts

I am using this sample code for now tell I figure out how it all works
ODEL *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,

if (result != D3D_OK)
MessageBox(NULL, "Could not find texture file", "Error", MB_OK);
return NULL;

//done using material buffer

return model;

VOID DeleteModel(MODEL *model)
//remove materials from memory
if( model->materials != NULL )
delete[] model->materials;

//remove textures from memory
if (model->textures != NULL)
for( DWORD i = 0; i < model->material_count; i++)
if (model->textures[i] != NULL)
delete[] model->textures;

//remove mesh from memory
if (model->mesh != NULL)

//remove model struct from memory
if (model != NULL)


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 );

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