Sign in to follow this  
CarlML

DirectX - Manipulating vertex buffer

Recommended Posts

CarlML    215
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
FoxHunter2    157
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
CarlML    215
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
CarlML    215
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
FoxHunter2    157
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this