Dynamic vertex buffers?

Started by
3 comments, last by kauna 11 years, 8 months ago
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.
Advertisement
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!
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.
thanks, didnt know you could create the vertecis array on render frame, now porblem is fixed! thanks!
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!

This topic is closed to new replies.

Advertisement