diff options
Diffstat (limited to 'js/lib')
-rwxr-xr-x | js/lib/drawing/world.js | 187 | ||||
-rwxr-xr-x | js/lib/nj-base.js | 13 |
2 files changed, 93 insertions, 107 deletions
diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js index ae8c2cce..657c849f 100755 --- a/js/lib/drawing/world.js +++ b/js/lib/drawing/world.js | |||
@@ -33,7 +33,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { | |||
33 | if(preserveDrawingBuffer) { | 33 | if(preserveDrawingBuffer) { |
34 | this._glContext = canvas.getContext("experimental-webgl", {preserveDrawingBuffer: true}); | 34 | this._glContext = canvas.getContext("experimental-webgl", {preserveDrawingBuffer: true}); |
35 | } else { | 35 | } else { |
36 | this._glContext = canvas.getContext("experimental-webgl"); | 36 | this._glContext = canvas.getContext("experimental-webgl"); |
37 | } | 37 | } |
38 | } else { | 38 | } else { |
39 | this._2DContext = canvas.getContext( "2d" ); | 39 | this._2DContext = canvas.getContext( "2d" ); |
@@ -364,7 +364,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { | |||
364 | // 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 |
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 | RDGE.globals.engine.unregisterCanvas( this._canvas ); | 368 | RDGE.globals.engine.unregisterCanvas( this._canvas ); |
369 | RDGE.globals.engine.registerCanvas(this._canvas, this); | 369 | RDGE.globals.engine.registerCanvas(this._canvas, this); |
370 | RDGE.RDGEStart( this._canvas ); | 370 | RDGE.RDGEStart( this._canvas ); |
@@ -435,36 +435,36 @@ World.prototype.updateObject = function (obj) { | |||
435 | } | 435 | } |
436 | }; | 436 | }; |
437 | 437 | ||
438 | World.prototype.addObject = function (obj) { | 438 | World.prototype.addObject = function( obj ) { |
439 | if (!obj) return; | 439 | if (!obj) return; |
440 | 440 | ||
441 | try { | 441 | try { |
442 | // undefine all the links of the object | 442 | // undefine all the links of the object |
443 | obj.setChild(undefined); | 443 | obj.setChild( undefined ); |
444 | obj.setNext(undefined); | 444 | obj.setNext( undefined ); |
445 | obj.setPrev(undefined); | 445 | obj.setPrev( undefined ); |
446 | obj.setParent(undefined); | 446 | obj.setParent( undefined ); |
447 | 447 | ||
448 | obj.setWorld(this); | 448 | obj.setWorld( this ); |
449 | 449 | ||
450 | if (this._geomRoot == null) { | 450 | if (this._geomRoot == null) { |
451 | this._geomRoot = obj; | 451 | this._geomRoot = obj; |
452 | } else { | 452 | } else { |
453 | var go = this._geomRoot; | 453 | var go = this._geomRoot; |
454 | while (go.getNext()) go = go.getNext(); | 454 | while (go.getNext()) go = go.getNext(); |
455 | go.setNext(obj); | 455 | go.setNext( obj ); |
456 | obj.setPrev(go); | 456 | obj.setPrev( go ); |
457 | } | 457 | } |
458 | 458 | ||
459 | // build the WebGL buffers | 459 | // build the WebGL buffers |
460 | if (this._useWebGL) { | 460 | if (this._useWebGL) { |
461 | obj.buildBuffers(); | 461 | obj.buildBuffers(); |
462 | this.restartRenderLoop(); | 462 | this.restartRenderLoop(); |
463 | } | 463 | } |
464 | } | 464 | } |
465 | 465 | ||
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 | ||
@@ -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 | RDGE.globals.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(); |
@@ -540,9 +540,10 @@ World.prototype.updateMaterials = function( obj, time ) { | |||
540 | var matArray = obj.getMaterialArray(); | 540 | var matArray = obj.getMaterialArray(); |
541 | if (matArray) { | 541 | if (matArray) { |
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 | this.updateMaterials( obj.getNext(), time ); | 548 | this.updateMaterials( obj.getNext(), time ); |
548 | this.updateMaterials( obj.getChild(), time ); | 549 | this.updateMaterials( obj.getChild(), time ); |
@@ -553,9 +554,8 @@ World.prototype.getNDCOrigin = function() { | |||
553 | var pt = MathUtils.transformPoint( [0,0,0], this.getCameraMatInverse() ); | 554 | var pt = MathUtils.transformPoint( [0,0,0], this.getCameraMatInverse() ); |
554 | var projMat = Matrix.makePerspective( this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar()); | 555 | var projMat = Matrix.makePerspective( this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar()); |
555 | var ndcPt = MathUtils.transformHomogeneousPoint( pt, projMat ); | 556 | var ndcPt = MathUtils.transformHomogeneousPoint( pt, projMat ); |
556 | var ndcOrigin = MathUtils.applyHomogeneousCoordinate( ndcPt ); | ||
557 | 557 | ||
558 | return ndcOrigin; | 558 | return MathUtils.applyHomogeneousCoordinate( ndcPt ); |
559 | }; | 559 | }; |
560 | 560 | ||
561 | World.prototype.worldToScreen = function(v) { | 561 | World.prototype.worldToScreen = function(v) { |
@@ -570,9 +570,8 @@ World.prototype.worldToScreen = function(v) { | |||
570 | var x = v2[0], y = v2[1], z = v2[2]; | 570 | var x = v2[0], y = v2[1], z = v2[2]; |
571 | 571 | ||
572 | var h = this.getGLContext().viewportHeight/2.0, w = this.getGLContext().viewportWidth/2.0; | 572 | var h = this.getGLContext().viewportHeight/2.0, w = this.getGLContext().viewportWidth/2.0; |
573 | var x2 = w*(1 + x), y2 = h*( 1 - y ), z2 = z; | 573 | var x2 = w * (1 + x), y2 = h * ( 1 - y ); |
574 | 574 | return [x2, y2, z, 1]; | |
575 | return [x2, y2, z2, 1]; | ||
576 | }; | 575 | }; |
577 | 576 | ||
578 | World.prototype.screenToView = function( x, y ) { | 577 | World.prototype.screenToView = function( x, y ) { |
@@ -735,74 +734,74 @@ World.prototype.getShapeFromPoint = function( offsetX, offsetY ) { | |||
735 | 734 | ||
736 | 735 | ||
737 | World.prototype.exportJSON = function () { | 736 | World.prototype.exportJSON = function () { |
738 | // world properties | 737 | // world properties |
739 | var worldObj = | 738 | var worldObj = |
740 | { | 739 | { |
741 | 'version': 1.1, | 740 | 'version' : 1.1, |
742 | 'id': this.getCanvas().getAttribute("data-RDGE-id"), | 741 | 'id' : this.getCanvas().getAttribute( "data-RDGE-id" ), |
743 | 'fov': this._fov, | 742 | 'fov' : this._fov, |
744 | 'zNear': this._zNear, | 743 | 'zNear' : this._zNear, |
745 | 'zFar': this._zFar, | 744 | 'zFar' : this._zFar, |
746 | 'viewDist': this._viewDist, | 745 | 'viewDist' : this._viewDist, |
747 | 'webGL': this._useWebGL | 746 | 'webGL' : this._useWebGL |
748 | }; | 747 | }; |
749 | 748 | ||
750 | // RDGE scenegraph | 749 | // RDGE scenegraph |
751 | if (this._useWebGL) | 750 | if (this._useWebGL) |
752 | worldObj.scenedata = this.myScene.exportJSON(); | 751 | worldObj.scenedata = this.myScene.exportJSON(); |
753 | 752 | ||
754 | // object data | 753 | // object data |
755 | var strArray = []; | 754 | var strArray = []; |
756 | this.exportObjectsJSON(this._geomRoot, worldObj); | 755 | this.exportObjectsJSON( this._geomRoot, worldObj ); |
757 | 756 | ||
758 | // You would think that the RDGE export function | 757 | // You would think that the RDGE export function |
759 | // would not be destructive of the data. You would be wrong... | 758 | // would not be destructive of the data. You would be wrong... |
760 | // We need to rebuild everything | 759 | // We need to rebuild everything |
761 | if (this._useWebGL) { | 760 | if (this._useWebGL) { |
762 | if (worldObj.children && (worldObj.children.length === 1)) { | 761 | if (worldObj.children && (worldObj.children.length === 1)) { |
763 | this.rebuildTree(this._geomRoot); | 762 | this.rebuildTree(this._geomRoot); |
764 | this.restartRenderLoop(); | 763 | this.restartRenderLoop(); |
765 | } | 764 | } |
766 | } | 765 | } |
767 | |||
768 | // convert the object to a string | ||
769 | var jStr = JSON.stringify(worldObj); | ||
770 | 766 | ||
771 | // prepend some version information to the string. | 767 | // convert the object to a string |
772 | // this string is also used to differentiate between JSON | 768 | var jStr = JSON.stringify( worldObj ); |
773 | // and pre-JSON versions of fileIO. | ||
774 | // the ending ';' in the version string is necessary | ||
775 | jStr = "v1.0;" + jStr; | ||
776 | 769 | ||
777 | return jStr; | 770 | // prepend some version information to the string. |
771 | // this string is also used to differentiate between JSON | ||
772 | // and pre-JSON versions of fileIO. | ||
773 | // the ending ';' in the version string is necessary | ||
774 | jStr = "v1.0;" + jStr; | ||
775 | |||
776 | return jStr; | ||
778 | }; | 777 | }; |
779 | 778 | ||
780 | World.prototype.rebuildTree = function (obj) { | 779 | World.prototype.rebuildTree = function (obj) { |
781 | if (!obj) return; | 780 | if (!obj) return; |
782 | 781 | ||
783 | obj.buildBuffers(); | 782 | obj.buildBuffers(); |
784 | 783 | ||
785 | if (obj.getChild()) { | 784 | if (obj.getChild()) { |
786 | this.rebuildTree(obj.getChild()); | 785 | this.rebuildTree( obj.getChild () ); |
787 | } | 786 | } |
788 | 787 | ||
789 | if (obj.getNext()) | 788 | if (obj.getNext()) |
790 | this.rebuildTree(obj.getNext()); | 789 | this.rebuildTree( obj.getNext() ); |
791 | }; | 790 | }; |
792 | 791 | ||
793 | World.prototype.exportObjectsJSON = function (obj, parentObj) { | 792 | World.prototype.exportObjectsJSON = function (obj, parentObj) { |
794 | if (!obj) return; | 793 | if (!obj) return; |
795 | 794 | ||
796 | var jObj = obj.exportJSON(); | 795 | var jObj = obj.exportJSON(); |
797 | if (!parentObj.children) parentObj.children = []; | 796 | if (!parentObj.children) parentObj.children = []; |
798 | parentObj.children.push(jObj); | 797 | parentObj.children.push( jObj ); |
799 | 798 | ||
800 | if (obj.getChild()) { | 799 |