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.js104
1 files changed, 62 insertions, 42 deletions
diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js
index 3d6c6fc4..049145ce 100755
--- a/js/lib/drawing/world.js
+++ b/js/lib/drawing/world.js
@@ -4,17 +4,6 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. 4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */ 5</copyright> */
6 6
7// Useless Global variables.
8// TODO: Remove this as soon as QE test pass
9/*
10var shaderProgramArray = new Array;
11var glContextArray = new Array;
12var vertexShaderSource = "";
13var fragmentShaderSource = "";
14var rdgeStarted = false;
15*/
16
17var nodeCounter = 0;
18 7
19var GeomObj = require("js/lib/geom/geom-obj").GeomObj; 8var GeomObj = require("js/lib/geom/geom-obj").GeomObj;
20var Line = require("js/lib/geom/line").Line; 9var Line = require("js/lib/geom/line").Line;
@@ -22,11 +11,13 @@ var Rectangle = require("js/lib/geom/rectangle").Rectangle;
22var Circle = require("js/lib/geom/circle").Circle; 11var Circle = require("js/lib/geom/circle").Circle;
23var MaterialsModel = require("js/models/materials-model").MaterialsModel; 12var MaterialsModel = require("js/models/materials-model").MaterialsModel;
24 13
14var worldCounter = 0;
15
25/////////////////////////////////////////////////////////////////////// 16///////////////////////////////////////////////////////////////////////
26// Class GLWorld 17// Class GLWorld
27// Manages display in a canvas 18// Manages display in a canvas
28/////////////////////////////////////////////////////////////////////// 19///////////////////////////////////////////////////////////////////////
29var World = function GLWorld( canvas, use3D ) { 20var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
30 /////////////////////////////////////////////////////////////////////// 21 ///////////////////////////////////////////////////////////////////////
31 // Instance variables 22 // Instance variables
32 /////////////////////////////////////////////////////////////////////// 23 ///////////////////////////////////////////////////////////////////////
@@ -39,7 +30,11 @@ var World = function GLWorld( canvas, use3D ) {
39 30
40 this._canvas = canvas; 31 this._canvas = canvas;
41 if (this._useWebGL) { 32 if (this._useWebGL) {
42 this._glContext = canvas.getContext("experimental-webgl"); 33 if(preserveDrawingBuffer) {
34 this._glContext = canvas.getContext("experimental-webgl", {preserveDrawingBuffer: true});
35 } else {
36 this._glContext = canvas.getContext("experimental-webgl");
37 }
43 } else { 38 } else {
44 this._2DContext = canvas.getContext( "2d" ); 39 this._2DContext = canvas.getContext( "2d" );
45 } 40 }
@@ -76,6 +71,12 @@ var World = function GLWorld( canvas, use3D ) {
76 // no animated materials 71 // no animated materials
77 this._firstRender = true; 72 this._firstRender = true;
78 73
74 this._worldCount = worldCounter;
75 worldCounter++;
76
77 // keep a counter for generating node names
78 this._nodeCounter = 0;
79
79 /////////////////////////////////////////////////////////////////////// 80 ///////////////////////////////////////////////////////////////////////
80 // Property accessors 81 // Property accessors
81 /////////////////////////////////////////////////////////////////////// 82 ///////////////////////////////////////////////////////////////////////
@@ -350,17 +351,18 @@ var World = function GLWorld( canvas, use3D ) {
350 return false; 351 return false;
351 }; 352 };
352 353
353 354 this.generateUniqueNodeID = function() {
354 // END RDGE 355 var str = "" + this._nodeCounter;
355 //////////////////////////////////////////////////////////////////////////////////// 356 this._nodeCounter++;
357 return str;
358 };
356 359
357 360
358 // start RDGE passing your runtime object, and false to indicate we don't need a an initialization state 361 // start RDGE passing your runtime object, and false to indicate we don't need a an initialization state
359 // in the case of a procedurally built scene an init state is not needed for loading data 362 // in the case of a procedurally built scene an init state is not needed for loading data
360 if (this._useWebGL) { 363 if (this._useWebGL) {
361 rdgeStarted = true; 364 rdgeStarted = true;
362 var id = this._canvas.getAttribute( "data-RDGE-id" ); 365 this._canvas.rdgeid = this._canvas.getAttribute( "data-RDGE-id" );
363 this._canvas.rdgeid = id;
364 g_Engine.registerCanvas(this._canvas, this); 366 g_Engine.registerCanvas(this._canvas, this);
365 RDGEStart( this._canvas ); 367 RDGEStart( this._canvas );
366 this._canvas.task.stop() 368 this._canvas.task.stop()
@@ -391,7 +393,7 @@ World.prototype.updateObject = function (obj) {
391 if (nPrims > 0) { 393 if (nPrims > 0) {
392 ctrTrNode = obj.getTransformNode(); 394 ctrTrNode = obj.getTransformNode();
393 if (ctrTrNode == null) { 395 if (ctrTrNode == null) {
394 ctrTrNode = createTransformNode("objRootNode_" + nodeCounter++); 396 ctrTrNode = createTransformNode("objRootNode_" + this._nodeCounter++);
395 this._rootNode.insertAsChild( ctrTrNode ); 397 this._rootNode.insertAsChild( ctrTrNode );
396 obj.setTransformNode( ctrTrNode ); 398 obj.setTransformNode( ctrTrNode );
397 } 399 }
@@ -401,7 +403,7 @@ World.prototype.updateObject = function (obj) {
401 }); 403 });
402 ctrTrNode.meshes = []; 404 ctrTrNode.meshes = [];
403 405
404 ctrTrNode.attachMeshNode(this.renderer.id + "_prim_" + nodeCounter++, prims[0]); 406 ctrTrNode.attachMeshNode(this.renderer.id + "_prim_" + this._nodeCounter++, prims[0]);
405 ctrTrNode.attachMaterial(materialNodes[0]); 407 ctrTrNode.attachMaterial(materialNodes[0]);
406 } 408 }
407 409
@@ -420,12 +422,12 @@ World.prototype.updateObject = function (obj) {
420 }); 422 });
421 childTrNode.meshes = []; 423 childTrNode.meshes = [];
422 } else { 424 } else {
423 childTrNode = createTransformNode("objNode_" + nodeCounter++); 425 childTrNode = createTransformNode("objNode_" + this._nodeCounter++);
424 ctrTrNode.insertAsChild(childTrNode); 426 ctrTrNode.insertAsChild(childTrNode);
425 } 427 }
426 428
427 // attach the instanced box goe 429 // attach the instanced box goe
428 childTrNode.attachMeshNode(this.renderer.id + "_prim_" + nodeCounter++, prim); 430 childTrNode.attachMeshNode(this.renderer.id + "_prim_" + this._nodeCounter++, prim);
429 childTrNode.attachMaterial(materialNodes[i]); 431 childTrNode.attachMaterial(materialNodes[i]);
430 } 432 }
431}; 433};
@@ -680,7 +682,7 @@ World.prototype.render = function() {
680 var root = this.getGeomRoot(); 682 var root = this.getGeomRoot();
681 this.hRender( root ); 683 this.hRender( root );
682 } else { 684 } else {
683 g_Engine.setContext( this._canvas.rdgeId ); 685 g_Engine.setContext( this._canvas.rdgeid );
684 //this.draw(); 686 //this.draw();
685 this.restartRenderLoop(); 687 this.restartRenderLoop();
686 } 688 }
@@ -727,7 +729,8 @@ World.prototype.getShapeFromPoint = function( offsetX, offsetY ) {
727 } 729 }
728}; 730};
729 731
730World.prototype.export = function() { 732World.prototype.export = function()
733{
731 var exportStr = "GLWorld 1.0\n"; 734 var exportStr = "GLWorld 1.0\n";
732 var id = this.getCanvas().getAttribute( "data-RDGE-id" ); 735 var id = this.getCanvas().getAttribute( "data-RDGE-id" );
733 exportStr += "id: " + id + "\n"; 736 exportStr += "id: " + id + "\n";
@@ -736,17 +739,29 @@ World.prototype.export = function() {
736 exportStr += "zNear: " + this._zNear + "\n"; 739 exportStr += "zNear: " + this._zNear + "\n";
737 exportStr += "zFar: " + this._zFar + "\n"; 740 exportStr += "zFar: " + this._zFar + "\n";
738 exportStr += "viewDist: " + this._viewDist + "\n"; 741 exportStr += "viewDist: " + this._viewDist + "\n";
742 if (this._useWebGL)
743 exportStr += "webGL: true\n";
739 744
740 // we need 2 export modes: One for save/restore, one for publish. 745 // we need 2 export modes: One for save/restore, one for publish.
741 // hardcoding for now 746 // hardcoding for now
742 var exportForPublish = false; 747 //var exportForPublish = false;
748 //if (!exportForPublish) exportForPublish = false;
749 var exportForPublish = true;
743 exportStr += "publish: " + exportForPublish + "\n"; 750 exportStr += "publish: " + exportForPublish + "\n";
744 751
745 if (exportForPublish) { 752 if (exportForPublish && this._useWebGL)
753 {
746 exportStr += "scenedata: " + this.myScene.exportJSON() + "endscene\n"; 754 exportStr += "scenedata: " + this.myScene.exportJSON() + "endscene\n";
747 } else { 755
756 // write out all of the objects
757 exportStr += "tree\n";
758 exportStr += this.exportObjects( this._geomRoot );
759 exportStr += "endtree\n";
760 }
761 else
762 {
748 // output the material library 763 // output the material library
749 exportStr += MaterialsModel.exportMaterials(); 764 //exportStr += MaterialsLibrary.export(); // THIS NEEDS TO BE DONE AT THE DOC LEVEL
750 765
751 // write out all of the objects 766 // write out all of the objects
752 exportStr += "tree\n"; 767 exportStr += "tree\n";
@@ -800,21 +815,26 @@ World.prototype.import = function( importStr ) {
800 815
801 // determine if the data was written for export (no Ninja objects) 816 // determine if the data was written for export (no Ninja objects)
802 // or for save/restore 817 // or for save/restore
803 var index = importStr.indexOf( "scenedata: " ); 818 //var index = importStr.indexOf( "scenedata: " );
804 if (index >= 0) { 819 var index = importStr.indexOf( "webGL: " );
805 var rdgeStr = importStr.substr( index+11 ); 820 this._useWebGL = (index >= 0)
806 var endIndex = rdgeStr.indexOf( "endscene\n" ); 821 if (this._useWebGL)
807 if (endIndex < 0) throw new Error( "ill-formed WebGL data" ); 822 {
808 var len = endIndex - index + 11; 823 // start RDGE
809 rdgeStr = rdgeStr.substr( 0, endIndex ); 824 rdgeStarted = true;
810 825 var id = this._canvas.getAttribute( "data-RDGE-id" );
811 this.myScene.importJSON( rdgeStr ); 826 this._canvas.rdgeid = id;
812 } else { 827 g_Engine.registerCanvas(this._canvas, this);
813 // load the material library 828 RDGEStart( this._canvas );
814 importStr = MaterialsModel.importMaterials( importStr ); 829 this._canvas.task.stop()
830 }
831
832 this.importObjects( importStr, this._rootNode );
815 833
816 // import the objects 834 if (!this._useWebGL)
817 this.importObjects( importStr, this._rootNode ); 835 {
836 // render using canvas 2D
837 this.render();
818 } 838 }
819}; 839};
820 840