createBuffer

View sourcecreateBuffer(gl: WebGLRenderingContext, type: number, size: number, bufferUsage: number): WebGLBuffer

Allocates a WebGL buffer for a target.

The helper calls WebGLRenderingContext#createBuffer, asserts that WebGL returned a buffer, binds it to the supplied BufferType with WebGLRenderingContext#bindBuffer, and allocates size bytes with WebGLRenderingContext#bufferData. The DrawUsage value is passed through as WebGL’s storage-usage hint. The newly created buffer remains bound to type when the function returns.

Examples

import { BufferType, DrawUsage, createBuffer } from '@wimaengine/render-webgl'

// `gl` is an existing WebGLRenderingContext.
const vertexBuffer = createBuffer(
  gl,
  BufferType.Array,
  positions.byteLength,
  DrawUsage.Static
)

Parameters

NameDescriptionDefault
gl

WebGL context that owns the buffer.

—
type

WebGL buffer target from BufferType.

—
size

Number of bytes to allocate with bufferData.

—
bufferUsage

WebGL usage hint from DrawUsage.

—

Throws

If gl.createBuffer() returns null or undefined.

Returns

The created and allocated WebGL buffer.