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