aboutsummaryrefslogtreecommitdiff
path: root/js/lib/drawing/world.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/drawing/world.js')
-rwxr-xr-xjs/lib/drawing/world.js295
1 files changed, 131 insertions, 164 deletions
diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js
index df24f556..945c9883 100755
--- a/js/lib/drawing/world.js
+++ b/js/lib/drawing/world.js
@@ -117,6 +117,8 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
117 117
118 this.getRenderer = function() { return this.renderer; }; 118 this.getRenderer = function() { return this.renderer; };
119 119
120 // Flag to play/pause animation at authortime
121 this._previewAnimation = true;
120 //////////////////////////////////////////////////////////////////////////////////// 122 ////////////////////////////////////////////////////////////////////////////////////
121 // RDGE 123 // RDGE
122 // local variables 124 // local variables
@@ -142,45 +144,45 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
142 144
143 // post-load processing of the scene 145 // post-load processing of the scene
144 this.init = function() { 146 this.init = function() {
145 var ctx1 = g_Engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle), 147 var ctx1 = RDGE.globals.engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle),
146 ctx2 = g_Engine.getContext(); 148 ctx2 = RDGE.globals.engine.getContext();
147 if (ctx1 != ctx2) console.log( "***** different contexts *****" ); 149 if (ctx1 != ctx2) console.log( "***** different contexts *****" );
148 this.renderer = ctx1.renderer; 150 this.renderer = ctx1.renderer;
149 this.renderer._world = this; 151 this.renderer._world = this;
150 152
151 // create a camera, set its perspective, and then point it at the origin 153 // create a camera, set its perspective, and then point it at the origin
152 var cam = new camera(); 154 var cam = new RDGE.camera();
153 this._camera = cam; 155 this._camera = cam;
154 cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar()); 156 cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar());
155 cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], vec3.up()); 157 cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], RDGE.vec3.up());
156 158
157 // make this camera the active camera 159 // make this camera the active camera
158 this.renderer.cameraManager().setActiveCamera(cam); 160 this.renderer.cameraManager().setActiveCamera(cam);
159 161
160 // change clear color 162 // change clear color
161 //this.renderer.setClearFlags(g_Engine.getContext().DEPTH_BUFFER_BIT); 163 //this.renderer.setClearFlags(RDGE.globals.engine.getContext().DEPTH_BUFFER_BIT);
162 this.renderer.setClearColor([0.0, 0.0, 0.0, 0.0]); 164 this.renderer.setClearColor([0.0, 0.0, 0.0, 0.0]);
163 //this.renderer.NinjaWorld = this; 165 //this.renderer.NinjaWorld = this;
164 166
165 // create an empty scene graph 167 // create an empty scene graph
166 this.myScene = new SceneGraph(); 168 this.myScene = new RDGE.SceneGraph();
167 169
168 // create some lights 170 // create some lights
169 // light 1 171 // light 1
170 this.light = createLightNode("myLight"); 172 this.light = RDGE.createLightNode("myLight");
171 this.light.setPosition([0,0,1.2]); 173 this.light.setPosition([0,0,1.2]);
172 this.light.setDiffuseColor([0.75,0.9,1.0,1.0]); 174 this.light.setDiffuseColor([0.75,0.9,1.0,1.0]);
173 175
174 // light 2 176 // light 2
175 this.light2 = createLightNode("myLight2"); 177 this.light2 = RDGE.createLightNode("myLight2");
176 this.light2.setPosition([-0.5,0,1.2]); 178 this.light2.setPosition([-0.5,0,1.2]);
177 this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]); 179 this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]);
178 180
179 // create a light transform 181 // create a light transform
180 var lightTr = createTransformNode("lightTr"); 182 var lightTr = RDGE.createTransformNode("lightTr");
181 183
182 // create and attach a material - materials hold the light data 184 // create and attach a material - materials hold the light data
183 lightTr.attachMaterial(createMaterialNode("lights")); 185 lightTr.attachMaterial(RDGE.createMaterialNode("lights"));
184 186
185 // enable light channels 1, 2 - channel 0 is used by the default shader 187 // enable light channels 1, 2 - channel 0 is used by the default shader
186 lightTr.materialNode.enableLightChannel(1, this.light); 188 lightTr.materialNode.enableLightChannel(1, this.light);
@@ -193,9 +195,9 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
193 this.myScene.addNode(lightTr); 195 this.myScene.addNode(lightTr);
194 196
195 // Add the scene to the engine - necessary if you want the engine to draw for you 197 // Add the scene to the engine - necessary if you want the engine to draw for you
196 //g_Engine.AddScene("myScene" + this._canvas.id, this.myScene); 198 //RDGE.globals.engine.AddScene("myScene" + this._canvas.id, this.myScene);
197 var name = this._canvas.getAttribute( "data-RDGE-id" ); 199 var name = this._canvas.getAttribute( "data-RDGE-id" );
198 g_Engine.AddScene("myScene" + name, this.myScene); 200 RDGE.globals.engine.AddScene("myScene" + name, this.myScene);
199 }; 201 };
200 202
201 // main code for handling user interaction and updating the scene 203 // main code for handling user interaction and updating the scene
@@ -207,7 +209,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
207 209
208 if (this._useWebGL) { 210 if (this._useWebGL) {
209 // changed the global position uniform of light 0, another way to change behavior of a light 211 // changed the global position uniform of light 0, another way to change behavior of a light
210 rdgeGlobalParameters.u_light0Pos.set( [5*Math.cos(this.elapsed), 5*Math.sin(this.elapsed), 20]); 212 RDGE.rdgeGlobalParameters.u_light0Pos.set([5 * Math.cos(this.elapsed), 5 * Math.sin(this.elapsed), 20]);
211 213
212 // orbit the light nodes around the boxes 214 // orbit the light nodes around the boxes
213 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)]); 215 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)]);
@@ -224,8 +226,8 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
224 // defining the draw function to control how the scene is rendered 226 // defining the draw function to control how the scene is rendered
225 this.draw = function() { 227 this.draw = function() {
226 if (this._useWebGL) { 228 if (this._useWebGL) {
227 g_Engine.setContext( this._canvas.rdgeid ); 229 RDGE.globals.engine.setContext( this._canvas.rdgeid );
228 var ctx = g_Engine.getContext(); 230 var ctx = RDGE.globals.engine.getContext();
229 var renderer = ctx.renderer; 231 var renderer = ctx.renderer;
230 if (renderer.unloadedTextureCount <= 0) { 232 if (renderer.unloadedTextureCount <= 0) {
231 renderer.disableCulling(); 233 renderer.disableCulling();
@@ -236,7 +238,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
236 if (this._canvas.task) { 238 if (this._canvas.task) {
237 this._firstRender = false; 239 this._firstRender = false;
238 240
239 if (!this.hasAnimatedMaterials()) { 241 if (!this.hasAnimatedMaterials() || !this._previewAnimation) {
240 this._canvas.task.stop(); 242 this._canvas.task.stop();
241 //this._renderCount = 10; 243 //this._renderCount = 10;
242 } 244 }
@@ -351,22 +353,21 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
351 return false; 353 return false;
352 }; 354 };
353 355
354 this.generateUniqueNodeID = function() 356 this.generateUniqueNodeID = function() {
355 {
356 var str = "" + this._nodeCounter; 357 var str = "" + this._nodeCounter;
357 this._nodeCounter++; 358 this._nodeCounter++;
358 return str; 359 return str;
359 } 360 };
360 361
361 362
362 // start RDGE passing your runtime object, and false to indicate we don't need a an initialization state 363 // start RDGE passing your runtime object, and false to indicate we don't need a an initialization state
363 // in the case of a procedurally built scene an init state is not needed for loading data 364 // in the case of a procedurally built scene an init state is not needed for loading data
364 if (this._useWebGL) { 365 if (this._useWebGL) {
365 rdgeStarted = true; 366 rdgeStarted = true;
366 var id = this._canvas.getAttribute( "data-RDGE-id" ); 367 this._canvas.rdgeid = this._canvas.getAttribute( "data-RDGE-id" );
367 this._canvas.rdgeid = id; 368 RDGE.globals.engine.unregisterCanvas( this._canvas );
368 g_Engine.registerCanvas(this._canvas, this); 369 RDGE.globals.engine.registerCanvas(this._canvas, this);
369 RDGEStart( this._canvas ); 370 RDGE.RDGEStart( this._canvas );
370 this._canvas.task.stop() 371 this._canvas.task.stop()
371 } 372 }
372}; 373};
@@ -395,13 +396,13 @@ World.prototype.updateObject = function (obj) {
395 if (nPrims > 0) { 396 if (nPrims > 0) {
396 ctrTrNode = obj.getTransformNode(); 397 ctrTrNode = obj.getTransformNode();
397 if (ctrTrNode == null) { 398 if (ctrTrNode == null) {
398 ctrTrNode = createTransformNode("objRootNode_" + this._nodeCounter++); 399 ctrTrNode = RDGE.createTransformNode("objRootNode_" + this._nodeCounter++);
399 this._rootNode.insertAsChild( ctrTrNode ); 400 this._rootNode.insertAsChild( ctrTrNode );
400 obj.setTransformNode( ctrTrNode ); 401 obj.setTransformNode( ctrTrNode );
401 } 402 }
402 403
403 ctrTrNode.meshes.forEach(function(thisMesh) { 404 ctrTrNode.meshes.forEach(function(thisMesh) {
404 g_meshMan.deleteMesh(thisMesh.mesh.name); 405 RDGE.globals.meshMan.deleteMesh(thisMesh.mesh.name);
405 }); 406 });
406 ctrTrNode.meshes = []; 407 ctrTrNode.meshes = [];
407 408
@@ -420,11 +421,11 @@ World.prototype.updateObject = function (obj) {
420 childTrNode = children[i-1].transformNode; 421 childTrNode = children[i-1].transformNode;
421 422
422 childTrNode.meshes.forEach(function(thisMesh) { 423 childTrNode.meshes.forEach(function(thisMesh) {
423 g_meshMan.deleteMesh(thisMesh.mesh.name); 424 RDGE.globals.meshMan.deleteMesh(thisMesh.mesh.name);
424 }); 425 });
425 childTrNode.meshes = []; 426 childTrNode.meshes = [];
426 } else { 427 } else {
427 childTrNode = createTransformNode("objNode_" + this._nodeCounter++); 428 childTrNode = RDGE.createTransformNode("objNode_" + this._nodeCounter++);
428 ctrTrNode.insertAsChild(childTrNode); 429 ctrTrNode.insertAsChild(childTrNode);
429 } 430 }
430 431
@@ -465,7 +466,7 @@ World.prototype.addObject = function( obj ) {
465 catch(e) { 466 catch(e) {
466 alert( "Exception in GLWorld.addObject " + e ); 467 alert( "Exception in GLWorld.addObject " + e );
467 } 468 }
468} 469};
469 470
470World.prototype.restartRenderLoop = function() { 471World.prototype.restartRenderLoop = function() {
471 //console.log( "restartRenderLoop" ); 472 //console.log( "restartRenderLoop" );
@@ -526,7 +527,7 @@ World.prototype.clearTree = function() {
526 if (this._useWebGL) { 527 if (this._useWebGL) {
527 var root = this._rootNode; 528 var root = this._rootNode;
528 root.children = new Array(); 529 root.children = new Array();
529 g_Engine.unregisterCanvas( this._canvas.rdgeid ) 530 RDGE.globals.engine.unregisterCanvas( this._canvas.rdgeid );
530 531
531 this.update( 0 ); 532 this.update( 0 );
532 this.draw(); 533 this.draw();
@@ -539,9 +540,10 @@ World.prototype.updateMaterials = function( obj, time ) {