-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaderLoader.js
42 lines (28 loc) · 1.02 KB
/
shaderLoader.js
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
function loadShader(gl, type, source){
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)){
alert(
`Unable to compile shader program ${gl.getShaderInfoLog(shader)}`
);
gl.deleteShader(shader);
return null;
}
return shader;
}
function initShader( gl, vsSource, fsSource ){
const vsShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
const fsShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource);
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vsShader);
gl.attachShader(shaderProgram, fsShader);
gl.linkProgram(shaderProgram);
gl.validateProgram(shaderProgram);
if(!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)){
alert(`Unable to initialize shader program ${gl.getProgramInfoLog(shaderProgram)}`);
return null;
}
return shaderProgram;
}
export{initShader};