From 07c48708a99b94a220c043ad5951a331bbd4fc2d Mon Sep 17 00:00:00 2001 From: hwc487 Date: Wed, 29 Feb 2012 15:23:28 -0800 Subject: WebGL file I/O --- js/helper-classes/RDGE/runtime/GLRuntime.js | 87 +++++++++++++++++++---------- 1 file changed, 56 insertions(+), 31 deletions(-) (limited to 'js/helper-classes/RDGE/runtime/GLRuntime.js') diff --git a/js/helper-classes/RDGE/runtime/GLRuntime.js b/js/helper-classes/RDGE/runtime/GLRuntime.js index 5c99be02..33186174 100644 --- a/js/helper-classes/RDGE/runtime/GLRuntime.js +++ b/js/helper-classes/RDGE/runtime/GLRuntime.js @@ -25,14 +25,24 @@ function GLRuntime( canvas, importStr ) this._rootNode = null; this._firstRender = true; + this._initialized = 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; /////////////////////////////////////////////////////////////////////// - // initialization code + // accessors /////////////////////////////////////////////////////////////////////// - var id = canvas.getAttribute( "data-RDGE-id" ); - canvas.rdgeid = id; - g_Engine.registerCanvas(canvas, this); - RDGEStart( canvas ); + 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.loadScene = function() { @@ -72,8 +82,7 @@ function GLRuntime( canvas, importStr ) // create an empty scene graph this.myScene = new SceneGraph(); - this.loadScene(); - + /* // create some lights // light 1 @@ -104,54 +113,70 @@ function GLRuntime( canvas, importStr ) */ // 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); + //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 (!dt) dt = 0.2; + if (this._initialized) + { + if (!dt) dt = 0.2; - dt = 0.01; // use our own internal throttle - this.elapsed += dt; + 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]); + // 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)]); + // 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); + // 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) + if (this._initialized) { - renderer.disableCulling(); - renderer._clear(); - this.myScene.render(); + g_Engine.setContext( this._canvas.rdgeid ); - if (this._firstRender) + var ctx = g_Engine.getContext(); + var renderer = ctx.renderer; + if (renderer.unloadedTextureCount <= 0) { - if (this._canvas.task) + renderer.disableCulling(); + renderer._clear(); + this.myScene.render(); + + if (this._firstRender) { - this._firstRender = false; - this._canvas.task.stop(); + if (this._canvas.task) + { + this._firstRender = false; + //this._canvas.task.stop(); + } } } } } + + // start RDGE + var id = canvas.getAttribute( "data-RDGE-id" ); + canvas.rdgeid = id; + g_Engine.registerCanvas(canvas, this); + RDGEStart( canvas ); + } -- cgit v1.2.3 From d044b6d9755d8ca686501fc3dd5644180e2ffbf0 Mon Sep 17 00:00:00 2001 From: hwc487 Date: Fri, 2 Mar 2012 09:36:31 -0800 Subject: WebGL File IO --- js/helper-classes/RDGE/runtime/GLRuntime.js | 182 +++++++++++++++++++++++----- 1 file changed, 150 insertions(+), 32 deletions(-) (limited to 'js/helper-classes/RDGE/runtime/GLRuntime.js') diff --git a/js/helper-classes/RDGE/runtime/GLRuntime.js b/js/helper-classes/RDGE/runtime/GLRuntime.js index 33186174..d86506ad 100644 --- a/js/helper-classes/RDGE/runtime/GLRuntime.js +++ b/js/helper-classes/RDGE/runtime/GLRuntime.js @@ -35,6 +35,11 @@ function GLRuntime( canvas, importStr ) this._aspect = canvas.width/canvas.height; + this._geomRoot; + + // all "live" materials + this._materials = []; + /////////////////////////////////////////////////////////////////////// // accessors /////////////////////////////////////////////////////////////////////// @@ -58,6 +63,9 @@ function GLRuntime( canvas, importStr ) rdgeStr = rdgeStr.substr( 0, endIndex ); this.myScene.importJSON( rdgeStr ); + this.importObjects( importStr ); + this.linkMaterials( this._geomRoot ); + this.initMaterials(); } } @@ -82,42 +90,13 @@ function GLRuntime( canvas, importStr ) // create an empty scene graph this.myScene = new SceneGraph(); - - /* - // 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 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); + var name = "myScene" + this._canvas.getAttribute( "data-RDGE-id" ); + g_Engine.AddScene(name, this.myScene); this._initialized = true; } @@ -139,11 +118,23 @@ function GLRuntime( canvas, importStr ) //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; + + 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) { + 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" ); @@ -67,6 +80,12 @@ function GLRuntime( canvas, importStr ) this.linkMaterials( this._geomRoot ); this.initMaterials(); } + else + { + this._context = this._canvas.getContext( "2d" ); + this.importObjects( importStr ); + this.render(); + } } this.init = function() @@ -230,6 +249,7 @@ function GLRuntime( canvas, importStr ) this.addObject = function( obj, parent ) { if (!obj) return; + obj.setWorld( this ); if (parent == null) this._geomRoot = obj; @@ -290,11 +310,35 @@ function GLRuntime( canvas, importStr ) } } - // start RDGE - var id = canvas.getAttribute( "data-RDGE-id" ); - canvas.rdgeid = id; - g_Engine.registerCanvas(canvas, this); - RDGEStart( canvas ); + this.render = function( obj ) + { + if (!obj) obj = this._geomRoot; + obj.render(); + + if (obj.children) + { + var nKids = obj.children.length; + for (var i=0; i= 0); if (this._useWebGL) { var id = canvas.getAttribute( "data-RDGE-id" ); -- cgit v1.2.3 From 818582d389f504c915be0c9052fafa33e3e76c92 Mon Sep 17 00:00:00 2001 From: hwc487 Date: Wed, 7 Mar 2012 16:48:48 -0800 Subject: File IO --- js/helper-classes/RDGE/runtime/GLRuntime.js | 348 ---------------------------- 1 file changed, 348 deletions(-) delete mode 100644 js/helper-classes/RDGE/runtime/GLRuntime.js (limited to 'js/helper-classes/RDGE/runtime/GLRuntime.js') diff --git a/js/helper-classes/RDGE/runtime/GLRuntime.js b/js/helper-classes/RDGE/runtime/GLRuntime.js deleted file mode 100644 index 58cb4e33..00000000 --- a/js/helper-classes/RDGE/runtime/GLRuntime.js +++ /dev/null @@ -1,348 +0,0 @@ -/* -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._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(); - } -} - - - - -- cgit v1.2.3