Jump to content
  • Advertisement
Sign in to follow this  
Prads

Help loading X files (Beginner)

This topic is 3022 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am a beginner in DirectX and I am using directx 9. I am trying to load x files, I know how to load them but this time it is little different. The number of x files to be loaded is decided at runtime. So, I created a mesh like this:


ID3DXMesh** objMesh;




And for loading materials and textures, I made a vector like this:


std::vector<D3DMATERIAL9> *objMtrls;
std::vector<IDirect3DTexture9*> *objTexture;




Now after deciding the number of mesh that needs to be loaded (this is decided in runtime), let's suppose for this case 2 meshes are to be loaded, I create mesh, material, and texture array like this:


objMesh = new ID3DXMesh* [2];
objMtrls = new std::vector<D3DMATERIAL9> [2];
objTex = new std::vector<IDirect3DTexture9*> [2];




Now, I have created a namespace called "d3dLoadGraphics" and in it a function called "loadXObjects" for loading x files. Here it is:


bool d3dLoadGraphics::loadXObjects(LPCSTR lpXFileName, ID3DXMesh **meshOut, IDirect3DDevice9 *d3dDev,
std::vector<D3DMATERIAL9> *mtrls,
std::vector<IDirect3DTexture9*> *textures) {
ID3DXBuffer* mtrlsBuff;
ID3DXBuffer* adajBuff;
DWORD numMtrls;

HRESULT retVal;

retVal = D3DXLoadMeshFromX(lpXFileName, D3DXMESH_MANAGED, d3dDev, &adajBuff, &mtrlsBuff,
NULL, &numMtrls, meshOut);

if (retVal != D3D_OK) {
MessageBox(NULL, lpXFileName, "LOADING MESH FAILED", MB_ICONERROR);
return false;
}

if (mtrlsBuff != 0 && numMtrls != 0) {
D3DXMATERIAL* xMtrls = (D3DXMATERIAL*) mtrlsBuff->GetBufferPointer();

for (unsigned int i = 0; i < numMtrls; i++) {
xMtrls.MatD3D.Ambient = xMtrls.MatD3D.Diffuse;
mtrls->push_back(xMtrls.MatD3D);
if (xMtrls.pTextureFilename != NULL) {
IDirect3DTexture9 *tex = 0;
retVal = D3DXCreateTextureFromFile(d3dDev, xMtrls.pTextureFilename, &tex);
if (retVal != D3D_OK) {
MessageBox(NULL, xMtrls.pTextureFilename, "LOADING TEXTURE FAILED", MB_ICONERROR);
return false;
}
textures->push_back(tex);
} else
textures->push_back(0);
}
release<ID3DXBuffer*>(mtrlsBuff);
}

retVal = meshOut[0]->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE,
(DWORD*)adajBuff->GetBufferPointer(), NULL, NULL, NULL);

if (retVal != D3D_OK) {
MessageBox(NULL, lpXFileName, "OPTIMIZING MESH FAILED", MB_ICONERROR);
return false;
}

return true;
}




I think there is a bug in above function. Here is how I load a mesh file:


d3dLoadGraphics::loadXObjects("test1.x", &objMesh[0], d3dDevice, &objMtrls[0], &objTex[0]);
d3dLoadGraphics::loadXObjects("test2.x", &objMesh[1], d3dDevice, &objMtrls[1], &objTex[1]);




After loading it, here is how I render it:


for(unsigned int i = 0; i < objMtrls[0].size(); i++) {
if (objTex[0])
d3dDevice->SetMaterial(&WhiteMtrl);
else
d3dDevice->SetMaterial(&objMtrls[0]);
d3dDevice->SetTexture(0, objTex[0]);
objMesh[0]->DrawSubset(0);
}

