glBufferData — creates and initializes a buffer object's data store
void glBufferData( | GLenum target, |
GLsizeiptr size, | |
const GLvoid * data, | |
GLenum usage) ; |
target
Specifies the target buffer object. The symbolic constant must be GL_ARRAY_BUFFER
, GL_ATOMIC_COUNTER_BUFFER
, GL_COPY_READ_BUFFER
, GL_COPY_WRITE_BUFFER
, GL_DRAW_INDIRECT_BUFFER
, GL_DISPATCH_INDIRECT_BUFFER
, GL_ELEMENT_ARRAY_BUFFER
, GL_PIXEL_PACK_BUFFER
, GL_PIXEL_UNPACK_BUFFER
, GL_SHADER_STORAGE_BUFFER
, GL_TRANSFORM_FEEDBACK_BUFFER
, or GL_UNIFORM_BUFFER
.
size
Specifies the size in bytes of the buffer object's new data store.
data
Specifies a pointer to data that will be copied into the data store for initialization, or NULL
if no data is to be copied.
usage
Specifies the expected usage pattern of the data store. The symbolic constant must be GL_STREAM_DRAW
, GL_STREAM_READ
, GL_STREAM_COPY
, GL_STATIC_DRAW
, GL_STATIC_READ
, GL_STATIC_COPY
, GL_DYNAMIC_DRAW
, GL_DYNAMIC_READ
, or GL_DYNAMIC_COPY
.
glBufferData
creates a new data store for the buffer object currently bound to target
. Any pre-existing data store is deleted. The new data store is created with the specified size
in bytes and usage
. If data
is not NULL
, the data store is initialized with data from this pointer. In its initial state, the new data store is not mapped, it has a NULL
mapped pointer, and its mapped access is GL_READ_WRITE
.
usage
is a hint to the GL implementation as to how a buffer object's data store will be accessed. This enables the GL implementation to make more intelligent decisions that may significantly impact buffer object performance. It does not, however, constrain the actual usage of the data store. usage
can be broken down into two parts: first, the frequency of access (modification and usage), and second, the nature of that access. The frequency of access may be one of these:
The data store contents will be modified once and used at most a few times.
The data store contents will be modified once and used many times.
The data store contents will be modified repeatedly and used many times.
The nature of access may be one of these:
The data store contents are modified by the application, and used as the source for GL drawing and image specification commands.
The data store contents are modified by reading data from the GL, and used to return that data when queried by the application.
The data store contents are modified by reading data from the GL, and used as the source for GL drawing and image specification commands.
If data
is NULL
, a data store of the specified size is still created, but its contents remain uninitialized and thus undefined.
Clients must align data elements consistently with the requirements of the client platform, with an additional base-level requirement that an offset within a buffer to a datum comprising bytes be a multiple of .
The GL_COPY_READ_BUFFER
, GL_COPY_WRITE_BUFFER
, GL_PIXEL_PACK_BUFFER
,GL_PIXEL_UNPACK_READ_BUFFER
, GL_TRANSFORM_FEEDBACK_BUFFER
and GL_UNIFORM_BUFFER
targets are available only if the GL ES version is 3.0 or greater.
The GL_ATOMIC_COUNTER_BUFFER
, GL_DISPATCH_INDIRECT_BUFFER
, GL_DRAW_INDIRECT_BUFFER
and GL_SHADER_STORAGE_BUFFER
targets are available only if the GL ES version is 3.1 or greater.
GL_INVALID_ENUM
is generated if target
is not one of the accepted buffer targets.
GL_INVALID_ENUM
is generated if usage
is not GL_STREAM_DRAW
, GL_STREAM_READ
, GL_STREAM_COPY
, GL_STATIC_DRAW
, GL_STATIC_READ
, GL_STATIC_COPY
, GL_DYNAMIC_DRAW
, GL_DYNAMIC_READ
, or GL_DYNAMIC_COPY
.
GL_INVALID_VALUE
is generated if size
is negative.
GL_INVALID_OPERATION
is generated if the reserved buffer object name 0 is bound to target
.
GL_OUT_OF_MEMORY
is generated if the GL is unable to create a data store with the specified size
.
glGetBufferParameter with argument GL_BUFFER_SIZE
or GL_BUFFER_USAGE
// data_size_in_bytes is the size in bytes of your vertex data. // data_vertices is your actual vertex data, probably a huge array of floats GLuint vertex_buffer; // Save this for later rendering glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBufferData(GL_ARRAY_BUFFER, data_size_in_bytes, 0, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, data_size_in_bytes, data_vertices); GLint size = 0; glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); if(data_size_in_bytes != size) { glDeleteBuffers(1, &vertex_buffer); // Log the error return; } // Success
// data_size_in_bytes is the size in bytes of your vertex data. // data_indices is an array of integer offsets into your vertex data. GLuint index_buffer; // Save this for later rendering glGenBuffers(1, &index_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, data_size_in_bytes, 0, GL_STATIC_DRAW); glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, data_size_in_bytes, data_indices); GLint size = 0; glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); if(data_size_in_bytes != size) { glDeleteBuffers(1, &index_buffer); // Log the error return; } // Success
Songho - OpenGL Pixel Buffer Object (PBO)
nehe.gamedev.net - iOS Lesson 02 - First Triangle
open.gl - Geometry Shaders
open.gl - The Graphics Pipeline
open.gl - Transform Feedback
opengl-tutorial.org - Particles / Instancing
opengl-tutorial.org - Tutorial 13 : Normal Mapping
opengl-tutorial.org - Tutorial 14 : Render To Texture
opengl-tutorial.org - Tutorial 2 : The first triangle
opengl-tutorial.org - Tutorial 4 : A Colored Cube
opengl-tutorial.org - Tutorial 7 : Model loading
opengl-tutorial.org - Tutorial 8 : Basic shading
OpenGL ES API Version | |||
---|---|---|---|
Function Name | 2.0 | 3.0 | 3.1 |
glBufferData | ✔ | ✔ | ✔ |
glBindBuffer, glBufferSubData, glMapBufferRange, glUnmapBuffer
Copyright © 2005 Addison-Wesley. Copyright © 2010-2014 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. https://opencontent.org/openpub/.