官方文档:https://docs.gl/es3/glBindBuffer

一、描述

bind a named buffer object.
绑定一个命名(ID)的缓冲区对象。

glBindBuffer binds a buffer object to the specified buffer binding point. Calling glBindBuffer with target set to one of the accepted symbolic constants and buffer set to the name of a buffer object binds that buffer object name to the target. If no buffer object with name buffer exists, one is created with that name. 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 (if supported for that target). Buffer object names and the corresponding buffer object contents are local to the shared object space of the current GL rendering context; two rendering contexts share buffer object names only if they explicitly enable sharing between contexts through the appropriate GL windows interfaces functions.

glGenBuffers may be used to generate a set of unused 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 a non-zero buffer object is bound to the GL_ARRAY_BUFFER target, the vertex array pointer parameter is interpreted as an offset within the buffer object measured in basic machine units.

When a non-zero buffer object is bound to the GL_DRAW_INDIRECT_BUFFER target, parameters for draws issued through glDrawArraysIndirect and glDrawElementsIndirect are sourced from the specified offset in that buffer object’s data store.

When a non-zero buffer object is bound to the GL_DISPATCH_INDIRECT_BUFFER target, the parameters for compute dispatches issued through glDispatchComputeIndirect are sourced from the specified offset in that buffer object’s data store.

While a non-zero buffer object is bound to the GL_ELEMENT_ARRAY_BUFFER target, the indices parameter of glDrawElements, glDrawElementsInstanced, glDrawRangeElements, 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: glReadPixels. The pointer parameter is 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: glCompressedTexImage2D, glCompressedTexImage3D, glCompressedTexSubImage2D, glCompressedTexSubImage3D, glTexImage2D, glTexImage3D, glTexSubImage2D, and glTexSubImage3D. The pointer parameter is interpreted as an offset within the buffer object measured in basic machine units.

The buffer targets GL_COPY_READ_BUFFER and GL_COPY_WRITE_BUFFER are provided to allow glCopyBufferSubData to be used without disturbing the state of other bindings. However, glCopyBufferSubData may be used with any pair of buffer binding points.

The GL_TRANSFORM_FEEDBACK_BUFFER buffer binding point may be passed to glBindBuffer, but will not directly affect transform feedback state. Instead, the indexed GL_TRANSFORM_FEEDBACK_BUFFER bindings must be used through a call to glBindBufferBase or glBindBufferRange. This will affect the generic GL_TRANSFORM_FEEDBACK_BUFFER binding.

Likewise, the GL_UNIFORM_BUFFER, GL_ATOMIC_COUNTER_BUFFER and GL_SHADER_STORAGE_BUFFER buffer binding points may be used, but do not directly affect uniform buffer, atomic counter buffer or shader storage buffer state, respectively. glBindBufferBase or glBindBufferRange must be used to bind a buffer to an indexed uniform buffer, atomic counter buffer or shader storage buffer binding point.

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允许您创建或使用命名缓冲区对象。 调用glBindBuffer,目标设置为GL_ARRAY_BUFFERGL_ELEMENT_ARRAY_BUFFER,缓冲区设置为新缓冲区对象的名称,这样就将缓冲区对象名称绑定到目标了。 当缓冲区对象绑定到目标时,该目标的先前绑定将自动中断。
缓冲区对象名称是无符号整数。 0值保留,但每个缓冲区对象目标没有默认缓冲区对象。 相反,缓冲区设置为0可以有效地取消绑定先前绑定的任何缓冲区对象,并恢复该缓冲区对象目标的客户机内存使用情况。 缓冲区对象名称和相应的缓冲区对象内容对于当前GL渲染上下文的共享对象空间是本地的。
您可以使用glGenBuffers生成一组新的缓冲区对象名称。
usageGL_STATIC_DRAW时第一次绑定后的缓冲区对象的状态是零大小的内存缓冲区。
绑定非零缓冲区对象名称时,绑定到的目标上的GL操作会影响绑定缓冲区对象,并且绑定到的目标的查询将从绑定缓冲区对象返回状态。 当绑定缓冲区对象名称零时,如在初始状态中,尝试修改或查询绑定到的目标上的状态会生成GL_INVALID_OPERATION错误。
当通过调用glVertexAttribPointer更改顶点数组指针状态时,当前缓冲区对象绑定(GL_ARRAY_BUFFER_BINDINGGL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING中的一个)被复制到要更改的顶点属性数组的相应客户端状态。当非零缓冲区对象绑定到GL_ARRAY_BUFFER目标时,传统上被解释为指向客户端内存的指针的顶点数组指针参数被解释为在基本机器单元中测量的缓冲区对象内的偏移量(glVertexAttribPointer原本的最后一个参数是顶点属性数组的指针,当绑定VBO后,最后一个参数就是指向所绑定的VBO的一个地址了)。
当非零缓冲区对象绑定到GL_ELEMENT_ARRAY_BUFFER目标时,glDrawElementsindices参数(传统上被解释为指向客户端内存的指针)被解释为在基本机器单元中测量的缓冲区对象内的偏移量。
使用glBindBuffer创建的缓冲区对象绑定保持活动状态,直到将不同的缓冲区对象名称绑定到同一目标,或者直到使用glDeleteBuffers删除绑定的缓冲区对象为止。
一旦创建,命名缓冲区对象可以根据需要经常重新绑定到任何目标。 但是,GL实现可以基于其初始绑定目标来选择如何优化缓冲区对象的存储。

