aboutsummaryrefslogtreecommitdiff
path: root/js/lib/drawing
diff options
context:
space:
mode:
authorValerio Virgillito2012-03-22 15:47:56 -0700
committerValerio Virgillito2012-03-22 15:47:56 -0700
commitfdeed8051c3af538d28ca3bc599121cea483c22c (patch)
treecd5db2db7a04fb1e3035132a840076480706e0e3 /js/lib/drawing
parent5308a9404ef131ba6457eec840b017a3e436b9da (diff)
downloadninja-fdeed8051c3af538d28ca3bc599121cea483c22c.tar.gz
Squashed commit of the following GL integration
Signed-off-by: Valerio Virgillito <valerio@motorola.com>
Diffstat (limited to 'js/lib/drawing')
-rwxr-xr-xjs/lib/drawing/world.js147
1 files changed, 147 insertions, 0 deletions
diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js
index 049145ce..07a2c3ae 100755
--- a/js/lib/drawing/world.js
+++ b/js/lib/drawing/world.js
@@ -363,6 +363,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
363 if (this._useWebGL) { 363 if (this._useWebGL) {
364 rdgeStarted = true; 364 rdgeStarted = true;
365 this._canvas.rdgeid = this._canvas.getAttribute( "data-RDGE-id" ); 365 this._canvas.rdgeid = this._canvas.getAttribute( "data-RDGE-id" );
366 g_Engine.unregisterCanvas( this._canvas )
366 g_Engine.registerCanvas(this._canvas, this); 367 g_Engine.registerCanvas(this._canvas, this);
367 RDGEStart( this._canvas ); 368 RDGEStart( this._canvas );
368 this._canvas.task.stop() 369 this._canvas.task.stop()
@@ -729,6 +730,72 @@ World.prototype.getShapeFromPoint = function( offsetX, offsetY ) {
729 } 730 }
730}; 731};
731 732
733World.prototype.exportJSON = function()
734{
735 // world properties
736 var worldObj =
737 {
738 'version' : 1.1,
739 'id' : this.getCanvas().getAttribute( "data-RDGE-id" ),
740 'fov' : this._fov,
741 'zNear' : this._zNear,
742 'zFar' : this._zFar,
743 'viewDist' : this._viewDist,
744 'webGL' : this._useWebGL
745 };
746
747 // RDGE scenegraph
748 if (this._useWebGL)
749 worldObj.scenedata = this.myScene.exportJSON();
750
751 // object data
752 var strArray = [];
753 this.exportObjectsJSON( this._geomRoot, worldObj );
754
755 // You would think that the RDGE export function
756 // would not be destructive of the data. You would be wrong...
757 // We need to rebuild everything
758 if (this._useWebGL)
759 {
760 var root = this._rootNode;
761 root.children = new Array();
762 if (worldObj.children && (worldObj.children.length === 1))
763 {
764 this.init();
765 this._geomRoot = undefined;
766 this.importObjectsJSON( worldObj.children[0] );
767 }
768 }
769
770 // convert the object to a string
771 var jStr = JSON.stringify( worldObj );
772
773 // prepend some version information to the string.
774 // this string is also used to differentiate between JSON
775 // and pre-JSON versions of fileIO.
776 // the ending ';' in the version string is necessary
777 jStr = "v1.0;" + jStr;
778
779 return jStr;
780}
781
782World.prototype.exportObjectsJSON = function( obj, parentObj )
783{
784 if (!obj) return;
785
786 var jObj = obj.exportJSON();
787 if (!parentObj.children) parentObj.children = [];
788 parentObj.children.push( jObj );
789
790 if (obj.getChild()) {
791 this.exportObjectsJSON( obj.getChild (), jObj );
792 }
793
794 if (obj.getNext())
795 this.exportObjectsJSON( obj.getNext(), parentObj );
796}
797
798/*
732World.prototype.export = function() 799World.prototype.export = function()
733{ 800{
734 var exportStr = "GLWorld 1.0\n"; 801 var exportStr = "GLWorld 1.0\n";
@@ -791,6 +858,7 @@ World.prototype.exportObjects = function( obj ) {
791 858
792 return rtnStr; 859 return rtnStr;
793}; 860};
861*/
794 862
795World.prototype.findTransformNodeByMaterial = function( materialNode, trNode ) { 863World.prototype.findTransformNodeByMaterial = function( materialNode, trNode ) {
796 //if (trNode == null) trNode = this._ctrNode; 864 //if (trNode == null) trNode = this._ctrNode;
@@ -810,6 +878,85 @@ World.prototype.findTransformNodeByMaterial = function( materialNode, trNode )
810 return rtnNode; 878 return rtnNode;
811}; 879};
812 880
881World.prototype.importJSON = function( jObj )
882{
883 if (jObj.webGL)
884 {
885 // start RDGE
886 rdgeStarted = true;
887 var id = this._canvas.getAttribute( "data-RDGE-id" );
888 this._canvas.rdgeid = id;
889 g_Engine.registerCanvas(this._canvas, this);
890 RDGEStart( this._canvas );
891 this._canvas.task.stop()
892 }
893
894 // import the objects
895 // there should be exactly one child of the parent object
896 if (jObj.children && (jObj.children.length === 1))
897 this.importObjectsJSON( jObj.children[0] );
898 else
899 throw new Error ("unrecoverable canvas import error - inconsistent root object: " + jObj.children );
900
901 if (!this._useWebGL)
902 {
903 // render using canvas 2D
904 this.render();
905 }
906 else
907 this.restartRenderLoop();
908}
909
910World.prototype.importObjectsJSON = function( jObj, parentGeomObj )
911{
912 // read the next object
913 var gObj = this.importObjectJSON( jObj, parentGeomObj );
914
915 // determine if we have children
916 if (jObj.children)
917 {
918 var nKids = ojObjbj.chilodren.length;
919 for (var i=0; i<nKids; i++)
920 {
921 var child = jObj.children[i];
922 this.importObjectsJSON( child, gObj );
923 }
924 }
925}
926
927World.prototype.importObjectJSON = function( jObj, parentGeomObj )
928{
929 var type = jObj.type;
930
931 var obj;
932 switch (type)
933 {
934 case 1:
935 obj = new Rectangle();
936 obj.importJSON( jObj );
937 break;
938
939 case 2: // circle
940 obj = new Circle();
941 obj.importJSON( jObj );
942 break;
943
944 case 3: // line
945 obj = new Line();
946 obj.importJSON( jObj );
947 break;
948
949 default:
950 throw new Error( "Unrecognized object type: " + type );
951 break;
952 }
953
954 if (obj)
955 this.addObject( obj, parentGeomObj );
956
957 return obj;
958};
959
813World.prototype.import = function( importStr ) { 960World.prototype.import = function( importStr ) {
814 // import the worldattributes - not currently used 961 // import the worldattributes - not currently used
815 962