-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonFunctions.js
102 lines (87 loc) · 3.41 KB
/
commonFunctions.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
setup()
function setup() {
// Create a HTML tag to display to the user
var navTag = document.createElement('nav');
navTag.classList = "navbar navbar-expand-lg navbar-dark bg-dark";
navTag.innerHTML = `
<a class="navbar-brand" href="#">WebGL Introduction 2</a>
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="index.html">Example 1</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="example02.html">Example 2</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="example03.html">.obj Loader</a>
</li>
</ul>
`;
// Insert the tag into the HMTL document
document.getElementById('myNavBar').appendChild(navTag);
}
/**
* A custom error function. The tag with id `webglError` must be present
* @param {string} tag Main description
* @param {string} errorStr Detailed description
*/
function printError(tag, errorStr) {
// Create a HTML tag to display to the user
var errorTag = document.createElement('div');
errorTag.classList = 'alert alert-danger';
errorTag.innerHTML = '<strong>' + tag + '</strong><p>' + errorStr + '</p>';
// Insert the tag into the HMTL document
document.getElementById('webglError').appendChild(errorTag);
// Print to the console as well
console.error(tag + ": " + errorStr);
}
/**
* @param {} gl WebGL2 Context
* @param {string} vsSource Vertex shader GLSL source code
* @param {string} fsSource Fragment shader GLSL source code
* @returns {} A shader program object. This is `null` on failure
*/
function initShaderProgram(gl, vsSource, fsSource) {
// Use our custom function to load and compile the shader objects
const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource);
// Create the shader program by attaching and linking the shader objects
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
// If creating the shader program failed, alert
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert('Unable to link the shader program' + gl.getProgramInfoLog(shaderProgram));
return null;
}
return shaderProgram;
}
/**
* Loads a shader from source into a shader object. This should later be linked into a program.
* @param {} gl WebGL2 context
* @param {} type Type of shader. Typically either VERTEX_SHADER or FRAGMENT_SHADER
* @param {string} source GLSL source code
*/
function loadShader(gl, type, source) {
// Create a new shader object
const shader = gl.createShader(type);
// Send the source to the shader object
gl.shaderSource(shader, source);
// Compile the shader program
gl.compileShader(shader);
// See if it compiled successfully
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
// Fail with an error message
var typeStr = '';
if (type === gl.VERTEX_SHADER) {
typeStr = 'VERTEX';
} else if (type === gl.FRAGMENT_SHADER) {
typeStr = 'FRAGMENT';
}
printError('An error occurred compiling the shader: ' + typeStr, gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}