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