OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 '#ifdef GL_ES\n' + | |
104 'precision mediump float;\n' + | |
105 '#endif\n' + | |
106 'uniform sampler2D tex;\n' + | |
107 'varying vec2 texCoord;\n' + | |
108 'void main() {\n' + | |
109 ' gl_FragData[0] = texture2D(tex, texCoord);\n' + | |
110 '}\n'; | |
111 | |
112 /** | |
113 * Creates a simple texture vertex shader. | |
114 * @param {!WebGLContext} gl The WebGLContext to use. | |
115 * @return {!WebGLShader} | |
116 */ | |
117 var setupSimpleTextureVertexShader = function(gl) { | |
118 return loadShader(gl, simpleTextureVertexShader, gl.VERTEX_SHADER); | |
119 }; | |
120 | |
121 /** | |
122 * Creates a simple texture fragment shader. | |
123 * @param {!WebGLContext} gl The WebGLContext to use. | |
124 * @return {!WebGLShader} | |
125 */ | |
126 var setupSimpleTextureFragmentShader = function(gl) { | |
127 return loadShader( | |
128 gl, simpleTextureFragmentShader, gl.FRAGMENT_SHADER); | |
129 }; | |
130 | |
131 /** | |
132 * Creates a program, attaches shaders, binds attrib locations, links the | |
133 * program and calls useProgram. | |
134 * @param {!Array.<!WebGLShader>} shaders The shaders to attach . | |
135 * @param {!Array.<string>} opt_attribs The attribs names. | |
136 * @param {!Array.<number>} opt_locations The locations for the attribs. | |
137 */ | |
138 var setupProgram = function(gl, shaders, opt_attribs, opt_locations) { | |
139 var program = gl.createProgram(); | |
140 for (var ii = 0; ii < shaders.length; ++ii) { | |
141 gl.attachShader(program, shaders[ii]); | |
142 } | |
143 if (opt_attribs) { | |
144 for (var ii = 0; ii < opt_attribs.length; ++ii) { | |
145 gl.bindAttribLocation( | |
146 program, | |
147 opt_locations ? opt_locations[ii] : ii, | |
148 opt_attribs[ii]); | |
149 } | |
150 } | |
151 gl.linkProgram(program); | |
152 | |
153 // Check the link status | |
154 var linked = gl.getProgramParameter(program, gl.LINK_STATUS); | |
155 if (!linked) { | |
156 // something went wrong with the link | |
157 lastError = gl.getProgramInfoLog (program); | |
158 error("Error in program linking:" + lastError); | |
159 | |
160 gl.deleteProgram(program); | |
161 return null; | |
162 } | |
163 | |
164 gl.useProgram(program); | |
165 return program; | |
166 }; | |
167 | |
168 /** | |
169 * Creates a simple texture program. | |
170 * @param {!WebGLContext} gl The WebGLContext to use. | |
171 * @param {number} opt_positionLocation The attrib location for position. | |
172 * @param {number} opt_texcoordLocation The attrib location for texture coords. | |
173 * @return {WebGLProgram} | |
174 */ | |
175 var setupSimpleTextureProgram = function( | |
176 gl, opt_positionLocation, opt_texcoordLocation) { | |
177 opt_positionLocation = opt_positionLocation || 0; | |
178 opt_texcoordLocation = opt_texcoordLocation || 1; | |
179 var vs = setupSimpleTextureVertexShader(gl); | |
180 var fs = setupSimpleTextureFragmentShader(gl); | |
181 if (!vs || !fs) { | |
182 return null; | |
183 } | |
184 var program = setupProgram( | |
185 gl, | |
186 [vs, fs], | |
187 ['vPosition', 'texCoord0'], | |
188 [opt_positionLocation, opt_texcoordLocation]); | |
189 if (!program) { | |
190 gl.deleteShader(fs); | |
191 gl.deleteShader(vs); | |
192 } | |
193 gl.useProgram(program); | |
194 return program; | |
195 }; | |
196 | |
197 /** | |
198 * Creates buffers for a textured unit quad and attaches them to vertex attribs. | |
199 * @param {!WebGLContext} gl The WebGLContext to use. | |
200 * @param {number} opt_positionLocation The attrib location for position. | |
201 * @param {number} opt_texcoordLocation The attrib location for texture coords. | |
202 * @return {!Array.<WebGLBuffer>} The buffer objects that were | |
203 * created. | |
204 */ | |
205 var setupUnitQuad = function(gl, opt_positionLocation, opt_texcoordLocation) { | |
206 opt_positionLocation = opt_positionLocation || 0; | |
207 opt_texcoordLocation = opt_texcoordLocation || 1; | |
208 var objects = []; | |
209 | |
210 var vertexObject = gl.createBuffer(); | |
211 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); | |
212 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ | |
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, | |
217 -1.0, -1.0, 0.0, | |
218 1.0, -1.0, 0.0]), gl.STATIC_DRAW); | |
219 gl.enableVertexAttribArray(opt_positionLocation); | |
220 gl.vertexAttribPointer(opt_positionLocation, 3, gl.FLOAT, false, 0, 0); | |
221 objects.push(vertexObject); | |
222 | |
223 var vertexObject = gl.createBuffer(); | |
224 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); | |
225 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ | |
226 1.0, 1.0, | |
227 0.0, 1.0, | |
228 0.0, 0.0, | |
229 1.0, 1.0, | |
230 0.0, 0.0, | |
231 1.0, 0.0]), gl.STATIC_DRAW); | |
232 gl.enableVertexAttribArray(opt_texcoordLocation); | |
233 gl.vertexAttribPointer(opt_texcoordLocation, 2, gl.FLOAT, false, 0, 0); | |
234 objects.push(vertexObject); | |
235 return objects; | |
236 }; | |
237 | |
238 /** | |
239 * Creates a program and buffers for rendering a textured quad. | |
240 * @param {!WebGLContext} gl The WebGLContext to use. | |
241 * @param {number} opt_positionLocation The attrib location for position. | |
242 * @param {number} opt_texcoordLocation The attrib location for texture coords. | |
243 * @return {!WebGLProgram} | |
244 */ | |
245 var setupTexturedQuad = function( | |
246 gl, opt_positionLocation, opt_texcoordLocation) { | |
247 var program = setupSimpleTextureProgram( | |
248 gl, opt_positionLocation, opt_texcoordLocation); | |
249 setupUnitQuad(gl, opt_positionLocation, opt_texcoordLocation); | |
250 return program; | |
251 }; | |
252 | |
253 /** | |
254 * Fills the given texture with a solid color | |
255 * @param {!WebGLContext} gl The WebGLContext to use. | |
256 * @param {!WebGLTexture} tex The texture to fill. | |
257 * @param {number} width The width of the texture to create. | |
258 * @param {number} height The height of the texture to create. | |
259 * @param {!Array.<number>} color The color to fill with. A 4 element array | |
260 * where each element is in the range 0 to 255. | |
261 * @param {number} opt_level The level of the texture to fill. Default = 0. | |
262 */ | |
263 var fillTexture = function(gl, tex, width, height, color, opt_level) { | |
264 opt_level = opt_level || 0; | |
265 var numPixels = width * height; | |
266 var size = numPixels * 4; | |
267 var buf = new Uint8Array(size); | |
268 for (var ii = 0; ii < numPixels; ++ii) { | |
269 var off = ii * 4; | |
270 buf[off + 0] = color[0]; | |
271 buf[off + 1] = color[1]; | |
272 buf[off + 2] = color[2]; | |
273 buf[off + 3] = color[3]; | |
274 } | |
275 gl.bindTexture(gl.TEXTURE_2D, tex); | |
276 gl.texImage2D( | |
277 gl.TEXTURE_2D, opt_level, gl.RGBA, width, height, 0, | |
278 gl.RGBA, gl.UNSIGNED_BYTE, buf); | |
279 }; | |
280 | |
281 /** | |
282 * Creates a textures and fills it with a solid color | |
283 * @param {!WebGLContext} gl The WebGLContext to use. | |
284 * @param {number} width The width of the texture to create. | |
285 * @param {number} height The height of the texture to create. | |
286 * @param {!Array.<number>} color The color to fill with. A 4 element array | |
287 * where each element is in the range 0 to 255. | |
288 * @return {!WebGLTexture} | |
289 */ | |
290 var createColoredTexture = function(gl, width, height, color) { | |
291 var tex = gl.createTexture(); | |
292 fillTexture(gl, tex, width, height, color); | |
293 return tex; | |
294 }; | |
295 | |
296 /** | |
297 * Draws a previously setup quad. | |
298 * @param {!WebGLContext} gl The WebGLContext to use. | |
299 * @param {!Array.<number>} opt_color The color to fill clear with before | |
300 * drawing. A 4 element array where each element is in the range 0 to | |
301 * 255. Default [255, 255, 255, 255] | |
302 */ | |
303 var drawQuad = function(gl, opt_color) { | |
304 opt_color = opt_color || [255, 255, 255, 255]; | |
305 gl.clearColor( | |
306 opt_color[0] / 255, | |
307 opt_color[1] / 255, | |
308 opt_color[2] / 255, | |
309 opt_color[3] / 255); | |
310 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); | |
311 gl.drawArrays(gl.TRIANGLES, 0, 6); | |
312 }; | |
313 | |
314 /** | |
315 * Checks that a portion of a canvas is 1 color. | |
316 * @param {!WebGLContext} gl The WebGLContext to use. | |
317 * @param {number} x left corner of region to check. | |
318 * @param {number} y bottom corner of region to check. | |
319 * @param {number} width width of region to check. | |
320 * @param {number} height width of region to check. | |
321 * @param {!Array.<number>} color The color to fill clear with before drawing. A | |
322 * 4 element array where each element is in the range 0 to 255. | |
323 * @param {string} msg Message to associate with success. Eg ("should be red"). | |
324 * @param {number} errorRange Optional. Acceptable error in | |
325 * color checking. 0 by default. | |
326 */ | |
327 var checkCanvasRect = function(gl, x, y, width, height, color, msg, errorRange)
{ | |
328 errorRange = errorRange || 0; | |
329 var buf = new Uint8Array(width * height * 4); | |
330 gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf); | |
331 for (var i = 0; i < width * height; ++i) { | |
332 var offset = i * 4; | |
333 for (var j = 0; j < color.length; ++j) { | |
334 if (Math.abs(buf[offset + j] - color[j]) > errorRange) { | |
335 testFailed(msg); | |
336 var was = buf[offset + 0].toString(); | |
337 for (j = 1; j < color.length; ++j) { | |
338 was += "," + buf[offset + j]; | |
339 } | |
340 debug('expected: ' + color + ' was ' + was); | |
341 return; | |
342 } | |
343 } | |
344 } | |
345 testPassed(msg); | |
346 }; | |
347 | |
348 /** | |
349 * Checks that an entire canvas is 1 color. | |
350 * @param {!WebGLContext} gl The WebGLContext to use. | |
351 * @param {!Array.<number>} color The color to fill clear with before drawing. A | |
352 * 4 element array where each element is in the range 0 to 255. | |
353 * @param {string} msg Message to associate with success. Eg ("should be red"). | |
354 */ | |
355 var checkCanvas = function(gl, color, msg) { | |
356 checkCanvasRect(gl, 0, 0, gl.canvas.width, gl.canvas.height, color, msg); | |
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 new_url = prefix + str; | |
624 files = files.concat(readFileList(new_url)); | |
625 } | |
626 } | |
627 } else { | |
628 files.push(url); | |
629 } | |
630 return files; | |
631 }; | |
632 | |
633 /** | |
634 * Loads a shader. | |
635 * @param {!WebGLContext} gl The WebGLContext to use. | |
636 * @param {string} shaderSource The shader source. | |
637 * @param {number} shaderType The type of shader. | |
638 * @return {!WebGLShader} The created shader. | |
639 */ | |
640 var loadShader = function(gl, shaderSource, shaderType) { | |
641 // Create the shader object | |
642 var shader = gl.createShader(shaderType); | |
643 if (shader == null) { | |
644 error("*** Error: unable to create shader '"+shaderSource+"'"); | |
645 return null; | |
646 } | |
647 | |
648 // Load the shader source | |
649 gl.shaderSource(shader, shaderSource); | |
650 var err = gl.getError(); | |
651 if (err != gl.NO_ERROR) { | |
652 error("*** Error loading shader '" + shader + "':" + glEnumToString(gl, err)
); | |
653 return null; | |
654 } | |
655 | |
656 // Compile the shader | |
657 gl.compileShader(shader); | |
658 | |
659 // Check the compile status | |
660 var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS); | |
661 if (!compiled) { | |
662 // Something went wrong during compilation; get the error | |
663 lastError = gl.getShaderInfoLog(shader); | |
664 error("*** Error compiling shader '" + shader + "':" + lastError); | |
665 gl.deleteShader(shader); | |
666 return null; | |
667 } | |
668 | |
669 return shader; | |
670 } | |
671 | |
672 /** | |
673 * Loads a shader from a URL. | |
674 * @param {!WebGLContext} gl The WebGLContext to use. | |
675 * @param {file} file The URL of the shader source. | |
676 * @param {number} type The type of shader. | |
677 * @return {!WebGLShader} The created shader. | |
678 */ | |
679 var loadShaderFromFile = function(gl, file, type) { | |
680 var shaderSource = readFile(file); | |
681 return loadShader(gl, shaderSource, type); | |
682 }; | |
683 | |
684 /** | |
685 * Loads a shader from a script tag. | |
686 * @param {!WebGLContext} gl The WebGLContext to use. | |
687 * @param {string} scriptId The id of the script tag. | |
688 * @param {number} opt_shaderType The type of shader. If not passed in it will | |
689 * be derived from the type of the script tag. | |
690 * @return {!WebGLShader} The created shader. | |
691 */ | |
692 var loadShaderFromScript = function(gl, scriptId, opt_shaderType) { | |
693 var shaderSource = ""; | |
694 var shaderType; | |
695 var shaderScript = document.getElementById(scriptId); | |
696 if (!shaderScript) { | |
697 throw("*** Error: unknown script element" + scriptId); | |
698 } | |
699 shaderSource = shaderScript.text; | |
700 | |
701 if (!opt_shaderType) { | |
702 if (shaderScript.type == "x-shader/x-vertex") { | |
703 shaderType = gl.VERTEX_SHADER; | |
704 } else if (shaderScript.type == "x-shader/x-fragment") { | |
705 shaderType = gl.FRAGMENT_SHADER; | |
706 } else if (shaderType != gl.VERTEX_SHADER && shaderType != gl.FRAGMENT_SHADE
R) { | |
707 throw("*** Error: unknown shader type"); | |
708 return null; | |
709 } | |
710 } | |
711 | |
712 return loadShader( | |
713 gl, shaderSource, opt_shaderType ? opt_shaderType : shaderType); | |
714 }; | |
715 | |
716 var loadStandardProgram = function(gl) { | |
717 var program = gl.createProgram(); | |
718 gl.attachShader(program, loadStandardVertexShader(gl)); | |
719 gl.attachShader(program, loadStandardFragmentShader(gl)); | |
720 linkProgram(gl, program); | |
721 return program; | |
722 }; | |
723 | |
724 /** | |
725 * Loads shaders from files, creates a program, attaches the shaders and links. | |
726 * @param {!WebGLContext} gl The WebGLContext to use. | |
727 * @param {string} vertexShaderPath The URL of the vertex shader. | |
728 * @param {string} fragmentShaderPath The URL of the fragment shader. | |
729 * @return {!WebGLProgram} The created program. | |
730 */ | |
731 var loadProgramFromFile = function(gl, vertexShaderPath, fragmentShaderPath) { | |
732 var program = gl.createProgram(); | |
733 gl.attachShader( | |
734 program, | |
735 loadShaderFromFile(gl, vertexShaderPath, gl.VERTEX_SHADER)); | |
736 gl.attachShader( | |
737 program, | |
738 loadShaderFromFile(gl, fragmentShaderPath, gl.FRAGMENT_SHADER)); | |
739 linkProgram(gl, program); | |
740 return program; | |
741 }; | |
742 | |
743 /** | |
744 * Loads shaders from script tags, creates a program, attaches the shaders and | |
745 * links. | |
746 * @param {!WebGLContext} gl The WebGLContext to use. | |
747 * @param {string} vertexScriptId The id of the script tag that contains the | |
748 * vertex shader. | |
749 * @param {string} fragmentScriptId The id of the script tag that contains the | |
750 * fragment shader. | |
751 * @return {!WebGLProgram} The created program. | |
752 */ | |
753 var loadProgramFromScript = function loadProgramFromScript( | |
754 gl, vertexScriptId, fragmentScriptId) { | |
755 var program = gl.createProgram(); | |
756 gl.attachShader( | |
757 program, | |
758 loadShaderFromScript(gl, vertexScriptId, gl.VERTEX_SHADER)); | |
759 gl.attachShader( | |
760 program, | |
761 loadShaderFromScript(gl, fragmentScriptId, gl.FRAGMENT_SHADER)); | |
762 linkProgram(gl, program); | |
763 return program; | |
764 }; | |
765 | |
766 /** | |
767 * Loads shaders from script tags, creates a program, attaches the shaders and | |
768 * links. | |
769 * @param {!WebGLContext} gl The WebGLContext to use. | |
770 * @param {string} vertexShader The vertex shader. | |
771 * @param {string} fragmentShader The fragment shader. | |
772 * @return {!WebGLProgram} The created program. | |
773 */ | |
774 var loadProgram = function(gl, vertexShader, fragmentShader) { | |
775 var program = gl.createProgram(); | |
776 gl.attachShader( | |
777 program, | |
778 loadShader(gl, vertexShader, gl.VERTEX_SHADER)); | |
779 gl.attachShader( | |
780 program, | |
781 loadShader(gl, fragmentShader, gl.FRAGMENT_SHADER)); | |
782 linkProgram(gl, program); | |
783 return program; | |
784 }; | |
785 | |
786 var loadStandardVertexShader = function(gl) { | |
787 return loadShaderFromFile( | |
788 gl, "resources/vertexShader.vert", gl.VERTEX_SHADER); | |
789 }; | |
790 | |
791 var loadStandardFragmentShader = function(gl) { | |
792 return loadShaderFromFile( | |
793 gl, "resources/fragmentShader.frag", gl.FRAGMENT_SHADER); | |
794 }; | |
795 | |
796 /** | |
797 * Loads an image asynchronously. | |
798 * @param {string} url URL of image to load. | |
799 * @param {!function(!Element): void} callback Function to call | |
800 * with loaded image. | |
801 */ | |
802 var loadImageAsync = function(url, callback) { | |
803 var img = document.createElement('img'); | |
804 img.onload = function() { | |
805 callback(img); | |
806 }; | |
807 img.src = url; | |
808 }; | |
809 | |
810 /** | |
811 * Loads an array of images. | |
812 * @param {!Array.<string>} urls URLs of images to load. | |
813 * @param {!function(!{string, img}): void} callback. Callback | |
814 * that gets passed map of urls to img tags. | |
815 */ | |
816 var loadImagesAsync = function(urls, callback) { | |
817 var count = 1; | |
818 var images = { }; | |
819 function countDown() { | |
820 --count; | |
821 if (count == 0) { | |
822 callback(images); | |
823 } | |
824 } | |
825 function imageLoaded(url) { | |
826 return function(img) { | |
827 images[url] = img; | |
828 countDown(); | |
829 } | |
830 } | |
831 for (var ii = 0; ii < urls.length; ++ii) { | |
832 ++count; | |
833 loadImageAsync(urls[ii], imageLoaded(urls[ii])); | |
834 } | |
835 countDown(); | |
836 }; | |
837 | |
838 return { | |
839 create3DContext: create3DContext, | |
840 create3DContextWithWrapperThatThrowsOnGLError: | |
841 create3DContextWithWrapperThatThrowsOnGLError, | |
842 checkCanvas: checkCanvas, | |
843 checkCanvasRect: checkCanvasRect, | |
844 createColoredTexture: createColoredTexture, | |
845 drawQuad: drawQuad, | |
846 endsWith: endsWith, | |
847 getLastError: getLastError, | |
848 glEnumToString: glEnumToString, | |
849 glErrorShouldBe: glErrorShouldBe, | |
850 fillTexture: fillTexture, | |
851 loadImageAsync: loadImageAsync, | |
852 loadImagesAsync: loadImagesAsync, | |
853 loadProgram: loadProgram, | |
854 loadProgramFromFile: loadProgramFromFile, | |
855 loadProgramFromScript: loadProgramFromScript, | |
856 loadShader: loadShader, | |
857 loadShaderFromFile: loadShaderFromFile, | |
858 loadShaderFromScript: loadShaderFromScript, | |
859 loadStandardProgram: loadStandardProgram, | |
860 loadStandardVertexShader: loadStandardVertexShader, | |
861 loadStandardFragmentShader: loadStandardFragmentShader, | |
862 loadTexture: loadTexture, | |
863 log: log, | |
864 loggingOff: loggingOff, | |
865 error: error, | |
866 setupProgram: setupProgram, | |
867 setupSimpleTextureFragmentShader: setupSimpleTextureFragmentShader, | |
868 setupSimpleTextureProgram: setupSimpleTextureProgram, | |
869 setupSimpleTextureVertexShader: setupSimpleTextureVertexShader, | |
870 setupTexturedQuad: setupTexturedQuad, | |
871 setupUnitQuad: setupUnitQuad, | |
872 setupWebGLWithShaders: setupWebGLWithShaders, | |
873 startsWith: startsWith, | |
874 shouldGenerateGLError: shouldGenerateGLError, | |
875 readFile: readFile, | |
876 readFileList: readFileList, | |
877 | |
878 none: false | |
879 }; | |
880 | |
881 }()); | |
882 | |
883 | |
OLD | NEW |