OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 WebGLTestUtils = (function() { | |
6 | |
7 /** | |
8 * Wrapped logging function. | |
9 * @param {string} msg The message to log. | |
10 */ | |
11 var log = function(msg) { | |
12 if (window.console && window.console.log) { | |
13 window.console.log(msg); | |
14 } | |
15 }; | |
16 | |
17 /** | |
18 * Wrapped logging function. | |
19 * @param {string} msg The message to log. | |
20 */ | |
21 var error = function(msg) { | |
22 if (window.console) { | |
23 if (window.console.error) { | |
24 window.console.error(msg); | |
25 } | |
26 else if (window.console.log) { | |
27 window.console.log(msg); | |
28 } | |
29 } | |
30 }; | |
31 | |
32 /** | |
33 * Turn off all logging. | |
34 */ | |
35 var loggingOff = function() { | |
36 log = function() {}; | |
37 error = function() {}; | |
38 }; | |
39 | |
40 /** | |
41 * Converts a WebGL enum to a string | |
42 * @param {!WebGLContext} gl The WebGLContext to use. | |
43 * @param {number} value The enum value. | |
44 * @return {string} The enum as a string. | |
45 */ | |
46 var glEnumToString = function(gl, value) { | |
47 for (var p in gl) { | |
48 if (gl[p] == value) { | |
49 return p; | |
50 } | |
51 } | |
52 return "0x" + value.toString(16); | |
53 }; | |
54 | |
55 var lastError = ""; | |
56 | |
57 /** | |
58 * Returns the last compiler/linker error. | |
59 * @return {string} The last compiler/linker error. | |
60 */ | |
61 var getLastError = function() { | |
62 return lastError; | |
63 }; | |
64 | |
65 /** | |
66 * Whether a haystack ends with a needle. | |
67 * @param {string} haystack String to search | |
68 * @param {string} needle String to search for. | |
69 * @param {boolean} True if haystack ends with needle. | |
70 */ | |
71 var endsWith = function(haystack, needle) { | |
72 return haystack.substr(haystack.length - needle.length) === needle; | |
73 }; | |
74 | |
75 /** | |
76 * Whether a haystack starts with a needle. | |
77 * @param {string} haystack String to search | |
78 * @param {string} needle String to search for. | |
79 * @param {boolean} True if haystack starts with needle. | |
80 */ | |
81 var startsWith = function(haystack, needle) { | |
82 return haystack.substr(0, needle.length) === needle; | |
83 }; | |
84 | |
85 /** | |
86 * A vertex shader for a single texture. | |
87 * @type {string} | |
88 */ | |
89 var simpleTextureVertexShader = '' + | |
90 'attribute vec4 vPosition;\n' + | |
91 'attribute vec2 texCoord0;\n' + | |
92 'varying vec2 texCoord;\n' + | |
93 'void main() {\n' + | |
94 ' gl_Position = vPosition;\n' + | |
95 ' texCoord = texCoord0;\n' + | |
96 '}\n'; | |
97 | |
98 /** | |
99 * A fragment shader for a single texture. | |
100 * @type {string} | |
101 */ | |
102 var simpleTextureFragmentShader = '' + | |
103 'precision mediump float;\n' + | |
104 'uniform sampler2D tex;\n' + | |
105 'varying vec2 texCoord;\n' + | |
106 'void main() {\n' + | |
107 ' gl_FragData[0] = texture2D(tex, texCoord);\n' + | |
108 '}\n'; | |
109 | |
110 /** | |
111 * Creates a simple texture vertex shader. | |
112 * @param {!WebGLContext} gl The WebGLContext to use. | |
113 * @return {!WebGLShader} | |
114 */ | |
115 var setupSimpleTextureVertexShader = function(gl) { | |
116 return loadShader(gl, simpleTextureVertexShader, gl.VERTEX_SHADER); | |
117 }; | |
118 | |
119 /** | |
120 * Creates a simple texture fragment shader. | |
121 * @param {!WebGLContext} gl The WebGLContext to use. | |
122 * @return {!WebGLShader} | |
123 */ | |
124 var setupSimpleTextureFragmentShader = function(gl) { | |
125 return loadShader( | |
126 gl, simpleTextureFragmentShader, gl.FRAGMENT_SHADER); | |
127 }; | |
128 | |
129 /** | |
130 * Creates a program, attaches shaders, binds attrib locations, links the | |
131 * program and calls useProgram. | |
132 * @param {!Array.<!WebGLShader>} shaders The shaders to attach . | |
133 * @param {!Array.<string>} opt_attribs The attribs names. | |
134 * @param {!Array.<number>} opt_locations The locations for the attribs. | |
135 */ | |
136 var setupProgram = function(gl, shaders, opt_attribs, opt_locations) { | |
137 var program = gl.createProgram(); | |
138 for (var ii = 0; ii < shaders.length; ++ii) { | |
139 gl.attachShader(program, shaders[ii]); | |
140 } | |
141 if (opt_attribs) { | |
142 for (var ii = 0; ii < opt_attribs.length; ++ii) { | |
143 gl.bindAttribLocation( | |
144 program, | |
145 opt_locations ? opt_locations[ii] : ii, | |
146 opt_attribs[ii]); | |
147 } | |
148 } | |
149 gl.linkProgram(program); | |
150 | |
151 // Check the link status | |
152 var linked = gl.getProgramParameter(program, gl.LINK_STATUS); | |
153 if (!linked) { | |
154 // something went wrong with the link | |
155 lastError = gl.getProgramInfoLog (program); | |
156 error("Error in program linking:" + lastError); | |
157 | |
158 gl.deleteProgram(program); | |
159 return null; | |
160 } | |
161 | |
162 gl.useProgram(program); | |
163 return program; | |
164 }; | |
165 | |
166 /** | |
167 * Creates a simple texture program. | |
168 * @param {!WebGLContext} gl The WebGLContext to use. | |
169 * @param {number} opt_positionLocation The attrib location for position. | |
170 * @param {number} opt_texcoordLocation The attrib location for texture coords. | |
171 * @return {WebGLProgram} | |
172 */ | |
173 var setupSimpleTextureProgram = function( | |
174 gl, opt_positionLocation, opt_texcoordLocation) { | |
175 opt_positionLocation = opt_positionLocation || 0; | |
176 opt_texcoordLocation = opt_texcoordLocation || 1; | |
177 var vs = setupSimpleTextureVertexShader(gl); | |
178 var fs = setupSimpleTextureFragmentShader(gl); | |
179 if (!vs || !fs) { | |
180 return null; | |
181 } | |
182 var program = setupProgram( | |
183 gl, | |
184 [vs, fs], | |
185 ['vPosition', 'texCoord0'], | |
186 [opt_positionLocation, opt_texcoordLocation]); | |
187 if (!program) { | |
188 gl.deleteShader(fs); | |
189 gl.deleteShader(vs); | |
190 } | |
191 gl.useProgram(program); | |
192 return program; | |
193 }; | |
194 | |
195 /** | |
196 * Creates buffers for a textured unit quad and attaches them to vertex attribs. | |
197 * @param {!WebGLContext} gl The WebGLContext to use. | |
198 * @param {number} opt_positionLocation The attrib location for position. | |
199 * @param {number} opt_texcoordLocation The attrib location for texture coords. | |
200 * @return {!Array.<WebGLBuffer>} The buffer objects that were | |
201 * created. | |
202 */ | |
203 var setupUnitQuad = function(gl, opt_positionLocation, opt_texcoordLocation) { | |
204 opt_positionLocation = opt_positionLocation || 0; | |
205 opt_texcoordLocation = opt_texcoordLocation || 1; | |
206 var objects = []; | |
207 | |
208 var vertexObject = gl.createBuffer(); | |
209 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); | |
210 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ | |
211 1.0, 1.0, 0.0, | |
212 -1.0, 1.0, 0.0, | |
213 -1.0, -1.0, 0.0, | |
214 1.0, 1.0, 0.0, | |
215 -1.0, -1.0, 0.0, | |
216 1.0, -1.0, 0.0]), gl.STATIC_DRAW); | |
217 gl.enableVertexAttribArray(opt_positionLocation); | |
218 gl.vertexAttribPointer(opt_positionLocation, 3, gl.FLOAT, false, 0, 0); | |
219 objects.push(vertexObject); | |
220 | |
221 var vertexObject = gl.createBuffer(); | |
222 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); | |
223 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ | |
224 1.0, 1.0, | |
225 0.0, 1.0, | |
226 0.0, 0.0, | |
227 1.0, 1.0, | |
228 0.0, 0.0, | |
229 1.0, 0.0]), gl.STATIC_DRAW); | |
230 gl.enableVertexAttribArray(opt_texcoordLocation); | |
231 gl.vertexAttribPointer(opt_texcoordLocation, 2, gl.FLOAT, false, 0, 0); | |
232 objects.push(vertexObject); | |
233 return objects; | |
234 }; | |
235 | |
236 /** | |
237 * Creates a program and buffers for rendering a textured quad. | |
238 * @param {!WebGLContext} gl The WebGLContext to use. | |
239 * @param {number} opt_positionLocation The attrib location for position. | |
240 * @param {number} opt_texcoordLocation The attrib location for texture coords. | |
241 * @return {!WebGLProgram} | |
242 */ | |
243 var setupTexturedQuad = function( | |
244 gl, opt_positionLocation, opt_texcoordLocation) { | |
245 var program = setupSimpleTextureProgram( | |
246 gl, opt_positionLocation, opt_texcoordLocation); | |
247 setupUnitQuad(gl, opt_positionLocation, opt_texcoordLocation); | |
248 return program; | |
249 }; | |
250 | |
251 /** | |
252 * Fills the given texture with a solid color | |
253 * @param {!WebGLContext} gl The WebGLContext to use. | |
254 * @param {!WebGLTexture} tex The texture to fill. | |
255 * @param {number} width The width of the texture to create. | |
256 * @param {number} height The height of the texture to create. | |
257 * @param {!Array.<number>} color The color to fill with. A 4 element array | |
258 * where each element is in the range 0 to 255. | |
259 * @param {number} opt_level The level of the texture to fill. Default = 0. | |
260 */ | |
261 var fillTexture = function(gl, tex, width, height, color, opt_level) { | |
262 opt_level = opt_level || 0; | |
263 var numPixels = width * height; | |
264 var size = numPixels * 4; | |
265 var buf = new Uint8Array(size); | |
266 for (var ii = 0; ii < numPixels; ++ii) { | |
267 var off = ii * 4; | |
268 buf[off + 0] = color[0]; | |
269 buf[off + 1] = color[1]; | |
270 buf[off + 2] = color[2]; | |
271 buf[off + 3] = color[3]; | |
272 } | |
273 gl.bindTexture(gl.TEXTURE_2D, tex); | |
274 gl.texImage2D( | |
275 gl.TEXTURE_2D, opt_level, gl.RGBA, width, height, 0, | |
276 gl.RGBA, gl.UNSIGNED_BYTE, buf); | |
277 }; | |
278 | |
279 /** | |
280 * Creates a textures and fills it with a solid color | |
281 * @param {!WebGLContext} gl The WebGLContext to use. | |
282 * @param {number} width The width of the texture to create. | |
283 * @param {number} height The height of the texture to create. | |
284 * @param {!Array.<number>} color The color to fill with. A 4 element array | |
285 * where each element is in the range 0 to 255. | |
286 * @return {!WebGLTexture} | |
287 */ | |
288 var createColoredTexture = function(gl, width, height, color) { | |
289 var tex = gl.createTexture(); | |
290 fillTexture(gl, tex, width, height, color); | |
291 return tex; | |
292 }; | |
293 | |
294 /** | |
295 * Draws a previously setup quad. | |
296 * @param {!WebGLContext} gl The WebGLContext to use. | |
297 * @param {!Array.<number>} opt_color The color to fill clear with before | |
298 * drawing. A 4 element array where each element is in the range 0 to | |
299 * 255. Default [255, 255, 255, 255] | |
300 */ | |
301 var drawQuad = function(gl, opt_color) { | |
302 opt_color = opt_color || [255, 255, 255, 255]; | |
303 gl.clearColor( | |
304 opt_color[0] / 255, | |
305 opt_color[1] / 255, | |
306 opt_color[2] / 255, | |
307 opt_color[3] / 255); | |
308 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); | |
309 gl.drawArrays(gl.TRIANGLES, 0, 6); | |
310 }; | |
311 | |
312 /** | |
313 * Checks that a portion of a canvas is 1 color. | |
314 * @param {!WebGLContext} gl The WebGLContext to use. | |
315 * @param {number} x left corner of region to check. | |
316 * @param {number} y bottom corner of region to check. | |
317 * @param {number} width width of region to check. | |
318 * @param {number} height width of region to check. | |
319 * @param {!Array.<number>} color The color to fill clear with before drawing. A | |
320 * 4 element array where each element is in the range 0 to 255. | |
321 * @param {string} msg Message to associate with success. Eg ("should be red"). | |
322 * @param {number} errorRange Optional. Acceptable error in | |
323 * color checking. 0 by default. | |
324 */ | |
325 var checkCanvasRect = function(gl, x, y, width, height, color, msg, errorRange)
{ | |
326 errorRange = errorRange || 0; | |
327 var buf = new Uint8Array(width * height * 4); | |
328 gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf); | |
329 for (var i = 0; i < width * height; ++i) { | |
330 var offset = i * 4; | |
331 for (var j = 0; j < color.length; ++j) { | |
332 if (Math.abs(buf[offset + j] - color[j]) > errorRange) { | |
333 testFailed(msg); | |
334 var was = buf[offset + 0].toString(); | |
335 for (j = 1; j < color.length; ++j) { | |
336 was += "," + buf[offset + j]; | |
337 } | |
338 debug('expected: ' + color + ' was ' + was); | |
339 return; | |
340 } | |
341 } | |
342 } | |
343 testPassed(msg); | |
344 }; | |
345 | |
346 /** | |
347 * Checks that an entire canvas is 1 color. | |
348 * @param {!WebGLContext} gl The WebGLContext to use. | |
349 * @param {!Array.<number>} color The color to fill clear with before drawing. A | |
350 * 4 element array where each element is in the range 0 to 255. | |
351 * @param {string} msg Message to associate with success. Eg ("should be red"). | |
352 * @param {number} errorRange Optional. Acceptable error in | |
353 * color checking. 0 by default. | |
354 */ | |
355 var checkCanvas = function(gl, color, msg, errorRange) { | |
356 checkCanvasRect(gl, 0, 0, gl.canvas.width, gl.canvas.height, color, msg, error
Range); | |
357 }; | |
358 | |
359 /** | |
360 * Loads a texture, calls callback when finished. | |
361 * @param {!WebGLContext} gl The WebGLContext to use. | |
362 * @param {string} url URL of image to load | |
363 * @param {function(!Image): void} callback Function that gets called after | |
364 * image has loaded | |
365 * @return {!WebGLTexture} The created texture. | |
366 */ | |
367 var loadTexture = function(gl, url, callback) { | |
368 var texture = gl.createTexture(); | |
369 gl.bindTexture(gl.TEXTURE_2D, texture); | |
370 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); | |
371 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); | |
372 var image = new Image(); | |
373 image.onload = function() { | |
374 gl.bindTexture(gl.TEXTURE_2D, texture); | |
375 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); | |
376 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, imag
e); | |
377 callback(image); | |
378 }; | |
379 image.src = url; | |
380 return texture; | |
381 }; | |
382 | |
383 /** | |
384 * Creates a webgl context. | |
385 * @param {!Canvas} opt_canvas The canvas tag to get context from. If one is not | |
386 * passed in one will be created. | |
387 * @return {!WebGLContext} The created context. | |
388 */ | |
389 var create3DContext = function(opt_canvas, opt_attributes) { | |
390 opt_canvas = opt_canvas || document.createElement("canvas"); | |
391 var context = null; | |
392 try { | |
393 context = opt_canvas.getContext("webgl", opt_attributes); | |
394 } catch(e) {} | |
395 if (!context) { | |
396 try { | |
397 context = opt_canvas.getContext("experimental-webgl", opt_attributes); | |
398 } catch(e) {} | |
399 } | |
400 if (!context) { | |
401 testFailed("Unable to fetch WebGL rendering context for Canvas"); | |
402 } | |
403 return context; | |
404 } | |
405 | |
406 /** | |
407 * Gets a GLError value as a string. | |
408 * @param {!WebGLContext} gl The WebGLContext to use. | |
409 * @param {number} err The webgl error as retrieved from gl.getError(). | |
410 * @return {string} the error as a string. | |
411 */ | |
412 var getGLErrorAsString = function(gl, err) { | |
413 if (err === gl.NO_ERROR) { | |
414 return "NO_ERROR"; | |
415 } | |
416 for (var name in gl) { | |
417 if (gl[name] === err) { | |
418 return name; | |
419 } | |
420 } | |
421 return err.toString(); | |
422 }; | |
423 | |
424 /** | |
425 * Wraps a WebGL function with a function that throws an exception if there is | |
426 * an error. | |
427 * @param {!WebGLContext} gl The WebGLContext to use. | |
428 * @param {string} fname Name of function to wrap. | |
429 * @return {function} The wrapped function. | |
430 */ | |
431 var createGLErrorWrapper = function(context, fname) { | |
432 return function() { | |
433 var rv = context[fname].apply(context, arguments); | |
434 var err = context.getError(); | |
435 if (err != 0) | |
436 throw "GL error " + getGLErrorAsString(err) + " in " + fname; | |
437 return rv; | |
438 }; | |
439 }; | |
440 | |
441 /** | |
442 * Creates a WebGL context where all functions are wrapped to throw an exception | |
443 * if there is an error. | |
444 * @param {!Canvas} canvas The HTML canvas to get a context from. | |
445 * @return {!Object} The wrapped context. | |
446 */ | |
447 function create3DContextWithWrapperThatThrowsOnGLError(canvas) { | |
448 var context = create3DContext(canvas); | |
449 var wrap = {}; | |
450 for (var i in context) { | |
451 try { | |
452 if (typeof context[i] == 'function') { | |
453 wrap[i] = createGLErrorWrapper(context, i); | |
454 } else { | |
455 wrap[i] = context[i]; | |
456 } | |
457 } catch (e) { | |
458 error("createContextWrapperThatThrowsOnGLError: Error accessing " + i); | |
459 } | |
460 } | |
461 wrap.getError = function() { | |
462 return context.getError(); | |
463 }; | |
464 return wrap; | |
465 }; | |
466 | |
467 /** | |
468 * Tests that an evaluated expression generates a specific GL error. | |
469 * @param {!WebGLContext} gl The WebGLContext to use. | |
470 * @param {number} glError The expected gl error. | |
471 * @param {string} evalSTr The string to evaluate. | |
472 */ | |
473 var shouldGenerateGLError = function(gl, glError, evalStr) { | |
474 var exception; | |
475 try { | |
476 eval(evalStr); | |
477 } catch (e) { | |
478 exception = e; | |
479 } | |
480 if (exception) { | |
481 testFailed(evalStr + " threw exception " + exception); | |
482 } else { | |
483 var err = gl.getError(); | |
484 if (err != glError) { | |
485 testFailed(evalStr + " expected: " + getGLErrorAsString(gl, glError) + ".
Was " + getGLErrorAsString(gl, err) + "."); | |
486 } else { | |
487 testPassed(evalStr + " was expected value: " + getGLErrorAsString(gl, glEr
ror) + "."); | |
488 } | |
489 } | |
490 }; | |
491 | |
492 /** | |
493 * Tests that the first error GL returns is the specified error. | |
494 * @param {!WebGLContext} gl The WebGLContext to use. | |
495 * @param {number} glError The expected gl error. | |
496 * @param {string} opt_msg | |
497 */ | |
498 var glErrorShouldBe = function(gl, glError, opt_msg) { | |
499 opt_msg = opt_msg || ""; | |
500 var err = gl.getError(); | |
501 if (err != glError) { | |
502 testFailed("getError expected: " + getGLErrorAsString(gl, glError) + | |
503 ". Was " + getGLErrorAsString(gl, err) + " : " + opt_msg); | |
504 } else { | |
505 testPassed("getError was expected value: " + | |
506 getGLErrorAsString(gl, glError) + " : " + opt_msg); | |
507 } | |
508 }; | |
509 | |
510 /** | |
511 * Links a WebGL program, throws if there are errors. | |
512 * @param {!WebGLContext} gl The WebGLContext to use. | |
513 * @param {!WebGLProgram} program The WebGLProgram to link. | |
514 */ | |
515 var linkProgram = function(gl, program) { | |
516 // Link the program | |
517 gl.linkProgram(program); | |
518 | |
519 // Check the link status | |
520 var linked = gl.getProgramParameter(program, gl.LINK_STATUS); | |
521 if (!linked) { | |
522 // something went wrong with the link | |
523 var error = gl.getProgramInfoLog (program); | |
524 | |
525 testFailed("Error in program linking:" + error); | |
526 | |
527 gl.deleteProgram(program); | |
528 gl.deleteProgram(fragmentShader); | |
529 gl.deleteProgram(vertexShader); | |
530 } | |
531 }; | |
532 | |
533 /** | |
534 * Sets up WebGL with shaders. | |
535 * @param {string} canvasName The id of the canvas. | |
536 * @param {string} vshader The id of the script tag that contains the vertex | |
537 * shader source. | |
538 * @param {string} fshader The id of the script tag that contains the fragment | |
539 * shader source. | |
540 * @param {!Array.<string>} attribs An array of attrib names used to bind | |
541 * attribs to the ordinal of the name in this array. | |
542 * @param {!Array.<number>} opt_clearColor The color to cla | |
543 * @return {!WebGLContext} The created WebGLContext. | |
544 */ | |
545 var setupWebGLWithShaders = function( | |
546 canvasName, vshader, fshader, attribs) { | |
547 var canvas = document.getElementById(canvasName); | |
548 var gl = create3DContext(canvas); | |
549 if (!gl) { | |
550 testFailed("No WebGL context found"); | |
551 } | |
552 | |
553 // create our shaders | |
554 var vertexShader = loadShaderFromScript(gl, vshader); | |
555 var fragmentShader = loadShaderFromScript(gl, fshader); | |
556 | |
557 if (!vertexShader || !fragmentShader) { | |
558 return null; | |
559 } | |
560 | |
561 // Create the program object | |
562 program = gl.createProgram(); | |
563 | |
564 if (!program) { | |
565 return null; | |
566 } | |
567 | |
568 // Attach our two shaders to the program | |
569 gl.attachShader (program, vertexShader); | |
570 gl.attachShader (program, fragmentShader); | |
571 | |
572 // Bind attributes | |
573 for (var i in attribs) { | |
574 gl.bindAttribLocation (program, i, attribs[i]); | |
575 } | |
576 | |
577 linkProgram(gl, program); | |
578 | |
579 gl.useProgram(program); | |
580 | |
581 gl.clearColor(0,0,0,1); | |
582 gl.clearDepth(1); | |
583 | |
584 gl.enable(gl.DEPTH_TEST); | |
585 gl.enable(gl.BLEND); | |
586 gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); | |
587 | |
588 gl.program = program; | |
589 return gl; | |
590 }; | |
591 | |
592 /** | |
593 * Gets a file from a file/URL | |
594 * @param {string} file the URL of the file to get. | |
595 * @return {string} The contents of the file. | |
596 */ | |
597 var readFile = function(file) { | |
598 var xhr = new XMLHttpRequest(); | |
599 xhr.open("GET", file, false); | |
600 xhr.send(); | |
601 return xhr.responseText.replace(/\r/g, ""); | |
602 }; | |
603 | |
604 /** | |
605 * Gets a file from a URL and parses it for filenames. IF a file name ends | |
606 * in .txt recursively reads that file and adds it to the list. | |
607 */ | |
608 var readFileList = function(url) { | |
609 var files = []; | |
610 if (url.substr(url.length - 4) == '.txt') { | |
611 var lines = readFile(url).split('\n'); | |
612 var prefix = ''; | |
613 var lastSlash = url.lastIndexOf('/'); | |
614 if (lastSlash >= 0) { | |
615 prefix = url.substr(0, lastSlash + 1); | |
616 } | |
617 for (var ii = 0; ii < lines.length; ++ii) { | |
618 var str = lines[ii].replace(/^\s\s*/, '').replace(/\s\s*$/, ''); | |
619 if (str.length > 4 && | |
620 str[0] != '#' && | |
621 str[0] != ";" && | |
622 str.substr(0, 2) != "//") { | |
623 var names = str.split(/ +/); | |
624 if (names.length == 1) { | |
625 new_url = prefix + str; | |
626 files = files.concat(readFileList(new_url)); | |
627 } else { | |
628 var s = ""; | |
629 var p = ""; | |
630 for (var jj = 0; jj < names.length; ++jj) { | |
631 s += p + prefix + names[jj]; | |
632 p = " "; | |
633 } | |
634 files.push(s); | |
635 } | |
636 } | |
637 } | |
638 } else { | |
639 files.push(url); | |
640 } | |
641 return files; | |
642 }; | |
643 | |
644 /** | |
645 * Loads a shader. | |
646 * @param {!WebGLContext} gl The WebGLContext to use. | |
647 * @param {string} shaderSource The shader source. | |
648 * @param {number} shaderType The type of shader. | |
649 * @return {!WebGLShader} The created shader. | |
650 */ | |
651 var loadShader = function(gl, shaderSource, shaderType) { | |
652 // Create the shader object | |
653 var shader = gl.createShader(shaderType); | |
654 if (shader == null) { | |
655 error("*** Error: unable to create shader '"+shaderSource+"'"); | |
656 return null; | |
657 } | |
658 | |
659 // Load the shader source | |
660 gl.shaderSource(shader, shaderSource); | |
661 var err = gl.getError(); | |
662 if (err != gl.NO_ERROR) { | |
663 error("*** Error loading shader '" + shader + "':" + glEnumToString(gl, err)
); | |
664 return null; | |
665 } | |
666 | |
667 // Compile the shader | |
668 gl.compileShader(shader); | |
669 | |
670 // Check the compile status | |
671 var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS); | |
672 if (!compiled) { | |
673 // Something went wrong during compilation; get the error | |
674 lastError = gl.getShaderInfoLog(shader); | |
675 error("*** Error compiling shader '" + shader + "':" + lastError); | |
676 gl.deleteShader(shader); | |
677 return null; | |
678 } | |
679 | |
680 return shader; | |
681 } | |
682 | |
683 /** | |
684 * Loads a shader from a URL. | |
685 * @param {!WebGLContext} gl The WebGLContext to use. | |
686 * @param {file} file The URL of the shader source. | |
687 * @param {number} type The type of shader. | |
688 * @return {!WebGLShader} The created shader. | |
689 */ | |
690 var loadShaderFromFile = function(gl, file, type) { | |
691 var shaderSource = readFile(file); | |
692 return loadShader(gl, shaderSource, type); | |
693 }; | |
694 | |
695 /** | |
696 * Loads a shader from a script tag. | |
697 * @param {!WebGLContext} gl The WebGLContext to use. | |
698 * @param {string} scriptId The id of the script tag. | |
699 * @param {number} opt_shaderType The type of shader. If not passed in it will | |
700 * be derived from the type of the script tag. | |
701 * @return {!WebGLShader} The created shader. | |
702 */ | |
703 var loadShaderFromScript = function(gl, scriptId, opt_shaderType) { | |
704 var shaderSource = ""; | |
705 var shaderType; | |
706 var shaderScript = document.getElementById(scriptId); | |
707 if (!shaderScript) { | |
708 throw("*** Error: unknown script element" + scriptId); | |
709 } | |
710 shaderSource = shaderScript.text; | |
711 | |
712 if (!opt_shaderType) { | |
713 if (shaderScript.type == "x-shader/x-vertex") { | |
714 shaderType = gl.VERTEX_SHADER; | |
715 } else if (shaderScript.type == "x-shader/x-fragment") { | |
716 shaderType = gl.FRAGMENT_SHADER; | |
717 } else if (shaderType != gl.VERTEX_SHADER && shaderType != gl.FRAGMENT_SHADE
R) { | |
718 throw("*** Error: unknown shader type"); | |
719 return null; | |
720 } | |
721 } | |
722 | |
723 return loadShader( | |
724 gl, shaderSource, opt_shaderType ? opt_shaderType : shaderType); | |
725 }; | |
726 | |
727 var loadStandardProgram = function(gl) { | |
728 var program = gl.createProgram(); | |
729 gl.attachShader(program, loadStandardVertexShader(gl)); | |
730 gl.attachShader(program, loadStandardFragmentShader(gl)); | |
731 linkProgram(gl, program); | |
732 return program; | |
733 }; | |
734 | |
735 /** | |
736 * Loads shaders from files, creates a program, attaches the shaders and links. | |
737 * @param {!WebGLContext} gl The WebGLContext to use. | |
738 * @param {string} vertexShaderPath The URL of the vertex shader. | |
739 * @param {string} fragmentShaderPath The URL of the fragment shader. | |
740 * @return {!WebGLProgram} The created program. | |
741 */ | |
742 var loadProgramFromFile = function(gl, vertexShaderPath, fragmentShaderPath) { | |
743 var program = gl.createProgram(); | |
744 gl.attachShader( | |
745 program, | |
746 loadShaderFromFile(gl, vertexShaderPath, gl.VERTEX_SHADER)); | |
747 gl.attachShader( | |
748 program, | |
749 loadShaderFromFile(gl, fragmentShaderPath, gl.FRAGMENT_SHADER)); | |
750 linkProgram(gl, program); | |
751 return program; | |
752 }; | |
753 | |
754 /** | |
755 * Loads shaders from script tags, creates a program, attaches the shaders and | |
756 * links. | |
757 * @param {!WebGLContext} gl The WebGLContext to use. | |
758 * @param {string} vertexScriptId The id of the script tag that contains the | |
759 * vertex shader. | |
760 * @param {string} fragmentScriptId The id of the script tag that contains the | |
761 * fragment shader. | |
762 * @return {!WebGLProgram} The created program. | |
763 */ | |
764 var loadProgramFromScript = function loadProgramFromScript( | |
765 gl, vertexScriptId, fragmentScriptId) { | |
766 var program = gl.createProgram(); | |
767 gl.attachShader( | |
768 program, | |
769 loadShaderFromScript(gl, vertexScriptId, gl.VERTEX_SHADER)); | |
770 gl.attachShader( | |
771 program, | |
772 loadShaderFromScript(gl, fragmentScriptId, gl.FRAGMENT_SHADER)); | |
773 linkProgram(gl, program); | |
774 return program; | |
775 }; | |
776 | |
777 /** | |
778 * Loads shaders from script tags, creates a program, attaches the shaders and | |
779 * links. | |
780 * @param {!WebGLContext} gl The WebGLContext to use. | |
781 * @param {string} vertexShader The vertex shader. | |
782 * @param {string} fragmentShader The fragment shader. | |
783 * @return {!WebGLProgram} The created program. | |
784 */ | |
785 var loadProgram = function(gl, vertexShader, fragmentShader) { | |
786 var program = gl.createProgram(); | |
787 gl.attachShader( | |
788 program, | |
789 loadShader(gl, vertexShader, gl.VERTEX_SHADER)); | |
790 gl.attachShader( | |
791 program, | |
792 loadShader(gl, fragmentShader, gl.FRAGMENT_SHADER)); | |
793 linkProgram(gl, program); | |
794 return program; | |
795 }; | |
796 | |
797 var loadStandardVertexShader = function(gl) { | |
798 return loadShaderFromFile( | |
799 gl, "resources/vertexShader.vert", gl.VERTEX_SHADER); | |
800 }; | |
801 | |
802 var loadStandardFragmentShader = function(gl) { | |
803 return loadShaderFromFile( | |
804 gl, "resources/fragmentShader.frag", gl.FRAGMENT_SHADER); | |
805 }; | |
806 | |
807 /** | |
808 * Loads an image asynchronously. | |
809 * @param {string} url URL of image to load. | |
810 * @param {!function(!Element): void} callback Function to call | |
811 * with loaded image. | |
812 */ | |
813 var loadImageAsync = function(url, callback) { | |
814 var img = document.createElement('img'); | |
815 img.onload = function() { | |
816 callback(img); | |
817 }; | |
818 img.src = url; | |
819 }; | |
820 | |
821 /** | |
822 * Loads an array of images. | |
823 * @param {!Array.<string>} urls URLs of images to load. | |
824 * @param {!function(!{string, img}): void} callback. Callback | |
825 * that gets passed map of urls to img tags. | |
826 */ | |
827 var loadImagesAsync = function(urls, callback) { | |
828 var count = 1; | |
829 var images = { }; | |
830 function countDown() { | |
831 --count; | |
832 if (count == 0) { | |
833 callback(images); | |
834 } | |
835 } | |
836 function imageLoaded(url) { | |
837 return function(img) { | |
838 images[url] = img; | |
839 countDown(); | |
840 } | |
841 } | |
842 for (var ii = 0; ii < urls.length; ++ii) { | |
843 ++count; | |
844 loadImageAsync(urls[ii], imageLoaded(urls[ii])); | |
845 } | |
846 countDown(); | |
847 }; | |
848 | |
849 return { | |
850 create3DContext: create3DContext, | |
851 create3DContextWithWrapperThatThrowsOnGLError: | |
852 create3DContextWithWrapperThatThrowsOnGLError, | |
853 checkCanvas: checkCanvas, | |
854 checkCanvasRect: checkCanvasRect, | |
855 createColoredTexture: createColoredTexture, | |
856 drawQuad: drawQuad, | |
857 endsWith: endsWith, | |
858 getLastError: getLastError, | |
859 glEnumToString: glEnumToString, | |
860 glErrorShouldBe: glErrorShouldBe, | |
861 fillTexture: fillTexture, | |
862 loadImageAsync: loadImageAsync, | |
863 loadImagesAsync: loadImagesAsync, | |
864 loadProgram: loadProgram, | |
865 loadProgramFromFile: loadProgramFromFile, | |
866 loadProgramFromScript: loadProgramFromScript, | |
867 loadShader: loadShader, | |
868 loadShaderFromFile: loadShaderFromFile, | |
869 loadShaderFromScript: loadShaderFromScript, | |
870 loadStandardProgram: loadStandardProgram, | |
871 loadStandardVertexShader: loadStandardVertexShader, | |
872 loadStandardFragmentShader: loadStandardFragmentShader, | |
873 loadTexture: loadTexture, | |
874 log: log, | |
875 loggingOff: loggingOff, | |
876 error: error, | |
877 setupProgram: setupProgram, | |
878 setupSimpleTextureFragmentShader: setupSimpleTextureFragmentShader, | |
879 setupSimpleTextureProgram: setupSimpleTextureProgram, | |
880 setupSimpleTextureVertexShader: setupSimpleTextureVertexShader, | |
881 setupTexturedQuad: setupTexturedQuad, | |
882 setupUnitQuad: setupUnitQuad, | |
883 setupWebGLWithShaders: setupWebGLWithShaders, | |
884 startsWith: startsWith, | |
885 shouldGenerateGLError: shouldGenerateGLError, | |
886 readFile: readFile, | |
887 readFileList: readFileList, | |
888 | |
889 none: false | |
890 }; | |
891 | |
892 }()); | |
893 | |
894 | |
OLD | NEW |