diff options
Diffstat (limited to 'assets/canvas-runtime.js')
-rw-r--r-- | assets/canvas-runtime.js | 429 |
1 files changed, 201 insertions, 228 deletions
diff --git a/assets/canvas-runtime.js b/assets/canvas-runtime.js index f53a4ef6..2ca76962 100644 --- a/assets/canvas-runtime.js +++ b/assets/canvas-runtime.js | |||
@@ -4,11 +4,12 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | 4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. |
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | 7 | // namespace for the Ninja Canvas Runtime | |
8 | var NinjaCvsRt = NinjaCvsRt || {}; | ||
8 | 9 | ||
9 | /////////////////////////////////////////////////////////////////////// | 10 | /////////////////////////////////////////////////////////////////////// |
10 | //Loading webGL/canvas data | 11 | //Loading webGL/canvas data |
11 | function initWebGl (rootElement, directory) { | 12 | NinjaCvsRt.initWebGl = function (rootElement, directory) { |
12 | var cvsDataMngr, ninjaWebGlData = JSON.parse((document.querySelectorAll(['script[data-ninja-webgl]'])[0].innerHTML.replace('(', '')).replace(')', '')); | 13 | var cvsDataMngr, ninjaWebGlData = JSON.parse((document.querySelectorAll(['script[data-ninja-webgl]'])[0].innerHTML.replace('(', '')).replace(')', '')); |
13 | if (ninjaWebGlData && ninjaWebGlData.data) { | 14 | if (ninjaWebGlData && ninjaWebGlData.data) { |
14 | for (var n=0; ninjaWebGlData.data[n]; n++) { | 15 | for (var n=0; ninjaWebGlData.data[n]; n++) { |
@@ -16,16 +17,16 @@ function initWebGl (rootElement, directory) { | |||
16 | } | 17 | } |
17 | } | 18 | } |
18 | //Creating data manager | 19 | //Creating data manager |
19 | cvsDataMngr = new CanvasDataManager(); | 20 | cvsDataMngr = new NinjaCvsRt.CanvasDataManager(); |
20 | //Loading data to canvas(es) | 21 | //Loading data to canvas(es) |
21 | cvsDataMngr.loadGLData(rootElement, ninjaWebGlData.data, directory); | 22 | cvsDataMngr.loadGLData(rootElement, ninjaWebGlData.data, directory); |
22 | } | 23 | }; |
23 | 24 | ||
24 | /////////////////////////////////////////////////////////////////////// | 25 | /////////////////////////////////////////////////////////////////////// |
25 | // Class ShapeRuntime | 26 | // Class ShapeRuntime |
26 | // Manages runtime shape display | 27 | // Manages runtime shape display |
27 | /////////////////////////////////////////////////////////////////////// | 28 | /////////////////////////////////////////////////////////////////////// |
28 | function CanvasDataManager() | 29 | NinjaCvsRt.CanvasDataManager = function () |
29 | { | 30 | { |
30 | this.loadGLData = function(root, valueArray, assetPath ) | 31 | this.loadGLData = function(root, valueArray, assetPath ) |
31 | { | 32 | { |
@@ -54,31 +55,12 @@ function CanvasDataManager() | |||
54 | var canvas = this.findCanvasWithID( id, root ); | 55 | var canvas = this.findCanvasWithID( id, root ); |
55 | if (canvas) | 56 | if (canvas) |
56 | { | 57 | { |
57 | new GLRuntime( canvas, jObj, assetPath ); | 58 | new NinjaCvsRt.GLRuntime( canvas, jObj, assetPath ); |
58 | } | 59 | } |
59 | } | 60 | } |
60 | } | 61 | } |
61 | } | 62 | } |
62 | } | 63 | }; |
63 | |||
64 | this.collectGLData = function( elt, dataArray ) | ||
65 | { | ||
66 | if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld) | ||
67 | { | ||
68 | var data = elt.elementModel.shapeModel.GLWorld.export( true ); | ||
69 | dataArray.push( data ); | ||
70 | } | ||
71 | |||
72 | if (elt.children) | ||
73 | { | ||
74 | var nKids = elt.children.length; | ||
75 | for (var i=0; i<nKids; i++) | ||
76 | { | ||
77 | var child = elt.children[i]; | ||
78 | this.collectGLData( child, dataArray ); | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | 64 | ||
83 | this.findCanvasWithID = function( id, elt ) | 65 | this.findCanvasWithID = function( id, elt ) |
84 | { | 66 | { |
@@ -95,14 +77,14 @@ function CanvasDataManager() | |||
95 | if (foundElt) return foundElt; | 77 | if (foundElt) return foundElt; |
96 | } | 78 | } |
97 | } | 79 | } |
98 | } | 80 | }; |
99 | } | 81 | }; |
100 | 82 | ||
101 | /////////////////////////////////////////////////////////////////////// | 83 | /////////////////////////////////////////////////////////////////////// |
102 | // Class GLRuntime | 84 | // Class GLRuntime |
103 | // Manages runtime fora WebGL canvas | 85 | // Manages runtime fora WebGL canvas |
104 | /////////////////////////////////////////////////////////////////////// | 86 | /////////////////////////////////////////////////////////////////////// |
105 | function GLRuntime( canvas, jObj, assetPath ) | 87 | NinjaCvsRt.GLRuntime = function ( canvas, jObj, assetPath ) |
106 | { | 88 | { |
107 | /////////////////////////////////////////////////////////////////////// | 89 | /////////////////////////////////////////////////////////////////////// |
108 | // Instance variables | 90 | // Instance variables |
@@ -122,6 +104,7 @@ function GLRuntime( canvas, jObj, assetPath ) | |||
122 | this._initialized = false; | 104 | this._initialized = false; |
123 | 105 | ||
124 | this._useWebGL = false; | 106 | this._useWebGL = false; |
107 | this._assetPath = undefined; | ||
125 | 108 | ||
126 | // view parameters | 109 | // view parameters |
127 | this._fov = 45.0; | 110 | this._fov = 45.0; |
@@ -146,19 +129,23 @@ function GLRuntime( canvas, jObj, assetPath ) | |||
146 | this._assetPath += '/'; | 129 | this._assetPath += '/'; |
147 | } | 130 | } |
148 | 131 | ||
132 | if(this._assetPath !== undefined) { | ||
133 | RDGE.globals.engine.setAssetPath(this._assetPath); | ||
134 | } | ||
135 | |||
149 | /////////////////////////////////////////////////////////////////////// | 136 | /////////////////////////////////////////////////////////////////////// |
150 | // accessors | 137 | // accessors |
151 | /////////////////////////////////////////////////////////////////////// | 138 | /////////////////////////////////////////////////////////////////////// |
152 | this.getZNear = function() { return this._zNear; } | 139 | this.getZNear = function() { return this._zNear; }; |
153 | this.getZFar = function() { return this._zFar; } | 140 | this.getZFar = function() { return this._zFar; }; |
154 | this.getFOV = function() { return this._fov; } | 141 | this.getFOV = function() { return this._fov; }; |
155 | this.getAspect = function() { return this._aspect; } | 142 | this.getAspect = function() { return this._aspect; }; |
156 | this.getViewDistance = function() { return this._viewDist; } | 143 | this.getViewDistance = function() { return this._viewDist; }; |
157 | 144 | ||
158 | this.get2DContext = function() { return this._context; } | 145 | this.get2DContext = function() { return this._context; }; |
159 | 146 | ||
160 | this.getViewportWidth = function() { return this._canvas.width; } | 147 | this.getViewportWidth = function() { return this._canvas.width; }; |
161 | this.getViewportHeight = function() { return this._canvas.height; } | 148 | this.getViewportHeight = function() { return this._canvas.height; }; |
162 | 149 | ||
163 | /////////////////////////////////////////////////////////////////////// | 150 | /////////////////////////////////////////////////////////////////////// |
164 | // accessors | 151 | // accessors |
@@ -188,20 +175,20 @@ function GLRuntime( canvas, jObj, assetPath ) | |||
188 | this.importObjects( root ); | 175 | this.importObjects( root ); |
189 | this.render(); | 176 | this.render(); |
190 | } | 177 | } |
191 | } | 178 | }; |
192 | 179 | ||
193 | this.init = function() | 180 | this.init = function() |
194 | { | 181 | { |
195 | var ctx1 = g_Engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle), | 182 | var ctx1 = RDGE.globals.engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle), |
196 | ctx2 = g_Engine.getContext(); | 183 | ctx2 = RDGE.globals.engine.getContext(); |
197 | if (ctx1 != ctx2) console.log( "***** different contexts *****" ); | 184 | if (ctx1 != ctx2) console.log( "***** different contexts *****" ); |
198 | this.renderer = ctx1.renderer; | 185 | this.renderer = ctx1.renderer; |
199 | 186 | ||
200 | // create a camera, set its perspective, and then point it at the origin | 187 | // create a camera, set its perspective, and then point it at the origin |
201 | var cam = new camera(); | 188 | var cam = new RDGE.camera(); |
202 | this._camera = cam; | 189 | this._camera = cam; |
203 | cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar()); | 190 | cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar()); |
204 | cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], vec3.up()); | 191 | cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], RDGE.vec3.up()); |
205 | 192 | ||
206 | // make this camera the active camera | 193 | // make this camera the active camera |
207 | this.renderer.cameraManager().setActiveCamera(cam); | 194 | this.renderer.cameraManager().setActiveCamera(cam); |
@@ -210,17 +197,17 @@ function GLRuntime( canvas, jObj, assetPath ) | |||
210 | this.renderer.setClearColor([1.0, 1.0, 1.0, 0.0]); | 197 | this.renderer.setClearColor([1.0, 1.0, 1.0, 0.0]); |
211 | 198 | ||
212 | // create an empty scene graph | 199 | // create an empty scene graph |
213 | this.myScene = new SceneGraph(); | 200 | this.myScene = new RDGE.SceneGraph(); |
214 | 201 | ||
215 | // load the scene graph data | 202 | // load the scene graph data |
216 | this.loadScene(); | 203 | this.loadScene(); |
217 | 204 | ||
218 | // Add the scene to the engine - necessary if you want the engine to draw for you | 205 | // Add the scene to the engine - necessary if you want the engine to draw for you |
219 | var name = "myScene" + this._canvas.getAttribute( "data-RDGE-id" ); | 206 | var name = "myScene" + this._canvas.getAttribute( "data-RDGE-id" ); |
220 | g_Engine.AddScene(name, this.myScene); | 207 | RDGE.globals.engine.AddScene(name, this.myScene); |
221 | 208 | ||
222 | this._initialized = true; | 209 | this._initialized = true; |
223 | } | 210 | }; |
224 | 211 | ||
225 | // main code for handling user interaction and updating the scene | 212 | // main code for handling user interaction and updating the scene |
226 | this.update = function(dt) | 213 | this.update = function(dt) |
@@ -233,7 +220,7 @@ function GLRuntime( canvas, jObj, assetPath ) | |||
233 | this.elapsed += dt; | 220 | this.elapsed += dt; |
234 | 221 | ||
235 | // changed the global position uniform of light 0, another way to change behavior of a light | 222 | // changed the global position uniform of light 0, another way to change behavior of a light |
236 | rdgeGlobalParameters.u_light0Pos.set( [5*Math.cos(this.elapsed), 5*Math.sin(this.elapsed), 20]); | 223 | RDGE.rdgeGlobalParameters.u_light0Pos.set( [5*Math.cos(this.elapsed), 5*Math.sin(this.elapsed), 20]); |
237 | 224 | ||
238 | // orbit the light nodes around the boxes | 225 | // orbit the light nodes around the boxes |
239 | if (this.light ) 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)]); | 226 | if (this.light ) 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)]); |
@@ -244,7 +231,7 @@ function GLRuntime( canvas, jObj, assetPath ) | |||
244 | // now update all the nodes in the scene | 231 | // now update all the nodes in the scene |
245 | this.myScene.update(dt); | 232 | this.myScene.update(dt); |
246 | } | 233 | } |
247 | } | 234 | }; |
248 | 235 | ||
249 | this.updateMaterials = function() | 236 | this.updateMaterials = function() |
250 | { | 237 | { |