-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvertex-buffer.ts
More file actions
32 lines (29 loc) · 1.1 KB
/
Copy pathvertex-buffer.ts
File metadata and controls
32 lines (29 loc) · 1.1 KB
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
export class VertexBuffer {
public buffer: WebGLBuffer;
public bufferData: Float32Array;
private _gl: WebGLRenderingContext;
sizePerVertex: number;
numberOfVertices: number;
isStatic: boolean;
constructor(gl: WebGLRenderingContext, numberOfVertices: number, sizePerVertex: number, isStatic = false) {
this._gl = gl;
const total = numberOfVertices * sizePerVertex;
this.buffer = gl.createBuffer();
this.numberOfVertices = numberOfVertices;
this.sizePerVertex = sizePerVertex;
this.isStatic = isStatic;
this.bufferData = new Float32Array(total);
}
fill(filler: (data: Float32Array, numberOfVertices: number, sizePerVertex: number) => any) {
filler(this.bufferData, this.numberOfVertices, this.sizePerVertex);
}
bind() {
const gl = this._gl;
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
}
upload() {
const gl = this._gl;
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
gl.bufferData(gl.ARRAY_BUFFER, this.bufferData, this.isStatic ? gl.STATIC_DRAW : gl.DYNAMIC_DRAW);
}
}