A hackable template for creating small and fast browser games.
1import {GL_COMPILE_STATUS, GL_FRAGMENT_SHADER, GL_LINK_STATUS, GL_VERTEX_SHADER} from "./webgl.js";
2
3export interface Material<L> {
4 Mode: GLenum;
5 Program: WebGLProgram;
6 Locations: L;
7}
8
9export function link(gl: WebGLRenderingContext, vertex: string, fragment: string) {
10 let program = gl.createProgram()!;
11 gl.attachShader(program, compile(gl, GL_VERTEX_SHADER, vertex));
12 gl.attachShader(program, compile(gl, GL_FRAGMENT_SHADER, fragment));
13 gl.linkProgram(program);
14
15 if (DEBUG && !gl.getProgramParameter(program, GL_LINK_STATUS)) {
16 throw new Error(gl.getProgramInfoLog(program)!);
17 }
18
19 return program;
20}
21
22function compile(gl: WebGLRenderingContext, type: GLenum, source: string) {
23 let shader = gl.createShader(type)!;
24 gl.shaderSource(shader, source);
25 gl.compileShader(shader);
26
27 if (DEBUG && !gl.getShaderParameter(shader, GL_COMPILE_STATUS)) {
28 throw new Error(gl.getShaderInfoLog(shader)!);
29 }
30
31 return shader;
32}