Skip to main content
GameDev.net gamedev.net
🔒 Locked

LockVertexBuffer et al.

Started by Thomo Apr 28, 2006 at 5:53 AM 0 replies 450+ views
Original Post
Thomo
Thomo
Hi, I have a problem that is a bit tricky. I am loading in one (or more) dirext .x meshes at runtime. The meshes of each object are stored in an apporpriate mesh, then I use LockVertexBuffer to read in the vertex positions of each mesh into a double pointer of CUSTOMVERTEX. When I print the vertices to the screen I get spurious results. I'm sure I'm doing something wrong but not sure what it is. I was hoping someone might help me. The code for it is below:


// double pointer to CUSTOMVERTEX
CUSTOMVERTEX **pVertexMesh;

// directx meshes
LPD3DXMESH g_pMesh[MAX_MESHES]= {NULL};

//allocate memory for pointers
pVertexMesh = (CUSTOMVERTEX**) calloc(numMeshes * numVerts, sizeof(CUSTOMVERTEX*));

for (int mesh = 0; mesh <= iMeshCount-1; mesh++) {

// read in the vetices of each morphable mesh
g_pMesh[mesh]->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertexMesh[mesh]);

g_pMesh[mesh]->UnlockVertexBuffer();

}

sprintf(old, "sumX = %5.2f", pVertexMesh[0][2].position.x);

The first vertex (pVertexMesh[0][0].position.x) displays correctly but subsequent vertices (pVertexMesh[0][1,2,3....].position.x) do not. Thanks, Thomo.
Evil Steve
Evil Steve
LockVertexBuffer doesn't copy the data into the pointer you pass it, it just gives you a straight pointer to the data allocated by D3D. You'll have to memcpy() the data into your array yourself, like so:
// double pointer to CUSTOMVERTEXCUSTOMVERTEX **pVertexMesh;// directx meshesLPD3DXMESH g_pMesh[MAX_MESHES]= {NULL};//allocate memory for pointers// No point in using calloc, since you're going to overwrite the data anyway. Just use malloc.pVertexMesh = (CUSTOMVERTEX**)malloc(numMeshes * sizeof(CUSTOMVERTEX*));for(int i=0; i<numMeshes; ++i){   pVertexMesh = (CUSTOMVERTEX*)malloc(numVertices * sizeof(CUSTOMVERTEX));}for (int mesh=0; mesh<iMeshCount; ++mesh){   // read in the vetices of each morphable mesh   void* pLock;   g_pMesh[mesh]->LockVertexBuffer(D3DLOCK_READONLY, &pLock);   memcpy(pVertexMesh[mesh], pLock, numVertices*sizeof(CUSTOMVERTEX));   g_pMesh[mesh]->UnlockVertexBuffer();}sprintf(old, "sumX = %5.2f", pVertexMesh[0][2].position.x);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.