# Modfiying a d3dxmesh vertexbuffer

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

## Recommended Posts

Hi,

I have the following situation:

1. A mesh is loaded into a D3DXMESH using D3DXLoadMeshFromX

2. A IDirect3DVertexBuffer9 is part of the mes class

3. The vertexbuffer is retrieved from the mesh to the vertexbuffer variable, by:

mMesh->GetVertexBuffer(&mVtxBuffer)

4. I access the vertex buffer to calculate boundingsphere, box etc.. Accessing the vertices is done by this code:

	TVERTEX *verticesPointer;
TVERTEX *vertices = new TVERTEX[pMesh.mMesh->GetNumVertices()];

pMesh.mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, D3DLOCK_READONLY);		// ,0 = default
memcpy(vertices, verticesPointer, pMesh.mMesh->GetNumVertices()*D3DXGetDeclVertexSize(vtxdecl, 0));
// VERTICES ARE NOT SCALED/ROTATED/TRANSLATED, AND ARE IN MODEL SPACE

// do stuff, read only with vertices

pMesh.mVtxBuffer->Unlock();
delete[] vertices;


That's basically my current situation.

Now my goal is to transform the vertices of the meshes to their child modelspace's, instead of parent.

The only thing I don't know how to do is, how can I change the data in the vertexbuffer?

This is what I've tried to test out:

	// TESTING VERTEX BUFFERS COPYING //
Crealysm_dxmath::TVERTEX *verticesPointer;
Crealysm_dxmath::TVERTEX *vertices = new Crealysm_dxmath::TVERTEX[mMesh->GetNumVertices()];

mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, 0);		// ,0 = default
memcpy(vertices, verticesPointer, mMesh->GetNumVertices()*D3DXGetDeclVertexSize(Crealysm_dxmath::vtxdecl, 0));
// VERTICES ARE NOT SCALED/ROTATED/TRANSLATED, AND ARE IN MODEL SPACE

for(DWORD vtx=0;vtx<mMesh->GetNumVertices();++vtx)
{
vertices[vtx].position.z = 5.0f;
}

mVtxBuffer->Unlock();



This test would make my world 'float',  by giving all meshes vertices Z = 5.0.

But the result is that nothing changes at all.

Do you have any idea how to solve this?

(is it possible at all to change the data, or do I need to make a new vertexbuffer and 'link' that/ use that for rendering, instead of the one retrieved from the d3dxmesh)

##### Share on other sites

Regarding this code:

TVERTEX *verticesPointer;
TVERTEX *vertices = new TVERTEX[pMesh.mMesh->GetNumVertices()];

pMesh.mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, D3DLOCK_READONLY); // ,0 = default
memcpy(vertices, verticesPointer, pMesh.mMesh->GetNumVertices()*D3DXGetDeclVertexSize(vtxdecl, 0));
// VERTICES ARE NOT SCALED/ROTATED/TRANSLATED, AND ARE IN MODEL SPACE

// do stuff, read only with vertices

pMesh.mVtxBuffer->Unlock();
delete[] vertices;


Just to check some things out:

1. Have you have ensured that your TVERTEX structure is identical to the pMesh vertex structure? If TVERTEX differs from the pMesh vertex structure, things could go really badly.

2. You can unlock the mesh vertex buffer immediately after the memcpy if desired.

In your second bit of code, nothing changes because all you do it copy the data out. You never put it back in the buffer.

The order should be:

0. Lock the buffer.

1. Read the data.

2. Modify the data.

3. Copy the modified data back into the buffer.

4. Unlock the buffer.

EDIT: 5. Release the buffer pointer.

Once again, your TVERTEX structure must be identical to the mesh's vertex structure!

EDIT: IIRC, you need to release the mesh vertexbuffer when you're done with it.

EDIT2: If your modification (setting all z's to some number) is only for testing, that's fine. Just be aware that you may not know whether you're successful or not. That will likely change the winding order of triangles and other "features" such as degenerate triangles, etc. You may just see a mess.

Edited by Buckeye

##### Share on other sites

Maybe

D3DLOCK_READONLY


is a problem. You lock the vertices only for reading.

And I think the system will ignore the result while unlocking.

##### Share on other sites

Maybe

D3DLOCK_READONLY


is a problem. You lock the vertices only for reading.

And I think the system will ignore the result while unlocking.

In his second bit of code, he doesn't specify D3DLOCK_READONLY, so that section of code is fine.

##### Share on other sites

Buckeye - you are right. Point 3. would be memcpy back to the Direct3D buffer.

I am sorry for making mess.

##### Share on other sites

Thanks all, I got it working now.

See the paste below.

Next step is do determine the spherecenter of the 'childs' and transform the vertices of the childs to that position. So I'm able to transform childs on their own (their vertices belong to their own origin). It is a bit slow though, about half a second loading time longer for a relatively simple scene.

Might have to consider to use a file format (saving / loading) where the renderables/ childs's vertices are already in their 'own space'.

bool CD3dmesh::UpdateVtxBufferRenderables()
{
// TESTING VERTEX BUFFERS COPYING //
Crealysm_dxmath::TVERTEX *verticesPointer;
Crealysm_dxmath::TVERTEX *vertices = new Crealysm_dxmath::TVERTEX[mMesh->GetNumVertices()];

mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, 0);		// ,0 = default
memcpy(vertices, verticesPointer, mMesh->GetNumVertices()*D3DXGetDeclVertexSize(Crealysm_dxmath::vtxdecl, 0));
// VERTICES ARE NOT SCALED/ROTATED/TRANSLATED, AND ARE IN MODEL SPACE

for(DWORD vtx=0;vtx<mMesh->GetNumVertices();++vtx)
{
vertices[vtx].position.x += 1.0f;
vertices[vtx].position.y += 1.0f;
vertices[vtx].position.z += 1.0f;
}
memcpy(verticesPointer, vertices, mMesh->GetNumVertices()*D3DXGetDeclVertexSize(Crealysm_dxmath::vtxdecl, 0));

mVtxBuffer->Unlock();

delete[] vertices;
return true;
}


Edited by cozzie