From cf61d1545bffb25a807706fb56b2fe7f291adc2c Mon Sep 17 00:00:00 2001 From: Nivesh Rajbhandari Date: Tue, 6 Mar 2012 09:45:23 -0800 Subject: Create element models based on nodeName. Signed-off-by: Nivesh Rajbhandari --- js/lib/NJUtils.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'js/lib') diff --git a/js/lib/NJUtils.js b/js/lib/NJUtils.js index e16715a4..4f1082f9 100755 --- a/js/lib/NJUtils.js +++ b/js/lib/NJUtils.js @@ -116,7 +116,39 @@ exports.NJUtils = Object.create(Object.prototype, { ///// TODO: Selection and model should be based on the element type makeModelFromElement: { value: function(el) { - this.makeElementModel(el, "Div", "block", false); + var selection = "div", + controller = "block", + isShape = false; + switch(el.nodeName.toLowerCase()) + { + case "div": + break; + case "img": + selection = "image"; + controller = "image"; + break; + case "video": + selection = "video"; + controller = "video"; + break; + case "canvas": + isShape = el.getAttribute("data-RDGE-id"); + if(isShape) + { + // TODO - Need more info about the shape + selection = "canvas"; + controller = "shape"; + } + else + { + selection = "canvas"; + controller = "canvas"; + } + break; + case "shape": + break; + } + this.makeElementModel(el, selection, controller, isShape); } }, -- cgit v1.2.3 From 40f3fc5feae866c99af818a886e8bf9d8cf2b8dd Mon Sep 17 00:00:00 2001 From: Pushkar Joshi Date: Tue, 6 Mar 2012 17:23:58 -0800 Subject: merge Valerio's architecture changes into pen tool --- js/lib/geom/sub-path.js | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'js/lib') diff --git a/js/lib/geom/sub-path.js b/js/lib/geom/sub-path.js index ab54d1e9..0a65511b 100755 --- a/js/lib/geom/sub-path.js +++ b/js/lib/geom/sub-path.js @@ -584,6 +584,90 @@ GLSubpath.prototype.pickAnchor = function (pickX, pickY, pickZ, radius) { return selAnchorIndex; }; +GLSubpath.prototype.isWithinBBox =function(x,y,z){ + if (this._BBoxMin[0]>x || this._BBoxMin[1]>y || this._BBoxMin[2]>z){ + return false; + } + if (this._BBoxMax[0]=0 && this._selectedAnchorIndex=0 && selAnchorIndex === -1) { + var distSq = this._Anchors[this._selectedAnchorIndex].getPrevDistanceSq(pickX, pickY, pickZ); + if (distSq < minDistance && distSq < radSq){ + selAnchorIndex = this._selectedAnchorIndex; + minDistance = distSq; + } else { + //check the next for this anchor point + distSq = this._Anchors[this._selectedAnchorIndex].getNextDistanceSq(pickX, pickY, pickZ); + if (distSq0) { - var threshold = this._WETNESS_FACTOR*this._strokeWidth; + var threshold = 1;//this._WETNESS_FACTOR*this._strokeWidth; var prevPt = this._Points[numPoints-1]; var diffPt = [prevPt[0]-pt[0], prevPt[1]-pt[1]]; var diffPtMag = Math.sqrt(diffPt[0]*diffPt[0] + diffPt[1]*diffPt[1]); @@ -173,6 +175,14 @@ var BrushStroke = function GLBrushStroke() { this._strokeColor = c; }; + this.setSecondStrokeColor = function(c){ + this._secondStrokeColor=c; + } + + this.setStrokeHardness = function(h){ + this._strokeHardness=h; + } + this.getStrokeStyle = function () { return this._strokeStyle; }; @@ -219,7 +229,8 @@ var BrushStroke = function GLBrushStroke() { var numPoints = this._Points.length; //**** add samples to the path if needed...linear interpolation for now - if (numPoints>1) { + //if (numPoints>1) { + if (0){ var threshold = this._WETNESS_FACTOR*this._strokeWidth; var prevPt = this._Points[0]; var prevIndex = 0; @@ -250,6 +261,44 @@ var BrushStroke = function GLBrushStroke() { } } } + //**** add samples to the long sections of the path --- Catmull-Rom spline interpolation + if (numPoints>1) { + var numInsertedPoints = 0; + var threshold = 5;//0.25*this._strokeWidth; //this determines whether a segment between two sample is too long + var prevPt = this._Points[0]; + for (var i=1;ithreshold){ + //build the control polygon for the Catmull-Rom spline (prev. 2 points and next 2 points) + var prev = (i===1) ? i-1 : i-2; + var next = (i===numPoints-1) ? i : i+1; + var ctrlPts = [this._Points[prev], this._Points[i-1], this._Points[i], this._Points[next]]; + //insert points along the prev. to current point + var numNewPoints = Math.floor(distance/threshold); + for (var j=0;j this._MAX_ALLOWED_SAMPLES){ + console.log("leaving the resampling because numPoints is greater than limit:"+this._MAX_ALLOWED_SAMPLES); + break; + } + } + console.log("Inserted "+numInsertedPoints+" additional CatmullRom points"); + } // *** compute the bounding box ********* this._BBoxMin = [Infinity, Infinity, Infinity]; @@ -412,7 +461,7 @@ var BrushStroke = function GLBrushStroke() { } */ - + /* var R2 = this._strokeWidth; var R = R2*0.5; var hardness = 0; //for a pencil, this is always 1 //TODO get hardness parameter from user interface @@ -440,6 +489,109 @@ var BrushStroke = function GLBrushStroke() { //ctx.globalCompositeOperation = 'source-in'; //ctx.rect(x-R, y-R, R2, R2); } + */ + + /* + //build the stamp for the brush stroke + //todo get this directly from the UI + var t=0; + var numTraces = this._strokeWidth; + var halfNumTraces = numTraces/2; + var startPos = [-this._strokeWidth/2,0]; + var brushStamp = []; + + //build an angled (calligraphic) brush stamp + var deltaDisplacement = [1,1];//[this._strokeWidth/numTraces, 0]; //a horizontal line brush + for (t=0;t1) { + if (this._strokeDoSmoothing && numPoints>1) { var numInsertedPoints = 0; var threshold = 5;//0.25*this._strokeWidth; //this determines whether a segment between two sample is too long var prevPt = this._Points[0]; @@ -507,35 +512,6 @@ var BrushStroke = function GLBrushStroke() { brushStamp.push(brushPt); } - - //make a circular brush stamp - brushStamp=[]; - numTraces = this._strokeWidth*Math.PI; //figure out how to - var radius = this._strokeWidth/2; - for (t=0;t */ -// Useless Global variables. -// TODO: Remove this as soon as QE test pass -/* -var shaderProgramArray = new Array; -var glContextArray = new Array; -var vertexShaderSource = ""; -var fragmentShaderSource = ""; -var rdgeStarted = false; -*/ - -var nodeCounter = 0; var GeomObj = require("js/lib/geom/geom-obj").GeomObj; var Line = require("js/lib/geom/line").Line; @@ -22,6 +11,8 @@ var Rectangle = require("js/lib/geom/rectangle").Rectangle; var Circle = require("js/lib/geom/circle").Circle; var MaterialsModel = require("js/models/materials-model").MaterialsModel; +var worldCounter = 0; + /////////////////////////////////////////////////////////////////////// // Class GLWorld // Manages display in a canvas @@ -76,6 +67,12 @@ var World = function GLWorld( canvas, use3D ) { // no animated materials this._firstRender = true; + this._worldCount = worldCounter; + worldCounter++; + + // keep a counter for generating node names + this._nodeCounter = 0; + /////////////////////////////////////////////////////////////////////// // Property accessors /////////////////////////////////////////////////////////////////////// @@ -350,9 +347,12 @@ var World = function GLWorld( canvas, use3D ) { return false; }; - - // END RDGE - //////////////////////////////////////////////////////////////////////////////////// + this.generateUniqueNodeID = function() + { + var str = String( this._nodeCounter ); + this._nodeCounter++; + return str; + } // start RDGE passing your runtime object, and false to indicate we don't need a an initialization state @@ -391,7 +391,7 @@ World.prototype.updateObject = function (obj) { if (nPrims > 0) { ctrTrNode = obj.getTransformNode(); if (ctrTrNode == null) { - ctrTrNode = createTransformNode("objRootNode_" + nodeCounter++); + ctrTrNode = createTransformNode("objRootNode_" + this._nodeCounter++); this._rootNode.insertAsChild( ctrTrNode ); obj.setTransformNode( ctrTrNode ); } @@ -401,7 +401,7 @@ World.prototype.updateObject = function (obj) { }); ctrTrNode.meshes = []; - ctrTrNode.attachMeshNode(this.renderer.id + "_prim_" + nodeCounter++, prims[0]); + ctrTrNode.attachMeshNode(this.renderer.id + "_prim_" + this._nodeCounter++, prims[0]); ctrTrNode.attachMaterial(materialNodes[0]); } @@ -420,12 +420,12 @@ World.prototype.updateObject = function (obj) { }); childTrNode.meshes = []; } else { - childTrNode = createTransformNode("objNode_" + nodeCounter++); + childTrNode = createTransformNode("objNode_" + this._nodeCounter++); ctrTrNode.insertAsChild(childTrNode); } // attach the instanced box goe - childTrNode.attachMeshNode(this.renderer.id + "_prim_" + nodeCounter++, prim); + childTrNode.attachMeshNode(this.renderer.id + "_prim_" + this._nodeCounter++, prim); childTrNode.attachMaterial(materialNodes[i]); } }; @@ -727,7 +727,7 @@ World.prototype.getShapeFromPoint = function( offsetX, offsetY ) { } }; -World.prototype.export = function() { +World.prototype.export = function( exportForPublish ) { var exportStr = "GLWorld 1.0\n"; var id = this.getCanvas().getAttribute( "data-RDGE-id" ); exportStr += "id: " + id + "\n"; @@ -736,17 +736,28 @@ World.prototype.export = function() { exportStr += "zNear: " + this._zNear + "\n"; exportStr += "zFar: " + this._zFar + "\n"; exportStr += "viewDist: " + this._viewDist + "\n"; + if (this._useWebGL) + exportStr += "webGL: true\n"; // we need 2 export modes: One for save/restore, one for publish. // hardcoding for now - var exportForPublish = false; + //var exportForPublish = false; + if (!exportForPublish) exportForPublish = false; exportStr += "publish: " + exportForPublish + "\n"; - if (exportForPublish) { + if (exportForPublish && this._useWebGL) + { exportStr += "scenedata: " + this.myScene.exportJSON() + "endscene\n"; - } else { + + // write out all of the objects + exportStr += "tree\n"; + exportStr += this.exportObjects( this._geomRoot ); + exportStr += "endtree\n"; + } + else + { // output the material library - exportStr += MaterialsModel.exportMaterials(); + //exportStr += MaterialsLibrary.export(); // THIS NEEDS TO BE DONE AT THE DOC LEVEL // write out all of the objects exportStr += "tree\n"; @@ -800,21 +811,26 @@ World.prototype.import = function( importStr ) { // determine if the data was written for export (no Ninja objects) // or for save/restore - var index = importStr.indexOf( "scenedata: " ); - if (index >= 0) { - var rdgeStr = importStr.substr( index+11 ); - var endIndex = rdgeStr.indexOf( "endscene\n" ); - if (endIndex < 0) throw new Error( "ill-formed WebGL data" ); - var len = endIndex - index + 11; - rdgeStr = rdgeStr.substr( 0, endIndex ); - - this.myScene.importJSON( rdgeStr ); - } else { - // load the material library - importStr = MaterialsModel.importMaterials( importStr ); + //var index = importStr.indexOf( "scenedata: " ); + var index = importStr.indexOf( "webGL: " ); + this._useWebGL = (index >= 0) + if (this._useWebGL) + { + // start RDGE + rdgeStarted = true; + var id = this._canvas.getAttribute( "data-RDGE-id" ); + this._canvas.rdgeid = id; + g_Engine.registerCanvas(this._canvas, this); + RDGEStart( this._canvas ); + this._canvas.task.stop() + } + + this.importObjects( importStr, this._rootNode ); - // import the objects - this.importObjects( importStr, this._rootNode ); + if (!this._useWebGL) + { + // render using canvas 2D + this.render(); } }; diff --git a/js/lib/geom/circle.js b/js/lib/geom/circle.js index dd82a4cc..ad77383b 100755 --- a/js/lib/geom/circle.js +++ b/js/lib/geom/circle.js @@ -577,9 +577,10 @@ var Circle = function GLCircle() { } else { rtnStr += "flatMaterial"; } - rtnStr += "\n"; + rtnStr += this.exportMaterials(); + return rtnStr; }; diff --git a/js/lib/geom/geom-obj.js b/js/lib/geom/geom-obj.js index c5880843..852aab89 100755 --- a/js/lib/geom/geom-obj.js +++ b/js/lib/geom/geom-obj.js @@ -204,6 +204,53 @@ var GeomObj = function GLGeomObj() { return fillMaterial; }; + this.exportMaterials = function() + { + var rtnStr = ""; + if (this._materialArray && this._materialNodeArray) + { + var nMats = this._materialArray.length; + rtnStr += "nMaterials: " + nMats + "\n"; + for (var i=0; i +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. + */ + + +var GeomObj = require("js/lib/geom/geom-obj").GeomObj; +var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive; +var MaterialsModel = require("js/models/materials-model").MaterialsModel; +var GLRuntime = require("js/lib/rdge/runtime/GLRuntime").GLRuntime; + +/////////////////////////////////////////////////////////////////////// +// Class ShapeRuntime +// Manages runtime shape display +/////////////////////////////////////////////////////////////////////// +var CanvasDataManager = function CanvasDataManager() +{ + this.loadGLData = function(root, valueArray, NinjaUtils) + { + var value = valueArray; + var nWorlds = value.length; + for (var i=0; i= 0) + { + var endIndex = importStr.indexOf( "\n", startIndex ); + if (endIndex > 0) + { + var id = importStr.substring( startIndex+4, endIndex ); + var canvas = this.findCanvasWithID( id, root ); + if (canvas) + { + var rt = new GLRuntime( canvas, importStr ); + } + } + } + } + } + + this.collectGLData = function( elt, dataArray ) + { + if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld) + { + var data = elt.elementModel.shapeModel.GLWorld.export( true ); + dataArray.push( data ); + } + + if (elt.children) + { + var nKids = elt.children.length; + for (var i=0; i +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. + */ + + +var GeomObj = require("js/lib/geom/geom-obj").GeomObj, + RuntimeGeomObj = require("js/lib/rdge/runtime/RuntimeGeomObj"), + RuntimeRectangle = RuntimeGeomObj.RuntimeRectangle, + RuntimeOval = RuntimeGeomObj.RuntimeOval, + getPropertyFromString = require("js/lib/rdge/runtime/RuntimeGeomObj").getPropertyFromString; + +/////////////////////////////////////////////////////////////////////// +// Class GLRuntime +// Manages runtime fora WebGL canvas +/////////////////////////////////////////////////////////////////////// +var GLRuntime = function GLRuntime( canvas, importStr ) +{ + /////////////////////////////////////////////////////////////////////// + // Instance variables + /////////////////////////////////////////////////////////////////////// + this._canvas = canvas; + this._context = null; + this._importStr = importStr; + + this.renderer = null; + this.myScene = null; + this.light = null; + this.light2 = null; + this._rootNode = null; + + this._firstRender = true; + this._initialized = false; + + this._useWebGL = false; + + // view parameters + this._fov = 45.0; + this._zNear = 0.1; + this._zFar = 100.0; + this._viewDist = 5.0; + + this._aspect = canvas.width/canvas.height; + + this._geomRoot; + + // all "live" materials + this._materials = []; + + /////////////////////////////////////////////////////////////////////// + // accessors + /////////////////////////////////////////////////////////////////////// + this.getZNear = function() { return this._zNear; } + this.getZFar = function() { return this._zFar; } + this.getFOV = function() { return this._fov; } + this.getAspect = function() { return this._aspect; } + this.getViewDistance = function() { return this._viewDist; } + + this.get2DContext = function() { return this._context; } + + this.getViewportWidth = function() { return this._canvas.width; } + this.getViewportHeight = function() { return this._canvas.height; } + + /////////////////////////////////////////////////////////////////////// + // accessors + /////////////////////////////////////////////////////////////////////// + this.loadScene = function() + { + // parse the data + // the GL runtime must start with a "sceneData: " + var index = importStr.indexOf( "scenedata: " ); + if (index >= 0) + { + this._useWebGL = true; + + var rdgeStr = importStr.substr( index+11 ); + var endIndex = rdgeStr.indexOf( "endscene\n" ); + if (endIndex < 0) throw new Error( "ill-formed WebGL data" ); + var len = endIndex - index + 11; + rdgeStr = rdgeStr.substr( 0, endIndex ); + + this.myScene.importJSON( rdgeStr ); + this.importObjects( importStr ); + this.linkMaterials( this._geomRoot ); + this.initMaterials(); + } + else + { + this._context = this._canvas.getContext( "2d" ); + this.importObjects( importStr ); + this.render(); + } + } + + this.init = function() + { + var ctx1 = g_Engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle), + ctx2 = g_Engine.getContext(); + if (ctx1 != ctx2) console.log( "***** different contexts *****" ); + this.renderer = ctx1.renderer; + + // create a camera, set its perspective, and then point it at the origin + var cam = new camera(); + this._camera = cam; + cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar()); + cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], vec3.up()); + + // make this camera the active camera + this.renderer.cameraManager().setActiveCamera(cam); + + // change clear color + this.renderer.setClearColor([1.0, 1.0, 1.0, 0.0]); + + // create an empty scene graph + this.myScene = new SceneGraph(); + + // load the scene graph data + this.loadScene(); + + // Add the scene to the engine - necessary if you want the engine to draw for you + var name = "myScene" + this._canvas.getAttribute( "data-RDGE-id" ); + g_Engine.AddScene(name, this.myScene); + + this._initialized = true; + } + + // main code for handling user interaction and updating the scene + this.update = function(dt) + { + if (this._initialized) + { + if (!dt) dt = 0.2; + + dt = 0.01; // use our own internal throttle + this.elapsed += dt; + + // changed the global position uniform of light 0, another way to change behavior of a light + rdgeGlobalParameters.u_light0Pos.set( [5*Math.cos(this.elapsed), 5*Math.sin(this.elapsed), 20]); + + // orbit the light nodes around the boxes + //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)]); + //this.light2.setPosition([-1.2*Math.cos(this.elapsed*2.0), 1.2*Math.sin(this.elapsed*2.0), -1.2*Math.cos(this.elapsed)]); + + this.updateMaterials(); + + // now update all the nodes in the scene + this.myScene.update(dt); + } + } + + this.updateMaterials = function() + { + var nMats = this._materials.length; + for (var i=0; i= 0) + { + // update the string to the current object + importStr = importStr.substr( index+7 ); + + // read the next object + var obj = this.importObject( importStr, parent ); + + // determine if we have children + var endIndex = importStr.indexOf( "ENDOBJECT\n" ), + childIndex = importStr.indexOf( "OBJECT\n" ); + if (endIndex < 0) throw new Error( "ill-formed object data" ); + if ((childIndex >= 0) && (childIndex < endIndex)) + { + importStr = importStr.substr( childIndex + 7 ); + importStr = this.importObjects( importStr, obj ); + endIndex = importStr.indexOf( "ENDOBJECT\n" ) + } + + // remove the string for the object(s) just created + importStr = importStr.substr( endIndex ); + + // get the location of the next object + index = importStr.indexOf( "OBJECT\n", endIndex ); + } + + return importStr; + } + + this.importObject = function( objStr, parent ) + { + var type = Number( getPropertyFromString( "type: ", objStr ) ); + + var obj; + switch (type) + { + case 1: + obj = new RuntimeRectangle(); + obj.import( objStr, parent ); + break; + + case 2: // circle + obj = new RuntimeOval(); + obj.import( objStr, parent ); + break; + + case 3: // line + obj = new RuntimeLine(); + obj.import( objStr, parent ); + break; + + default: + throw new Error( "Attempting to load unrecognized object type: " + type ); + break; + } + + if (obj) + this.addObject( obj ); + + return obj; + } + + this.addObject = function( obj, parent ) + { + if (!obj) return; + obj.setWorld( this ); + + if (parent == null) + this._geomRoot = obj; + else + parent.addChild( obj ); + } + + this.linkMaterials = function( obj ) + { + if (!obj) return; + + // get the array of materials from the object + var matArray = obj._materials; + var nMats = matArray.length; + for (var i=0; i= 0); + if (this._useWebGL) + { + var id = canvas.getAttribute( "data-RDGE-id" ); + canvas.rdgeid = id; + g_Engine.registerCanvas(canvas, this); + RDGEStart( canvas ); + } + else + { + this.loadScene(); + } +} + + +if (typeof exports === "object") { + exports.GLRuntime = GLRuntime; +} + + + diff --git a/js/lib/rdge/runtime/RuntimeGeomObj.js b/js/lib/rdge/runtime/RuntimeGeomObj.js new file mode 100644 index 00000000..5b212dce --- /dev/null +++ b/js/lib/rdge/runtime/RuntimeGeomObj.js @@ -0,0 +1,630 @@ + +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever t