aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge')
-rw-r--r--js/lib/rdge/CanvasRuntime.js1338
1 files changed, 1338 insertions, 0 deletions
diff --git a/js/lib/rdge/CanvasRuntime.js b/js/lib/rdge/CanvasRuntime.js
new file mode 100644
index 00000000..d16613ca
--- /dev/null
+++ b/js/lib/rdge/CanvasRuntime.js
@@ -0,0 +1,1338 @@
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// Class ShapeRuntime
10// Manages runtime shape display
11///////////////////////////////////////////////////////////////////////
12function CanvasDataManager()
13{
14 this.loadGLData = function(root, valueArray )
15 {
16 var value = valueArray;
17 var nWorlds = value.length;
18 for (var i=0; i<nWorlds; i++)
19 {
20 var importStr = value[i];
21 var startIndex = importStr.indexOf( "id: " );
22 if (startIndex >= 0)
23 {
24 var endIndex = importStr.indexOf( "\n", startIndex );
25 if (endIndex > 0)
26 {
27 var id = importStr.substring( startIndex+4, endIndex );
28 var canvas = this.findCanvasWithID( id, root );
29 if (canvas)
30 {
31 var rt = new GLRuntime( canvas, importStr );
32 }
33 }
34 }
35 }
36 }
37
38 this.collectGLData = function( elt, dataArray )
39 {
40 if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld)
41 {
42 var data = elt.elementModel.shapeModel.GLWorld.export( true );
43 dataArray.push( data );
44 }
45
46 if (elt.children)
47 {
48 var nKids = elt.children.length;
49 for (var i=0; i<nKids; i++)
50 {
51 var child = elt.children[i];
52 this.collectGLData( child, dataArray );
53 }
54 }
55 }
56
57 this.findCanvasWithID = function( id, elt )
58 {
59 var cid = elt.getAttribute( "data-RDGE-id" );
60 if (cid == id) return elt;
61
62 if (elt.children)
63 {
64 var nKids = elt.children.length;
65 for (var i=0; i<nKids; i++)
66 {
67 var child = elt.children[i];
68 var foundElt = this.findCanvasWithID( id, child );
69 if (foundElt) return foundElt;
70 }
71 }
72 }
73}
74
75///////////////////////////////////////////////////////////////////////
76// Class GLRuntime
77// Manages runtime fora WebGL canvas
78///////////////////////////////////////////////////////////////////////
79function GLRuntime( canvas, importStr )
80{
81 ///////////////////////////////////////////////////////////////////////
82 // Instance variables
83 ///////////////////////////////////////////////////////////////////////
84 this._canvas = canvas;
85 this._context = null;
86 this._importStr = importStr;
87
88 this.renderer = null;
89 this.myScene = null;
90 this.light = null;
91 this.light2 = null;
92 this._rootNode = null;
93
94 this._firstRender = true;
95 this._initialized = false;
96
97 this._useWebGL = false;
98
99 // view parameters
100 this._fov = 45.0;
101 this._zNear = 0.1;
102 this._zFar = 100.0;
103 this._viewDist = 5.0;
104
105 this._aspect = canvas.width/canvas.height;
106
107 this._geomRoot;
108
109 // all "live" materials
110 this._materials = [];
111
112 ///////////////////////////////////////////////////////////////////////
113 // accessors
114 ///////////////////////////////////////////////////////////////////////
115 this.getZNear = function() { return this._zNear; }
116 this.getZFar = function() { return this._zFar; }
117 this.getFOV = function() { return this._fov; }
118 this.getAspect = function() { return this._aspect; }
119 this.getViewDistance = function() { return this._viewDist; }
120
121 this.get2DContext = function() { return this._context; }
122
123 this.getViewportWidth = function() { return this._canvas.width; }
124 this.getViewportHeight = function() { return this._canvas.height; }
125
126 ///////////////////////////////////////////////////////////////////////
127 // accessors
128 ///////////////////////////////////////////////////////////////////////
129 this.loadScene = function()
130 {
131 // parse the data
132 // the GL runtime must start with a "sceneData: "
133 var index = importStr.indexOf( "scenedata: " );
134 if (index >= 0)
135 {
136 this._useWebGL = true;
137
138 var rdgeStr = importStr.substr( index+11 );
139 var endIndex = rdgeStr.indexOf( "endscene\n" );
140 if (endIndex < 0) throw new Error( "ill-formed WebGL data" );
141 var len = endIndex - index + 11;
142 rdgeStr = rdgeStr.substr( 0, endIndex );
143
144 this.myScene.importJSON( rdgeStr );
145 this.importObjects( importStr );
146 this.linkMaterials( this._geomRoot );
147 this.initMaterials();
148 }
149 else
150 {
151 this._context = this._canvas.getContext( "2d" );
152 this.importObjects( importStr );
153 this.render();
154 }
155 }
156
157 this.init = function()
158 {
159 var ctx1 = g_Engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle),
160 ctx2 = g_Engine.getContext();
161 if (ctx1 != ctx2) console.log( "***** different contexts *****" );
162 this.renderer = ctx1.renderer;
163
164 // create a camera, set its perspective, and then point it at the origin
165 var cam = new camera();
166 this._camera = cam;
167 cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar());
168 cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], vec3.up());
169
170 // make this camera the active camera
171 this.renderer.cameraManager().setActiveCamera(cam);
172
173 // change clear color
174 this.renderer.setClearColor([1.0, 1.0, 1.0, 0.0]);
175
176 // create an empty scene graph
177 this.myScene = new SceneGraph();
178
179 // load the scene graph data
180 this.loadScene();
181
182 // Add the scene to the engine - necessary if you want the engine to draw for you
183 var name = "myScene" + this._canvas.getAttribute( "data-RDGE-id" );
184 g_Engine.AddScene(name, this.myScene);
185
186 this._initialized = true;
187 }
188
189 // main code for handling user interaction and updating the scene
190 this.update = function(dt)
191 {
192 if (this._initialized)
193 {
194 if (!dt) dt = 0.2;
195
196 dt = 0.01; // use our own internal throttle
197 this.elapsed += dt;
198
199 // changed the global position uniform of light 0, another way to change behavior of a light
200 rdgeGlobalParameters.u_light0Pos.set( [5*Math.cos(this.elapsed), 5*Math.sin(this.elapsed), 20]);
201
202 // orbit the light nodes around the boxes
203 //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)]);
204 //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)]);
205
206 this.updateMaterials();
207
208 // now update all the nodes in the scene
209 this.myScene.update(dt);
210 }
211 }
212
213 this.updateMaterials = function()
214 {
215 var nMats = this._materials.length;
216 for (var i=0; i<nMats; i++)
217 {
218 var mat = this._materials[i];
219 mat.update();
220 }
221 }
222
223 // defining the draw function to control how the scene is rendered
224 this.draw = function()
225 {
226 if (this._initialized)
227 {
228 g_Engine.setContext( this._canvas.rdgeid );
229
230 var ctx = g_Engine.getContext();
231 var renderer = ctx.renderer;
232 if (renderer.unloadedTextureCount <= 0)
233 {
234 renderer.disableCulling();