-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.js
268 lines (214 loc) · 9.36 KB
/
program.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* WebGL core teaching framwork
* (C)opyright Hartmut Schirmacher, hschirmacher.beuth-hochschule.de
*
* Module: Program
*
* A Program encapsulates a WebGL program object and has methods
* to create the program from shader source code, enable the program,
* query for attribute locations, and set attributes.
*
* Method Overview:
* - use() : makes this the active program
* - getAttribLocation(): returns the "handle" for a named attribute
* - setUniform() : define the value of a uniform shader variable
* - setTexture() : define a texture to be used in the shaders
* - glRenderContext() : returns associated (native) WebGL rendering context
* - glProgram() : returns associated (native) WebGL program object
*
*/
/* requireJS module definition */
define(["util"],
(function(util) {
"use strict";
/*
* "private static" variable within this module, keeping track of the currently active program
* for each rendering context
*
*/
window.console.log("executing module program.js");
var _currentProgramList = [];
/* private function to check compilation status */
var checkCompilationStatus = function(gl, shader, name) {
var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!compiled) {
// Something went wrong during compilation; get the error
var error = gl.getShaderInfoLog(shader);
throw name + ": " + error;
gl.deleteShader(shader);
return false;
};
return true;
};
/*
Object: Program
The program holds a set of compiled shaders. During rendering of a scene,
it is possible to switch between programs by calling their
respective use() methods. The program is associated with a
specific rendering context.
Parameters to the constructor function:
- gl : WebGL rendering context created via initWebGL()
- vertexShaderSource : string containing source code of the vertex shader.
- fragmentShaderSource : string containing source code of the fragment shader.
*/
var Program = function(gl, vertexShaderSource, fragmentShaderSource) {
// store associated WebGL rendering context
this.gl = gl;
// create a new WebGL program object
this.glProgram = gl.createProgram();
// compile and attach vertex shader
var vshader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vshader, vertexShaderSource);
gl.compileShader(vshader);
if(!checkCompilationStatus(gl, vshader,"vertex shader")) {
return null;
};
gl.attachShader(this.glProgram, vshader);
// compile and attach fragment shader
var fshader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fshader, fragmentShaderSource);
gl.compileShader(fshader);
if(!checkCompilationStatus(gl, fshader,"fragment shader")) {
return null;
}
gl.attachShader(this.glProgram, fshader);
// link the program so it can be used
gl.linkProgram(this.glProgram);
};
/*
* method use(): switch between WebGL programs, activate this program.
* Please note that for performance reasons, the program will only
* be switched if it is not already the active program.
*
*/
Program.prototype.use = function() {
if(_currentProgramList[this.gl] === this) {
// do nothing because a gl.useProgram() is expensive!!!
} else {
this.gl.useProgram(this.glProgram);
_currentProgramList[this.gl] = this;
};
};
/*
* method getAttributeLocation() is a wrapper for the WebGL getAttribLocation.
*
*/
Program.prototype.getAttribLocation = function(name) {
return this.gl.getAttribLocation(this.glProgram, name);
};
/* method: return WebGL rendering context */
Program.prototype.glRenderContext = function() {
return this.gl;
};
/* method: return WebGL program object */
Program.prototype.glProgramObject = function() {
return this.glProgram;
};
/*
method: Set the value of a uniform variable in a WebGL shader.
parameters:
- name [string]: the name of the uniform variable as it appears
in the shader source code
- type [string]: "bool", "int", float", "vec2", vec3", "vec4", "mat3", "mat4"
- value [any type]: the actual value of the variable, with a type
matching the respective WebGL type
return Value:
- true if the uniform was set successfully, else false.
*/
Program.prototype.setUniform = function(name, type, value, showWarning) {
if(showWarning === undefined) {
showWarning = false;
};
// get the location of the uniform within the program object
var location = this.gl.getUniformLocation(this.glProgram, name);
if(location == null) {
if(showWarning) {
window.console.log("WARNING: uniform variable " + name + " not used in shader.");
};
} else {
switch(type) {
case "mat4":
this.gl.uniformMatrix4fv(location, false, value);
return true;
case "mat3":
this.gl.uniformMatrix3fv(location, false, value);
return true;
case "mat2":
this.gl.uniformMatrix2fv(location, false, value);
return true;
case "vec4":
this.gl.uniform4fv(location, value);
return true;
case "vec3":
this.gl.uniform3fv(location, value);
return true;
case "vec2":
this.gl.uniform2fv(location, value);
return true;
case "float":
this.gl.uniform1f(location, value);
return true;
case "int":
this.gl.uniform1i(location, value);
return true;
case "bool": // bool is transferred as integer!
this.gl.uniform1i(location, value);
return true;
default:
throw "setUniform(): unknown uniform type '"+type+"'";
return false;
}; // end switch
}; // end else
return false;
}; // end setUniform()
/*
method setTexture(): bind a texture to a shader variable name and to a texture unit
parameters:
- uniformName: a string containing the name of the uniform variable in the shader
- textureUnit: an integer specifying which texture unit shall be used
- texture: a wrapper object for a WebGL texture (specifics see below)
- showWarning: optional flag whether to show warnings or not (default: true)
the texture object must be a wrapper for a WebGL texture providing
the following two methods:
- isLoaded() returns true if the associated image data has already been loaded
- glTextureObject() returns the associated native WebGL texture object
*/
Program.prototype.setTexture = function(uniformName, textureUnit, texture, showWarning) {
// by default, show warnings
if(showWarning === undefined) {
showWarning = true;
};
// does the texture object have an isLoaded() method?
if( texture===undefined || texture.isLoaded===undefined || texture.glTextureObject===undefined ) {
throw "setTexture(): not a valid texture wrapper object.";
return false;
};
// has the texture already been loaded?
if( !texture.isLoaded() ) {
window.console.log("info: image file for texture "+uniformName+" not loaded yet.");
return false;
};
// is the desired texture unit within the allowed range?
var max = this.gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS;
if(textureUnit < 0 || textureUnit >= max) {
throw "setTexture(): texture unit " + textureUnit +
" out of range [0 ... " + max-1 + "]";
return false;
};
// find location of texture's uniform variable
var location = this.gl.getUniformLocation(this.glProgram, uniformName);
if(location === null) {
window.console.log("uniform sampler " + uniformName + " not used in shader.");
return;
};
// window.console.log("using texture unit "+textureUnit+" for sampler "+samplerName);
// bind the texture unit to the sampler's location/name
this.gl.uniform1i(location, textureUnit);
// activate the right texture unit
this.gl.activeTexture(this.gl.TEXTURE0 + textureUnit);
// bind the actual texture object to the texture unit
this.gl.bindTexture(this.gl.TEXTURE_2D, texture.glTextureObject());
}; // setTexture()
// this module only returns the Program constructor function
return Program;
})); // define