Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: samples/GoogleIO-2009/step03.html

Issue 149438: Add Google IO sample. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/o3d/
Patch Set: '' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/GoogleIO-2009/step02.html ('k') | samples/GoogleIO-2009/step04.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 <link rel="stylesheet" type="text/css" href="assets/style.css" />
45 <!-- Include sample javascript library functions-->
46 <script type="text/javascript" src="../o3djs/base.js"></script>
47
48 <!-- Our javascript code -->
49 <script type="text/javascript">
50 o3djs.require('o3djs.util');
51 o3djs.require('o3djs.math');
52 o3djs.require('o3djs.rendergraph');
53 o3djs.require('o3djs.primitives');
54 o3djs.require('o3djs.material');
55
56 // Events
57 // init() once the page has finished loading.
58 // unload() when the page is unloaded.
59 window.onload = init;
60 window.onunload= unload;
61
62 // global variables
63 var g_o3dElement;
64 var g_o3d;
65 var g_math;
66 var g_client;
67 var g_viewInfo;
68 var g_pack;
69 var g_root;
70 var g_globalParams;
71 var g_o3dWidth;
72 var g_o3dHeight;
73 var g_o3dElement;
74 var g_keyDown = []; // which keys are down by key code.
75 var g_playerTransform;
76 var g_playerXPosition = 0;
77 var g_playerZPosition = 0;
78 var g_eye = [15, 25, 50];
79 var g_target = [0, 10, 0];
80 var g_up = [0, 1, 0];
81 var g_viewMatrix;
82
83 /**
84 * Updates the projection matrix.
85 */
86 function updateProjection() {
87 g_viewInfo.drawContext.projection = g_math.matrix4.perspective(
88 g_math.degToRad(45), // field of view.
89 g_o3dWidth / g_o3dHeight, // aspect ratio
90 0.1, // Near plane.
91 5000); // Far plane.
92 }
93
94 /*
95 * Updates the camera.
96 */
97 function updateCamera() {
98 g_viewMatrix = g_math.matrix4.lookAt(g_eye, g_target, g_up);
99 g_viewInfo.drawContext.view = g_viewMatrix;
100 };
101
102 /**
103 * Updates global variables of the client's size if they have changed.
104 */
105 function updateClientSize() {
106 var newWidth = g_client.width;
107 var newHeight = g_client.height;
108 if (g_o3dWidth != newWidth || g_o3dHeight != newHeight) {
109 g_o3dWidth = newWidth;
110 g_o3dHeight = newHeight;
111 updateProjection();
112 }
113 }
114
115 /**
116 * Creates the client area.
117 */
118 function init() {
119 o3djs.util.makeClients(initStep2);
120 }
121
122 /**
123 * Initializes O3D and creates one shape.
124 * @param {Array} clientElements Array of o3d object elements.
125 */
126 function initStep2(clientElements) {
127 // Initializes global variables and libraries.
128 g_o3dElement = clientElements[0];
129 g_o3d = g_o3dElement.o3d;
130 g_math = o3djs.math;
131 g_client = g_o3dElement.client;
132
133 // Creates a pack to manage our resources/assets
134 g_pack = g_client.createPack();
135
136 g_root = g_pack.createObject('Transform');
137
138 g_viewInfo = o3djs.rendergraph.createBasicView(
139 g_pack,
140 g_root,
141 g_client.renderGraphRoot);
142
143 updateCamera();
144
145 var redMaterial = o3djs.material.createBasicMaterial(
146 g_pack,
147 g_viewInfo,
148 [0.2, 1, 0.2, 1]); // green
149
150 var checkerMaterial = o3djs.material.createMaterialFromFile(
151 g_pack, 'shaders/checker.shader', g_viewInfo.performanceDrawList);
152
153 g_globalParams = o3djs.material.createAndBindStandardParams(g_pack);
154 g_globalParams.lightWorldPos.value = [30, 60, 40];
155 g_globalParams.lightColor.value = [1, 1, 1, 1];
156
157 // Create a ground plane.
158 var shape = o3djs.primitives.createPlane(
159 g_pack, checkerMaterial, 100, 100, 10, 10);
160 var transform = g_pack.createObject('Transform');
161 transform.parent = g_root;
162 transform.addShape(shape);
163
164 // Create a cylinder.
165 var shape = o3djs.primitives.createCylinder(
166 g_pack, redMaterial, 2.5, 5, 20, 1,
167 g_math.matrix4.translation([0, 2.5, 0]));
168 var transform = g_pack.createObject('Transform');
169 transform.parent = g_root;
170 transform.addShape(shape);
171
172 g_playerTransform = transform;
173
174 // Setup a render callback for per frame processing.
175 g_client.setRenderCallback(onRender);
176
177 o3djs.event.addEventListener(g_o3dElement, 'keydown', onKeyDown);
178 o3djs.event.addEventListener(g_o3dElement, 'keyup', onKeyUp);
179
180 window.g_finished = true; // for selenium testing.
181 }
182
183 /**
184 * Tracks key down events.
185 * @param {Event} e keyboard event.
186 */
187 function onKeyDown(e) {
188 g_keyDown[e.keyCode] = true;
189 }
190
191 /**
192 * Tracks key up events.
193 * @param {Event} e keyboard event.
194 */
195 function onKeyUp(e) {
196 g_keyDown[e.keyCode] = false;
197 }
198
199 /**
200 * Look at keys.
201 */
202 function handleMoveKeys() {
203 var directionX = 0;
204 var directionZ = 0;
205
206 if (g_keyDown[37] || g_keyDown[65]) { directionX = -3; }
207 if (g_keyDown[39] || g_keyDown[68]) { directionX = 3; }
208 if (g_keyDown[38] || g_keyDown[87]) { directionZ = -3; }
209 if (g_keyDown[40] || g_keyDown[83]) { directionZ = 3; }
210
211 g_playerXPosition += directionX;
212 g_playerZPosition += directionZ;
213
214 g_playerTransform.identity();
215 g_playerTransform.translate(g_playerXPosition, 0, g_playerZPosition);
216 }
217
218 /**
219 * Called every frame.
220 * @param {!o3d.RenderEvent} renderEvent Rendering Information.
221 */
222 function onRender(renderEvent) {
223 var elapsedTime = renderEvent.elapsedTime;
224
225 updateClientSize();
226 handleMoveKeys();
227 };
228
229 /**
230 * Remove any callbacks so they don't get called after the page has unloaded.
231 */
232 function unload() {
233 if (g_client) {
234 g_client.cleanup();
235 }
236 }
237 </script>
238 </head>
239 <body>
240 <table style="width: 100%; height:100%;">
241 <tr style="height: 50px;"><td>
242 <div style="width: 100%; height: 50px; font-size: large;">
243 <img src="assets/colorbar.png" width="100%" height="10px"/><br/>
244 Google I/O 2009 O3D Sample
245 </div>
246 </td></tr>
247 <tr style="height: 100%;"><td>
248 <div style="width: 100%; height: 100%;">
249 <div id="o3d" style="width: 100%; height: 100%;"></div>
250 </div>
251 </td></tr>
252 </table>
253 </body>
254 </html>
OLDNEW
« no previous file with comments | « samples/GoogleIO-2009/step02.html ('k') | samples/GoogleIO-2009/step04.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698