-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitShaders.js
executable file
·64 lines (54 loc) · 1.59 KB
/
initShaders.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const initShaders = (gl, vertexShaderId, fragmentShaderId) => {
let vertShdr
let fragShdr
const vertElem = document.getElementById(vertexShaderId)
if (!vertElem) {
alert('Unable to load vertex shader ' + vertexShaderId)
return -1
} else {
vertShdr = gl.createShader(gl.VERTEX_SHADER)
gl.shaderSource(vertShdr, vertElem.textContent.replace(/^\s+|\s+$/g, ''))
gl.compileShader(vertShdr)
if (!gl.getShaderParameter(vertShdr, gl.COMPILE_STATUS)) {
alert(
'Vertex shader failed to compile. The error log is:' +
'<pre>' +
gl.getShaderInfoLog(vertShdr) +
'</pre>'
)
return -1
}
}
const fragElem = document.getElementById(fragmentShaderId)
if (!fragElem) {
alert('Unable to load vertex shader ' + fragmentShaderId)
return -1
} else {
fragShdr = gl.createShader(gl.FRAGMENT_SHADER)
gl.shaderSource(fragShdr, fragElem.textContent.replace(/^\s+|\s+$/g, ''))
gl.compileShader(fragShdr)
if (!gl.getShaderParameter(fragShdr, gl.COMPILE_STATUS)) {
alert(
'Fragment shader failed to compile. The error log is:' +
'<pre>' +
gl.getShaderInfoLog(fragShdr) +
'</pre>'
)
return -1
}
}
const program = gl.createProgram()
gl.attachShader(program, vertShdr)
gl.attachShader(program, fragShdr)
gl.linkProgram(program)
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
alert(
'Shader program failed to link. The error log is:' +
'<pre>' +
gl.getProgramInfoLog(program) +
'</pre>'
)
return -1
}
return program
}