/* * Copyright (C) 2009 Apple Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // // initWebGL // // Initialize the Canvas element with the passed name as a WebGL object and return the // WebGLRenderingContext. // // Load shaders with the passed names and create a program with them. Return this program // in the 'program' property of the returned context. // // For each string in the passed attribs array, bind an attrib with that name at that index. // Once the attribs are bound, link the program and then use it. // // Set the clear color to the passed array (4 values) and set the clear depth to the passed value. // Enable depth testing and blending with a blend func of (SRC_ALPHA, ONE_MINUS_SRC_ALPHA) // // A console function is added to the context: console(string). This can be replaced // by the caller. By default, it maps to the window.console() function on WebKit and to // an empty function on other browsers. // function initWebGL(canvasName, vshader, fshader, attribs, clearColor, clearDepth) { var canvas = document.getElementById(canvasName); var gl = canvas.getContext("experimental-webgl"); if (!gl) { alert("No WebGL context found"); return null; } // Add a console gl.console = ("console" in window) ? window.console : { log: function() { } }; // create our shaders var vertexShader = loadShader(gl, vshader); var fragmentShader = loadShader(gl, fshader); if (!vertexShader || !fragmentShader) return null; // Create the program object gl.program = gl.createProgram(); if (!gl.program) return null; // Attach our two shaders to the program gl.attachShader (gl.program, vertexShader); gl.attachShader (gl.program, fragmentShader); // Bind attributes for (var i in attribs) gl.bindAttribLocation (gl.program, i, attribs[i]); // Link the program gl.linkProgram(gl.program); // Check the link status var linked = gl.getProgramParameter(gl.program, gl.LINK_STATUS); if (!linked) { // something went wrong with the link var error = gl.getProgramInfoLog (gl.program); gl.console.log("Error in program linking:"+error); gl.deleteProgram(gl.program); gl.deleteProgram(fragmentShader); gl.deleteProgram(vertexShader); return null; } gl.useProgram(gl.program); gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); gl.clearDepth(clearDepth); // gl.enable(gl.DEPTH_TEST); gl.enable(gl.BLEND); gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); return gl; } // // loadShader // // 'shaderId' is the id of a