aboutsummaryrefslogtreecommitdiff
path: root/js/lib/drawing/world.js
diff options
context:
space:
mode:
authorValerio Virgillito2012-04-04 15:58:49 -0700
committerValerio Virgillito2012-04-04 15:58:49 -0700
commit347b4179be5581841fc94825dcb18901e625c670 (patch)
tree93556d56b049aa47c615a8b58add230cd96da166 /js/lib/drawing/world.js
parent9f1806a7134ec4744a8009e6a7467f5cea04cabf (diff)
parente721a7c1009f298a1bd8fea583da14535e039880 (diff)
downloadninja-347b4179be5581841fc94825dcb18901e625c670.tar.gz
Merge branch 'refs/heads/master' into components
Diffstat (limited to 'js/lib/drawing/world.js')
-rwxr-xr-xjs/lib/drawing/world.js290
1 files changed, 46 insertions, 244 deletions
diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js
index e348f5e8..657c849f 100755
--- a/js/lib/drawing/world.js
+++ b/js/lib/drawing/world.js
@@ -144,45 +144,45 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
144 144
145 // post-load processing of the scene 145 // post-load processing of the scene
146 this.init = function() { 146 this.init = function() {
147 var ctx1 = g_Engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle), 147 var ctx1 = RDGE.globals.engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle),
148 ctx2 = g_Engine.getContext(); 148 ctx2 = RDGE.globals.engine.getContext();
149 if (ctx1 != ctx2) console.log( "***** different contexts *****" ); 149 if (ctx1 != ctx2) console.log( "***** different contexts *****" );
150 this.renderer = ctx1.renderer; 150 this.renderer = ctx1.renderer;
151 this.renderer._world = this; 151 this.renderer._world = this;
152 152
153 // 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
154 var cam = new camera(); 154 var cam = new RDGE.camera();
155 this._camera = cam; 155 this._camera = cam;
156 cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar()); 156 cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar());
157 cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], vec3.up()); 157 cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], RDGE.vec3.up());
158 158
159 // make this camera the active camera 159 // make this camera the active camera
160 this.renderer.cameraManager().setActiveCamera(cam); 160 this.renderer.cameraManager().setActiveCamera(cam);
161 161
162 // change clear color 162 // change clear color
163 //this.renderer.setClearFlags(g_Engine.getContext().DEPTH_BUFFER_BIT); 163 //this.renderer.setClearFlags(RDGE.globals.engine.getContext().DEPTH_BUFFER_BIT);
164 this.renderer.setClearColor([0.0, 0.0, 0.0, 0.0]); 164 this.renderer.setClearColor([0.0, 0.0, 0.0, 0.0]);
165 //this.renderer.NinjaWorld = this; 165 //this.renderer.NinjaWorld = this;
166 166
167 // create an empty scene graph 167 // create an empty scene graph
168 this.myScene = new SceneGraph(); 168 this.myScene = new RDGE.SceneGraph();
169 169
170 // create some lights 170 // create some lights
171 // light 1 171 // light 1
172 this.light = createLightNode("myLight"); 172 this.light = RDGE.createLightNode("myLight");
173 this.light.setPosition([0,0,1.2]); 173 this.light.setPosition([0,0,1.2]);
174 this.light.setDiffuseColor([0.75,0.9,1.0,1.0]); 174 this.light.setDiffuseColor([0.75,0.9,1.0,1.0]);
175 175
176 // light 2 176 // light 2
177 this.light2 = createLightNode("myLight2"); 177 this.light2 = RDGE.createLightNode("myLight2");
178 this.light2.setPosition([-0.5,0,1.2]); 178 this.light2.setPosition([-0.5,0,1.2]);
179 this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]); 179 this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]);
180 180
181 // create a light transform 181 // create a light transform
182 var lightTr = createTransformNode("lightTr"); 182 var lightTr = RDGE.createTransformNode("lightTr");
183 183
184 // create and attach a material - materials hold the light data 184 // create and attach a material - materials hold the light data
185 lightTr.attachMaterial(createMaterialNode("lights")); 185 lightTr.attachMaterial(RDGE.createMaterialNode("lights"));
186 186
187 // 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
188 lightTr.materialNode.enableLightChannel(1, this.light); 188 lightTr.materialNode.enableLightChannel(1, this.light);
@@ -195,9 +195,9 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
195 this.myScene.addNode(lightTr); 195 this.myScene.addNode(lightTr);
196 196
197 // 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
198 //g_Engine.AddScene("myScene" + this._canvas.id, this.myScene); 198 //RDGE.globals.engine.AddScene("myScene" + this._canvas.id, this.myScene);
199 var name = this._canvas.getAttribute( "data-RDGE-id" ); 199 var name = this._canvas.getAttribute( "data-RDGE-id" );
200 g_Engine.AddScene("myScene" + name, this.myScene); 200 RDGE.globals.engine.AddScene("myScene" + name, this.myScene);
201 }; 201 };
202 202
203 // main code for handling user interaction and updating the scene 203 // main code for handling user interaction and updating the scene
@@ -209,7 +209,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
209 209
210 if (this._useWebGL) { 210 if (this._useWebGL) {
211 // 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
212 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]);
213 213
214 // orbit the light nodes around the boxes 214 // orbit the light nodes around the boxes
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)]); 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)]);
@@ -226,8 +226,8 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
226 // defining the draw function to control how the scene is rendered 226 // defining the draw function to control how the scene is rendered
227 this.draw = function() { 227 this.draw = function() {
228 if (this._useWebGL) { 228 if (this._useWebGL) {
229 g_Engine.setContext( this._canvas.rdgeid ); 229 RDGE.globals.engine.setContext( this._canvas.rdgeid );
230 var ctx = g_Engine.getContext(); 230 var ctx = RDGE.globals.engine.getContext();
231 var renderer = ctx.renderer; 231 var renderer = ctx.renderer;
232 if (renderer.unloadedTextureCount <= 0) { 232 if (renderer.unloadedTextureCount <= 0) {
233 renderer.disableCulling(); 233 renderer.disableCulling();
@@ -365,9 +365,9 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
365 if (this._useWebGL) { 365 if (this._useWebGL) {
366 rdgeStarted = true; 366 rdgeStarted = true;
367 this._canvas.rdgeid = this._canvas.getAttribute( "data-RDGE-id" ); 367 this._canvas.rdgeid = this._canvas.getAttribute( "data-RDGE-id" );
368 g_Engine.unregisterCanvas( this._canvas ) 368 RDGE.globals.engine.unregisterCanvas( this._canvas );
369 g_Engine.registerCanvas(this._canvas, this); 369 RDGE.globals.engine.registerCanvas(this._canvas, this);
370 RDGEStart( this._canvas ); 370 RDGE.RDGEStart( this._canvas );
371 this._canvas.task.stop() 371 this._canvas.task.stop()
372 } 372 }
373}; 373};
@@ -396,13 +396,13 @@ World.prototype.updateObject = function (obj) {
396 if (nPrims > 0) { 396 if (nPrims > 0) {
397 ctrTrNode = obj.getTransformNode(); 397 ctrTrNode = obj.getTransformNode();
398 if (ctrTrNode == null) { 398 if (ctrTrNode == null) {
399 ctrTrNode = createTransformNode("objRootNode_" + this._nodeCounter++); 399 ctrTrNode = RDGE.createTransformNode("objRootNode_" + this._nodeCounter++);
400 this._rootNode.insertAsChild( ctrTrNode ); 400 this._rootNode.insertAsChild( ctrTrNode );
401 obj.setTransformNode( ctrTrNode ); 401 obj.setTransformNode( ctrTrNode );
402 } 402 }
403 403
404 ctrTrNode.meshes.forEach(function(thisMesh) { 404 ctrTrNode.meshes.forEach(function(thisMesh) {
405 g_meshMan.deleteMesh(thisMesh.mesh.name); 405 RDGE.globals.meshMan.deleteMesh(thisMesh.mesh.name);
406 }); 406 });
407 ctrTrNode.meshes = []; 407 ctrTrNode.meshes = [];
408 408
@@ -421,11 +421,11 @@ World.prototype.updateObject = function (obj) {
421 childTrNode = children[i-1].transformNode; 421 childTrNode = children[i-1].transformNode;
422 422
423 childTrNode.meshes.forEach(function(thisMesh) { 423 childTrNode.meshes.forEach(function(thisMesh) {
424 g_meshMan.deleteMesh(thisMesh.mesh.name); 424 RDGE.globals.meshMan.deleteMesh(thisMesh.mesh.name);
425 }); 425 });
426 childTrNode.meshes = []; 426 childTrNode.meshes = [];
427 } else { 427 } else {
428 childTrNode = createTransformNode("objNode_" + this._nodeCounter++); 428 childTrNode = RDGE.createTransformNode("objNode_" + this._nodeCounter++);
429 ctrTrNode.insertAsChild(childTrNode); 429 ctrTrNode.insertAsChild(childTrNode);
430 } 430 }
431 431
@@ -466,7 +466,7 @@ World.prototype.addObject = function( obj ) {
466 catch(e) { 466 catch(e) {
467 alert( "Exception in GLWorld.addObject " + e ); 467 alert( "Exception in GLWorld.addObject " + e );
468 } 468 }
469} 469};
470 470
471World.prototype.restartRenderLoop = function() { 471World.prototype.restartRenderLoop = function() {
472 //console.log( "restartRenderLoop" ); 472 //console.log( "restartRenderLoop" );
@@ -527,7 +527,7 @@ World.prototype.clearTree = function() {
527 if (this._useWebGL) { 527 if (this._useWebGL) {
528 var root = this._rootNode; 528 var root = this._rootNode;
529 root.children = new Array(); 529 root.children = new Array();
530 g_Engine.unregisterCanvas( this._canvas.rdgeid ); 530 RDGE.globals.engine.unregisterCanvas( this._canvas.rdgeid );
531 531
532 this.update( 0 ); 532 this.update( 0 );
533 this.draw(); 533 this.draw();
@@ -542,7 +542,7 @@ World.prototype.updateMaterials = function( obj, time ) {
542 var n = matArray.length; 542 var n = matArray.length;
543 for (var i=0; i<n; i++) { 543 for (var i=0; i<n; i++) {
544 matArray[i].update( time ); 544 matArray[i].update( time );
545 } 545 }
546 } 546 }
547 547
548 this.updateMaterials( obj.getNext(), time ); 548 this.updateMaterials( obj.getNext(), time );
@@ -661,9 +661,9 @@ World.prototype.setMVMatrix = function() {
661 gl.uniformMatrix4fv(this._shaderProgram.mvMatrixUniform, false, new Float32Array(mvMatrix)); 661 gl.uniformMatrix4fv(this._shaderProgram.mvMatrixUniform, false, new Float32Array(mvMatrix));
662 662
663 var normalMatrix = mat3.create(); 663 var normalMatrix = mat3.create();
664 // mat4.toInverseMat3(mvMatrix, normalMatrix); 664 // RDGE.mat4.toInverseMat3(mvMatrix, normalMatrix);
665 // mat4.toInverseMat3(new Float32Array(mvMatrix.flatten()), normalMatrix); 665 // RDGE.mat4.toInverseMat3(new Float32Array(mvMatrix.flatten()), normalMatrix);
666 mat4.toInverseMat3(new Float32Array(mvMatrix), normalMatrix); 666 RDGE.mat4.toInverseMat3(new Float32Array(mvMatrix), normalMatrix);
667 mat3.transpose(normalMatrix); 667 mat3.transpose(normalMatrix);
668 gl.uniformMatrix3fv(this._shaderProgram.nMatrixUniform, false, normalMatrix); 668 gl.uniformMatrix3fv(this._shaderProgram.nMatrixUniform, false, normalMatrix);
669 } 669 }
@@ -684,7 +684,7 @@ World.prototype.render = function() {
684 var root = this.getGeomRoot(); 684 var root = this.getGeomRoot();
685 this.hRender( root ); 685 this.hRender( root );
686 } else { 686 } else {
687 g_Engine.setContext( this._canvas.rdgeid ); 687 RDGE.globals.engine.setContext( this._canvas.rdgeid );
688 //this.draw(); 688 //this.draw();
689 this.restartRenderLoop(); 689 this.restartRenderLoop();
690 } 690