From 2bb869eb1c0b71e379f159eb0f95dfa168496d8d Mon Sep 17 00:00:00 2001 From: hwc487 Date: Wed, 22 Feb 2012 15:22:13 -0800 Subject: Runtime classes --- js/helper-classes/RDGE/GLCircle.js | 2 +- js/helper-classes/RDGE/GLLine.js | 2 +- js/helper-classes/RDGE/GLRectangle.js | 2 +- js/helper-classes/RDGE/GLWorld.js | 29 ++--- js/helper-classes/RDGE/runtime/CanvasLoader.js | 72 +++++++++++ js/helper-classes/RDGE/runtime/GLRuntime.js | 159 +++++++++++++++++++++++++ 6 files changed, 246 insertions(+), 20 deletions(-) create mode 100644 js/helper-classes/RDGE/runtime/CanvasLoader.js create mode 100644 js/helper-classes/RDGE/runtime/GLRuntime.js (limited to 'js') diff --git a/js/helper-classes/RDGE/GLCircle.js b/js/helper-classes/RDGE/GLCircle.js index 5b32547e..712544c0 100755 --- a/js/helper-classes/RDGE/GLCircle.js +++ b/js/helper-classes/RDGE/GLCircle.js @@ -135,7 +135,7 @@ function GLCircle() if (!world._useWebGL) return; // make sure RDGE has the correct context - g_Engine.setContext( world.getCanvas().uuid ); + g_Engine.setContext( world.getCanvas().rdgeid ); // create the gl buffer var gl = world.getGLContext(); diff --git a/js/helper-classes/RDGE/GLLine.js b/js/helper-classes/RDGE/GLLine.js index 0d815145..f715a43c 100755 --- a/js/helper-classes/RDGE/GLLine.js +++ b/js/helper-classes/RDGE/GLLine.js @@ -171,7 +171,7 @@ function GLLine( world, xOffset, yOffset, width, height, slope, strokeSize, stro if (!world._useWebGL) return; // make sure RDGE has the correct context - g_Engine.setContext( world.getCanvas().uuid ); + g_Engine.setContext( world.getCanvas().rdgeid ); // create the gl buffer var gl = world.getGLContext(); diff --git a/js/helper-classes/RDGE/GLRectangle.js b/js/helper-classes/RDGE/GLRectangle.js index 5b6ff94f..3c1cb7dc 100755 --- a/js/helper-classes/RDGE/GLRectangle.js +++ b/js/helper-classes/RDGE/GLRectangle.js @@ -214,7 +214,7 @@ function GLRectangle() if (!world._useWebGL) return; // make sure RDGE has the correct context - g_Engine.setContext( world.getCanvas().uuid ); + g_Engine.setContext( world.getCanvas().rdgeid ); // create the gl buffer var gl = world.getGLContext(); diff --git a/js/helper-classes/RDGE/GLWorld.js b/js/helper-classes/RDGE/GLWorld.js index 1edd5cff..0addcadc 100755 --- a/js/helper-classes/RDGE/GLWorld.js +++ b/js/helper-classes/RDGE/GLWorld.js @@ -136,13 +136,6 @@ function GLWorld( canvas, use3D ) var camMat = Matrix.I(4); camMat[14] = this.getViewDistance(); this.setCameraMat( camMat ); - - ////////////////////////////////////////// - // test call to SVG importer -// console.log( "***** SVG TEST *****" ); -// var svgImporter = new SVGParse( this ); -// svgImporter.importSVG(); - ////////////////////////////////////////// // post-load processing of the scene this.init = function() @@ -198,7 +191,9 @@ function GLWorld( canvas, use3D ) this.myScene.addNode(lightTr); // Add the scene to the engine - necessary if you want the engine to draw for you - g_Engine.AddScene("myScene" + this._canvas.id, this.myScene); + //g_Engine.AddScene("myScene" + this._canvas.id, this.myScene); + var name = this._canvas.getAttribute( "data-RDGE-id" ); + g_Engine.AddScene("myScene" + name, this.myScene); } // main code for handling user interaction and updating the scene @@ -227,11 +222,11 @@ function GLWorld( canvas, use3D ) } // defining the draw function to control how the scene is rendered - this.draw = function() + this.draw = function() { if (this._useWebGL) { - g_Engine.setContext( this._canvas.uuid ); + g_Engine.setContext( this._canvas.rdgeid ); var ctx = g_Engine.getContext(); var renderer = ctx.renderer; if (renderer.unloadedTextureCount <= 0) @@ -391,12 +386,10 @@ function GLWorld( canvas, use3D ) if (this._useWebGL) { rdgeStarted = true; - this._canvas.rdgeid = this._canvas.uuid; + var id = this._canvas.getAttribute( "data-RDGE-id" ); + this._canvas.rdgeid = id; g_Engine.registerCanvas(this._canvas, this); RDGEStart( this._canvas ); - - //this._canvas.fpsTracker = new fpsTracker( '0' ); - //this._canvas.task = new RDGETask(this._canvas, false); this._canvas.task.stop() } } @@ -765,7 +758,7 @@ GLWorld.prototype.render = function() else { // console.log( "GLWorld.render, " + this._worldCount ); - g_Engine.setContext( this._canvas.uuid ); + g_Engine.setContext( this._canvas.rdgeId ); //this.draw(); this.restartRenderLoop(); } @@ -821,7 +814,9 @@ GLWorld.prototype.getShapeFromPoint = function( offsetX, offsetY ) GLWorld.prototype.export = function() { var exportStr = "GLWorld 1.0\n"; - exportStr += "id: " + this._canvas.rdgeid + "\n"; + var id = this.getCanvas().getAttribute( "data-RDGE-id" ); + exportStr += "id: " + id + "\n"; + //exportStr += "id: " + this._canvas.rdgeid + "\n"; exportStr += "fov: " + this._fov + "\n"; exportStr += "zNear: " + this._zNear + "\n"; exportStr += "zFar: " + this._zFar + "\n"; @@ -829,7 +824,7 @@ GLWorld.prototype.export = function() // we need 2 export modes: One for save/restore, one for publish. // hardcoding for now - var exportForPublish = false; + var exportForPublish = true; exportStr += "publish: " + exportForPublish + "\n"; if (exportForPublish) diff --git a/js/helper-classes/RDGE/runtime/CanvasLoader.js b/js/helper-classes/RDGE/runtime/CanvasLoader.js new file mode 100644 index 00000000..12a985d3 --- /dev/null +++ b/js/helper-classes/RDGE/runtime/CanvasLoader.js @@ -0,0 +1,72 @@ +/* +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. +
*/ + + + +/////////////////////////////////////////////////////////////////////// +// Class ShapeRuntime +// Manages runtime shape display +/////////////////////////////////////////////////////////////////////// +function CanvasLoader( root, valueArray, loadForAuthoring ) +{ + 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) + { + if (loadForAuthoring) + { + if (!canvas.elementModel) + { + NJUtils.makeElementModel(canvas, "Canvas", "shape", true); + } + + if (canvas.elementModel) + { + if (canvas.elementModel.shapeModel.GLWorld) + canvas.elementModel.shapeModel.GLWorld.clearTree(); + + var world = new GLWorld( canvas ); + canvas.elementModel.shapeModel.GLWorld = world; + world.import( importStr ); + } + } + else + { + var rt = new GLRuntime( canvas, importStr ); + } + } + } + } + } + + this.findCanvasWithID = function( id, elt ) + { + var cid = elt.getAttribute( "data-RDGE-id" ); + if (cid == id) return elt; + + 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. + */ + + + +/////////////////////////////////////////////////////////////////////// +// Class GLRuntime +// Manages runtime fora WebGL canvas +/////////////////////////////////////////////////////////////////////// +function GLRuntime( canvas, importStr ) +{ + /////////////////////////////////////////////////////////////////////// + // Instance variables + /////////////////////////////////////////////////////////////////////// + this._canvas = canvas; + this._importStr = importStr; + + this.renderer = null; + this.myScene = null; + this.light = null; + this.light2 = null; + this._rootNode = null; + + this._firstRender = true; + + /////////////////////////////////////////////////////////////////////// + // initialization code + /////////////////////////////////////////////////////////////////////// + var id = canvas.getAttribute( "data-RDGE-id" ); + canvas.rdgeid = id; + g_Engine.registerCanvas(canvas, this); + RDGEStart( canvas ); + + this.loadScene = function() + { + // parse the data + // the GL runtime must start with a "sceneData: " + 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 ); + } + } + + 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(); + this.loadScene(); + + /* + // create some lights + // light 1 + this.light = createLightNode("myLight"); + this.light.setPosition([0,0,1.2]); + this.light.setDiffuseColor([0.75,0.9,1.0,1.0]); + + // light 2 + this.light2 = createLightNode("myLight2"); + this.light2.setPosition([-0.5,0,1.2]); + this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]); + + // create a light transform + var lightTr = createTransformNode("lightTr"); + + // create and attach a material - materials hold the light data + lightTr.attachMaterial(createMaterialNode("lights")); + + // enable light channels 1, 2 - channel 0 is used by the default shader + lightTr.materialNode.enableLightChannel(1, this.light); + lightTr.materialNode.enableLightChannel(2, this.light2); + + // all added objects are parented to the light node + this._rootNode = lightTr; + + // add the light node to the scene + this.myScene.addNode(lightTr); + */ + + // load the scene graph data + + // 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); + } + + // main code for handling user interaction and updating the scene + this.update = function(dt) + { + 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)]); + + // now update all the nodes in the scene + this.myScene.update(dt); + } + + // defining the draw function to control how the scene is rendered + this.draw = function() + { + g_Engine.setContext( this._canvas.rdgeid ); + + var ctx = g_Engine.getContext(); + var renderer = ctx.renderer; + if (renderer.unloadedTextureCount <= 0) + { + renderer.disableCulling(); + renderer._clear(); + this.myScene.render(); + + if (this._firstRender) + { + if (this._canvas.task) + { + this._firstRender = false; + this._canvas.task.stop(); + } + } + } + } +} + + + + -- cgit v1.2.3