I'm currently writing a 3D rail shooter with DirectX and everything's going fine apart from the class I've written to display .x files. It renders the meshes fine but won't render any textures. I've compared my code to several tutorials and can't see where I'm going wrong and I've been through it with breakpoints, as far as I can tell it's loading the texture but by the time it reaches the render method the output says the texture contains a bad pointer. The meshes I'm using work perfectly in the DirectX visualiser so I'm totally stuck with what I'm doing wrong, does anyone have any suggestions?
The constructor is:
SimpleXfile::SimpleXfile(LPCSTR fileName, LPDIRECT3DDEVICE9 device):
myDevice(device)
{
HRESULT hr = NULL;
LPD3DXBUFFER mtrlBuffer;
DWORD numMtrls = NULL;
// load the xfile
hr = D3DXLoadMeshFromX(fileName, D3DXMESH_SYSTEMMEM, myDevice, NULL, &mtrlBuffer, NULL, &numMtrls, &myMesh);
if (FAILED(hr))
{
printf("SimpleXfile - Failed to load mesh");
}
if (mtrlBuffer && numMtrls != 0)
{
D3DXMATERIAL * mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();
for (int i = 0; i < int(numMtrls); ++i)
{
// set the ambient value
mtrls.MatD3D.Ambient = mtrls.MatD3D.Diffuse;
// save the material
myMaterials.push_back(mtrls.MatD3D);
// if the material has an associated texture, load and save it
if (mtrls.pTextureFilename)
{
IDirect3DTexture9 * tex = NULL;
D3DXCreateTextureFromFile(myDevice, mtrls.pTextureFilename, &tex);
myTextures.push_back(tex);
}
else
{
myTextures.push_back(NULL);
}
}
}
mtrlBuffer->Release();
// create vertex normals if the xfile doesn't have any
if ( !(myMesh->GetFVF() & D3DFVF_NORMAL) )
{
// no normals so create a clone of the mesh and add them
ID3DXMesh * tempMesh = NULL;
myMesh->CloneMeshFVF(D3DXMESH_MANAGED, myMesh->GetFVF() | D3DFVF_NORMAL, myDevice, &tempMesh);
D3DXComputeNormals(tempMesh, 0);
myMesh->Release();
myMesh = tempMesh;
}
}
and the render method is:
void SimpleXfile::Render()
{
for (unsigned int i = 0; i < myMaterials.size(); ++i)
{
myDevice->SetMaterial(&myMaterials);
myDevice->SetTexture(0, myTextures);
myMesh->DrawSubset(i);
}
}
It'd be great if someone could help me out, this is holding up my project and I don't have a clue how to fix it, thanks