# DirectX - Manipulating vertex buffer

## Recommended Posts

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?

##### Share on other sites
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

##### Share on other sites
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;};

##### Share on other sites
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();

##### Share on other sites
Quote:
 Original post by MJPLike 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?

##### Share on other sites
Quote:
 Original post by CarlMLAlright 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.

##### Share on other sites
Depending on what you are trying to do, doing a vertex texture fetch in your vertex shader might be a better option.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627651
• Total Posts
2978407

• 10
• 12
• 22
• 13
• 33