Modfiying a d3dxmesh vertexbuffer

Started by
4 comments, last by cozzie 10 years ago

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)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Maybe


D3DLOCK_READONLY

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

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

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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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

I am sorry for making mess.

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;
}

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement