michael@0: /* michael@0: Utilities for the OpenGL ES 2.0 HTML Canvas context michael@0: michael@0: Copyright (C) 2011 Ilmari Heikkinen michael@0: michael@0: Permission is hereby granted, free of charge, to any person michael@0: obtaining a copy of this software and associated documentation michael@0: files (the "Software"), to deal in the Software without michael@0: restriction, including without limitation the rights to use, michael@0: copy, modify, merge, publish, distribute, sublicense, and/or sell michael@0: copies of the Software, and to permit persons to whom the michael@0: Software is furnished to do so, subject to the following michael@0: conditions: michael@0: michael@0: The above copyright notice and this permission notice shall be michael@0: included in all copies or substantial portions of the Software. michael@0: michael@0: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, michael@0: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES michael@0: OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND michael@0: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT michael@0: HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, michael@0: WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING michael@0: FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR michael@0: OTHER DEALINGS IN THE SOFTWARE. michael@0: */ michael@0: michael@0: function loadTexture(gl, elem, mipmaps) { michael@0: var tex = gl.createTexture(); michael@0: gl.bindTexture(gl.TEXTURE_2D, tex); michael@0: gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, elem); michael@0: if (mipmaps != false) michael@0: gl.generateMipmap(gl.TEXTURE_2D); michael@0: gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); michael@0: gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); michael@0: gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); michael@0: if (mipmaps) michael@0: gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR); michael@0: else michael@0: gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); michael@0: return tex; michael@0: } michael@0: michael@0: function getShader(gl, id) { michael@0: var shaderScript = document.getElementById(id); michael@0: if (!shaderScript) { michael@0: throw(new Error("No shader element with id: "+id)); michael@0: } michael@0: michael@0: var str = ""; michael@0: var k = shaderScript.firstChild; michael@0: while (k) { michael@0: if (k.nodeType == 3) michael@0: str += k.textContent; michael@0: k = k.nextSibling; michael@0: } michael@0: michael@0: var shader; michael@0: if (shaderScript.type == "x-shader/x-fragment") { michael@0: shader = gl.createShader(gl.FRAGMENT_SHADER); michael@0: } else if (shaderScript.type == "x-shader/x-vertex") { michael@0: shader = gl.createShader(gl.VERTEX_SHADER); michael@0: } else { michael@0: throw(new Error("Unknown shader type "+shaderScript.type)); michael@0: } michael@0: michael@0: gl.shaderSource(shader, str); michael@0: gl.compileShader(shader); michael@0: michael@0: if (gl.getShaderParameter(shader, gl.COMPILE_STATUS) != 1) { michael@0: var ilog = gl.getShaderInfoLog(shader); michael@0: gl.deleteShader(shader); michael@0: throw(new Error("Failed to compile shader "+shaderScript.id + ", Shader info log: " + ilog)); michael@0: } michael@0: return shader; michael@0: } michael@0: michael@0: function loadShaderArray(gl, shaders) { michael@0: var id = gl.createProgram(); michael@0: var shaderObjs = []; michael@0: for (var i=0; i