Here is the important parts.
This is called after the D3D device is created.
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
HRESULT hr;
V( pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ) );
// Turn on ambient lighting
V( pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff ) );
pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC );
pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_ANISOTROPIC );
pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC );
V( loadXFile( L"temple.x" ) );
return S_OK;
}
This the called every frame to render onscreen.
void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
HRESULT hr;
// Clear the render target and the zbuffer
V( pd3dDevice->Clear( 0, NULL, ( D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER ), D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ) );
// Render the scene
if( SUCCEEDED( pd3dDevice->BeginScene( ) ) )
{
void SetupMatrices( );
// Meshes are divided into subsets, one for each material. Render them in
// a loop
for( DWORD i = 0; i < g_dwNumMaterials; i++ )
{
// Set the material and texture for this subset
pd3dDevice->SetMaterial( &g_pMeshMaterials[ i ] );
pd3dDevice->SetTexture( 0, g_pMeshTextures[ i ] );
// Draw the mesh subset
g_pMesh->DrawSubset( i );
}
V( pd3dDevice->EndScene( ) );
}
}
And this is the function for loading the XMesh.
HRESULT loadXFile( LPWSTR fileName )
{
LPD3DXBUFFER pD3DXMtrlBuffer;
if( FAILED( D3DXLoadMeshFromX( fileName, D3DXMESH_SYSTEMMEM, DXUTGetD3DDevice( ), NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, &g_pMesh ) ) )
{
MessageBox( NULL, L"File not found.", L"ERROR", ( MB_OK | MB_ICONEXCLAMATION ) );
return E_FAIL;
}
D3DXMATERIAL* d3dxMaterials = ( D3DXMATERIAL* ) pD3DXMtrlBuffer->GetBufferPointer( );
g_pMeshMaterials = new D3DMATERIAL9[ g_dwNumMaterials ];
if( g_pMeshMaterials == NULL ) return E_OUTOFMEMORY;
g_pMeshTextures = new LPDIRECT3DTEXTURE9[ g_dwNumMaterials ];
if( g_pMeshTextures == NULL ) return E_OUTOFMEMORY;
for( DWORD i = 0; i < g_dwNumMaterials; i++ )
{
// Copy the material
g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;
g_pMeshTextures[i] = NULL;
int size = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, d3dxMaterials[i].pTextureFilename, -1, NULL, 0 );
LPWSTR buffer = new WCHAR[ size ];
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, d3dxMaterials[i].pTextureFilename, -1, buffer, size );
if( d3dxMaterials[i].pTextureFilename != NULL && lstrlenW(buffer) > 0 )
{
// Create the texture
if( FAILED( D3DXCreateTextureFromFile( DXUTGetD3DDevice( ), buffer, &g_pMeshTextures[i] ) ) )
{
MessageBox( NULL, L"File not found.", L"ERROR", ( MB_OK | MB_ICONEXCLAMATION ) );
}
}
delete [ ] buffer;
}
// Done with the material buffer
pD3DXMtrlBuffer->Release();
return S_OK;
}
Yet, despite all of this, the mesh does not render on screen. I have tried different .X mesh files and even played around with the view matrix and near and far clipping planes.
Here is the complete file: demo.cpp.
Note that it compiles fine with no errors/warnings and does not crash what so ever.
Thank you for any help you can provide. I'm sure I'm just missing something stupid.












