glBindBuffer — bind a named buffer object
void glBindBuffer( | GLenum | target, |
GLuint | buffer) ; |
target
Specifies the target to which the buffer object is bound. The symbolic constant must be GL_ARRAY_BUFFER
, GL_ELEMENT_ARRAY_BUFFER
, GL_PIXEL_PACK_BUFFER
, or GL_PIXEL_UNPACK_BUFFER
.
buffer
Specifies the name of a buffer object.
glBindBuffer
lets you create or use a named buffer object. Calling glBindBuffer
with target
set to GL_ARRAY_BUFFER
, GL_ELEMENT_ARRAY_BUFFER
, GL_PIXEL_PACK_BUFFER
or GL_PIXEL_UNPACK_BUFFER
and buffer
set to the name of the new buffer object binds the buffer object name to the target. When a buffer object is bound to a target, the previous binding for that target is automatically broken.
Buffer object names are unsigned integers. The value zero is reserved, but there is no default buffer object for each buffer object target. Instead, buffer
set to zero effectively unbinds any buffer object previously bound, and restores client memory usage for that buffer object target. Buffer object names and the corresponding buffer object contents are local to the shared display-list space (see glXCreateContext) of the current GL rendering context; two rendering contexts share buffer object names only if they also share display lists.
You may use glGenBuffers to generate a set of new buffer object names.
The state of a buffer object immediately after it is first bound is an unmapped zero-sized memory buffer with GL_READ_WRITE
access and GL_STATIC_DRAW
usage.
While a non-zero buffer object name is bound, GL operations on the target to which it is bound affect the bound buffer object, and queries of the target to which it is bound return state from the bound buffer object. While buffer object name zero is bound, as in the initial state, attempts to modify or query state on the target to which it is bound generates an GL_INVALID_OPERATION
error.
When vertex array pointer state is changed, for example by a call to glNormalPointer, the current buffer object binding (GL_ARRAY_BUFFER_BINDING
) is copied into the corresponding client state for the vertex array type being changed, for example GL_NORMAL_ARRAY_BUFFER_BINDING
. While a non-zero buffer object is bound to the GL_ARRAY_BUFFER
target, the vertex array pointer parameter that is traditionally interpreted as a pointer to client-side memory is instead interpreted as an offset within the buffer object measured in basic machine units.
While a non-zero buffer object is bound to the GL_ELEMENT_ARRAY_BUFFER
target, the indices parameter of glDrawElements, glDrawRangeElements, or glMultiDrawElements that is traditionally interpreted as a pointer to client-side memory is instead interpreted as an offset within the buffer object measured in basic machine units.
While a non-zero buffer object is bound to the GL_PIXEL_PACK_BUFFER
target, the following commands are affected: glGetCompressedTexImage, glGetConvolutionFilter, glGetHistogram, glGetMinmax, glGetPixelMap, glGetPolygonStipple, glGetSeparableFilter, glGetTexImage, and glReadPixels. The pointer parameter that is traditionally interpreted as a pointer to client-side memory where the pixels are to be packed is instead interpreted as an offset within the buffer object measured in basic machine units.
While a non-zero buffer object is bound to the GL_PIXEL_UNPACK_BUFFER
target, the following commands are affected: glBitmap, glColorSubTable, glColorTable, glCompressedTexImage1D, glCompressedTexImage2D, glCompressedTexImage3D, glCompressedTexSubImage1D, glCompressedTexSubImage2D, glCompressedTexSubImage3D, glConvolutionFilter1D, glConvolutionFilter2D, glDrawPixels, glPixelMap, glPolygonStipple, glSeparableFilter2D, glTexImage1D, glTexImage2D, glTexImage3D, glTexSubImage1D, glTexSubImage2D, and glTexSubImage3D. The pointer parameter that is traditionally interpreted as a pointer to client-side memory from which the pixels are to be unpacked is instead interpreted as an offset within the buffer object measured in basic machine units.
A buffer object binding created with glBindBuffer
remains active until a different buffer object name is bound to the same target, or until the bound buffer object is deleted with glDeleteBuffers.
Once created, a named buffer object may be re-bound to any target as often as needed. However, the GL implementation may make choices about how to optimize the storage of a buffer object based on its initial binding target.
glBindBuffer
is available only if the GL version is 1.5 or greater.
GL_PIXEL_PACK_BUFFER
and GL_PIXEL_UNPACK_BUFFER
are available only if the GL version is 2.1 or greater.
GL_INVALID_ENUM
is generated if target
is not one of the allowable values.
GL_INVALID_OPERATION
is generated if glBindBuffer
is executed between the execution of glBegin and the corresponding execution of glEnd.
glGet with argument GL_ARRAY_BUFFER_BINDING
glGet with argument GL_ELEMENT_ARRAY_BUFFER_BINDING
glGet with argument GL_PIXEL_PACK_BUFFER_BINDING
glGet with argument GL_PIXEL_UNPACK_BUFFER_BINDING
// 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
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); // vertex_buffer is retrieved from glGenBuffers glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer); // index_buffer is retrieved from glGenBuffers glEnableVertexAttribArray(texcoord_attrib_index); // Attribute indexes were received from calls to glGetAttribLocation, or passed into glBindAttribLocation. glEnableVertexAttribArray(normal_attrib_index); glEnableVertexAttribArray(position_attrib_index); // vertex_stride is the size of bytes of each vertex in the buffer object // vertex_position_offset and kin are the offset in bytes of the position data // in each vertex. For example if your vertex structure is // [ position, texcoord, normal ] then position vertex_position_offset will // have offset 0, vertex_texcoord_offset is 12 (position is 3 * sizeof(float) // bytes large, and texcoord comes just after) and vertex_normal_offset is // 20 = 5 * sizeof(float). GLintptr vertex_texcoord_offset = 3 * sizeof(float); GLintptr vertex_normal_offset = 5 * sizeof(float); GLintptr vertex_position_offset = 0 * sizeof(float); glVertexAttribPointer(texcoord_attrib_index, 2, GL_FLOAT, false, vertex_stride, (GLvoid*)vertex_texcoord_offset); glVertexAttribPointer(normal_attrib_index, 3, GL_FLOAT, false, vertex_stride, (GLvoid*)vertex_normal_offset); glVertexAttribPointer(position_attrib_index, 3, GL_FLOAT, false, vertex_stride, (GLvoid*)vertex_position_offset); // num_vertices is the number of verts in your vertex_data. // index_data is an array of unsigned int offsets into vertex_data. glDrawElements(GL_TRIANGLES, num_vertices, GL_UNSIGNED_INT, NULL); glDisableVertexAttribArray(position_attrib_index); glDisableVertexAttribArray(texcoord_attrib_index); glDisableVertexAttribArray(normal_attrib_index);
Songho - OpenGL Pixel Buffer Object (PBO)
Songho - OpenGL Vertex Buffer Object (VBO)
open.gl - The Graphics Pipeline
Copyright © 2005 Addison-Wesley. 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/.