]> git.r.bdr.sh - rbdr/lissajous/blob - lib/webgl_utils.js
1386bf71bbbf18ce18303378a2f5234b207e0f35
[rbdr/lissajous] / lib / webgl_utils.js
1 /**
2 * Initializes the WebGL Shader program
3 */
4 export function initializeShaderProgram(gl, vertexShaderSource, fragmentShaderSource) {
5
6 const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
7 const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
8
9 const shaderProgram = gl.createProgram();
10 gl.attachShader(shaderProgram, vertexShader);
11 gl.attachShader(shaderProgram, fragmentShader);
12
13 gl.linkProgram(shaderProgram);
14
15 if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
16 console.error(`Unable to initialize shader program: ${gl.getProgramInfoLog(shaderProgram)}`);
17 return null;
18 }
19
20 return shaderProgram;
21 }
22
23 /**
24 * Initializes the buffers
25 */
26 export function initializeBuffers(gl) {
27
28 const positionBuffer = gl.createBuffer();
29
30 gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
31
32 return {
33 position: positionBuffer
34 };
35 }
36
37
38 function loadShader(gl, type, source) {
39
40 const shader = gl.createShader(type);
41 gl.shaderSource(shader, source);
42 gl.compileShader(shader);
43
44 if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
45 console.error(`Unable to compile shader: ${gl.getShaderInfoLog(shader)}`);
46 gl.deleteShader(shader);
47 return null;
48 }
49
50 return shader;
51 }