DirectX - Manipulating vertex buffer

Started by
6 comments, last by Clapfoot 15 years, 8 months ago
Hey, I have a vertex buffer (LPDIRECT3DVERTEXBUFFER9) filled with info that I'm able to draw some geometry with. How do I manually change the position of the vertices in the vertex buffer between drawing frames? Is there a way to access and set the x,y,z components of a vertex in a buffer?
Advertisement
You can access the data by locking it, modifying the bits and unlocking it again.

Here's an example how to do this. Note that locking the VB/IB can take a serious performance hit, though.


regards
Hey FoxHunter2 thanks, I actually just found that section of the site myself.

I believe I can get that working but I'm not sure how I actually access the values in memory after that...

First I guess i would do this:

// LPDIRECT3DVERTEXBUFFER9 g_Vertices has been initializedVOID* pVertices;if(FAILED(g_pVB->Lock(0, sizeof(g_Vertices),(BYTE**)&pVertices, 0 ) ) )return E_FAIL;memcpy(pVertices, g_Vertices, sizeof(g_Vertices));g_pVB->Unlock();


but then...?

If I want to get to the position of vertex 10 do I write something like:

pVertices += 10*sizeof(OURCUSTOMVERTEX);


..and how do I access the memory at that point which should be 3 floats in a row for x,y,z?

My vertex structure looks like this:

struct OURCUSTOMVERTEX{	float x,y,z;	D3DVECTOR normal;	DWORD color;	float tu, tv;};
Like this:

OURCUSTOMVERTEX* pVertices;if(FAILED(g_pVB->Lock(0, sizeof(g_Vertices),(void**)&pVertices, 0 ) ) )return E_FAIL;// Modify the positionpVertices[10].x += 5;g_pVB->Unlock();

Quote:Original post by MJP
Like this:

*** Source Snippet Removed ***

Alright sweet, I got that working. Thanks.

Is that the most efficient way of moving vertices around or is there a better way, perhaps using a mesh?

Take a look at Chapter 5. Modeling and Chapter 19. D3DX Mesh Objects from my book "The Direct3D Graphcis Pipeline" for more information on modeling directly and using mesh objects.

My free book on Direct3D: "The Direct3D Graphics Pipeline"
My blog on programming, vintage computing, music, politics, etc.: Legalize Adulthood!

Quote:Original post by CarlML
Alright sweet, I got that working. Thanks.

Is that the most efficient way of moving vertices around or is there a better way, perhaps using a mesh?


This depends on what you're exactly trying to do. Depending on the goal, it might also be possible to do the calculations in the vertex shader, which would be more efficient.
Depending on what you are trying to do, doing a vertex texture fetch in your vertex shader might be a better option.

This topic is closed to new replies.

Advertisement