/* * 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. function initWebGL(canvasName, vshader, fshader, attribs, clearColor, clearDepth) { var canvas = document.getElementById(canvasName); return gl = WebGLUtils.setupWebGL(canvas); } function log(msg) { if (window.console && window.console.log) { window.console.log(msg); } } // 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 simpleSetup(gl, vshader, fshader, attribs, clearColor, clearDepth) { // create our shaders var vertexShader = loadShader(gl, vshader); var fragmentShader = loadShader(gl, fshader); // Create the program object var program = gl.createProgram(); // Attach our two shaders to the program gl.attachShader (program, vertexShader); gl.attachShader (program, fragmentShader); // Bind attributes for (var i = 0; i < attribs.length; ++i) gl.bindAttribLocation (program, i, attribs[i]); // Link the program gl.linkProgram(program); // Check the link status var linked = gl.getProgramParameter(program, gl.LINK_STATUS); if (!linked && !gl.isContextLost()) { // something went wrong with the link var error = gl.getProgramInfoLog (program); log("Error in program linking:"+error); gl.deleteProgram(program); gl.deleteProgram(fragmentShader); gl.deleteProgram(vertexShader); return null; } gl.useProgram(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 program; } // // loadShader // // 'shaderId' is the id of a