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 <!-- |
| 33 O3D Tutorial A1 |
| 34 |
| 35 In this tutorial, we load and display a scene in O3D. |
| 36 --> |
| 37 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
| 38 "http://www.w3.org/TR/html4/loose.dtd"> |
| 39 <html> |
| 40 <head> |
| 41 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 42 <title> |
| 43 Tutorial A1: Loading a scene |
| 44 </title> |
| 45 <!-- Include sample javascript library functions--> |
| 46 <script type="text/javascript" src="../o3d-webgl/base.js"></script> |
| 47 <script type="text/javascript" src="../o3djs/base.js"></script> |
| 48 |
| 49 <!-- Our javascript code --> |
| 50 <script type="text/javascript" id="o3dscript"> |
| 51 o3djs.base.o3d = o3d; |
| 52 o3djs.require('o3djs.webgl'); |
| 53 o3djs.require('o3djs.util'); |
| 54 o3djs.require('o3djs.math'); |
| 55 o3djs.require('o3djs.rendergraph'); |
| 56 o3djs.require('o3djs.camera'); |
| 57 o3djs.require('o3djs.pack'); |
| 58 o3djs.require('o3djs.scene'); |
| 59 |
| 60 // Events |
| 61 // init() once the page has finished loading. |
| 62 window.onload = init; |
| 63 // unload() when leaving the page. |
| 64 window.onunload = unload; |
| 65 |
| 66 // global variables |
| 67 var g_o3d; |
| 68 var g_math; |
| 69 var g_client; |
| 70 var g_viewInfo; |
| 71 var g_pack; |
| 72 var g_finished = false; // for selenium testing |
| 73 |
| 74 /** |
| 75 * Loads a scene into the transform graph. |
| 76 * @param {!o3d.Pack} pack Pack to load scene into. |
| 77 * @param {string} fileName filename of the scene. |
| 78 * @param {!o3d.Transform} parent parent node in the transform graph to |
| 79 * which to load the scene into. |
| 80 */ |
| 81 function loadScene(pack, fileName, parent) { |
| 82 // Get our full path to the scene |
| 83 var scenePath = o3djs.util.getCurrentURI() + fileName; |
| 84 |
| 85 // Load the file given the full path, and call the callback function |
| 86 // when its done loading. |
| 87 o3djs.scene.loadScene(g_client, pack, parent, scenePath, callback); |
| 88 |
| 89 /** |
| 90 * Our callback is called once the scene has been loaded into memory |
| 91 * from the web or locally. |
| 92 * @param {!o3d.Pack} pack The pack that was passed in above. |
| 93 * @param {!o3d.Transform} parent The parent that was passed in above. |
| 94 * @param {*} exception null if loading succeeded. |
| 95 */ |
| 96 function callback(pack, parent, exception) { |
| 97 if (exception) { |
| 98 alert('Could not load: ' + fileName + '\n' + exception); |
| 99 return; |
| 100 } |
| 101 // Get a CameraInfo (an object with a view and projection matrix) |
| 102 // using our javascript library function |
| 103 var cameraInfo = o3djs.camera.getViewAndProjectionFromCameras( |
| 104 parent, |
| 105 g_client.width, |
| 106 g_client.height); |
| 107 |
| 108 // Copy the view and projection to the draw context. |
| 109 g_viewInfo.drawContext.view = cameraInfo.view; |
| 110 g_viewInfo.drawContext.projection = cameraInfo.projection; |
| 111 |
| 112 // Generate draw elements and setup material draw lists. |
| 113 o3djs.pack.preparePack(pack, g_viewInfo); |
| 114 |
| 115 g_finished = true; // for selenium testing. |
| 116 } |
| 117 } |
| 118 |
| 119 /** |
| 120 * Creates the client area. |
| 121 */ |
| 122 function init() { |
| 123 o3djs.webgl.makeClients(initStep2); |
| 124 } |
| 125 |
| 126 /** |
| 127 * Remove any callbacks so they don't get called after the page has unloaded. |
| 128 */ |
| 129 function unload() { |
| 130 if (g_client) { |
| 131 g_client.cleanup(); |
| 132 } |
| 133 } |
| 134 |
| 135 /** |
| 136 * Initializes O3D and loads the scene into the transform graph. |
| 137 * @param {Array} clientElements Array of o3d object elements. |
| 138 */ |
| 139 function initStep2(clientElements) { |
| 140 // Initializes global variables and libraries. |
| 141 var o3dElement = clientElements[0]; |
| 142 g_o3d = o3dElement.o3d; |
| 143 g_math = o3djs.math; |
| 144 g_client = o3dElement.client; |
| 145 |
| 146 // Creates a pack to manage our resources/assets |
| 147 g_pack = g_client.createPack(); |
| 148 |
| 149 // Create the render graph for a view. |
| 150 g_viewInfo = o3djs.rendergraph.createBasicView( |
| 151 g_pack, |
| 152 g_client.root, |
| 153 g_client.renderGraphRoot); |
| 154 |
| 155 // Creates a transform to put our data on. |
| 156 var myDataRoot = g_pack.createObject('Transform'); |
| 157 |
| 158 // Connects our root to the client root. |
| 159 myDataRoot.parent = g_client.root; |
| 160 |
| 161 // Load the scene into the transform graph as a child myDataRoot |
| 162 loadScene(g_pack, '../assets/teapot/scene.json', myDataRoot); |
| 163 } |
| 164 </script> |
| 165 </head> |
| 166 <body> |
| 167 <h1>Loading a scene.</h1> |
| 168 This tutorial shows how we load and display a scene in O3D. |
| 169 <br/> |
| 170 <!-- Start of O3D plugin --> |
| 171 <div id="o3d" style="width: 600px; height: 600px;"></div> |
| 172 <!-- End of O3D plugin --> |
| 173 </body> |
| 174 </html> |
OLD | NEW |