sdasdadas
sdasdadas

Reputation: 25096

Should I be removing vertex buffer objects on every draw?

I have some Haskell / OpenGLRaw code that does something like this:

verticesPtr <- newArray ...
glVertexPointer 3 gl_DOUBLE 0 verticesPtr

buffersPtr <- malloc
glGenBuffers 1 buffersPtr
buffer <- peek buffersPtr

glBindBuffer gl_ARRAY_BUFFER buffer
glBufferData gl_ARRAY_BUFFER 4 verticesPtr gl_STREAM_DRAW

glDrawArrays gl_LINE_STRIP 0 4
glDeleteBuffers 1 buffersPtr

I have two questions concerning this code:

  1. I am calling this from within the draw callback. Does this entirely negate the usefulness of storing my vertex data in the server?
  2. If I should place this code outside draw, should I change the gl_STREAM_DRAW command to something more static?

Upvotes: 2

Views: 594

Answers (1)

bcrist
bcrist

Reputation: 1530

  1. Yes, you are throwing away most of the benefits of buffer objects by using them in this manner. In all likelihood, it will still be faster than glBegin/glEnd since the driver becomes aware of all of the data simultaneously, rather than incrementally, but there's no guarantee it will be faster, and may even be slower for small buffer sizes due to the overhead of creating and destroying the buffer object.

  2. Yes, as described in the glBufferData API docs you should avoid STREAM_DRAW unless the buffer is changing every or close to every frame. My personal rule of thumb is:

    • Changes only once in a while --> STATIC
    • Changes often, but less often than it stays the same --> DYNAMIC
    • Changes more often than it stays the same --> STREAM

Upvotes: 2

Related Questions