void LoadXFileStatic(LPCSTR filename)
{
LPD3DXBUFFER materialBuffer;
DWORD numMaterials; // Note: DWORD is a typedef for unsigned long
LPD3DXMESH mesh;
// Load the mesh from the specified file
HRESULT hr=D3DXLoadMeshFromX(filename, D3DXMESH_SYSTEMMEM,
d3ddev, NULL,
&materialBuffer,NULL, &numMaterials,
&mesh );
if(S_OK != hr) // Check to determine whether method failed
{
MessageBox(NULL,
"Failed to load one of the Objects",
"Error!",
MB_ICONERROR | MB_OK);
}
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer();
static D3DMATERIAL9 *meshMaterials = new D3DMATERIAL9[numMaterials];
static LPDIRECT3DTEXTURE9 *meshTextures = new LPDIRECT3DTEXTURE9[numMaterials];
for (DWORD i=0; i < numMaterials; i++)
{
// Copy the material
meshMaterials[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
// Create the texture if it exists - it may not
meshTextures[i] = NULL;
if (d3dxMaterials[i].pTextureFilename)
{
D3DXCreateTextureFromFile(d3ddev, d3dxMaterials[i].pTextureFilename, &meshTextures[i]);
}
}
materialBuffer->Release();
for (DWORD i=0; i < numMaterials; i++)
{
// Set the material and texture for this subset
d3ddev->SetMaterial(&meshMaterials[i]);
d3ddev->SetTexture(0,meshTextures[i]);
// Draw the mesh subset
mesh->DrawSubset( i );
}
}
So that's the code for non-animated .x file loading.
Let's say it loads one object and renders after I set the world transform. Since everything goes in a loop, this causes great lag and soon lags me too much to exit the application. That I can understand.
I've tried:
Separating the loading part with an if statement, which successfully loads that one object once, and then proceeds to continuously rendering. What I did is put the meshMaterials, meshTextures and mesh variables outside of the if statement, so that they don't go out of scope. I also declared them static, because otherwise the program crashed for some reason. After I made those static, everything seemed to render, but the texture on the object I loaded was very dark and shaded in blue. That object is the one I made myself. If I used a skeleton (another model I did not make), it was textureless. If I did some more tweaking here and there, and added more than one object, the rest of the objects rendered but had the same textures as the first one. No amount of further tweaking fixed this.
I also tried adding multiple objects by making a dynamic array of those variables. I was probably getting ahead of myself.
I used to use XAnimator and I successfully automated everything. I could render every object and walk around with my character and easily enter/modify the coordinates. I scrapped everything when I've been working on the collision detection for month's and when I've finally gotten everything to work and rendered the bounds of the boxes on the screen. The problem was that for absolutely mysterious reasons, XAnimator decided that just one object, regardless which object I load in which order, has to be several float values longer when dealing with bounds.
--------------------------
So my problem right now:
I do not know how do I successfully separate rendering from loading, load only once without writing messy code.
I still have a suspicion that perhaps there is no way but to always load and redraw. If my suspicions are correct, I will probably make it load and draw only after one of the objects has changed a coordinate/rotated/scaled. I still fear that it will cause major lag and that it is the incorrect method of handling things.
With XAnimator, the loading and rendering were two different functions. I cannot seem to separate those two in the code above without getting some sort of errors or crashes.
Please help!
Theo