Buffer Object

From OpenGL.org
Revision as of 03:50, 15 August 2009 by Alfonse (Talk | contribs)

Jump to: navigation, search

Buffer Objects is the general term for unformatted linear memory allocated by the OpenGL context. These can be used to store vertex data, pixel data retrieved from images or the framebuffer, and a variety of other things.


Buffer objects were initially introduced in the ARB_vertex_buffer_object extension. They have been part of core OpenGL since version 1.5. This discussion will pertain specifically to the core version of buffer objects, though it is essentially identical to the extension version.

While buffer objects themselves are relatively old, some of the uses of them are much more recent.


Buffer Objects are OpenGL Objects; they therefore follow all of the rules of regular OpenGL objects. To create a buffer object, you call glGenBuffers. Deleting them uses glDeleteBuffers. These use the standard Gen/Delete paradigm as most OpenGL objects.

As with the standard OpenGL object paradighm, this only creates the object's name, the reference to the object. To actually create the object itself, you must bind it to the context. You do this using the following API:

void glBindBuffer(enum target, uint bufferName)

The target defines how you intend to use this binding of the buffer object. When you're just creating and/or filling the buffer object with data, the target you use doesn't matter much. It matters more when you intend to tell OpenGL to use the data in the buffer in some way.

To create the actual memory storage for a buffer object, you use this API:

void glBufferData(enum target, sizeiptr size, const void *data, enum usage)

The target parameter is just like the one for glBindBuffer; it says which bound buffer to modify. size represents how many bytes you want to allocate in this buffer object.

While it technically does not matter how you bind the buffer for creations vs. how you use it (create it with one target, unbind and bind it for use as another target) this is not recommended if you can at all help it. OpenGL implementations can, and will, make assumptions based on what target you used to create a buffer object, so don't try to confuse them.

The data parameter is a pointer to user memory that will be copied into the buffer object's data store. If this value is NULL, then no copying will occur, and the buffer object's data will be undefined.

The usage parameter can be very confusing.