I've made a small 3D viewer application using DirectX 9.0. Now I would like to add some editing functionality. Suppose that I want to edit a large polygon. While the user edits the shape, vertices would be added, deleted and moved. Right now each polygon is stored as a vertex buffer. My question is, what is the best way to store and display the shape while the user edits it? If i destroy and re-create the vertex buffer each time a change happens, I believe it will be too resource intensive and suboptimal.
I though I should ask my questions in the main post as well so here they are :
I have three different tasks, two of which I don't know how to implement :
1) Editing a vertex : Easy, I will just update the old vertex with the new position in the VB.
2) Deleting a vertex : What happens here? How do I remove it without creating a new VB? Should I just add a blank VB?
3) Adding a vertex : What about here? Can I change the length of a dynamic VB and add vertices at the end?
Another thought here that I believe would work :
1) Editing is easy
2) Deleting would just mean that I will overwrite the deleted vertex with possibly the position of the previous or the next vertex, so it will not be visible.
3) Addition will create a new vertex buffer, but will resize like a Vector or a List. Each time it's re-created it's size would be something like
NewSize = OldSize * 1.1 (adding 10% more)
so that successive additions do not have to re-create the VB.
So 1 & 2 never create a new VB and 3 might sometimes create one. How does this sound?
You don't have to destroy the buffer every frame. Create a dynamic buffer.
Check out the article Using Vertex Buffers With DirectX, if you haven't already:
Remember though, the data still has to be sent to the pipeline for every update. So there will definitely be some performance cost compared to static buffers with consistent data.
Also you should really try keeping buffer updates and also buffer switches to a minimum. Are there many such editable polygons in your application? If the answer is yes, maybe consider putting them into one buffer.
The official Q/A site for graphics/game developers: https://gamedev.stackexchange.com/
Update
Sounds pretty good to me.
This is taken from the article and is pretty much the answer for 1. and 2.. Instead of updating single vertices or carefully selecting which of them should be overwritten I would just update the whole buffer content. The complete buffer has to be sent to the device anyways. You would have to test it though, just to be sure.
Concerning 3.: You cannot change the buffer's size after it has been created, but you can create a larger buffer than actually needed. So try to estimate a good margin. If the buffer is still too small you will have to create a new one. There is no other solution to this. You have already found a possible algorythm for increasing the buffer size dynamically.
There are so many parameters when it comes to graphics performance, it is nearly impossible to give definite answers. Are you already encountering limits? If not, do not bother too much. Be generous with your resources while you are still developing.