aboutsummaryrefslogtreecommitdiff
path: root/web/pw-frontend/src/lib/visualizer/webgl/vertexBufferLayout.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-frontend/src/lib/visualizer/webgl/vertexBufferLayout.ts
parent3eeaab6cec70e7a06a99a1ac2662974f71064bee (diff)
downloadplanetwars.dev-0c6d978442b244ca3f29c1ffdd44b5007ae7ad93.tar.xz
planetwars.dev-0c6d978442b244ca3f29c1ffdd44b5007ae7ad93.zip
separate out visualizer library
Diffstat (limited to 'web/pw-frontend/src/lib/visualizer/webgl/vertexBufferLayout.ts')
-rw-r--r--web/pw-frontend/src/lib/visualizer/webgl/vertexBufferLayout.ts115
1 files changed, 0 insertions, 115 deletions
diff --git a/web/pw-frontend/src/lib/visualizer/webgl/vertexBufferLayout.ts b/web/pw-frontend/src/lib/visualizer/webgl/vertexBufferLayout.ts
deleted file mode 100644
index f44ed47..0000000
--- a/web/pw-frontend/src/lib/visualizer/webgl/vertexBufferLayout.ts
+++ /dev/null
@@ -1,115 +0,0 @@
-import type { VertexBuffer } from './buffer';
-import type { Shader } from './shader';
-
-export class VertexBufferElement {
- type: number;
- amount: number;
- type_size: number;
- normalized: boolean;
- index: string;
-
- constructor(
- type: number,
- amount: number,
- type_size: number,
- index: string,
- normalized: boolean,
- ) {
- this.type = type;
- this.amount = amount;
- this.type_size = type_size;
- this.normalized = normalized;
- this.index = index;
- }
-}
-
-export class VertexBufferLayout {
- elements: VertexBufferElement[];
- stride: number;
- offset: number;
-
- constructor(offset = 0) {
- this.elements = [];
- this.stride = 0;
- this.offset = offset;
- }
-
- // Maybe wrong normalized type
- push(
- type: number,
- amount: number,
- type_size: number,
- index: string,
- normalized = false,
- ) {
- this.elements.push(new VertexBufferElement(type, amount, type_size, index, normalized));
- this.stride += amount * type_size;
- }
-
- getElements(): VertexBufferElement[] {
- return this.elements;
- }
-
- getStride(): number {
- return this.stride;
- }
-}
-
-// glEnableVertexAttribArray is to specify what location of the current program the follow data is needed
-// glVertexAttribPointer tells gl that that data is at which location in the supplied data
-export class VertexArray {
- // There is no renderer ID, always at bind buffers and use glVertexAttribPointer
- buffers: VertexBuffer[];
- layouts: VertexBufferLayout[];
-
- constructor() {
- this.buffers = [];
- this.layouts = [];
- }
-
- addBuffer(vb: VertexBuffer, layout: VertexBufferLayout) {
- this.buffers.push(vb);
- this.layouts.push(layout);
- }
-
- updateBuffer(gl: WebGLRenderingContext, index: number, data: number[]) {
- this.buffers[index].updateData(gl, data);
- }
-
- /// Bind buffers providing program data
- bind(gl: WebGLRenderingContext, shader: Shader) {
- shader.bind(gl);
- for(let i = 0; i < this.buffers.length; i ++) {
- const buffer = this.buffers[i];
- const layout = this.layouts[i];
-
- buffer.bind(gl);
- const elements = layout.getElements();
- let offset = layout.offset;
-
- for (let j = 0; j < elements.length; j ++) {
- const element = elements[j];
- const location = shader.getAttribLocation(gl, element.index);
-
- if (location >= 0) {
- gl.enableVertexAttribArray(location);
- gl.vertexAttribPointer(
- location, element.amount, element.type,
- element.normalized, layout.stride, offset
- );
- }
-
- offset += element.amount * element.type_size;
- }
- }
- }
-
- /// Undo bind operation
- unbind(gl: WebGLRenderingContext) {
- this.layouts.forEach((layout) => {
- layout.getElements().forEach((_, index) => {
- gl.disableVertexAttribArray(index);
- });
- })
- }
-}