diff --git a/src/shaders.ts b/src/shaders.ts new file mode 100644 index 0000000..47ba477 --- /dev/null +++ b/src/shaders.ts @@ -0,0 +1,31 @@ +export function loadShader(gl: WebGLRenderingContext, type: GLenum, source: string) { + const shader = gl.createShader(type); + if (!shader) { + throw new Error("WebGL fails to create shader for given type"); + } + + gl.shaderSource(shader, source); + gl.compileShader(shader); + + if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { + const compileError = gl.getShaderInfoLog(shader); + throw new Error("Error when compiling shader: " + compileError); + } +} + + +export function loadProgram(gl: WebGLRenderingContext, vertexShader: WebGLShader, fragmentShader: WebGLShader) { + const program = gl.createProgram(); + if (!program) { + throw new Error("WebGL fails to create program"); + } + + gl.attachShader(program, vertexShader); + gl.attachShader(program, fragmentShader); + gl.linkProgram(program); + + if (!gl.getProgramParameter(program, gl.LINK_STATUS)) { + const linkError = gl.getProgramInfoLog(program); + throw new Error("Error when linking program: " + linkError); + } +} \ No newline at end of file