-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupProgram.js
More file actions
60 lines (54 loc) · 2.46 KB
/
setupProgram.js
File metadata and controls
60 lines (54 loc) · 2.46 KB
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
export default async function setupProgram(/** @type {WebGLRenderingContext} */ context, /** @type {String} */ vertexUrl, /** @type {String} */ fragmentUrl, locationMap) {
const vertexPromise = fetch(vertexUrl).then(response => response.text());
const fragmentPromise = fetch(fragmentUrl).then(response => response.text());
const [vertexSource, fragmentSource] = await Promise.all([vertexPromise, fragmentPromise]);
const vertexShader = context.createShader(context.VERTEX_SHADER);
context.shaderSource(vertexShader, vertexSource);
context.compileShader(vertexShader);
if (!context.getShaderParameter(vertexShader, context.COMPILE_STATUS)) {
alert('Failed to compile the vertex shader: ' + context.getShaderInfoLog(vertexShader));
context.deleteShader(vertexShader);
if (!context.getShaderParameter(vertexShader, context.DELETE_STATUS)) {
alert('Failed to delete the vertex shader.');
}
}
const fragmentShader = context.createShader(context.FRAGMENT_SHADER);
context.shaderSource(fragmentShader, fragmentSource);
context.compileShader(fragmentShader);
if (!context.getShaderParameter(fragmentShader, context.COMPILE_STATUS)) {
alert('Failed to compile the fragment shader: ' + context.getShaderInfoLog(fragmentShader));
context.deleteShader(fragmentShader);
if (!context.getShaderParameter(fragmentShader, context.DELETE_STATUS)) {
alert('Failed to delete the fragment shader.');
}
}
const program = context.createProgram();
context.attachShader(program, vertexShader);
context.attachShader(program, fragmentShader);
context.linkProgram(program);
context.validateProgram(program);
if (!context.getProgramParameter(program, context.LINK_STATUS)) {
alert('Failed to link the shader program: ' + context.getProgramInfoLog(program));
context.deleteProgram(program);
if (!context.getProgramParameter(program, context.DELETE_STATUS)) {
alert('Failed to delete the shader program.');
}
}
context.useProgram(program);
for (let key in locationMap) {
switch (locationMap[key]) {
case 'attribute': {
locationMap[key] = context.getAttribLocation(program, key);
break;
}
case 'uniform': {
locationMap[key] = context.getUniformLocation(program, key);
break;
}
default: {
throw new Error(`${key} is neither an attribute nor a uniform!`);
}
}
}
return locationMap;
}