WebGLRenderingContext.bufferData()
方法在
WebGL API
initializes and creates the buffer object's data store.
// WebGL1: void gl.bufferData(target, size, usage); void gl.bufferData(target, ArrayBuffer? srcData, usage); void gl.bufferData(target, ArrayBufferView srcData, usage); // WebGL2: void gl.bufferData(target, ArrayBufferView srcData, usage, srcOffset, length);
target
GLenum
specifying the binding point (target). Possible values:
gl.ARRAY_BUFFER
: Buffer containing vertex attributes, such as vertex coordinates, texture coordinate data, or vertex color data.
gl.ELEMENT_ARRAY_BUFFER
: Buffer used for element indices.
gl.COPY_READ_BUFFER
: Buffer for copying from one buffer object to another.
gl.COPY_WRITE_BUFFER
: Buffer for copying from one buffer object to another.
gl.TRANSFORM_FEEDBACK_BUFFER
: Buffer for transform feedback operations.
gl.UNIFORM_BUFFER
: Buffer used for storing uniform blocks.
gl.PIXEL_PACK_BUFFER
: Buffer used for pixel transfer operations.
gl.PIXEL_UNPACK_BUFFER
: Buffer used for pixel transfer operations.
size
GLsizeiptr
setting the size in bytes of the buffer object's data store.
srcData
可选
ArrayBuffer
,
SharedArrayBuffer
or one of the
ArrayBufferView
typed array types that will be copied into the data store. If
null
, a data store is still created, but the content is uninitialized and undefined.
usage
GLenum
specifying the intended usage pattern of the data store for optimization purposes. Possible values:
gl.STATIC_DRAW
: The contents are intended to be specified once by the application, and used many times as the source for WebGL drawing and image specification commands.
gl.DYNAMIC_DRAW
: The contents are intended to be respecified repeatedly by the application, and used many times as the source for WebGL drawing and image specification commands.
gl.STREAM_DRAW
: The contents are intended to be specified once by the application, and used at most a few times as the source for WebGL drawing and image specification commands.
gl.STATIC_READ
: The contents are intended to be specified once by reading data from WebGL, and queried many times by the application.
gl.DYNAMIC_READ
: The contents are intended to be respecified repeatedly by reading data from WebGL, and queried many times by the application.
gl.STREAM_READ
: The contents are intended to be specified once by reading data from WebGL, and queried at most a few times by the application
gl.STATIC_COPY
: The contents are intended to be specified once by reading data from WebGL, and used many times as the source for WebGL drawing and image specification commands.
gl.DYNAMIC_COPY
: The contents are intended to be respecified repeatedly by reading data from WebGL, and used many times as the source for WebGL drawing and image specification commands.
gl.STREAM_COPY
: The contents are intended to be specified once by reading data from WebGL, and used at most a few times as the source for WebGL drawing and image specification commands.
srcOffset
GLuint
specifying the element index offset where to start reading the buffer.
length
可选
GLuint
defaulting to 0.
None.
gl.OUT_OF_MEMORY
error is thrown if the context is unable to create a data store with the given
size
.
gl.INVALID_VALUE
error is thrown if
size
为负。
gl.INVALID_ENUM
error is thrown if
target
or
usage
are not one of the allowed enums.
var canvas = document.getElementById('canvas');
var gl = canvas.getContext('webgl');
var buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, 1024, gl.STATIC_DRAW);
To check the current buffer usage and buffer size, use the
WebGLRenderingContext.getBufferParameter()
方法。
gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE); gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_USAGE);
To calculate size parameter for a typed array.
var dataArray = new Float32Array([1, 2, 3, 4]); var sizeInBytes = dataArray.length * dataArray.BYTES_PER_ELEMENT;
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebGL 1.0
The definition of 'bufferData' in that specification. |
推荐 | 初始定义。 |
|
OpenGL ES 2.0
The definition of 'glBufferData' in that specification. |
标准 | Man page of the OpenGL API. |
|
OpenGL ES 3.0
The definition of 'glBufferData' in that specification. |
标准 |
Man page of the (similar) OpenGL ES 3 API.
Adds new
target
buffers:
gl.COPY_READ_BUFFER
,
gl.COPY_WRITE_BUFFER
,
gl.TRANSFORM_FEEDBACK_BUFFER
,
gl.UNIFORM_BUFFER
,
gl.PIXEL_PACK_BUFFER
,
gl.PIXEL_UNPACK_BUFFER
Adds new
usage
hints:
gl.STATIC_READ
,
gl.DYNAMIC_READ
,
gl.STREAM_READ
,
gl.STATIC_COPY
,
gl.DYNAMIC_COPY
,
gl.STREAM_COPY
.
|
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
bufferData
|
Chrome 9 | Edge 12 | Firefox 4 | IE 11 | Opera 12 | Safari 5.1 | WebView Android Yes | Chrome Android 25 | Firefox Android Yes | Opera Android 12 | Safari iOS 8 | Samsung Internet Android 1.5 |
WebGL2
|
Chrome 56 | Edge 79 | Firefox 51 | IE No | Opera 43 | Safari No | WebView Android 58 | Chrome Android 58 | Firefox Android 51 | Opera Android 43 | Safari iOS No | Samsung Internet Android 7.0 |
完整支持
不支持
WebGLRenderingContext.createBuffer()
WebGLRenderingContext.bufferSubData()
WebGLFramebuffer
,
WebGLRenderbuffer
WebGLRenderingContext
activeTexture()
attachShader()
bindAttribLocation()
bindBuffer()
bindFramebuffer()
bindRenderbuffer()
bindTexture()
blendColor()
blendEquation()
blendEquationSeparate()
blendFunc()
blendFuncSeparate()
bufferData()
bufferSubData()
checkFramebufferStatus()
clear()
clearColor()
clearDepth()
clearStencil()
colorMask()
commit()
compileShader()
compressedTexImage[23]D()
compressedTexSubImage2D()
copyTexImage2D()
copyTexSubImage2D()
createBuffer()
createFramebuffer()
createProgram()
createRenderbuffer()
createShader()
createTexture()
cullFace()
deleteBuffer()
deleteFramebuffer()
deleteProgram()
deleteRenderbuffer()
deleteShader()
deleteTexture()
depthFunc()
depthMask()
depthRange()
detachShader()
disable()
disableVertexAttribArray()
drawArrays()
drawElements()
enable()
enableVertexAttribArray()
finish()
flush()
framebufferRenderbuffer()
framebufferTexture2D()
frontFace()
generateMipmap()
getActiveAttrib()
getActiveUniform()
getAttachedShaders()
getAttribLocation()
getBufferParameter()
getContextAttributes()
getError()
getExtension()
getFramebufferAttachmentParameter()
getParameter()
getProgramInfoLog()
getProgramParameter()
getRenderbufferParameter()
getShaderInfoLog()
getShaderParameter()
getShaderPrecisionFormat()
getShaderSource()
getSupportedExtensions()
getTexParameter()
getUniform()
getUniformLocation()
getVertexAttrib()
getVertexAttribOffset()
hint()
isBuffer()
isContextLost()
isEnabled()
isFramebuffer()
isProgram()
isRenderbuffer()
isShader()
isTexture()
lineWidth()
linkProgram()
pixelStorei()
polygonOffset()
readPixels()
renderbufferStorage()
sampleCoverage()
scissor()
shaderSource()
stencilFunc()
stencilFuncSeparate()
stencilMask()
stencilMaskSeparate()
stencilOp()
stencilOpSeparate()
texImage2D()
texParameter[fi]()
texSubImage2D()
uniform[1234][fi][v]()
uniformMatrix[234]fv()
useProgram()
validateProgram()
vertexAttrib[1234]f[v]()
vertexAttribPointer()
viewport()
ANGLE_instanced_arrays
EXT_blend_minmax
EXT_color_buffer_half_float
EXT_disjoint_timer_query
EXT_frag_depth
EXT_sRGB
EXT_shader_texture_lod
EXT_texture_filter_anisotropic
OES_element_index_uint
OES_standard_derivatives
OES_texture_float
OES_texture_float_linear
OES_texture_half_float
OES_texture_half_float_linear
OES_vertex_array_object
WEBGL_color_buffer_float
WEBGL_compressed_texture_atc
WEBGL_compressed_texture_etc1
WEBGL_compressed_texture_pvrtc
WEBGL_compressed_texture_s3tc
WEBGL_compressed_texture_s3tc_srgb
WEBGL_debug_renderer_info
WEBGL_debug_shaders
WEBGL_depth_texture
WEBGL_draw_buffers
WEBGL_lose_context
WebGL2RenderingContext
WebGLActiveInfo
WebGLBuffer
WebGLContextEvent
WebGLFramebuffer
WebGLObject
WebGLProgram
WebGLQuery
WebGLRenderbuffer
WebGLSampler
WebGLShader
WebGLShaderPrecisionFormat
WebGLSync
WebGLTexture
WebGLTransformFeedback
WebGLUniformLocation
WebGLVertexArrayObject