UNPKG

gl-conformance

Version:
1,313 lines (1,188 loc) 111 kB
/*eslint-disable */ var _windowShim = require("../lib/shims/window-shim"); var _documentShim = require("../lib/shims/document-shim"); var _canvasShim = require("../lib/shims/canvas-shim"); var _imageShim = require("../lib/shims/image-shim"); var _rafShim = require("../lib/shims/raf-shim"); var _RESOURCES = require("./resources.json"); function glsl_implicit_subtract_int_mat2_vert(ENVIRONMENT) { var HTMLElement = function() {}; ENVIRONMENT.CONTEXT_LIST = []; ENVIRONMENT.tape.end = (function(tape_end) { return function() { _rafShim.clear(); ENVIRONMENT.CONTEXT_LIST.forEach(function(gl) { (gl.destroy && gl.destroy()); }); ENVIRONMENT.CONTEXT_LIST = []; tape_end.call(ENVIRONMENT.tape); } })(ENVIRONMENT.tape.end); ENVIRONMENT._createContext = ENVIRONMENT.createContext; ENVIRONMENT.createContext = function(w, h, o) { var gl = ENVIRONMENT._createContext(w, h, o); ENVIRONMENT.CONTEXT_LIST.push(gl); return gl; }; ENVIRONMENT.document = _documentShim(ENVIRONMENT); ENVIRONMENT.window = _windowShim(ENVIRONMENT); ENVIRONMENT.scriptList = { "vertexShader": { "type": "text/something-not-javascript", "text": "\n\n// implicit cast of int to mat2 in subtraction should fail\nvoid main()\n{\n mat2 f = mat2(1.0) - 1;\n}\n" } }; ENVIRONMENT.canvasList = [].map(function(opts) { return _canvasShim(ENVIRONMENT, opts); }); ENVIRONMENT.RESOURCES = _RESOURCES; ENVIRONMENT.BASEPATH = "glsl/implicit"; var document = ENVIRONMENT.document; var window = ENVIRONMENT.window; var Image = _imageShim; var requestAnimationFrame = _rafShim.requestAnimationFrame; var cancelAnimationFrame = _rafShim.cancelAnimationFrame;; /* ** Copyright (c) 2012 The Khronos Group Inc. ** ** Permission is hereby granted, free of charge, to any person obtaining a ** copy of this software and/or associated documentation files (the ** "Materials"), to deal in the Materials without restriction, including ** without limitation the rights to use, copy, modify, merge, publish, ** distribute, sublicense, and/or sell copies of the Materials, and to ** permit persons to whom the Materials are furnished to do so, subject to ** the following conditions: ** ** The above copyright notice and this permission notice shall be included ** in all copies or substantial portions of the Materials. ** ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. */ var CONSOLE = (1, eval)("console") function enableJSTestPreVerboseLogging() {} function initTestingHarnessWaitUntilDone() {} function initTestingHarness() {} function nonKhronosFrameworkNotifyDone() { // WebKit Specific code. Add your code here. ENVIRONMENT.tape.end() } function reportTestResultsToHarness(success, msg) { //Garbage } function notifyFinishedToHarness() { ENVIRONMENT.tape.end() } function description(msg) { CONSOLE.log("DESCRIPTION:", msg) } function debug(msg) { CONSOLE.log("DEBUG:", msg) } function escapeHTML(text) { return text.replace(/&/g, "&amp;").replace(/</g, "&lt;"); } function testPassed(msg) { ENVIRONMENT.tape.pass(msg) } function testFailed(msg) { ENVIRONMENT.tape.fail(msg) } function areArraysEqual(_a, _b) { try { if (_a.length !== _b.length) return false; for (var i = 0; i < _a.length; i++) if (_a[i] !== _b[i]) return false; } catch (ex) { return false; } return true; } function isMinusZero(n) { // the only way to tell 0 from -0 in JS is the fact that 1/-0 is // -Infinity instead of Infinity return n === 0 && 1 / n < 0; } function isResultCorrect(_actual, _expected) { if (_expected === 0) return _actual === _expected && (1 / _actual) === (1 / _expected); if (_actual === _expected) return true; if (typeof(_expected) == "number" && isNaN(_expected)) return typeof(_actual) == "number" && isNaN(_actual); if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([])) return areArraysEqual(_actual, _expected); return false; } function stringify(v) { if (v === 0 && 1 / v < 0) return "-0"; else return "" + v; } function evalAndLog(_a) { if (typeof _a != "string") debug("WARN: tryAndLog() expects a string argument"); // Log first in case things go horribly wrong or this causes a sync event. debug(_a); var _av; try { _av = eval(_a); } catch (e) { testFailed(_a + " threw exception " + e); } return _av; } function shouldBe(_a, _b, quiet) { if (typeof _a != "string" || typeof _b != "string") debug("WARN: shouldBe() expects string arguments"); var exception; var _av; try { _av = eval(_a); } catch (e) { exception = e; } var _bv = eval(_b); if (exception) testFailed(_a + " should be " + _bv + ". Threw exception " + exception); else if (isResultCorrect(_av, _bv)) { if (!quiet) { testPassed(_a + " is " + _b); } } else if (typeof(_av) == typeof(_bv)) testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + "."); else testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ")."); } function shouldNotBe(_a, _b, quiet) { if (typeof _a != "string" || typeof _b != "string") debug("WARN: shouldNotBe() expects string arguments"); var exception; var _av; try { _av = eval(_a); } catch (e) { exception = e; } var _bv = eval(_b); if (exception) testFailed(_a + " should not be " + _bv + ". Threw exception " + exception); else if (!isResultCorrect(_av, _bv)) { if (!quiet) { testPassed(_a + " is not " + _b); } } else testFailed(_a + " should not be " + _bv + "."); } function shouldBeTrue(_a) { shouldBe(_a, "true"); } function shouldBeFalse(_a) { shouldBe(_a, "false"); } function shouldBeNaN(_a) { shouldBe(_a, "NaN"); } function shouldBeNull(_a) { shouldBe(_a, "null"); } function shouldBeEqualToString(a, b) { var unevaledString = '"' + b.replace(/"/g, "\"") + '"'; shouldBe(a, unevaledString); } function shouldEvaluateTo(actual, expected) { // A general-purpose comparator. 'actual' should be a string to be // evaluated, as for shouldBe(). 'expected' may be any type and will be // used without being eval'ed. if (expected == null) { // Do this before the object test, since null is of type 'object'. shouldBeNull(actual); } else if (typeof expected == "undefined") { shouldBeUndefined(actual); } else if (typeof expected == "function") { // All this fuss is to avoid the string-arg warning from shouldBe(). try { actualValue = eval(actual); } catch (e) { testFailed("Evaluating " + actual + ": Threw exception " + e); return; } shouldBe("'" + actualValue.toString().replace(/\n/g, "") + "'", "'" + expected.toString().replace(/\n/g, "") + "'"); } else if (typeof expected == "object") { shouldBeTrue(actual + " == '" + expected + "'"); } else if (typeof expected == "string") { shouldBe(actual, expected); } else if (typeof expected == "boolean") { shouldBe("typeof " + actual, "'boolean'"); if (expected) shouldBeTrue(actual); else shouldBeFalse(actual); } else if (typeof expected == "number") { shouldBe(actual, stringify(expected)); } else { debug(expected + " is unknown type " + typeof expected); shouldBeTrue(actual, "'" + expected.toString() + "'"); } } function shouldBeNonZero(_a) { var exception; var _av; try { _av = eval(_a); } catch (e) { exception = e; } if (exception) testFailed(_a + " should be non-zero. Threw exception " + exception); else if (_av != 0) testPassed(_a + " is non-zero."); else testFailed(_a + " should be non-zero. Was " + _av); } function shouldBeNonNull(_a) { var exception; var _av; try { _av = eval(_a); } catch (e) { exception = e; } if (exception) testFailed(_a + " should be non-null. Threw exception " + exception); else if (_av != null) testPassed(_a + " is non-null."); else testFailed(_a + " should be non-null. Was " + _av); } function shouldBeUndefined(_a) { var exception; var _av; try { _av = eval(_a); } catch (e) { exception = e; } if (exception) testFailed(_a + " should be undefined. Threw exception " + exception); else if (typeof _av == "undefined") testPassed(_a + " is undefined."); else testFailed(_a + " should be undefined. Was " + _av); } function shouldBeDefined(_a) { var exception; var _av; try { _av = eval(_a); } catch (e) { exception = e; } if (exception) testFailed(_a + " should be defined. Threw exception " + exception); else if (_av !== undefined) testPassed(_a + " is defined."); else testFailed(_a + " should be defined. Was " + _av); } function shouldBeGreaterThanOrEqual(_a, _b) { if (typeof _a != "string" || typeof _b != "string") debug("WARN: shouldBeGreaterThanOrEqual expects string arguments"); var exception; var _av; try { _av = eval(_a); } catch (e) { exception = e; } var _bv = eval(_b); if (exception) testFailed(_a + " should be >= " + _b + ". Threw exception " + exception); else if (typeof _av == "undefined" || _av < _bv) testFailed(_a + " should be >= " + _b + ". Was " + _av + " (of type " + typeof _av + ")."); else testPassed(_a + " is >= " + _b); } function expectTrue(v, msg) { if (v) { testPassed(msg); } else { testFailed(msg); } } function shouldThrow(_a, _e) { var exception; var _av; try { _av = eval(_a); } catch (e) { exception = e; } var _ev; if (_e) _ev = eval(_e); if (exception) { if (typeof _e == "undefined" || exception == _ev) testPassed(_a + " threw exception " + exception + "."); else testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + "."); } else if (typeof _av == "undefined") testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was undefined."); else testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + _av + "."); } function shouldBeType(_a, _type) { var exception; var _av; try { _av = eval(_a); } catch (e) { exception = e; } var _typev = eval(_type); if (_av instanceof _typev) { testPassed(_a + " is an instance of " + _type); } else { testFailed(_a + " is not an instance of " + _type); } } function assertMsg(assertion, msg) { if (assertion) { testPassed(msg); } else { testFailed(msg); } } function gc() {} function finishTest() { ENVIRONMENT.tape.end() };; /* ** Copyright (c) 2012 The Khronos Group Inc. ** ** Permission is hereby granted, free of charge, to any person obtaining a ** copy of this software and/or associated documentation files (the ** "Materials"), to deal in the Materials without restriction, including ** without limitation the rights to use, copy, modify, merge, publish, ** distribute, sublicense, and/or sell copies of the Materials, and to ** permit persons to whom the Materials are furnished to do so, subject to ** the following conditions: ** ** The above copyright notice and this permission notice shall be included ** in all copies or substantial portions of the Materials. ** ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. */ var canvas = null function createTestUtils() { "use strict" var console = (1, eval)("console") var path = require("path").posix /** * Wrapped logging function. * @param {string} msg The message to log. */ var log = function(msg) { console.log(msg) }; /** * Wrapped logging function. * @param {string} msg The message to log. */ var error = function(msg) { console.log(msg) }; /** * Turn off all logging. */ var loggingOff = function() { log = function() {}; error = function() {}; }; /** * Converts a WebGL enum to a string * @param {!WebGLContext} gl The WebGLContext to use. * @param {number} value The enum value. * @return {string} The enum as a string. */ var glEnumToString = function(gl, value) { for (var p in gl) { if (gl[p] == value) { return p; } } return "0x" + value.toString(16); }; var lastError = ""; /** * Returns the last compiler/linker error. * @return {string} The last compiler/linker error. */ var getLastError = function() { return lastError; }; /** * Whether a haystack ends with a needle. * @param {string} haystack String to search * @param {string} needle String to search for. * @param {boolean} True if haystack ends with needle. */ var endsWith = function(haystack, needle) { return haystack.substr(haystack.length - needle.length) === needle; }; /** * Whether a haystack starts with a needle. * @param {string} haystack String to search * @param {string} needle String to search for. * @param {boolean} True if haystack starts with needle. */ var startsWith = function(haystack, needle) { return haystack.substr(0, needle.length) === needle; }; /** * A vertex shader for a single texture. * @type {string} */ var simpleTextureVertexShader = [ 'attribute vec4 vPosition;', 'attribute vec2 texCoord0;', 'varying vec2 texCoord;', 'void main() {', ' gl_Position = vPosition;', ' texCoord = texCoord0;', '}' ].join('\n'); /** * A fragment shader for a single texture. * @type {string} */ var simpleTextureFragmentShader = [ 'precision mediump float;', 'uniform sampler2D tex;', 'varying vec2 texCoord;', 'void main() {', ' gl_FragData[0] = texture2D(tex, texCoord);', '}' ].join('\n'); /** * A vertex shader for a single texture. * @type {string} */ var noTexCoordTextureVertexShader = [ 'attribute vec4 vPosition;', 'varying vec2 texCoord;', 'void main() {', ' gl_Position = vPosition;', ' texCoord = vPosition.xy * 0.5 + 0.5;', '}' ].join('\n'); /** * A vertex shader for a uniform color. * @type {string} */ var simpleColorVertexShader = [ 'attribute vec4 vPosition;', 'void main() {', ' gl_Position = vPosition;', '}' ].join('\n'); /** * A fragment shader for a uniform color. * @type {string} */ var simpleColorFragmentShader = [ 'precision mediump float;', 'uniform vec4 u_color;', 'void main() {', ' gl_FragData[0] = u_color;', '}' ].join('\n'); /** * A vertex shader for vertex colors. * @type {string} */ var simpleVertexColorVertexShader = [ 'attribute vec4 vPosition;', 'attribute vec4 a_color;', 'varying vec4 v_color;', 'void main() {', ' gl_Position = vPosition;', ' v_color = a_color;', '}' ].join('\n'); /** * A fragment shader for vertex colors. * @type {string} */ var simpleVertexColorFragmentShader = [ 'precision mediump float;', 'varying vec4 v_color;', 'void main() {', ' gl_FragData[0] = v_color;', '}' ].join('\n'); /** * Creates a simple texture vertex shader. * @param {!WebGLContext} gl The WebGLContext to use. * @return {!WebGLShader} */ var setupSimpleTextureVertexShader = function(gl) { return loadShader(gl, simpleTextureVertexShader, gl.VERTEX_SHADER); }; /** * Creates a simple texture fragment shader. * @param {!WebGLContext} gl The WebGLContext to use. * @return {!WebGLShader} */ var setupSimpleTextureFragmentShader = function(gl) { return loadShader( gl, simpleTextureFragmentShader, gl.FRAGMENT_SHADER); }; /** * Creates a texture vertex shader that doesn't need texcoords. * @param {!WebGLContext} gl The WebGLContext to use. * @return {!WebGLShader} */ var setupNoTexCoordTextureVertexShader = function(gl) { return loadShader(gl, noTexCoordTextureVertexShader, gl.VERTEX_SHADER); }; /** * Creates a simple vertex color vertex shader. * @param {!WebGLContext} gl The WebGLContext to use. * @return {!WebGLShader} */ var setupSimpleVertexColorVertexShader = function(gl) { return loadShader(gl, simpleVertexColorVertexShader, gl.VERTEX_SHADER); }; /** * Creates a simple vertex color fragment shader. * @param {!WebGLContext} gl The WebGLContext to use. * @return {!WebGLShader} */ var setupSimpleVertexColorFragmentShader = function(gl) { return loadShader( gl, simpleVertexColorFragmentShader, gl.FRAGMENT_SHADER); }; /** * Creates a simple color vertex shader. * @param {!WebGLContext} gl The WebGLContext to use. * @return {!WebGLShader} */ var setupSimpleColorVertexShader = function(gl) { return loadShader(gl, simpleColorVertexShader, gl.VERTEX_SHADER); }; /** * Creates a simple color fragment shader. * @param {!WebGLContext} gl The WebGLContext to use. * @return {!WebGLShader} */ var setupSimpleColorFragmentShader = function(gl) { return loadShader( gl, simpleColorFragmentShader, gl.FRAGMENT_SHADER); }; /** * Creates a program, attaches shaders, binds attrib locations, links the * program and calls useProgram. * @param {!Array.<!WebGLShader|string>} shaders The shaders to * attach, or the source, or the id of a script to get * the source from. * @param {!Array.<string>} opt_attribs The attribs names. * @param {!Array.<number>} opt_locations The locations for the attribs. */ var setupProgram = function(gl, shaders, opt_attribs, opt_locations) { var realShaders = []; var program = gl.createProgram(); var shaderType = undefined; for (var ii = 0; ii < shaders.length; ++ii) { var shader = shaders[ii]; if (typeof shader == 'string') { var element = ENVIRONMENT.scriptList[shader]; if (element) { if (element.type != "x-shader/x-vertex" && element.type != "x-shader/x-fragment") shaderType = ii ? gl.FRAGMENT_SHADER : gl.VERTEX_SHADER; shader = loadShaderFromScript(gl, shader, shaderType); } else if (endsWith(shader, ".vert")) { shader = loadShaderFromFile(gl, shader, gl.VERTEX_SHADER); } else if (endsWith(shader, ".frag")) { shader = loadShaderFromFile(gl, shader, gl.FRAGMENT_SHADER); } else { shader = loadShader(gl, shader, ii ? gl.FRAGMENT_SHADER : gl.VERTEX_SHADER); } } gl.attachShader(program, shader); } if (opt_attribs) { for (var ii = 0; ii < opt_attribs.length; ++ii) { gl.bindAttribLocation( program, opt_locations ? opt_locations[ii] : ii, opt_attribs[ii]); } } gl.linkProgram(program); // Check the link status var linked = gl.getProgramParameter(program, gl.LINK_STATUS); if (!linked) { // something went wrong with the link lastError = gl.getProgramInfoLog(program); error("Error in program linking:" + lastError); gl.deleteProgram(program); return null; } gl.useProgram(program); return program; }; /** * Creates a simple texture program. * @param {!WebGLContext} gl The WebGLContext to use. * @param {number} opt_positionLocation The attrib location for position. * @param {number} opt_texcoordLocation The attrib location for texture coords. * @return {WebGLProgram} */ var setupSimpleTextureProgram = function( gl, opt_positionLocation, opt_texcoordLocation) { opt_positionLocation = opt_positionLocation || 0; opt_texcoordLocation = opt_texcoordLocation || 1; var vs = setupSimpleTextureVertexShader(gl); var fs = setupSimpleTextureFragmentShader(gl); if (!vs || !fs) { return null; } var program = setupProgram( gl, [vs, fs], ['vPosition', 'texCoord0'], [opt_positionLocation, opt_texcoordLocation]); if (!program) { gl.deleteShader(fs); gl.deleteShader(vs); } gl.useProgram(program); return program; }; /** * Creates a simple texture program. * @param {!WebGLContext} gl The WebGLContext to use. * @return {WebGLProgram} */ var setupNoTexCoordTextureProgram = function(gl) { var vs = setupNoTexCoordTextureVertexShader(gl); var fs = setupSimpleTextureFragmentShader(gl); if (!vs || !fs) { return null; } var program = setupProgram( gl, [vs, fs], ['vPosition'], [0]); if (!program) { gl.deleteShader(fs); gl.deleteShader(vs); } gl.useProgram(program); return program; }; /** * Creates a simple texture program. * @param {!WebGLContext} gl The WebGLContext to use. * @param {number} opt_positionLocation The attrib location for position. * @param {number} opt_texcoordLocation The attrib location for texture coords. * @return {WebGLProgram} */ var setupSimpleTextureProgram = function( gl, opt_positionLocation, opt_texcoordLocation) { opt_positionLocation = opt_positionLocation || 0; opt_texcoordLocation = opt_texcoordLocation || 1; var vs = setupSimpleTextureVertexShader(gl); var fs = setupSimpleTextureFragmentShader(gl); if (!vs || !fs) { return null; } var program = setupProgram( gl, [vs, fs], ['vPosition', 'texCoord0'], [opt_positionLocation, opt_texcoordLocation]); if (!program) { gl.deleteShader(fs); gl.deleteShader(vs); } gl.useProgram(program); return program; }; /** * Creates a simple vertex color program. * @param {!WebGLContext} gl The WebGLContext to use. * @param {number} opt_positionLocation The attrib location for position. * @param {number} opt_vertexColorLocation The attrib location * for vertex colors. * @return {WebGLProgram} */ var setupSimpleVertexColorProgram = function( gl, opt_positionLocation, opt_vertexColorLocation) { opt_positionLocation = opt_positionLocation || 0; opt_vertexColorLocation = opt_vertexColorLocation || 1; var vs = setupSimpleVertexColorVertexShader(gl); var fs = setupSimpleVertexColorFragmentShader(gl); if (!vs || !fs) { return null; } var program = setupProgram( gl, [vs, fs], ['vPosition', 'a_color'], [opt_positionLocation, opt_vertexColorLocation]); if (!program) { gl.deleteShader(fs); gl.deleteShader(vs); } gl.useProgram(program); return program; }; /** * Creates a simple color program. * @param {!WebGLContext} gl The WebGLContext to use. * @param {number} opt_positionLocation The attrib location for position. * @return {WebGLProgram} */ var setupSimpleColorProgram = function(gl, opt_positionLocation) { opt_positionLocation = opt_positionLocation || 0; var vs = setupSimpleColorVertexShader(gl); var fs = setupSimpleColorFragmentShader(gl); if (!vs || !fs) { return null; } var program = setupProgram( gl, [vs, fs], ['vPosition'], [opt_positionLocation]); if (!program) { gl.deleteShader(fs); gl.deleteShader(vs); } gl.useProgram(program); return program; }; /** * Creates buffers for a textured unit quad and attaches them to vertex attribs. * @param {!WebGLRenderingContext} gl The WebGLRenderingContext to use. * @param {number} opt_positionLocation The attrib location for position. * @param {number} opt_texcoordLocation The attrib location for texture coords. * @return {!Array.<WebGLBuffer>} The buffer objects that were * created. */ var setupUnitQuad = function(gl, opt_positionLocation, opt_texcoordLocation) { return setupUnitQuadWithTexCoords(gl, [0.0, 0.0], [1.0, 1.0], opt_positionLocation, opt_texcoordLocation); }; /** * Creates buffers for a textured unit quad with specified lower left * and upper right texture coordinates, and attaches them to vertex * attribs. * @param {!WebGLRenderingContext} gl The WebGLRenderingContext to use. * @param {!Array.<number>} lowerLeftTexCoords The texture coordinates for the lower left corner. * @param {!Array.<number>} upperRightTexCoords The texture coordinates for the upper right corner. * @param {number} opt_positionLocation The attrib location for position. * @param {number} opt_texcoordLocation The attrib location for texture coords. * @return {!Array.<WebGLBuffer>} The buffer objects that were * created. */ var setupUnitQuadWithTexCoords = function( gl, lowerLeftTexCoords, upperRightTexCoords, opt_positionLocation, opt_texcoordLocation) { return setupQuad(gl, { positionLocation: opt_positionLocation || 0, texcoordLocation: opt_texcoordLocation || 1, lowerLeftTexCoords: lowerLeftTexCoords, upperRightTexCoords: upperRightTexCoords, }); }; /** * Makes a quad with various options. * @param {!WebGLRenderingContext} gl The WebGLRenderingContext to use. * @param {!Object} options. * * scale: scale to multiple unit quad values by. default 1.0. * positionLocation: attribute location for position. * texcoordLocation: attribute location for texcoords. * If this does not exist no texture coords are created. * lowerLeftTexCoords: an array of 2 values for the * lowerLeftTexCoords. * upperRightTexCoords: an array of 2 values for the * upperRightTexCoords. */ var setupQuad = function(gl, options) { var positionLocation = options.positionLocation || 0; var scale = options.scale || 1; var objects = []; var vertexObject = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 1.0 * scale, 1.0 * scale, -1.0 * scale, 1.0 * scale, -1.0 * scale, -1.0 * scale, 1.0 * scale, 1.0 * scale, -1.0 * scale, -1.0 * scale, 1.0 * scale, -1.0 * scale, ]), gl.STATIC_DRAW); gl.enableVertexAttribArray(positionLocation); gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0); objects.push(vertexObject); if (options.texcoordLocation !== undefined) { var llx = options.lowerLeftTexCoords[0]; var lly = options.lowerLeftTexCoords[1]; var urx = options.upperRightTexCoords[0]; var ury = options.upperRightTexCoords[1]; var vertexObject = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ urx, ury, llx, ury, llx, lly, urx, ury, llx, lly, urx, lly ]), gl.STATIC_DRAW); gl.enableVertexAttribArray(options.texcoordLocation); gl.vertexAttribPointer(options.texcoordLocation, 2, gl.FLOAT, false, 0, 0); objects.push(vertexObject); } return objects; }; /** * Creates a program and buffers for rendering a textured quad. * @param {!WebGLRenderingContext} gl The WebGLRenderingContext to use. * @param {number} opt_positionLocation The attrib location for * position. Default = 0. * @param {number} opt_texcoordLocation The attrib location for * texture coords. Default = 1. * @return {!WebGLProgram} */ var setupTexturedQuad = function( gl, opt_positionLocation, opt_texcoordLocation) { var program = setupSimpleTextureProgram( gl, opt_positionLocation, opt_texcoordLocation); setupUnitQuad(gl, opt_positionLocation, opt_texcoordLocation); return program; }; /** * Creates a program and buffers for rendering a color quad. * @param {!WebGLRenderingContext} gl The WebGLRenderingContext to use. * @param {number} opt_positionLocation The attrib location for position. * @return {!WebGLProgram} */ var setupColorQuad = function(gl, opt_positionLocation) { opt_positionLocation = opt_positionLocation || 0; var program = setupSimpleColorProgram(gl); setupUnitQuad(gl, opt_positionLocation); return program; }; /** * Creates a program and buffers for rendering a textured quad with * specified lower left and upper right texture coordinates. * @param {!WebGLRenderingContext} gl The WebGLRenderingContext to use. * @param {!Array.<number>} lowerLeftTexCoords The texture coordinates for the lower left corner. * @param {!Array.<number>} upperRightTexCoords The texture coordinates for the upper right corner. * @param {number} opt_positionLocation The attrib location for position. * @param {number} opt_texcoordLocation The attrib location for texture coords. * @return {!WebGLProgram} */ var setupTexturedQuadWithTexCoords = function( gl, lowerLeftTexCoords, upperRightTexCoords, opt_positionLocation, opt_texcoordLocation) { var program = setupSimpleTextureProgram( gl, opt_positionLocation, opt_texcoordLocation); setupUnitQuadWithTexCoords(gl, lowerLeftTexCoords, upperRightTexCoords, opt_positionLocation, opt_texcoordLocation); return program; }; /** * Creates a unit quad with only positions of a given resolution. * @param {!WebGLRenderingContext} gl The WebGLRenderingContext to use. * @param {number} gridRes The resolution of the mesh grid, * expressed in the number of quads across and down. * @param {number} opt_positionLocation The attrib location for position. */ var setupIndexedQuad = function( gl, gridRes, opt_positionLocation, opt_flipOddTriangles) { return setupIndexedQuadWithOptions(gl, { gridRes: gridRes, positionLocation: opt_positionLocation, flipOddTriangles: opt_flipOddTriangles }); }; /** * Creates a quad with various options. * @param {!WebGLRenderingContext} gl The WebGLRenderingContext to use. * @param {!Object) options The options. See below. * @return {!Array.<WebGLBuffer>} The created buffers. * [positions, <colors>, indices] * * Options: * gridRes: number of quads across and down grid. * positionLocation: attrib location for position * flipOddTriangles: reverse order of vertices of every other * triangle * positionOffset: offset added to each vertex * positionMult: multipier for each vertex * colorLocation: attrib location for vertex colors. If * undefined no vertex colors will be created. */ var setupIndexedQuadWithOptions = function(gl, options) { var positionLocation = options.positionLocation || 0; var objects = []; var gridRes = options.gridRes || 1; var positionOffset = options.positionOffset || 0; var positionMult = options.positionMult || 1; var vertsAcross = gridRes + 1; var numVerts = vertsAcross * vertsAcross; var positions = new Float32Array(numVerts * 3); var indices = new Uint16Array(6 * gridRes * gridRes); var poffset = 0; for (var yy = 0; yy <= gridRes; ++yy) { for (var xx = 0; xx <= gridRes; ++xx) { positions[poffset + 0] = (-1 + 2 * xx / gridRes) * positionMult + positionOffset; positions[poffset + 1] = (-1 + 2 * yy / gridRes) * positionMult + positionOffset; positions[poffset + 2] = 0; poffset += 3; } } var buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf); gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW); gl.enableVertexAttribArray(positionLocation); gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0); objects.push(buf); if (options.colorLocation !== undefined) { var colors = new Float32Array(numVerts * 4); for (var yy = 0; yy <= gridRes; ++yy) { for (var xx = 0; xx <= gridRes; ++xx) { if (options.color !== undefined) { colors[poffset + 0] = options.color[0]; colors[poffset + 1] = options.color[1]; colors[poffset + 2] = options.color[2]; colors[poffset + 3] = options.color[3]; } else { colors[poffset + 0] = xx / gridRes; colors[poffset + 1] = yy / gridRes; colors[poffset + 2] = (xx / gridRes) * (yy / gridRes); colors[poffset + 3] = (yy % 2) * 0.5 + 0.5; } poffset += 4; } } var buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf); gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW); gl.enableVertexAttribArray(options.colorLocation); gl.vertexAttribPointer(options.colorLocation, 4, gl.FLOAT, false, 0, 0); objects.push(buf); } var tbase = 0; for (var yy = 0; yy < gridRes; ++yy) { var index = yy * vertsAcross; for (var xx = 0; xx < gridRes; ++xx) { indices[tbase + 0] = index + 0; indices[tbase + 1] = index + 1; indices[tbase + 2] = index + vertsAcross; indices[tbase + 3] = index + vertsAcross; indices[tbase + 4] = index + 1; indices[tbase + 5] = index + vertsAcross + 1; if (options.flipOddTriangles) { indices[tbase + 4] = index + vertsAcross + 1; indices[tbase + 5] = index + 1; } index += 1; tbase += 6; } } var buf = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buf); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW); objects.push(buf); return objects; }; /** * Fills the given texture with a solid color * @param {!WebGLContext} gl The WebGLContext to use. * @param {!WebGLTexture} tex The texture to fill. * @param {number} width The width of the texture to create. * @param {number} height The height of the texture to create. * @param {!Array.<number>} color The color to fill with. A 4 element array * where each element is in the range 0 to 255. * @param {number} opt_level The level of the texture to fill. Default = 0. */ var fillTexture = function(gl, tex, width, height, color, opt_level) { opt_level = opt_level || 0; var numPixels = width * height; var size = numPixels * 4; var buf = new Uint8Array(size); for (var ii = 0; ii < numPixels; ++ii) { var off = ii * 4; buf[off + 0] = color[0]; buf[off + 1] = color[1]; buf[off + 2] = color[2]; buf[off + 3] = color[3]; } gl.bindTexture(gl.TEXTURE_2D, tex); gl.texImage2D( gl.TEXTURE_2D, opt_level, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, buf); }; /** * Creates a textures and fills it with a solid color * @param {!WebGLContext} gl The WebGLContext to use. * @param {number} width The width of the texture to create. * @param {number} height The height of the texture to create. * @param {!Array.<number>} color The color to fill with. A 4 element array * where each element is in the range 0 to 255. * @return {!WebGLTexture} */ var createColoredTexture = function(gl, width, height, color) { var tex = gl.createTexture(); fillTexture(gl, tex, width, height, color); return tex; }; var ubyteToFloat = function(c) { return c / 255; }; var ubyteColorToFloatColor = function(color) { var floatColor = []; for (var ii = 0; ii < color.length; ++ii) { floatColor[ii] = ubyteToFloat(color[ii]); } return floatColor; }; /** * Sets the "u_color" uniform of the current program to color. * @param {!WebGLContext} gl The WebGLContext to use. * @param {!Array.<number> color 4 element array of 0-1 color * components. */ var setFloatDrawColor = function(gl, color) { var program = gl.getParameter(gl.CURRENT_PROGRAM); var colorLocation = gl.getUniformLocation(program, "u_color"); gl.uniform4fv(colorLocation, color); }; /** * Sets the "u_color" uniform of the current program to color. * @param {!WebGLContext} gl The WebGLContext to use. * @param {!Array.<number> color 4 element array of 0-255 color * components. */ var setUByteDrawColor = function(gl, color) { setFloatDrawColor(gl, ubyteColorToFloatColor(color)); }; /** * Draws a previously setup quad in the given color. * @param {!WebGLContext} gl The WebGLContext to use. * @param {!Array.<number>} color The color to draw with. A 4 * element array where each element is in the range 0 to * 1. */ var drawFloatColorQuad = function(gl, color) { var program = gl.getParameter(gl.CURRENT_PROGRAM); var colorLocation = gl.getUniformLocation(program, "u_color"); gl.uniform4fv(colorLocation, color); gl.drawArrays(gl.TRIANGLES, 0, 6); }; /** * Draws a previously setup quad in the given color. * @param {!WebGLContext} gl The WebGLContext to use. * @param {!Array.<number>} color The color to draw with. A 4 * element array where each element is in the range 0 to * 255. */ var drawUByteColorQuad = function(gl, color) { drawFloatColorQuad(gl, ubyteColorToFloatColor(color)); }; /** * Draws a previously setupUnitQuad. * @param {!WebGLContext} gl The WebGLContext to use. */ var drawUnitQuad = function(gl) { gl.drawArrays(gl.TRIANGLES, 0, 6); }; /** * Clears then Draws a previously setupUnitQuad. * @param {!WebGLContext} gl The WebGLContext to use. * @param {!Array.<number>} opt_color The color to fill clear with before * drawing. A 4 element array where each element is in the range 0 to * 255. Default [255, 255, 255, 255] */ var clearAndDrawUnitQuad = function(gl, opt_color) { opt_color = opt_color || [255, 255, 255, 255]; gl.clearColor( opt_color[0] / 255, opt_color[1] / 255, opt_color[2] / 255, opt_color[3] / 255); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); drawUnitQuad(gl); }; /** * Draws a quad previsouly settup with setupIndexedQuad. * @param {!WebGLContext} gl The WebGLContext to use. * @param {number} gridRes Resolution of grid. */ var drawIndexedQuad = function(gl, gridRes) { gl.drawElements(gl.TRIANGLES, gridRes * gridRes * 6, gl.UNSIGNED_SHORT, 0); }; /** * Draws a previously setupIndexedQuad * @param {!WebGLContext} gl The WebGLContext to use. * @param {number} gridRes Resolution of grid. * @param {!Array.<number>} opt_color The color to fill clear with before * drawing. A 4 element array where each element is in the range 0 to * 255. Default [255, 255, 255, 255] */ var clearAndDrawIndexedQuad = function(gl, gridRes, opt_color) { opt_color = opt_color || [255, 255, 255, 255]; gl.clearColor( opt_color[0] / 255, opt_color[1] / 255, opt_color[2] / 255, opt_color[3] / 255); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); drawIndexedQuad(gl, gridRes); }; /** * Checks that a portion of a canvas is 1 color. * @param {!WebGLContext} gl The WebGLContext to use. * @param {number} x left corner of region to check. * @param {number} y bottom corner of region to check. * @param {number} width width of region to check. * @param {number} height width of region to check. * @param {!Array.<number>} color The color to fill clear with before drawing. A * 4 element array where each element is in the range 0 to 255. * @param {number} opt_errorRange Optional. Acceptable error in * color checking. 0 by default. * @param {!function()} sameFn Function to call if all pixels * are the same as color. * @param {!function()} differentFn Function to call if a pixel * is different than color * @param {!function()} logFn Function to call for logging. */ var checkCanvasRectColor = function(gl, x, y, width, height, color, opt_errorRange, sameFn, differentFn, logFn) { var errorRange = opt_errorRange || 0; if (!errorRange.length) { errorRange = [errorRange, errorRange, errorRange, errorRange] } var buf; if (gl instanceof WebGLRenderingContext) { buf = new Uint8Array(width * height * 4); gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf); } else { buf = gl.getImageData(x, y, width, height).data; } for (var i = 0; i < width * height; ++i) { var offset = i * 4; for (var j = 0; j < color.length; ++j) { if (Math.abs(buf[offset + j] - color[j]) > errorRange[j]) { differentFn(); var was = buf[offset + 0].toString(); for (j = 1; j < color.length; ++j) { was += "," + buf[offset + j]; } logFn('at (' + (x + (i % width)) + ', ' + (y + Math.floor(i / width)) + ') expected: ' + color + ' was ' + was); return; }