diff options
Diffstat (limited to 'o3d')
-rw-r--r-- | o3d/samples/o3d-webgl/client.js | 37 | ||||
-rw-r--r-- | o3d/samples/o3djs/webgl.js | 52 |
2 files changed, 78 insertions, 11 deletions
diff --git a/o3d/samples/o3d-webgl/client.js b/o3d/samples/o3d-webgl/client.js index b580215..cef816b 100644 --- a/o3d/samples/o3d-webgl/client.js +++ b/o3d/samples/o3d-webgl/client.js @@ -192,6 +192,15 @@ o3d.ClientInfo.prototype.software_renderer = false; o3d.ClientInfo.prototype.non_power_of_two_textures = true; + +/** + * True if shaders need to be GLSL instead of Cg/HLSL. + * In this implementation of o3d, that is true by default. + **/ +o3d.ClientInfo.prototype.glsl = true; + + + /** * The Client class is the main point of entry to O3D. It defines methods * for creating and deleting packs. Each new object created by the Client is @@ -209,6 +218,7 @@ o3d.Client = function() { this.renderGraphRoot = tempPack.createObject('RenderNode'); this.clientId = o3d.Client.nextId++; this.packs_ = [tempPack]; + this.clientInfo = tempPack.createObject('ClientInfo'); if (o3d.Renderer.clients_.length == 0) o3d.Renderer.installRenderInterval(); @@ -574,6 +584,7 @@ o3d.Client.prototype.__defineSetter__('height', /** * Initializes this client using the canvas. * @param {Canvas} + * @return {boolean} Success. */ o3d.Client.prototype.initWithCanvas = function(canvas) { var gl; @@ -586,16 +597,22 @@ o3d.Client.prototype.initWithCanvas = function(canvas) { premultipliedAlpha : true }; + if (!canvas || !canvas.getContext) { + return false; + } try {gl = canvas.getContext("experimental-webgl", standard_attributes) } catch(e) { } - if (!gl) - try {gl = canvas.getContext("moz-webgl") } catch(e) { } if (!gl) { - alert("No WebGL context found"); - return null; + try { + gl = canvas.getContext("moz-webgl") + } catch(e) { } } - // TODO(petersont): hack workaround for WebGLRenderingContext.canvas - // not being implemented in Firefox. Remove. + if (!gl) { + return false; + } + + // TODO(petersont): Remove this workaround once WebGLRenderingContext.canvas + // is implemented in Firefox. gl.hack_canvas = canvas; this.gl = gl; this.root.gl = gl; @@ -604,6 +621,8 @@ o3d.Client.prototype.initWithCanvas = function(canvas) { gl.client = this; gl.displayInfo = {width: canvas.width, height: canvas.height}; + + return true; }; @@ -1076,6 +1095,12 @@ o3d.Client.prototype.clientId = 0; /** + * Gets info about the client. + */ +o3d.Client.prototype.clientInfo = null; + + +/** * The canvas associated with this client. * @type {Element} */ diff --git a/o3d/samples/o3djs/webgl.js b/o3d/samples/o3djs/webgl.js index c0594af..7f979ee 100644 --- a/o3d/samples/o3djs/webgl.js +++ b/o3d/samples/o3djs/webgl.js @@ -77,7 +77,9 @@ o3djs.webgl.makeClients = function(callback, } } var objElem = o3djs.webgl.createClient(element, features, opt_debug); - clientElements.push(objElem); + if (objElem) { + clientElements.push(objElem); + } } // Wait for the client elements to be fully initialized. This @@ -138,13 +140,44 @@ o3djs.webgl.addDebuggingWrapper = function(context) { /** + * Inserts text indicating that a WebGL context could not be created under + * the given node and links to the site about WebGL capable browsers. + */ +o3djs.webgl.webGlCanvasError = function(parentNode, unavailableElement) { + var background = document.createElement('div'); + background.style.backgroundColor='#ccffff'; + background.style.textAlign='center'; + background.style.margin='10px'; + + var message = document.createElement('p'); + var messageText = document.createTextNode( + unavailableElement + ' unavailable. ' + + 'Make sure you are using a WebGL capable browser ' + + 'and WebGL is enabled. Click here for more information:'); + message.appendChild(messageText); + + var url = 'http://www.khronos.org/webgl/wiki/Getting_a_WebGL_Implementation'; + var link = document.createElement('a'); + link.appendChild(document.createTextNode(url)); + link.href = url; + + background.appendChild(message); + background.appendChild(link); + background.appendChild(document.createElement('br')); + + parentNode.appendChild(background); +}; + + +/** * Creates a canvas under the given parent element and an o3d.Client * under that. * - * @ param {!Element} element The element under which to insert the client. - * @ param {string} opt_features Features to turn on. - * @ param {boolean} opt_debug Whether gl debugging features should be + * @param {!Element} element The element under which to insert the client. + * @param {string} opt_features Features to turn on. + * @param {boolean} opt_debug Whether gl debugging features should be * enabled. + * @return {HTMLCanvas} The canvas element, or null if initializaton failed. */ o3djs.webgl.createClient = function(element, opt_features, opt_debug) { opt_features = opt_features || ''; @@ -162,6 +195,11 @@ o3djs.webgl.createClient = function(element, opt_features, opt_debug) { canvas.style.width = "100%"; canvas.style.height = "100%"; + if (!canvas) { + o3djs.webgl.webGlCanvasError(element, 'HTMLCanvas'); + return null; + } + var client = new o3d.Client; var resizeHandler = function() { @@ -175,7 +213,11 @@ o3djs.webgl.createClient = function(element, opt_features, opt_debug) { window.addEventListener('resize', resizeHandler, false); setTimeout(resizeHandler, 0); - client.initWithCanvas(canvas); + if (!client.initWithCanvas(canvas)) { + o3djs.webgl.webGlCanvasError(element, 'WebGL context'); + return null; + } + canvas.client = client; canvas.o3d = o3d; |