sawine@6: /* sawine@6: * Copyright 2010, Google Inc. sawine@6: * All rights reserved. sawine@6: * sawine@6: * Redistribution and use in source and binary forms, with or without sawine@6: * modification, are permitted provided that the following conditions are sawine@6: * met: sawine@6: * sawine@6: * * Redistributions of source code must retain the above copyright sawine@6: * notice, this list of conditions and the following disclaimer. sawine@6: * * Redistributions in binary form must reproduce the above sawine@6: * copyright notice, this list of conditions and the following disclaimer sawine@6: * in the documentation and/or other materials provided with the sawine@6: * distribution. sawine@6: * * Neither the name of Google Inc. nor the names of its sawine@6: * contributors may be used to endorse or promote products derived from sawine@6: * this software without specific prior written permission. sawine@6: * sawine@6: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS sawine@6: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT sawine@6: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR sawine@6: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT sawine@6: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sawine@6: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT sawine@6: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, sawine@6: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY sawine@6: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT sawine@6: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE sawine@6: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. sawine@6: */ sawine@6: sawine@6: sawine@6: /** sawine@6: * @fileoverview This file contains functions every webgl program will need sawine@6: * a version of one way or another. sawine@6: * sawine@6: * Instead of setting up a context manually it is recommended to sawine@6: * use. This will check for success or failure. On failure it sawine@6: * will attempt to present an approriate message to the user. sawine@6: * sawine@6: * gl = WebGLUtils.setupWebGL(canvas); sawine@6: * sawine@6: * For animated WebGL apps use of setTimeout or setInterval are sawine@6: * discouraged. It is recommended you structure your rendering sawine@6: * loop like this. sawine@6: * sawine@6: * function render() { sawine@6: * window.requestAnimFrame(render, canvas); sawine@6: * sawine@6: * // do rendering sawine@6: * ... sawine@6: * } sawine@6: * render(); sawine@6: * sawine@6: * This will call your rendering function up to the refresh rate sawine@6: * of your display but will stop rendering if your app is not sawine@6: * visible. sawine@6: */ sawine@6: sawine@6: WebGLUtils = function() { sawine@6: sawine@6: /** sawine@6: * Creates the HTLM for a failure message sawine@6: * @param {string} canvasContainerId id of container of th sawine@6: * canvas. sawine@6: * @return {string} The html. sawine@6: */ sawine@6: var makeFailHTML = function(msg) { sawine@6: return '' + sawine@6: '' + sawine@6: '
' + sawine@6: '
' + sawine@6: '
' + msg + '
' + sawine@6: '
' + sawine@6: '
'; sawine@6: }; sawine@6: sawine@6: /** sawine@6: * Mesasge for getting a webgl browser sawine@6: * @type {string} sawine@6: */ sawine@6: var GET_A_WEBGL_BROWSER = '' + sawine@6: 'This page requires a browser that supports WebGL.
' + sawine@6: 'Click here to upgrade your browser.'; sawine@6: sawine@6: /** sawine@6: * Mesasge for need better hardware sawine@6: * @type {string} sawine@6: */ sawine@6: var OTHER_PROBLEM = '' + sawine@6: "It doesn't appear your computer can support WebGL.
" + sawine@6: 'Click here for more information.'; sawine@6: sawine@6: /** sawine@6: * Creates a webgl context. If creation fails it will sawine@6: * change the contents of the container of the sawine@6: * tag to an error message with the correct links for WebGL. sawine@6: * @param {Element} canvas. The canvas element to create a sawine@6: * context from. sawine@6: * @param {WebGLContextCreationAttirbutes} opt_attribs Any sawine@6: * creation attributes you want to pass in. sawine@6: * @return {WebGLRenderingContext} The created context. sawine@6: */ sawine@6: var setupWebGL = function(canvas, opt_attribs) { sawine@6: function showLink(str) { sawine@6: var container = canvas.parentNode; sawine@6: if (container) { sawine@6: container.innerHTML = makeFailHTML(str); sawine@6: } sawine@6: }; sawine@6: sawine@6: if (!window.WebGLRenderingContext) { sawine@6: showLink(GET_A_WEBGL_BROWSER); sawine@6: return null; sawine@6: } sawine@6: sawine@6: var context = create3DContext(canvas, opt_attribs); sawine@6: if (!context) { sawine@6: showLink(OTHER_PROBLEM); sawine@6: } sawine@6: return context; sawine@6: }; sawine@6: sawine@6: /** sawine@6: * Creates a webgl context. sawine@6: * @param {!Canvas} canvas The canvas tag to get context sawine@6: * from. If one is not passed in one will be created. sawine@6: * @return {!WebGLContext} The created context. sawine@6: */ sawine@6: var create3DContext = function(canvas, opt_attribs) { sawine@6: var names = ["webgl", "experimental-webgl", "webkit-3d", "moz-webgl"]; sawine@6: var context = null; sawine@6: for (var ii = 0; ii < names.length; ++ii) { sawine@6: try { sawine@6: context = canvas.getContext(names[ii], opt_attribs); sawine@6: } catch(e) {} sawine@6: if (context) { sawine@6: break; sawine@6: } sawine@6: } sawine@6: return context; sawine@6: } sawine@6: sawine@6: return { sawine@6: create3DContext: create3DContext, sawine@6: setupWebGL: setupWebGL sawine@6: }; sawine@6: }(); sawine@6: sawine@6: /** sawine@6: * Provides requestAnimationFrame in a cross browser way. sawine@6: */ sawine@6: window.requestAnimFrame = (function() { sawine@6: return window.requestAnimationFrame || sawine@6: window.webkitRequestAnimationFrame || sawine@6: window.mozRequestAnimationFrame || sawine@6: window.oRequestAnimationFrame || sawine@6: window.msRequestAnimationFrame || sawine@6: function(/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) { sawine@6: window.setTimeout(callback, 1000/60); sawine@6: }; sawine@6: })(); sawine@6: sawine@6: