Sign in to follow this  
Kurt-olsson

Dynamic vertex buffers?

Recommended Posts

Kurt-olsson    254
Hi!

i have a vertexbuffer with verticies that i am making a trianglelist of.
i create my vertexbuffer array in init() and then draw my triangles on render_frame() with draw_primitives.

it looks like this in init()

[source lang="java"] CUSTOMVERTEX vertices[300] =
{
{ -10, 0, 29, D3DCOLOR_XRGB(0, 0, 255), },
{ -1, 10, 29, D3DCOLOR_XRGB(0, 255, 0), },
{ -1, 0, 29, D3DCOLOR_XRGB(255, 0, 0), },

{ -10, 0, 29, D3DCOLOR_XRGB(0, 0, 255), },
{ -10, 10, 29, D3DCOLOR_XRGB(0, 255, 0), },
{ -1, 10, 29, D3DCOLOR_XRGB(255, 0, 0), },

{ -1, 10, 29, D3DCOLOR_XRGB(0, 0, 255), },
{ -1, 10, -40, D3DCOLOR_XRGB(0, 255, 0), },
{ -1, 0, 29, D3DCOLOR_XRGB(255, 0, 0), },

{ -1, 0, 29, D3DCOLOR_XRGB(0, 0, 255), },
{ -1, 10, -40, D3DCOLOR_XRGB(0, 255, 0), },
{ -1, 0, -40, D3DCOLOR_XRGB(255, 0, 0), },

{ -3,4,-50, D3DCOLOR_XRGB(0, 0, 255), },
{ -18,3,-50, D3DCOLOR_XRGB(0, 255, 0), },
{ -4,22,-50, D3DCOLOR_XRGB(255, 0, 0), }
};

triangle t1 = triangle(D3DXVECTOR3(-1, 0, 29),D3DXVECTOR3(-1, 10, -40),D3DXVECTOR3(-1, 0, -40));
triangle t2 = triangle(D3DXVECTOR3(-1, 10, 29),D3DXVECTOR3(-1, 10, -40),D3DXVECTOR3(-1, 0, 29));
triangle t3 = triangle(D3DXVECTOR3(-10, 0, 29),D3DXVECTOR3(-1, 10, 29),D3DXVECTOR3(-1, 0, 29));
triangle t4 = triangle(D3DXVECTOR3(-10, 0, 29),D3DXVECTOR3(-10, 10, 29),D3DXVECTOR3(-1, 10, 29));
triangle t5 = triangle(D3DXVECTOR3(-3,4,-50),D3DXVECTOR3(-18,3,-50),D3DXVECTOR3(-4,22,-50));

lstTrangles.push_back(t1);
lstTrangles.push_back(t2);
lstTrangles.push_back(t3);
lstTrangles.push_back(t4);
lstTrangles.push_back(t5);[/source]

But i want to alter these values on the fly!
i also want to be able to loop and create my vertexbuffer from my std::vector triangleList.

there must be a way so that i can for example move my triangle left or right in realtime!?

how do i do this?

iam using small triangles as particles for my guns etc in my game. so i need to be able to change the positions all the time.

Share this post


Link to post
Share on other sites
kauna    2922
Well, create a dynamic vertex buffer and fill it every frame with map/unmap or lock/unlock depending on the D3D version used.

In any case, do not destroy and recreate your buffer every frame. Make the one used buffer big enough and if it isn't enough, you may split your draw calls.

Cheers!

Share this post


Link to post
Share on other sites
CryZe    773
Also, you shouldn't transform every vertex every frame on the CPU. Instead use a vertex shader. The GPU is optimized to transform your vertices. Simply upload a transformation matrix to the graphics card and let a vertex shader deal with the transformation.
If you actually want to transform each vertex in a different way, updating your vertex buffer every frame is the way to go.

Share this post


Link to post
Share on other sites
kauna    2922
As stated earlier, you should make use of the world matrix (or equivalent) to transform your mesh to different position. Especially, if the vertices stay at the same position in relation to the other vertices, changing the world matrix is the way to go.

Dynamic vertex buffers are usable for meshes which change their form somehow, particle systems, transferring instance data etc.

Even a bit more complex deformations such as skinned meshes are typically static in the sense that the vertex data doesn't change and they can be stored in a static vertex buffer.

Cheers!

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