Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Modfiying a d3dxmesh vertexbuffer


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 cozzie   Members   -  Reputation: 2763

Like
0Likes
Like

Posted 06 April 2014 - 12:19 PM

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)



Sponsor:

#2 Buckeye   GDNet+   -  Reputation: 9106

Like
0Likes
Like

Posted 06 April 2014 - 03:48 PM

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, 06 April 2014 - 05:20 PM.

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.


#3 kubera   Members   -  Reputation: 1104

Like
0Likes
Like

Posted 07 April 2014 - 01:56 AM

Maybe

D3DLOCK_READONLY

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

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



#4 Buckeye   GDNet+   -  Reputation: 9106

Like
1Likes
Like

Posted 07 April 2014 - 09:02 AM

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.


#5 kubera   Members   -  Reputation: 1104

Like
0Likes
Like

Posted 07 April 2014 - 11:40 AM

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

I am sorry for making mess.



#6 cozzie   Members   -  Reputation: 2763

Like
0Likes
Like

Posted 07 April 2014 - 02:28 PM

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, 07 April 2014 - 02:29 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS