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