Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to modify OpenGL vertex buffer?

Tags:

opengl

I'm setting up a vertex buffer in OpenGL, like this:

int vboVertexHandle = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle); glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW); 

Later, if I want to add or remove vertices to "vertexData", what is the proper way to do this? Is it even possible? I'm assuming I can't just modify the array directly without re-sending it to the GPU.

If I modify the vertexData array, then call this again:

glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle); glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW); 

...will that overwrite the old buffer with my new data? Or do I also have to delete the old one? Is there a better way?

like image 872
Mossen Avatar asked Apr 04 '13 21:04

Mossen


People also ask

What is a vertex buffer in OpenGL?

A vertex buffer object (VBO) is an OpenGL feature that provides methods for uploading vertex data (position, normal vector, color, etc.) to the video device for non-immediate-mode rendering.

How do OpenGL buffers work?

Buffer Objects are OpenGL Objects that store an array of unformatted memory allocated by the OpenGL context (AKA the GPU). These can be used to store vertex data, pixel data retrieved from images or the framebuffer, and a variety of other things.

How do I remove OpenGL buffers?

glDeleteBuffers deletes n buffer objects named by the elements of the array buffers . After a buffer object is deleted, it has no contents, and its name is free for reuse (for example by glGenBuffers). If a buffer object that is currently bound is deleted, the binding reverts to 0 (the absence of any buffer object).

What is Gl_static_draw?

GL_STATIC_DRAW. The data store contents will be modified once and used many times as the source for GL drawing commands. GL_DYNAMIC_DRAW. The data store contents will be modified repeatedly and used many times as the source for GL drawing commands.


1 Answers

The size of any OpenGL buffer object is set when you call glBufferData. That is, OpenGL will allocate the amount of memory you specify in the second argument of glBufferData (which isn't listed in the OP). In fact, if you call, for example glBufferData( GL_ARRAY_BUFFER, bufferSize, NULL, GL_DYNAMIC_DRAW ); OpenGL will create a buffer of bufferSize bytes of uninitialized data.

You can load any amount of data (up to the size of the buffer) using glBufferSubData, glMapBuffer, or any of the other routines for passing data. The only way to resize the buffer is to call glBufferData with a new size for the same buffer id (the value returned from glGenBuffers).

That said, you can always use a subset of the data in the buffer (which would be akin to deleting vertices), and if you render using glDrawElements, you can randomly access elements in the buffer. Adding vertices to a buffer would require allocating a larger buffer, and then you'd need to reload all of the data in the buffer.

like image 58
radical7 Avatar answered Sep 20 '22 20:09

radical7