D3DXMatrixTranslation(&W, 3.0f, 0, 0);
d3dDevice->SetTransform(D3DTS_WORLD, &W);
for(unsigned int i = 0; i < objMtrls[1].size(); i++) {
if (objTex[1])
d3dDevice->SetMaterial(&WhiteMtrl);
else
d3dDevice->SetMaterial(&objMtrls[1]);
d3dDevice->SetTexture(0, objTex[1]);
objMesh[1]->DrawSubset(0);
}




Ok now the problem is, object get loaded but without texture. Can you check the function loadXObjects and tell me if I wrote it correctly or not? Also, this line:


retVal = meshOut[0]->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE,
(DWORD*)adajBuff->GetBufferPointer(), NULL, NULL, NULL);




Is this line correct? Please help me!!!

Share this post


Link to post
Share on other sites
Advertisement
Actually, "loadXObject" function loads the texture from file and the pushes it back in vector "textures". Then all I have to do is SetTexture in the device and then render the object. I haven't used any shaders and effect files. It's a straight forward process, I just don't know why it's not working. :(

Share this post


Link to post
Share on other sites
First try removing the optimization code (just in case it has something to do with it), and if that doesn't work, try using the debug runtime and see if you get any useful output.

Share this post


Link to post
Share on other sites
General comment: Rather than "if(retVal != D3D_OK)..", use the FAILED and SUCCEEDED macros.
Quote:
object get loaded but without texture

Does Debug Runtime give you any clues?

How do you know the textures aren't loaded? Have you set a breakpoint and verified that the texture pointer is NULL?

If you have doubts about the Optimize call, comment it out until other problems are solved. Work on one problem at a time.

EDIT: [wink] See Gage64's comments. Ninja'd.

Share this post


Link to post
Share on other sites
Thanks for the replies. Debug runtime doesn't show much, here's what it shows in output window:


Direct3D9: :====> ENTER: DLLMAIN(6368a170): Process Attach: 0000056c, tid=00000e1c
Direct3D9: :====> EXIT: DLLMAIN(6368a170): Process Attach: 0000056c
Direct3D9: (INFO) :Direct3D9 Debug Runtime selected.
Direct3D9: (INFO) :======================= Hal HWVP device selected

Direct3D9: (INFO) :HalDevice Driver Style b

Direct3D9: :DoneExclusiveMode
Direct3D9: (INFO) :Using FF to VS converter

Direct3D9: (INFO) :Using FF to PS converter

D3D9 Helper: Warning: Default value for D3DRS_POINTSIZE_MAX is 2.19902e+012f, not 1.31009e-316f. This is ok.
D3DX: (INFO) Using SSE2 optimizations
D3DX: Matrix should be 16-byte aligned for better performance
Direct3D9: :====> ENTER: DLLMAIN(6368a170): Process Detach 0000056c, tid=00000e1c
Direct3D9: (INFO) :MemFini!
Direct3D9: :====> EXIT: DLLMAIN(6368a170): Process Detach 0000056c




That's all.

I also tried commenting out the optimize code, but that didn't help.

About texture not being loaded, I set the breakpoint and found that texture is being loaded. However, one interesting thing I found was, I set the Render State to WIREFRAME and it seems like the subset where textures are being used are not being loaded. I can't see the wires of that subset. Only the subsets without texture are being drawn.

Share this post


Link to post
Share on other sites
HAHAHA, I feel so happy as well as so stupid at the same time. I fixed the problem, it was in the code. Had to change:


objMesh[0]->DrawSubset(0);




objMesh[0]->DrawSubset(i);



Can't believe I made such a stupid mistake. lol

But could someone verify that opitimize code is correct or not? Thanks!!!

Share this post


Link to post
Share on other sites
Your OptimizeInPlace call appears correct. If you want to test further to see what your call accomplishes, you can load the mesh, save a copy of the attribute buffer, optimize, and compare the new attribute buffer to the original to see if face attributes have been reordered.

You can also compare number of vertices before and after to see if any duplicate vertices have been eliminated (if there are any dups to begin with).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!