| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 var features = opt_features; | 70 var features = opt_features; |
| 71 if (!features) { | 71 if (!features) { |
| 72 var o3d_features = element.getAttribute('o3d_features'); | 72 var o3d_features = element.getAttribute('o3d_features'); |
| 73 if (o3d_features) { | 73 if (o3d_features) { |
| 74 features = o3d_features; | 74 features = o3d_features; |
| 75 } else { | 75 } else { |
| 76 features = ''; | 76 features = ''; |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 var objElem = o3djs.webgl.createClient(element, features, opt_debug); | 79 var objElem = o3djs.webgl.createClient(element, features, opt_debug); |
| 80 clientElements.push(objElem); | 80 if (objElem) { |
| 81 clientElements.push(objElem); |
| 82 } |
| 81 } | 83 } |
| 82 | 84 |
| 83 // Wait for the client elements to be fully initialized. This | 85 // Wait for the client elements to be fully initialized. This |
| 84 // involves waiting for the page to fully layout and the initial | 86 // involves waiting for the page to fully layout and the initial |
| 85 // resize event to be processed. | 87 // resize event to be processed. |
| 86 var clearId = window.setInterval(function() { | 88 var clearId = window.setInterval(function() { |
| 87 for (var cc = 0; cc < clientElements.length; ++cc) { | 89 for (var cc = 0; cc < clientElements.length; ++cc) { |
| 88 var element = clientElements[cc]; | 90 var element = clientElements[cc]; |
| 89 if (!element.sizeInitialized_) { | 91 if (!element.sizeInitialized_) { |
| 90 return; | 92 return; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 } | 133 } |
| 132 } | 134 } |
| 133 wrap.getError = function() { | 135 wrap.getError = function() { |
| 134 return context.getError(); | 136 return context.getError(); |
| 135 }; | 137 }; |
| 136 return wrap; | 138 return wrap; |
| 137 }; | 139 }; |
| 138 | 140 |
| 139 | 141 |
| 140 /** | 142 /** |
| 143 * Inserts text indicating that a WebGL context could not be created under |
| 144 * the given node and links to the site about WebGL capable browsers. |
| 145 */ |
| 146 o3djs.webgl.webGlCanvasError = function(parentNode, unavailableElement) { |
| 147 var background = document.createElement('div'); |
| 148 background.style.backgroundColor='#ccffff'; |
| 149 background.style.textAlign='center'; |
| 150 background.style.margin='10px'; |
| 151 |
| 152 var message = document.createElement('p'); |
| 153 var messageText = document.createTextNode( |
| 154 unavailableElement + ' unavailable. ' + |
| 155 'Make sure you are using a WebGL capable browser ' + |
| 156 'and WebGL is enabled. Click here for more information:'); |
| 157 message.appendChild(messageText); |
| 158 |
| 159 var url = 'http://www.khronos.org/webgl/wiki/Getting_a_WebGL_Implementation'; |
| 160 var link = document.createElement('a'); |
| 161 link.appendChild(document.createTextNode(url)); |
| 162 link.href = url; |
| 163 |
| 164 background.appendChild(message); |
| 165 background.appendChild(link); |
| 166 background.appendChild(document.createElement('br')); |
| 167 |
| 168 parentNode.appendChild(background); |
| 169 }; |
| 170 |
| 171 |
| 172 /** |
| 141 * Creates a canvas under the given parent element and an o3d.Client | 173 * Creates a canvas under the given parent element and an o3d.Client |
| 142 * under that. | 174 * under that. |
| 143 * | 175 * |
| 144 * @ param {!Element} element The element under which to insert the client. | 176 * @param {!Element} element The element under which to insert the client. |
| 145 * @ param {string} opt_features Features to turn on. | 177 * @param {string} opt_features Features to turn on. |
| 146 * @ param {boolean} opt_debug Whether gl debugging features should be | 178 * @param {boolean} opt_debug Whether gl debugging features should be |
| 147 * enabled. | 179 * enabled. |
| 180 * @return {HTMLCanvas} The canvas element, or null if initializaton failed. |
| 148 */ | 181 */ |
| 149 o3djs.webgl.createClient = function(element, opt_features, opt_debug) { | 182 o3djs.webgl.createClient = function(element, opt_features, opt_debug) { |
| 150 opt_features = opt_features || ''; | 183 opt_features = opt_features || ''; |
| 151 opt_debug = opt_debug || false; | 184 opt_debug = opt_debug || false; |
| 152 | 185 |
| 153 // If we're creating a webgl client, the assumption is we're using webgl, | 186 // If we're creating a webgl client, the assumption is we're using webgl, |
| 154 // in which case the only acceptable shader language is glsl. So, here | 187 // in which case the only acceptable shader language is glsl. So, here |
| 155 // we set the shader language to glsl. | 188 // we set the shader language to glsl. |
| 156 o3djs.effect.setLanguage('glsl'); | 189 o3djs.effect.setLanguage('glsl'); |
| 157 | 190 |
| 158 // Make the canvas automatically resize to fill the containing | 191 // Make the canvas automatically resize to fill the containing |
| 159 // element (div), and initialize its size correctly. | 192 // element (div), and initialize its size correctly. |
| 160 var canvas; | 193 var canvas; |
| 161 canvas = document.createElement('canvas'); | 194 canvas = document.createElement('canvas'); |
| 162 canvas.style.width = "100%"; | 195 canvas.style.width = "100%"; |
| 163 canvas.style.height = "100%"; | 196 canvas.style.height = "100%"; |
| 164 | 197 |
| 198 if (!canvas) { |
| 199 o3djs.webgl.webGlCanvasError(element, 'HTMLCanvas'); |
| 200 return null; |
| 201 } |
| 202 |
| 165 var client = new o3d.Client; | 203 var client = new o3d.Client; |
| 166 | 204 |
| 167 var resizeHandler = function() { | 205 var resizeHandler = function() { |
| 168 var width = Math.max(1, canvas.clientWidth); | 206 var width = Math.max(1, canvas.clientWidth); |
| 169 var height = Math.max(1, canvas.clientHeight); | 207 var height = Math.max(1, canvas.clientHeight); |
| 170 canvas.width = width; | 208 canvas.width = width; |
| 171 canvas.height = height; | 209 canvas.height = height; |
| 172 canvas.sizeInitialized_ = true; | 210 canvas.sizeInitialized_ = true; |
| 173 client.gl.displayInfo = {width: canvas.width, height: canvas.height}; | 211 client.gl.displayInfo = {width: canvas.width, height: canvas.height}; |
| 174 }; | 212 }; |
| 175 window.addEventListener('resize', resizeHandler, false); | 213 window.addEventListener('resize', resizeHandler, false); |
| 176 setTimeout(resizeHandler, 0); | 214 setTimeout(resizeHandler, 0); |
| 177 | 215 |
| 178 client.initWithCanvas(canvas); | 216 if (!client.initWithCanvas(canvas)) { |
| 217 o3djs.webgl.webGlCanvasError(element, 'WebGL context'); |
| 218 return null; |
| 219 } |
| 220 |
| 179 canvas.client = client; | 221 canvas.client = client; |
| 180 canvas.o3d = o3d; | 222 canvas.o3d = o3d; |
| 181 | 223 |
| 182 if (opt_debug) { | 224 if (opt_debug) { |
| 183 client.gl = o3djs.webgl.addDebuggingWrapper(client.gl); | 225 client.gl = o3djs.webgl.addDebuggingWrapper(client.gl); |
| 184 } | 226 } |
| 185 | 227 |
| 186 element.appendChild(canvas); | 228 element.appendChild(canvas); |
| 187 return canvas; | 229 return canvas; |
| 188 }; | 230 }; |
| 189 | 231 |
| 190 | 232 |
| OLD | NEW |