Clearing / updating ofBufferObject

I’ve got some ofBufferObjects that I’ve filled with a vector of glm::vec3s. At intervals, I want to upload new data into each. Sometimes there will be more data, sometimes there will be less data, but it’ll be a different amount each time I upload.

Right now I’m doing

buffer.allocate(dataVector, GL_STATIC_DRAW);
buffer.setData(dataVector, GL_STATIC_DRAW);

each time I want to refresh with new data, but it doesn’t seem to be refreshing with the new values. There’s also ofBufferObject::updateData, but that doesn’t seem to increase the size of the buffer if there was fewer items to start with.

Intuitively, what I would like to do is

  • clear the buffer
  • set the size of the buffer
  • upload the data into the buffer

What’s the correct way to approach this?

Thanks!

So (for future reference) what I’m now doing is the following-

  • allocate a buffer to the maximum feasible size that I might use, once when the program starts with ofBufferObject::allocate(emptyVectorOfMaximumSize, GL_DYNAMIC_DRAW)
  • each time I want to update, call ofBufferObject::updateData(0, dataToBeUsed) to upload the new data

This works, but I’m still curious about clearing data.

Hey!

ofBufferObject does not offer this functionality but you can use the OpenGL API instead; glClearBufferData is what you’re looking for. Below is an example where I specify a clear color but you can also use NULL which sets all buffer data to zero.

uint8_t clearColor[4] = { 255, 0, 0, 255 };
glClearNamedBufferData(buffer.getId(), GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, &clearColor);

Wiki for future reference: https://www.khronos.org/opengl/wiki/Buffer_Object#Clearing

Thought I’d said thanks but it seems I hadn’t, so thanks!

1 Like