Spaces:
Sleeping
Sleeping
File size: 1,823 Bytes
9a14526 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | // @flow
import assert from 'assert';
import type {StructArray} from '../util/struct_array';
import type {TriangleIndexArray, LineIndexArray, LineStripIndexArray} from '../data/index_array_type';
import type Context from '../gl/context';
class IndexBuffer {
context: Context;
buffer: WebGLBuffer;
dynamicDraw: boolean;
constructor(context: Context, array: TriangleIndexArray | LineIndexArray | LineStripIndexArray, dynamicDraw?: boolean) {
this.context = context;
const gl = context.gl;
this.buffer = gl.createBuffer();
this.dynamicDraw = Boolean(dynamicDraw);
// The bound index buffer is part of vertex array object state. We don't want to
// modify whatever VAO happens to be currently bound, so make sure the default
// vertex array provided by the context is bound instead.
this.context.unbindVAO();
context.bindElementBuffer.set(this.buffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, array.arrayBuffer, this.dynamicDraw ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW);
if (!this.dynamicDraw) {
delete array.arrayBuffer;
}
}
bind() {
this.context.bindElementBuffer.set(this.buffer);
}
updateData(array: StructArray) {
const gl = this.context.gl;
assert(this.dynamicDraw);
// The right VAO will get this buffer re-bound later in VertexArrayObject#bind
// See https://github.com/mapbox/mapbox-gl-js/issues/5620
this.context.unbindVAO();
this.bind();
gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, array.arrayBuffer);
}
destroy() {
const gl = this.context.gl;
if (this.buffer) {
gl.deleteBuffer(this.buffer);
delete this.buffer;
}
}
}
export default IndexBuffer;
|