aboutsummaryrefslogtreecommitdiff
path: root/js/lib/drawing
diff options
context:
space:
mode:
authorKris Kowal2012-07-09 16:38:08 -0700
committerKris Kowal2012-07-09 16:38:08 -0700
commit7bee50379c1df86bb571e0e8d6c08e24d25231f5 (patch)
tree5b11abd0414e0a3ab50ec6276b6334fbd168db7e /js/lib/drawing
parent26d4b5ce30e6e0ea6e0fde870853c1e2a673a7b4 (diff)
downloadninja-7bee50379c1df86bb571e0e8d6c08e24d25231f5.tar.gz
BSD License
Diffstat (limited to 'js/lib/drawing')
-rwxr-xr-xjs/lib/drawing/world.js89
1 files changed, 45 insertions, 44 deletions
diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js
index fb70d18c..0c11726f 100755
--- a/js/lib/drawing/world.js
+++ b/js/lib/drawing/world.js
@@ -1,24 +1,25 @@
1/* <copyright> 1/* <copyright>
2Copyright (c) 2012, Motorola Mobility, Inc 2Copyright (c) 2012, Motorola Mobility LLC.
3All Rights Reserved. 3All Rights Reserved.
4BSD License.
5 4
6Redistribution and use in source and binary forms, with or without 5Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met: 6modification, are permitted provided that the following conditions are met:
8 7
9 - Redistributions of source code must retain the above copyright notice, 8* Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer. 9 this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright 10
12 notice, this list of conditions and the following disclaimer in the 11* Redistributions in binary form must reproduce the above copyright notice,
13 documentation and/or other materials provided with the distribution. 12 this list of conditions and the following disclaimer in the documentation
14 - Neither the name of Motorola Mobility nor the names of its contributors 13 and/or other materials provided with the distribution.
15 may be used to endorse or promote products derived from this software 14
16 without specific prior written permission. 15* Neither the name of Motorola Mobility LLC nor the names of its
16 contributors may be used to endorse or promote products derived from this
17 software without specific prior written permission.
17 18
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
@@ -68,7 +69,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
68 { 69 {
69 this._2DContext = canvas.getContext( "2d" ); 70 this._2DContext = canvas.getContext( "2d" );
70 } 71 }
71 72
72 this._viewportWidth = canvas.width; 73 this._viewportWidth = canvas.width;
73 this._viewportHeight = canvas.height; 74 this._viewportHeight = canvas.height;
74 75
@@ -152,7 +153,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
152 153
153 // Flag to play/pause animation at authortime 154 // Flag to play/pause animation at authortime
154 this._previewAnimation = true; 155 this._previewAnimation = true;
155 156
156 //////////////////////////////////////////////////////////////////////////////////// 157 ////////////////////////////////////////////////////////////////////////////////////
157 // RDGE 158 // RDGE
158 // local variables 159 // local variables
@@ -175,7 +176,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
175 var camMat = Matrix.I(4); 176 var camMat = Matrix.I(4);
176 camMat[14] = this.getViewDistance(); 177 camMat[14] = this.getViewDistance();
177 this.setCameraMat( camMat ); 178 this.setCameraMat( camMat );
178 179
179 // post-load processing of the scene 180 // post-load processing of the scene
180 this.init = function() 181 this.init = function()
181 { 182 {
@@ -184,13 +185,13 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
184 if (ctx1 != ctx2) console.log( "***** different contexts *****" ); 185 if (ctx1 != ctx2) console.log( "***** different contexts *****" );
185 this.renderer = ctx1.renderer; 186 this.renderer = ctx1.renderer;
186 this.renderer._world = this; 187 this.renderer._world = this;
187 188
188 // create a camera, set its perspective, and then point it at the origin 189 // create a camera, set its perspective, and then point it at the origin
189 var cam = new RDGE.camera(); 190 var cam = new RDGE.camera();
190 this._camera = cam; 191 this._camera = cam;
191 cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar()); 192 cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar());
192 cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], RDGE.vec3.up()); 193 cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], RDGE.vec3.up());
193 194
194 // make this camera the active camera 195 // make this camera the active camera
195 this.renderer.cameraManager().setActiveCamera(cam); 196 this.renderer.cameraManager().setActiveCamera(cam);
196 197
@@ -198,61 +199,61 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
198 //this.renderer.setClearFlags(RDGE.globals.engine.getContext().DEPTH_BUFFER_BIT); 199 //this.renderer.setClearFlags(RDGE.globals.engine.getContext().DEPTH_BUFFER_BIT);
199 this.renderer.setClearColor([0.0, 0.0, 0.0, 0.0]); 200 this.renderer.setClearColor([0.0, 0.0, 0.0, 0.0]);
200 //this.renderer.NinjaWorld = this; 201 //this.renderer.NinjaWorld = this;
201 202
202 // create an empty scene graph 203 // create an empty scene graph
203 this.myScene = new RDGE.SceneGraph(); 204 this.myScene = new RDGE.SceneGraph();
204 205
205 // create some lights 206 // create some lights
206 // light 1 207 // light 1
207// this.light = RDGE.createLightNode("myLight"); 208// this.light = RDGE.createLightNode("myLight");
208// this.light.setPosition([0,0,1.2]); 209// this.light.setPosition([0,0,1.2]);
209// this.light.setDiffuseColor([0.75,0.9,1.0,1.0]); 210// this.light.setDiffuseColor([0.75,0.9,1.0,1.0]);
210 211
211 // light 2 212 // light 2
212// this.light2 = RDGE.createLightNode("myLight2"); 213// this.light2 = RDGE.createLightNode("myLight2");
213// this.light2.setPosition([-0.5,0,1.2]); 214// this.light2.setPosition([-0.5,0,1.2]);
214// this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]); 215// this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]);
215 216
216 // create a light transform 217 // create a light transform
217 var lightTr = RDGE.createTransformNode("lightTr"); 218 var lightTr = RDGE.createTransformNode("lightTr");
218 219
219 // create and attach a material - materials hold the light data 220 // create and attach a material - materials hold the light data
220 lightTr.attachMaterial(RDGE.createMaterialNode("lights")); 221 lightTr.attachMaterial(RDGE.createMaterialNode("lights"));
221 222
222 // enable light channels 1, 2 - channel 0 is used by the default shader 223 // enable light channels 1, 2 - channel 0 is used by the default shader
223// lightTr.materialNode.enableLightChannel(1, this.light); 224// lightTr.materialNode.enableLightChannel(1, this.light);
224// lightTr.materialNode.enableLightChannel(2, this.light2); 225// lightTr.materialNode.enableLightChannel(2, this.light2);
225 226
226 // all added objects are parented to the light node 227 // all added objects are parented to the light node
227 this._rootNode = lightTr; 228 this._rootNode = lightTr;
228 229
229 // add the light node to the scene 230 // add the light node to the scene
230 this.myScene.addNode(lightTr); 231 this.myScene.addNode(lightTr);
231 232
232 // Add the scene to the engine - necessary if you want the engine to draw for you 233 // Add the scene to the engine - necessary if you want the engine to draw for you
233 //RDGE.globals.engine.AddScene("myScene" + this._canvas.id, this.myScene); 234 //RDGE.globals.engine.AddScene("myScene" + this._canvas.id, this.myScene);
234 var name = this._canvas.getAttribute( "data-RDGE-id" ); 235 var name = this._canvas.getAttribute( "data-RDGE-id" );
235 RDGE.globals.engine.AddScene("myScene" + name, this.myScene); 236 RDGE.globals.engine.AddScene("myScene" + name, this.myScene);
236 }; 237 };
237 238
238 // main code for handling user interaction and updating the scene 239 // main code for handling user interaction and updating the scene
239 this.update = function(dt) 240 this.update = function(dt)
240 { 241 {
241 if (!dt) dt = 0.2; 242 if (!dt) dt = 0.2;
242 243
243 dt = 0.01; // use our own internal throttle 244 dt = 0.01; // use our own internal throttle
244 this.elapsed += dt; 245 this.elapsed += dt;
245 246
246 if (this._useWebGL) 247 if (this._useWebGL)
247 { 248 {
248 // changed the global position uniform of light 0, another way to change behavior of a light 249 // changed the global position uniform of light 0, another way to change behavior of a light
249 RDGE.rdgeGlobalParameters.u_light0Pos.set([5 * Math.cos(this.elapsed), 5 * Math.sin(this.elapsed), 20]); 250 RDGE.rdgeGlobalParameters.u_light0Pos.set([5 * Math.cos(this.elapsed), 5 * Math.sin(this.elapsed), 20]);
250 251
251 // orbit the light nodes around the boxes 252 // orbit the light nodes around the boxes
252// this.light.setPosition([1.2*Math.cos(this.elapsed*2.0), 1.2*Math.sin(this.elapsed*2.0), 1.2*Math.cos(this.elapsed*2.0)]); 253// this.light.setPosition([1.2*Math.cos(this.elapsed*2.0), 1.2*Math.sin(this.elapsed*2.0), 1.2*Math.cos(this.elapsed*2.0)]);
253// this.light2.setPosition([-1.2*Math.cos(this.elapsed*2.0), 1.2*Math.sin(this.elapsed*2.0), -1.2*Math.cos(this.elapsed)]); 254// this.light2.setPosition([-1.2*Math.cos(this.elapsed*2.0), 1.2*Math.sin(this.elapsed*2.0), -1.2*Math.cos(this.elapsed)]);
254 } 255 }
255 256
256 this.updateMaterials( this.getGeomRoot(), this.elapsed ); 257 this.updateMaterials( this.getGeomRoot(), this.elapsed );
257 258
258 // now update all the nodes in the scene 259 // now update all the nodes in the scene
@@ -260,7 +261,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
260 this.myScene.update(dt); 261 this.myScene.update(dt);
261 }; 262 };
262 263
263 // defining the draw function to control how the scene is rendered 264 // defining the draw function to control how the scene is rendered
264 this.draw = function() 265 this.draw = function()
265 { 266 {
266 if (this._useWebGL) 267 if (this._useWebGL)
@@ -306,12 +307,12 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
306 this.render(); 307 this.render();
307 } 308 }
308 }; 309 };
309 310
310 this.onRunState = function() { 311 this.onRunState = function() {
311// console.log( "GLWorld.onRunState" ); 312// console.log( "GLWorld.onRunState" );
312 this.restartRenderLoop(); 313 this.restartRenderLoop();
313 }; 314 };
314 315
315 this.onLoadState = function() { 316 this.onLoadState = function() {
316// console.log( "GLWorld.onLoadState" ); 317// console.log( "GLWorld.onLoadState" );
317 }; 318 };
@@ -375,7 +376,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
375 world.textureMapLoaded( name ); 376 world.textureMapLoaded( name );
376 } 377 }
377 }; 378 };
378 379
379 this.hasAnimatedMaterials = function() { 380 this.hasAnimatedMaterials = function() {
380 var root = this.getGeomRoot(); 381 var root = this.getGeomRoot();