| OLD | NEW |
| (Empty) | |
| 1 <!-- |
| 2 Copyright 2009, Google Inc. |
| 3 All rights reserved. |
| 4 |
| 5 Redistribution and use in source and binary forms, with or without |
| 6 modification, are permitted provided that the following conditions are |
| 7 met: |
| 8 |
| 9 * Redistributions of source code must retain the above copyright |
| 10 notice, this list of conditions and the following disclaimer. |
| 11 * Redistributions in binary form must reproduce the above |
| 12 copyright notice, this list of conditions and the following disclaimer |
| 13 in the documentation and/or other materials provided with the |
| 14 distribution. |
| 15 * Neither the name of Google Inc. nor the names of its |
| 16 contributors may be used to endorse or promote products derived from |
| 17 this software without specific prior written permission. |
| 18 |
| 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 --> |
| 31 <!-- |
| 32 Google I/O O3D Sample. |
| 33 |
| 34 This sample shows the steps to make a simple frame rate independent game. |
| 35 --> |
| 36 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
| 37 "http://www.w3.org/TR/html4/loose.dtd"> |
| 38 <html> |
| 39 <head> |
| 40 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 41 <title> |
| 42 Google I/O O3D Sample |
| 43 </title> |
| 44 <style type="text/css"> |
| 45 html, body { |
| 46 height: 100%; |
| 47 margin: 0; |
| 48 padding: 0; |
| 49 border: none; |
| 50 font-family: Arial, sans-serif; |
| 51 } |
| 52 </style> |
| 53 <!-- Include sample javascript library functions--> |
| 54 <script type="text/javascript" src="../o3djs/base.js"></script> |
| 55 |
| 56 <!-- Our javascript code --> |
| 57 <script type="text/javascript"> |
| 58 o3djs.require('o3djs.util'); |
| 59 o3djs.require('o3djs.math'); |
| 60 o3djs.require('o3djs.rendergraph'); |
| 61 o3djs.require('o3djs.primitives'); |
| 62 o3djs.require('o3djs.material'); |
| 63 |
| 64 // Events |
| 65 // init() once the page has finished loading. |
| 66 // unload() when the page is unloaded. |
| 67 window.onload = init; |
| 68 window.onunload= unload; |
| 69 |
| 70 // constants |
| 71 var MOVE_VELOCITY = 25; // in units per second. |
| 72 |
| 73 // global variables |
| 74 var g_o3dElement; |
| 75 var g_o3d; |
| 76 var g_math; |
| 77 var g_client; |
| 78 var g_viewInfo; |
| 79 var g_pack; |
| 80 var g_globalParams; |
| 81 var g_o3dWidth; |
| 82 var g_o3dHeight; |
| 83 var g_o3dElement; |
| 84 var g_keyDown = []; // which keys are down by key code. |
| 85 var g_playerTransform; |
| 86 var g_playerXPosition = 0; |
| 87 var g_playerZPosition = 0; |
| 88 var g_eye = [15, 25, 50]; |
| 89 var g_target = [0, 10, 0]; |
| 90 var g_up = [0, 1, 0]; |
| 91 var g_viewMatrix; |
| 92 |
| 93 /** |
| 94 * Updates the projection matrix. |
| 95 */ |
| 96 function updateProjection() { |
| 97 g_viewInfo.drawContext.projection = g_math.matrix4.perspective( |
| 98 g_math.degToRad(45), // field of view. |
| 99 g_o3dWidth / g_o3dHeight, // aspect ratio |
| 100 0.1, // Near plane. |
| 101 5000); // Far plane. |
| 102 } |
| 103 |
| 104 /* |
| 105 * Updates the camera. |
| 106 */ |
| 107 function updateCamera() { |
| 108 g_viewMatrix = g_math.matrix4.lookAt(g_eye, g_target, g_up); |
| 109 g_viewInfo.drawContext.view = g_viewMatrix; |
| 110 }; |
| 111 |
| 112 /** |
| 113 * Updates global variables of the client's size if they have changed. |
| 114 */ |
| 115 function updateClientSize() { |
| 116 var newWidth = g_client.width; |
| 117 var newHeight = g_client.height; |
| 118 if (g_o3dWidth != newWidth || g_o3dHeight != newHeight) { |
| 119 g_o3dWidth = newWidth; |
| 120 g_o3dHeight = newHeight; |
| 121 updateProjection(); |
| 122 } |
| 123 } |
| 124 |
| 125 /** |
| 126 * Creates the client area. |
| 127 */ |
| 128 function init() { |
| 129 o3djs.util.makeClients(initStep2); |
| 130 } |
| 131 |
| 132 /** |
| 133 * Initializes O3D and creates one shape. |
| 134 * @param {Array} clientElements Array of o3d object elements. |
| 135 */ |
| 136 function initStep2(clientElements) { |
| 137 // Initializes global variables and libraries. |
| 138 g_o3dElement = clientElements[0]; |
| 139 g_o3d = g_o3dElement.o3d; |
| 140 g_math = o3djs.math; |
| 141 g_client = g_o3dElement.client; |
| 142 |
| 143 // Creates a pack to manage our resources/assets |
| 144 g_pack = g_client.createPack(); |
| 145 |
| 146 g_root = g_pack.createObject('Transform'); |
| 147 |
| 148 g_viewInfo = o3djs.rendergraph.createBasicView( |
| 149 g_pack, |
| 150 g_root, |
| 151 g_client.renderGraphRoot); |
| 152 |
| 153 updateCamera(); |
| 154 |
| 155 var redMaterial = o3djs.material.createBasicMaterial( |
| 156 g_pack, |
| 157 g_viewInfo, |
| 158 [0.2, 1, 0.2, 1]); // green |
| 159 |
| 160 var checkerMaterial = o3djs.material.createMaterialFromFile( |
| 161 g_pack, 'shaders/checker.shader', g_viewInfo.performanceDrawList); |
| 162 |
| 163 g_globalParams = o3djs.material.createAndBindStandardParams(g_pack); |
| 164 g_globalParams.lightWorldPos.value = [30, 60, 40]; |
| 165 g_globalParams.lightColor.value = [1, 1, 1, 1]; |
| 166 |
| 167 // Create a ground plane. |
| 168 var shape = o3djs.primitives.createPlane( |
| 169 g_pack, checkerMaterial, 100, 100, 10, 10); |
| 170 var transform = g_pack.createObject('Transform'); |
| 171 transform.parent = g_root; |
| 172 transform.addShape(shape); |
| 173 |
| 174 // Create a cylinder. |
| 175 var shape = o3djs.primitives.createCylinder( |
| 176 g_pack, redMaterial, 2.5, 5, 20, 1, |
| 177 g_math.matrix4.translation([0, 2.5, 0])); |
| 178 var transform = g_pack.createObject('Transform'); |
| 179 transform.parent = g_root; |
| 180 transform.addShape(shape); |
| 181 |
| 182 g_playerTransform = transform; |
| 183 |
| 184 // Setup a render callback for per frame processing. |
| 185 g_client.setRenderCallback(onRender); |
| 186 |
| 187 o3djs.event.addEventListener(g_o3dElement, 'keydown', onKeyDown); |
| 188 o3djs.event.addEventListener(g_o3dElement, 'keyup', onKeyUp); |
| 189 |
| 190 window.g_finished = true; // for selenium testing. |
| 191 } |
| 192 |
| 193 /** |
| 194 * Tracks key down events. |
| 195 * @param {Event} e keyboard event. |
| 196 */ |
| 197 function onKeyDown(e) { |
| 198 g_keyDown[e.keyCode] = true; |
| 199 } |
| 200 |
| 201 /** |
| 202 * Tracks key up events. |
| 203 * @param {Event} e keyboard event. |
| 204 */ |
| 205 function onKeyUp(e) { |
| 206 g_keyDown[e.keyCode] = false; |
| 207 } |
| 208 |
| 209 /** |
| 210 * Look at keys. |
| 211 */ |
| 212 function handleMoveKeys(elapsedTime) { |
| 213 var directionX = 0; |
| 214 var directionZ = 0; |
| 215 |
| 216 if (g_keyDown[37] || g_keyDown[65]) { directionX = -1; } |
| 217 if (g_keyDown[39] || g_keyDown[68]) { directionX = 1; } |
| 218 if (g_keyDown[38] || g_keyDown[87]) { directionZ = -1; } |
| 219 if (g_keyDown[40] || g_keyDown[83]) { directionZ = 1; } |
| 220 |
| 221 g_playerXPosition += MOVE_VELOCITY * directionX * elapsedTime; |
| 222 g_playerZPosition += MOVE_VELOCITY * directionZ * elapsedTime; |
| 223 |
| 224 g_playerTransform.identity(); |
| 225 g_playerTransform.translate(g_playerXPosition, 0, g_playerZPosition); |
| 226 } |
| 227 |
| 228 /** |
| 229 * Called every frame. |
| 230 * @param {!o3d.RenderEvent} renderEvent Rendering Information. |
| 231 */ |
| 232 function onRender(renderEvent) { |
| 233 var elapsedTime = renderEvent.elapsedTime; |
| 234 |
| 235 updateClientSize(); |
| 236 handleMoveKeys(elapsedTime); |
| 237 }; |
| 238 |
| 239 /** |
| 240 * Remove any callbacks so they don't get called after the page has unloaded. |
| 241 */ |
| 242 function unload() { |
| 243 if (g_client) { |
| 244 g_client.cleanup(); |
| 245 } |
| 246 } |
| 247 </script> |
| 248 </head> |
| 249 <body> |
| 250 <table style="width: 100%; height:100%;"> |
| 251 <tr style="height: 50px;"><td> |
| 252 <div style="width: 100%; height: 50px; font-size: large;"> |
| 253 <img src="assets/colorbar.png" width="100%" height="10px"/><br/> |
| 254 Google I/O 2009 O3D Sample |
| 255 </div> |
| 256 </td></tr> |
| 257 <tr style="height: 100%;"><td> |
| 258 <div style="width: 100%; height: 100%;"> |
| 259 <div id="o3d" style="width: 100%; height: 100%;"></div> |
| 260 </div> |
| 261 </td></tr> |
| 262 </table> |
| 263 </body> |
| 264 </html> |
| OLD | NEW |