• Advertisement
Sign in to follow this  

DirectX - Manipulating vertex buffer

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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 initialized

VOID* 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 this post


Link to post
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 position
pVertices[10].x += 5;

g_pVB->Unlock();


Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement