I have a simple mesh that I created in Blender, its just a maze that I will be trying to make into a small game...
But when I follow examples of how to load a mesh, they work fine, except for the mesh facing backwards!
If I set the Y axis rotation on the model when I render it, I can see that the mesh is indeed simply backwards, but I have no Idea why!
Here is the code I use to load the mesh:
bool Maze::LoadXFile()
{
HRESULT hr = 0;
//
// Load the XFile data.
//
//Adjacency Buffer
ID3DXBuffer* adjBuffer = 0;
//Materials Buffer
ID3DXBuffer* mtrlBuffer = 0;
//Number of Materials
DWORD numMtrls = 0;
hr = D3DXLoadMeshFromX(
TEXT("maze1.x"),
D3DXMESH_MANAGED,
this->mp_d3dDevice,
&adjBuffer,
&mtrlBuffer,
0,
&numMtrls,
&this->m_mesh);
if(FAILED(hr))
{
//MessageBox(0, TEXT("D3DXLoadMeshFromX() - FAILED"), 0, 0);
MessageBox(NULL, DXGetErrorDescription9( hr ), DXGetErrorString9( hr ), MB_ICONERROR);
return false;
}
//
// Extract the materials, and load textures.
//
if( mtrlBuffer != 0 && numMtrls != 0 )
{
D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();
for(int i = 0; i < numMtrls; i++)
{
// the MatD3D property doesn't have an ambient value set
// when its loaded, so set it now:
mtrls.MatD3D.Ambient = mtrls.MatD3D.Diffuse;
// save the ith material
m_mtrls.push_back( mtrls.MatD3D );
// check if the ith material has an associative texture
if( mtrls.pTextureFilename != 0 )
{
// load the texture for the ith subset
IDirect3DTexture9* tex = 0;
D3DXCreateTextureFromFile(
this->mp_d3dDevice,
//mtrls.pTextureFilename,
TEXT(""),
&tex);
// save the loaded texture
m_textures.push_back( tex );
}
else
{
// no texture for the ith subset
m_textures.push_back( 0 );
}
}
}
mtrlBuffer->Release(); // done w/ buffer
//
// Optimize the mesh.
//
hr = this->m_mesh->OptimizeInplace(
D3DXMESHOPT_ATTRSORT |
D3DXMESHOPT_COMPACT |
D3DXMESHOPT_VERTEXCACHE,
(DWORD*)adjBuffer->GetBufferPointer(),
0, 0, 0);
adjBuffer->Release(); // done w/ buffer
if(FAILED(hr))
{
//MessageBox(0, "OptimizeInplace() - FAILED", 0, 0);
MessageBox(NULL, DXGetErrorDescription9( hr ), DXGetErrorString9( hr ), MB_ICONERROR);
return false;
}
//
// Set texture filters.
//
mp_d3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
mp_d3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
mp_d3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
//
// Set Lights.
//
D3DXVECTOR3 dir(1.0f, -1.0f, 1.0f);
D3DXCOLOR col(1.0f, 1.0f, 1.0f, 1.0f);
D3DLIGHT9 light = InitDirectionalLight(&dir, &col);
mp_d3dDevice->SetLight(0, &light);
mp_d3dDevice->LightEnable(0, true);
mp_d3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
mp_d3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);
return true;
}
And here is the Render method of the Maze object:
void Maze::Render(float timeDelta)
{
if (this->mp_d3dDevice)
{
//
// Update: Rotate the mesh.
//
static float y = 0.0f;
D3DXMATRIX yRot;
D3DXMatrixRotationY(&yRot, y);
y += timeDelta;
if( y >= 6.28f )
y = 0.0f;
D3DXMATRIX World = yRot;
mp_d3dDevice->SetTransform(D3DTS_WORLD, &World);
D3DXVECTOR3 cameraPos;
this->m_camera->getPosition(&cameraPos);
D3DXVECTOR3 pos( cameraPos.x,
cameraPos.y,
cameraPos.z );
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(
&V,
&pos,
&target,
&up);
mp_d3dDevice->SetTransform(D3DTS_VIEW, &V);
//
// Set projection matrix.
//
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)648 / (float)480,
1.0f,
1000.0f);
mp_d3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
for(int i = 0; i < this->m_mtrls.size(); i++)
{
this->mp_d3dDevice->SetMaterial( &this->m_mtrls );
this->mp_d3dDevice->SetTexture(0, this->m_textures );
this->m_mesh->DrawSubset(i);
}
}
}
Any help is greatly appreciated!
Thanks
Mark