aboutsummaryrefslogtreecommitdiff
path: root/web/pw-visualizer/src/webgl/buffer.ts
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2021-12-29 21:24:57 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2021-12-29 21:25:29 +0100
commit0c6d978442b244ca3f29c1ffdd44b5007ae7ad93 (patch)
treebaae5fa459a49ecd362e548e0649e2f58c669a70 /web/pw-visualizer/src/webgl/buffer.ts
parent3eeaab6cec70e7a06a99a1ac2662974f71064bee (diff)
downloadplanetwars.dev-0c6d978442b244ca3f29c1ffdd44b5007ae7ad93.tar.xz
planetwars.dev-0c6d978442b244ca3f29c1ffdd44b5007ae7ad93.zip
separate out visualizer library
Diffstat (limited to 'web/pw-visualizer/src/webgl/buffer.ts')
-rw-r--r--web/pw-visualizer/src/webgl/buffer.ts55
1 files changed, 55 insertions, 0 deletions
diff --git a/web/pw-visualizer/src/webgl/buffer.ts b/web/pw-visualizer/src/webgl/buffer.ts
new file mode 100644
index 0000000..2739fbe
--- /dev/null
+++ b/web/pw-visualizer/src/webgl/buffer.ts
@@ -0,0 +1,55 @@
+
+export class Buffer {
+ buffer: WebGLBuffer;
+ data: any;
+ count: number;
+ type: number;
+
+ constructor(gl: WebGLRenderingContext, data: number[], type: number) {
+ this.buffer = gl.createBuffer();
+ this.type = type;
+
+ if (data)
+ this.updateData(gl, data);
+ }
+
+ _toArray(data: number[]): any {
+ return new Float32Array(data);
+ }
+
+ updateData(gl: WebGLRenderingContext, data: number[]) {
+ this.data = data;
+ this.count = data.length;
+ gl.bindBuffer(this.type, this.buffer);
+ gl.bufferData(this.type, this._toArray(data), gl.STATIC_DRAW);
+ }
+
+ bind(gl: WebGLRenderingContext) {
+ gl.bindBuffer(this.type, this.buffer);
+ }
+
+ getCount(): number {
+ return this.count;
+ }
+}
+
+export class VertexBuffer extends Buffer {
+ constructor(gl: WebGLRenderingContext, data: any) {
+ super(gl, data, gl.ARRAY_BUFFER);
+ }
+
+ _toArray(data: number[]): any {
+ return new Float32Array(data);
+ }
+}
+
+
+export class IndexBuffer extends Buffer {
+ constructor(gl: WebGLRenderingContext, data: any) {
+ super(gl, data, gl.ELEMENT_ARRAY_BUFFER);
+ }
+
+ _toArray(data: number[]): any {
+ return new Uint16Array(data);
+ }
+}