sdf-2d/src/graphics/graphics-library/program/fragment-shader-only-program.ts
schmelczerandras 78444d8cc6 Improve API
2020-09-16 11:31:26 +02:00

48 lines
1.3 KiB
TypeScript

import Program from './program';
export class FragmentShaderOnlyProgram extends Program {
private vao?: WebGLVertexArrayObject;
constructor(
gl: WebGL2RenderingContext,
sources: [string, string],
substitutions: { [name: string]: string }
) {
super(gl, sources, substitutions);
this.prepareScreenQuad('vertexPosition');
}
public bind() {
super.bind();
this.gl.bindVertexArray(this.vao!);
}
public draw() {
this.gl.drawArrays(this.gl.TRIANGLE_STRIP, 0, 4);
}
private prepareScreenQuad(attributeName: string) {
const positionAttributeLocation = this.gl.getAttribLocation(
this.program!,
attributeName
);
const positionBuffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, positionBuffer);
this.gl.bufferData(
this.gl.ARRAY_BUFFER,
new Float32Array([-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0]),
this.gl.STATIC_DRAW
);
const vao = this.gl.createVertexArray();
if (!vao) {
throw new Error('Could not create vertex array object');
}
this.vao = vao;
this.gl.bindVertexArray(this.vao);
this.gl.enableVertexAttribArray(positionAttributeLocation);
this.gl.vertexAttribPointer(positionAttributeLocation, 2, this.gl.FLOAT, false, 0, 0);
}
}