aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/runtime
diff options
context:
space:
mode:
authorhwc4872012-02-22 15:22:13 -0800
committerhwc4872012-02-22 15:22:13 -0800
commit2bb869eb1c0b71e379f159eb0f95dfa168496d8d (patch)
treea87c5bed45925b20ce0394d6ff22280e9afaf02c /js/helper-classes/RDGE/runtime
parentf5a9123ba661fe83442a873a05bc6a86edc396f8 (diff)
downloadninja-2bb869eb1c0b71e379f159eb0f95dfa168496d8d.tar.gz
Runtime classes
Diffstat (limited to 'js/helper-classes/RDGE/runtime')
-rw-r--r--js/helper-classes/RDGE/runtime/CanvasLoader.js72
-rw-r--r--js/helper-classes/RDGE/runtime/GLRuntime.js159
2 files changed, 231 insertions, 0 deletions
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 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7
8
9///////////////////////////////////////////////////////////////////////
10// Class ShapeRuntime
11// Manages runtime shape display
12///////////////////////////////////////////////////////////////////////
13function CanvasLoader( root, valueArray, loadForAuthoring )
14{
15 var value = valueArray;
16 var nWorlds = value.length;
17 for (var i=0; i<nWorlds; i++)
18 {
19 var importStr = value[i];
20 var startIndex = importStr.indexOf( "id: " );
21 if (startIndex >= 0)
22 {
23 var endIndex = importStr.indexOf( "\n", startIndex );
24 if (endIndex > 0)
25 {
26 var id = importStr.substring( startIndex+4, endIndex );
27 var canvas = this.findCanvasWithID( id, root );
28 if (canvas)
29 {
30 if (loadForAuthoring)
31 {
32 if (!canvas.elementModel)
33 {
34 NJUtils.makeElementModel(canvas, "Canvas", "shape", true);
35 }
36
37 if (canvas.elementModel)
38 {
39 if (canvas.elementModel.shapeModel.GLWorld)
40 canvas.elementModel.shapeModel.GLWorld.clearTree();
41
42 var world = new GLWorld( canvas );
43 canvas.elementModel.shapeModel.GLWorld = world;
44 world.import( importStr );
45 }
46 }
47 else
48 {
49 var rt = new GLRuntime( canvas, importStr );
50 }
51 }
52 }
53 }
54 }
55
56 this.findCanvasWithID = function( id, elt )
57 {
58 var cid = elt.getAttribute( "data-RDGE-id" );
59 if (cid == id) return elt;
60
61 if (elt.children)
62 {
63 var nKids = elt.children.length;
64 for (var i=0; i<nKids; i++)
65 {
66 var child = elt.children[i];
67 this.findCanvasWithID( id, child );
68 }
69 }
70 }
71}
72
diff --git a/js/helper-classes/RDGE/runtime/GLRuntime.js b/js/helper-classes/RDGE/runtime/GLRuntime.js
new file mode 100644
index 00000000..5c99be02
--- /dev/null
+++ b/js/helper-classes/RDGE/runtime/GLRuntime.js
@@ -0,0 +1,159 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7
8
9///////////////////////////////////////////////////////////////////////
10// Class GLRuntime
11// Manages runtime fora WebGL canvas
12///////////////////////////////////////////////////////////////////////
13function GLRuntime( canvas, importStr )
14{
15 ///////////////////////////////////////////////////////////////////////
16 // Instance variables
17 ///////////////////////////////////////////////////////////////////////
18 this._canvas = canvas;
19 this._importStr = importStr;
20
21 this.renderer = null;
22 this.myScene = null;
23 this.light = null;
24 this.light2 = null;
25 this._rootNode = null;
26
27 this._firstRender = true;
28
29 ///////////////////////////////////////////////////////////////////////
30 // initialization code
31 ///////////////////////////////////////////////////////////////////////
32 var id = canvas.getAttribute( "data-RDGE-id" );
33 canvas.rdgeid = id;
34 g_Engine.registerCanvas(canvas, this);
35 RDGEStart( canvas );
36
37 this.loadScene = function()
38 {
39 // parse the data
40 // the GL runtime must start with a "sceneData: "
41 var index = importStr.indexOf( "scenedata: " );
42 if (index >= 0)
43 {
44 var rdgeStr = importStr.substr( index+11 );
45 var endIndex = rdgeStr.indexOf( "endscene\n" );
46 if (endIndex < 0) throw new Error( "ill-formed WebGL data" );
47 var len = endIndex - index + 11;
48 rdgeStr = rdgeStr.substr( 0, endIndex );
49
50 this.myScene.importJSON( rdgeStr );
51 }
52 }
53
54 this.init = function()
55 {
56 var ctx1 = g_Engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle),
57 ctx2 = g_Engine.getContext();
58 if (ctx1 != ctx2) console.log( "***** different contexts *****" );
59 this.renderer = ctx1.renderer;
60
61 // create a camera, set its perspective, and then point it at the origin
62 var cam = new camera();
63 this._camera = cam;
64 cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar());
65 cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], vec3.up());
66
67 // make this camera the active camera
68 this.renderer.cameraManager().setActiveCamera(cam);
69
70 // change clear color
71 this.renderer.setClearColor([1.0, 1.0, 1.0, 0.0]);
72
73 // create an empty scene graph
74 this.myScene = new SceneGraph();
75 this.loadScene();
76
77 /*
78 // create some lights
79 // light 1
80 this.light = createLightNode("myLight");
81 this.light.setPosition([0,0,1.2]);
82 this.light.setDiffuseColor([0.75,0.9,1.0,1.0]);
83
84 // light 2
85 this.light2 = createLightNode("myLight2");
86 this.light2.setPosition([-0.5,0,1.2]);
87 this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]);
88
89 // create a light transform
90 var lightTr = createTransformNode("lightTr");
91
92 // create and attach a material - materials hold the light data
93 lightTr.attachMaterial(createMaterialNode("lights"));
94
95 // enable light channels 1, 2 - channel 0 is used by the default shader
96 lightTr.materialNode.enableLightChannel(1, this.light);
97 lightTr.materialNode.enableLightChannel(2, this.light2);
98
99 // all added objects are parented to the light node
100 this._rootNode = lightTr;
101
102 // add the light node to the scene
103 this.myScene.addNode(lightTr);
104 */
105
106 // load the scene graph data
107
108 // Add the scene to the engine - necessary if you want the engine to draw for you
109 var name = "myScene" + this._canvas.getAttribute( "data-RDGE-id" );
110 g_Engine.AddScene(name, this.myScene);
111 }
112
113 // main code for handling user interaction and updating the scene
114 this.update = function(dt)
115 {
116 if (!dt) dt = 0.2;
117
118 dt = 0.01; // use our own internal throttle
119 this.elapsed += dt;
120
121 // changed the global position uniform of light 0, another way to change behavior of a light
122 rdgeGlobalParameters.u_light0Pos.set( [5*Math.cos(this.elapsed), 5*Math.sin(this.elapsed), 20]);
123
124 // orbit the light nodes around the boxes
125 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)]);
126 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)]);
127
128 // now update all the nodes in the scene
129 this.myScene.update(dt);
130 }
131
132 // defining the draw function to control how the scene is rendered
133 this.draw = function()
134 {
135 g_Engine.setContext( this._canvas.rdgeid );
136
137 var ctx = g_Engine.getContext();
138 var renderer = ctx.renderer;
139 if (renderer.unloadedTextureCount <= 0)
140 {