question related to buffer object..

Hi.
In rendering an animation, I’ll use a buffer object for better rendering performance.
There is 2 ways to make it.

  1. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * numVertices
    * 3, VertexArray, GL_STREAM_DRAW);
  2. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * numVertices
    * 3, NULL, GL_STREAM_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) *
    numVertices * 3, VertexArray);
    How is 1 different from 2?
    It is said that 2 shows better rendering performance than 1. why is so?

#1 allocates the buffer “and” provides contents for it (since the data pointer is not NULL).

#2 allocates the buffer in the first statement (but doesn’t provide contents as the pointer is NULL), and then provides the contents for it in the separate, second statement.

The advantage to the second approach for a buffer where you need to update it again and again is that you only call the glBufferData call to allocate the buffer once, then to update the buffer use glSubBufferData. This allows you to not consume the overhead of needlessly reallocating the buffer every time you need to upload new contents for it. You just provide the new contents for the “existing” buffer. If you repeatedly call glBufferData, you are repeatedly reallocating the buffer, which is a waste.

Also, there are other ways besides these two to upload data to a buffer object, such as glMapBuffer and glMapBufferRange. And these may net you even higher upload performance.

Read this post for more details, paying particular note to Rob Barris’s posts: VBOs strangely slow?

Also check out this post.

Patrick