GL_COPY_READ_BUFFER
GL_COPY_WRITE_BUFFER
GL_PIXEL_PACK_BUFFE
GL_PIXEL_UNPACK_READ_BUFFER
GL_TRANSFORM_FEEDBACK_BUFFER
GL_UNIFORM_BUFFER
targets are available only if the GL ES version is 3.0 or greater.

GL_ATOMIC_COUNTER_BUFFER
GL_DISPATCH_INDIRECT_BUFFER
GL_DRAW_INDIRECT_BUFFER
GL_SHADER_STORAGE_BUFFER
targets are available only if the GL ES version is 3.1 or greater.

二、C函数

  1. void glBindBuffer(
  2. GLenum target,
  3. GLuint buffer
  4. );

参数_target

指定缓冲区对象绑定的目标。
Specifies the target to which the buffer object is bound. 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
  • GL_UNIFORM_BUFFER

    参数_buffer

    Specifies the name of a buffer object.
    指定缓冲区对象的名称(ID)。

    示例

    Load an index buffer into OpenGL for later rendering. ```cpp // 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

  1. Load a vertex buffer into OpenGL for later rendering.
  2. ```cpp
  3. // data_size_in_bytes is the size in bytes of your vertex data.
  4. // data_vertices is your actual vertex data, probably a huge array of floats
  5. GLuint vertex_buffer; // Save this for later rendering
  6. glGenBuffers(1, &vertex_buffer);
  7. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  8. glBufferData(GL_ARRAY_BUFFER, data_size_in_bytes, 0, GL_STATIC_DRAW);
  9. glBufferSubData(GL_ARRAY_BUFFER, 0, data_size_in_bytes, data_vertices);
  10. GLint size = 0;
  11. glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
  12. if(data_size_in_bytes != size)
  13. {
  14. glDeleteBuffers(1, &vertex_buffer);
  15. // Log the error
  16. return;
  17. }
  18. // Success

Render an indexed buffer object using texture UV and normal vertex attributes.

  1. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); // vertex_buffer is retrieved from glGenBuffers
  2. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer); // index_buffer is retrieved from glGenBuffers
  3. glEnableVertexAttribArray(texcoord_attrib_index); // Attribute indexes were received from calls to glGetAttribLocation, or passed into glBindAttribLocation.
  4. glEnableVertexAttribArray(normal_attrib_index);
  5. glEnableVertexAttribArray(position_attrib_index);
  6. // vertex_stride is the size of bytes of each vertex in the buffer object
  7. // vertex_position_offset and kin are the offset in bytes of the position data
  8. // in each vertex. For example if your vertex structure is
  9. // [ position, texcoord, normal ] then position vertex_position_offset will
  10. // have offset 0, vertex_texcoord_offset is 12 (position is 3 * sizeof(float)
  11. // bytes large, and texcoord comes just after) and vertex_normal_offset is
  12. // 20 = 5 * sizeof(float).
  13. GLintptr vertex_texcoord_offset = 3 * sizeof(float);
  14. GLintptr vertex_normal_offset = 5 * sizeof(float);
  15. GLintptr vertex_position_offset = 0 * sizeof(float);
  16. glVertexAttribPointer(texcoord_attrib_index, 2, GL_FLOAT, false, vertex_stride, (GLvoid*)vertex_texcoord_offset);
  17. glVertexAttribPointer(normal_attrib_index, 3, GL_FLOAT, false, vertex_stride, (GLvoid*)vertex_normal_offset);
  18. glVertexAttribPointer(position_attrib_index, 3, GL_FLOAT, false, vertex_stride, (GLvoid*)vertex_position_offset);
  19. // num_vertices is the number of verts in your vertex_data.
  20. // index_data is an array of unsigned int offsets into vertex_data.
  21. glDrawElements(GL_TRIANGLES, num_vertices, GL_UNSIGNED_INT, NULL);
  22. glDisableVertexAttribArray(position_attrib_index);
  23. glDisableVertexAttribArray(texcoord_attrib_index);
  24. glDisableVertexAttribArray(normal_attrib_index);

错误

GL_INVALID_ENUM

generated if target is not one of the allowable values.