Well, I started to work on a .x loader but there is a slight problem. I can´t get the mesh to render. I dunno is it´s just the camera or something wrong in the loading code but I would appreciate if someone could check this code out and maybe inform me on what´s wrong.
First the function that loads the model
bool cXEngine::GetXFile(void)
{
LPD3DXBUFFER lpD3DMatBuf; //Material and texture buffer
if(FAILED(D3DXLoadMeshFromX("tiger.x", D3DXMESH_SYSTEMMEM,
m_iD3DDev9, NULL, &lpD3DMatBuf, NULL, &m_dwNumMaterials, &m_iMesh)))
{
return false;
}
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)lpD3DMatBuf->GetBufferPointer();
m_matMaterials = new D3DMATERIAL9[m_dwNumMaterials];
m_matTextures = new LPDIRECT3DTEXTURE9[m_dwNumMaterials];
for(DWORD i=0;i<m_dwNumMaterials;i++)
{
m_matMaterials[i] = d3dxMaterials[i].MatD3D;
m_matMaterials[i].Ambient = m_matMaterials[i].Diffuse;
m_matTextures[i] = NULL;
// Create the texture.
if( FAILED( D3DXCreateTextureFromFile( m_iD3DDev9,
d3dxMaterials[i].pTextureFilename, &m_matTextures[i] ) ) )
{
return false;
}
m_matTextures[i] = NULL;
}
lpD3DMatBuf->Release();
return true;
}
And the code that displays it
void cXEngine::Render(void)
{
m_iD3DDev9->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255),1.0f,0);
SetupMatrices(); //Setup world,view,projection matrices
m_iD3DDev9->BeginScene();
//Render the mesh
for(DWORD i=1; i<m_dwNumMaterials;i++)
{
m_iD3DDev9->SetMaterial( &m_matMaterials[i] );
m_iD3DDev9->SetTexture( 0, m_matTextures[i] );
m_iMesh->DrawSubset( i );
}
m_iD3DDev9->EndScene();
m_iD3DDev9->Present(NULL,NULL,NULL,NULL); //Present the scene, Src=bb, dest=frontb,dest win=hWnd, dirty=NULL
}
Oh and the camera if that´s necessary
void cXEngine::SetupMatrices(void)
{
D3DXVECTOR3 vCamPos(0.0f, 3.0f,-5.0f);
D3DXVECTOR3 vCamLookAt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vCamUpDown(0.0f, 1.0f, 0.0f);
D3DXMATRIX matWorld; //World matrix
m_iD3DDev9->SetTransform(D3DTS_WORLD, &matWorld);
D3DXMATRIX matView; //View(camera) matrix
D3DXMatrixLookAtLH(&matView, &vCamPos, &vCamLookAt, &vCamUpDown);
m_iD3DDev9->SetTransform(D3DTS_VIEW, &matView);
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f,1.0f,100.0f);
m_iD3DDev9->SetTransform(D3DTS_PROJECTION, &matProj);
}
Bad Monkey Productions