/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
</copyright> */
// namespace for the Ninja Canvas Runtime
var NinjaCvsRt = NinjaCvsRt || {};
///////////////////////////////////////////////////////////////////////
//Loading webGL/canvas data
NinjaCvsRt.initWebGl = function (rootElement, directory) {
var cvsDataMngr, ninjaWebGlData = JSON.parse((document.querySelectorAll(['script[data-ninja-webgl]'])[0].innerHTML.replace('(', '')).replace(')', ''));
if (ninjaWebGlData && ninjaWebGlData.data) {
for (var n=0; ninjaWebGlData.data[n]; n++) {
ninjaWebGlData.data[n] = unescape(ninjaWebGlData.data[n]);
}
}
//Creating data manager
cvsDataMngr = new NinjaCvsRt.CanvasDataManager();
//Loading data to canvas(es)
cvsDataMngr.loadGLData(rootElement, ninjaWebGlData.data, directory);
};
///////////////////////////////////////////////////////////////////////
// Class ShapeRuntime
// Manages runtime shape display
///////////////////////////////////////////////////////////////////////
NinjaCvsRt.CanvasDataManager = function ()
{
this.loadGLData = function(root, valueArray, assetPath )
{
if (assetPath)
this._assetPath = assetPath.slice();
var value = valueArray;
var nWorlds = value.length;
for (var i=0; i<nWorlds; i++)
{
var importStr = value[i];
// there should be some version information in
// the form of 'v0.0;' Pull that off. (the trailing ';' should
// be in the first 24 characters).
var index = importStr.indexOf( ';' );
if ((importStr[0] === 'v') && (index < 24))
{
// JSON format. pull off the version info
importStr = importStr.substr( index+1 );
var jObj = JSON.parse( importStr );
var id = jObj.id;
if (id)
{
var canvas = this.findCanvasWithID( id, root );
if (canvas)
{
new NinjaCvsRt.GLRuntime( canvas, jObj, assetPath );
}
}
}
}
};
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<nKids; i++)
{
var child = elt.children[i];
var foundElt = this.findCanvasWithID( id, child );
if (foundElt) return foundElt;
}
}
};
};
///////////////////////////////////////////////////////////////////////
// Class GLRuntime
// Manages runtime fora WebGL canvas
///////////////////////////////////////////////////////////////////////
NinjaCvsRt.GLRuntime = function ( canvas, jObj, assetPath )
{
///////////////////////////////////////////////////////////////////////
// Instance variables
///////////////////////////////////////////////////////////////////////
this._canvas = canvas;
this._context = null;
//this._importStr = importStr;
this._jObj = jObj;
this.renderer = null;
this.myScene = null;
this.light = null;
this.light2 = null;
this._rootNode = null;
this._firstRender = true;
this._initialized = false;
this._useWebGL = false;
this._assetPath = undefined;
// view parameters
this._fov = 45.0;
this._zNear = 0.1;
this._zFar = 100.0;
this._viewDist = 5.0;
this.elapsed = 0;
this._aspect = canvas.width/canvas.height;
this._geomRoot = null;
// all "live" materials
this._materials = [];
if(this._assetPath !== undefined) {
RDGE.globals.engine.setAssetPath(this._assetPath);
}
///////////////////////////////////////////////////////////////////////
// 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()
{
var jObj = this._jObj;
if (!jObj.children || (jObj.children.length != 1))
throw new Error( "ill-formed JSON for runtime load: " + jObj );
var root = jObj.children[0];
// parse the data
if (jObj.scenedata)
{
this._useWebGL = true;
var rdgeStr = jObj.scenedata;
this.myScene.importJSON( rdgeStr );
this.importObjects( root );
this.linkMaterials( this._geomRoot );
this.initMaterials();
this.linkLights();
}
else
{
this._context = this._canvas.getContext( "2d" );
this.importObjects( root );
this.render();
}
};
this.init = function()
{
var ctx1 = RDGE.globals.engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle),
ctx2 = RDGE.globals.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 RDGE.camera();
this._camera = cam;
cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar());
cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], RDGE.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 RDGE.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" );
RDGE.globals.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;
|