I have a terrain mesh stored in a VBO. The mesh is a grid composed of right triangles. In other words, it looks like a rectilinear grid with diagonals. The width and height of the mesh are known, so it's easy to calculate the vertex indices for a given XY or vice-versa.
The terrain mesh will be editable. My question concerns rebuffering the vertex data when the terrain is edited. I will be able to determine the rectangular region of vertices that are dirtied by any edit operation, so obviously I'd prefer to rebuffer only those and leave the rest alone.
The first thing that comes to mind is glBufferSubData
. But I can't come up with a way to lay out my VBO such that glBufferSubData
would only affect the dirty vertices. For example, suppose my mesh is 5 x 5 vertices. (It would actually be much larger; this is just an example.) Like this:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
(Each number in the diagram above represents the vertex's offset from the start of the VBO.)
Suppose the 3 x 3 region in the center needs to be rebuffered. That means I want to hit vertices 6, 7, 8, 11, 12, 13, 16, 17, and 18. So I could call glBufferSubData
starting at index 6 and ending at 18:
0 1 2 3 4
5 *6 *7 *8 *9
*10 *11 *12 *13 *14
*15 *16 *17 *18 19
20 21 22 23 24
(In the diagram above, the vertices marked with *
are rebuffered.)
Notice that vertices 10, 14, and 15 are not dirty, and yet they get rebuffered, because they're in the range given to glBufferSubData
. This strikes me as inefficient. For a large mesh, I'd be rebuffering way more data than I need to in most cases.
Is there a well-known solution to this problem? Should I call glBufferSubData
once per row (which would solve the present problem, but would come with its own overhead)? Or is it standard just to buffer the full range and eat the cost of the unnecessary writing?
Also, terrain editing happens sometimes but not often. When it does, it will be animated, so the dirty vertices will have to be updated repeatedly while the animation is occurring. I'm thinking GL_DYNAMIC_DRAW
would be good. Does this sound right?
You shall use buffer object mapping. You can access to buffer object as a memory array, indeed accessing sparse vertices. The driver (hopefully) will optimize it for you.
The use of GL_DYNAMIC_DRAW
is correct.
来源:https://stackoverflow.com/questions/8735758/opengl-2-1-rebuffering-sub-region-in-vbo