Jump to content
  • Advertisement
Sign in to follow this  
The Nameless

Problem loading meshes...

This topic is 3411 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

hey.....i am making a game in which there will be some cubes...meshes.....i am importing the cubes in DX thru .x files...but whenever i import meshes with parameter ID3DXMesh* ...the program crashes...here is the code....
//--------------CODE-----------------------------------------
bool CGameCore::LoadWorld(LPCWSTR xFilename, ID3DXMesh* meshName){
    D3DXCreateBuffer(sizeof(int) * 4, &matBuffer);
    if(FAILED(D3DXLoadMeshFromXW(xFilename, D3DXMESH_MANAGED, pD3DObj->d3ddev,
                                 &adjBuffer, &matBuffer, NULL, &numMat, &meshName)))
        return FALSE;

    if(matBuffer != 0 && numMat != 0){
        D3DXMATERIAL* matInfo = (D3DXMATERIAL*)matBuffer->GetBufferPointer();
        for(DWORD i = 0; i < numMat; i++){
            matInfo.MatD3D.Ambient = matInfo.MatD3D.Diffuse;
            mat.push_back(matInfo.MatD3D);

            if(matInfo.pTextureFilename != 0){
                IDirect3DTexture9* texInfo = 0;
                if(FAILED(D3DXCreateTextureFromFileW(pD3DObj->d3ddev,(LPCWSTR)matInfo.pTextureFilename,
                                                     (LPDIRECT3DTEXTURE9*)&texInfo)))
                    return FALSE;
                tex.push_back(texInfo);
            }
            else tex.push_back(0);
        }
    }
    if(matBuffer){
        matBuffer->Release();
        matBuffer = NULL;
    }

    return TRUE;
}
//------------------------------CODE------------------------------------
void CGameCore::DrawMesh(float xx, float yy, float zz, ID3DXMesh* meshName){
    static float x = 0.0f;    //strafe
    static float y = 0.0f;    //fly
    static float z = 0.0f;    //walk

    if(KEY('W')) y += 0.5f;
    if(KEY('S')) y -= 0.5f;
    if(KEY('A')) x -= 0.5f;
    if(KEY('D')) x += 0.5f;

    D3DXMATRIX rotateMesh;
    D3DXMATRIX transMesh;

    D3DXMatrixRotationY(&rotateMesh, D3DXToRadian(-90));
    D3DXMatrixTranslation(&transMesh, x + xx, y + yy, z + zz);
    pD3DObj->d3ddev->SetTransform(D3DTS_WORLD, &(rotateMesh * transMesh));

    for(DWORD i = 0; i < mat.size(); i++){
        pD3DObj->d3ddev->SetMaterial(&mat);
        pD3DObj->d3ddev->SetTexture(0, tex);
        meshName->DrawSubset(i);
    }
}
//*************************************************************

Now when i call the these two function in the renderScene() --
//----------------------------CODE----------------------------
void CGameCore::RenderScene(){
    pD3DObj->ClearBackBuffer();
    pD3DObj->Begin();
    pD3DObj->RenderD3D9();
        LoadWorld(L"xHeli.x", mHeli);           //where mHeli & mWorld are the meshes for helicopter & cube
        DrawMesh(0, 0, 10);                     // -- which i want to load in my game
        LoadWorld(L"xWorld.x");
        DrawMesh(0, 0, 5,mWorld);
    pD3DObj->End();
}
//---------------------CODE----------------------------


the program crashes... I debugged it and found that the ID3DXMesh* meshName has NULL value...another thing is if i'm importing these two mesh without the ID3DXMesh* parameter in the LoadMesh() and DrawMesh() function...the mesh are loaded but the cube's (xWorld.x file) color, which has grey color, becomes red, which is actually helicopter's (xHeli.x) color.... so can anyone tell how solve this prob..... Thanx. [Edited by - The Nameless on April 17, 2009 5:33:35 PM]

Share this post


Link to post
Share on other sites
Advertisement
You have numerous problems.

1. There's no need to create a ID3DXBuffer for D3DXLoadMeshFromX, it will create one for you. All you're doing leaking memory, since you never release the odl buffer before getting a new one.

2. You're casting a CHAR string to a WCHAR string when you call D3DXCreateTextureFromFile. This is very bad. You can't convert a regular char string to a wide-character string just by casting, all this does is make the compiler stop telling you have an error and gives you a crash at runtime. You either have to convert the string using something like MultiByteToWideChar, or you need to call the Ansi version of D3DXCreateTextureFromFile.

3. You seem to be casting your "&texInfo" for no reason at all. You really need to be more careful with casting...it should only be done when absolutely necessary. It should also preferably be done with the C++ casting operators, and not C-style casts.

4. When D3DXCreateTextureFromFile fails you're adding the texture to your vector, and when it succeeds you're adding 0 instead.

5. You shouldn't be loading a mesh every single frame so you can draw it, this will completely ruin your performance. You'll also eventually run out of memory, since you leak memory every time you create a new mesh and you don't Release the old one.

6. Did you actually copy and paste this code? As it you've posted, it doesn't even compile (your first call to DrawMesh has 3 parameters not 4, and your second call to LoadWorld has 1 parameter instead of 2).

7. For posting code here, please use the "source" or "code" tags. See the FAQ.

Share this post


Link to post
Share on other sites
1) Fine....that means i have to give NULL instead of &matBuffer???

2)if i give D3DXCreateTextureFromFile(), the error says "Cannot convert parameter 2 from LPSTR to LPCWSTR.....


3) if i give D3DXCreateTextureFromFileA(), the error says "cannot convert parameter 3 from 'IDirect3DTexture9 *' to 'LPDIRECT3DTEXTURE9 *'

4)i mean
if(there is any texture) //!=0
add texture info
else
add 0
5)so i need to release the mesh at the end of the LoadWorld()???

6) well the code is not copy-pasted :| sorry for that parameters list it will look like this

void CGameCore::RenderScene(){
pD3DObj->ClearBackBuffer();
pD3DObj->Begin();
pD3DObj->RenderD3D9();
LoadWorld(L"xHeli.x", mHeli);
DrawMesh(0, 0, 10, mHeli);
LoadWorld(L"xWorld.x", mWorld);
DrawMesh(0, 0, 5, mWorld);
pD3DObj->End();
}



ps: actually i am still learning all this DX programming...so it will take time to develop all the logics regarding memory leaks

Share this post


Link to post
Share on other sites
the problem is solved and i got rid of the memory leak....now the program takes nearly 10 mb ram when i import 3 meshes :)

btw thanks a lot MJP for all ur help and criticism :)

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!