aboutsummaryrefslogtreecommitdiff
path: root/web/pw-visualizer/src/webgl/buffer.ts
blob: 2739fbe82877b34589b16376ce3652b5fb9cb2a6 (plain)
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
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);
    }
}