Jump to content
  • Advertisement
Sign in to follow this  
gamesky

Exporting mesh vertex group to text.

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

Advertisement

The following code provides you with vertices and face indices from a D3DXMesh. You will have to write the code that saves it to file yourself, but that should be easy.

LPD3DXMESH m_pD3DMesh;

void MeshXWrapper::GetMeshData(int *pMeshVertexCount, dVector3 **ppMeshVertices,
                               int *pMeshIndexCount, int **ppMeshIndices)
{
    LPDIRECT3DVERTEXBUFFER9 pVB = NULL;
    m_pD3DMesh->GetVertexBuffer(&pVB);

    struct MY_FVF {
        float x;
        float y;
        float z;
    };

    void *pbVertexData;
    if (SUCCEEDED(pVB->Lock(0, 0, &pbVertexData, D3DLOCK_READONLY))) {
        DWORD numBytesPerVertex = m_pD3DMesh->GetNumBytesPerVertex();

        int numVertices = m_pD3DMesh->GetNumVertices();
        *ppMeshVertices = new dVector3[numVertices];
        unsigned char *pVBDataPos = (unsigned char*)pbVertexData;
        for (int i=0; i < numVertices; i++) {
            (*ppMeshVertices)[i][0] = ((MY_FVF*)pVBDataPos)->x;
            (*ppMeshVertices)[i][1] = ((MY_FVF*)pVBDataPos)->y;
            (*ppMeshVertices)[i][2] = ((MY_FVF*)pVBDataPos)->z;
            (*ppMeshVertices)[i][3] = 0;
            pVBDataPos += numBytesPerVertex;
        }
        pVB->Unlock();
        *pMeshVertexCount = m_pD3DMesh->GetNumVertices();
    }


    // Index buffer

    LPDIRECT3DINDEXBUFFER9 pIB;
    m_pD3DMesh->GetIndexBuffer(&pIB);
    D3DINDEXBUFFER_DESC indexDesc;
    pIB->GetDesc(&indexDesc);

    LPVOID pbIndexData;
    if (SUCCEEDED(m_pD3DMesh->LockIndexBuffer(D3DLOCK_READONLY, &pbIndexData))) {
        *pMeshIndexCount = indexDesc.Size / sizeof(unsigned short);
        *ppMeshIndices = new int[*pMeshIndexCount];
        for (int i=0; i<(*pMeshIndexCount)/3; i++) {
            unsigned short i0 = ((unsigned short*)pbIndexData)[i*3+0];
            unsigned short i1 = ((unsigned short*)pbIndexData)[i*3+1];
            unsigned short i2 = ((unsigned short*)pbIndexData)[i*3+2];

            (*ppMeshIndices)[i*3+0] = i0;
            (*ppMeshIndices)[i*3+1] = i1;
            (*ppMeshIndices)[i*3+2] = i2;
        }
        m_pD3DMesh->UnlockIndexBuffer();
    }

    pIB->Release();
    pVB->Release();
}

Share this post


Link to post
Share on other sites

Kjell Andersson

thanks , i want to access to material group then save them to text files. i think your code just save total faces.

for exemple , there is a model with 3 group mesh inside it , evry mesh has his own material , i want to access to all groups and save the separatly to some files , so any group with his material in one file.

Edited by gamesky

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!