aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/runtime/GLRuntime.js
diff options
context:
space:
mode:
authorhwc4872012-03-02 09:36:31 -0800
committerhwc4872012-03-02 09:36:31 -0800
commitd044b6d9755d8ca686501fc3dd5644180e2ffbf0 (patch)
tree0ff6ca04987639417c0188ca8af2580b62d65f9f /js/helper-classes/RDGE/runtime/GLRuntime.js
parentcf32843b1d64c631e6e7c808cc4f13ac316e1baf (diff)
downloadninja-d044b6d9755d8ca686501fc3dd5644180e2ffbf0.tar.gz
WebGL File IO
Diffstat (limited to 'js/helper-classes/RDGE/runtime/GLRuntime.js')
-rw-r--r--js/helper-classes/RDGE/runtime/GLRuntime.js182
1 files changed, 150 insertions, 32 deletions
diff --git a/js/helper-classes/RDGE/runtime/GLRuntime.js b/js/helper-classes/RDGE/runtime/GLRuntime.js
index 33186174..d86506ad 100644
--- a/js/helper-classes/RDGE/runtime/GLRuntime.js
+++ b/js/helper-classes/RDGE/runtime/GLRuntime.js
@@ -35,6 +35,11 @@ function GLRuntime( canvas, importStr )
35 35
36 this._aspect = canvas.width/canvas.height; 36 this._aspect = canvas.width/canvas.height;
37 37
38 this._geomRoot;
39
40 // all "live" materials
41 this._materials = [];
42
38 /////////////////////////////////////////////////////////////////////// 43 ///////////////////////////////////////////////////////////////////////
39 // accessors 44 // accessors
40 /////////////////////////////////////////////////////////////////////// 45 ///////////////////////////////////////////////////////////////////////
@@ -58,6 +63,9 @@ function GLRuntime( canvas, importStr )
58 rdgeStr = rdgeStr.substr( 0, endIndex ); 63 rdgeStr = rdgeStr.substr( 0, endIndex );
59 64
60 this.myScene.importJSON( rdgeStr ); 65 this.myScene.importJSON( rdgeStr );
66 this.importObjects( importStr );
67 this.linkMaterials( this._geomRoot );
68 this.initMaterials();
61 } 69 }
62 } 70 }
63 71
@@ -82,42 +90,13 @@ function GLRuntime( canvas, importStr )
82 90
83 // create an empty scene graph 91 // create an empty scene graph
84 this.myScene = new SceneGraph(); 92 this.myScene = new SceneGraph();
85
86 /*
87 // create some lights
88 // light 1
89 this.light = createLightNode("myLight");
90 this.light.setPosition([0,0,1.2]);
91 this.light.setDiffuseColor([0.75,0.9,1.0,1.0]);
92
93 // light 2
94 this.light2 = createLightNode("myLight2");
95 this.light2.setPosition([-0.5,0,1.2]);
96 this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]);
97
98 // create a light transform
99 var lightTr = createTransformNode("lightTr");
100
101 // create and attach a material - materials hold the light data
102 lightTr.attachMaterial(createMaterialNode("lights"));
103
104 // enable light channels 1, 2 - channel 0 is used by the default shader
105 lightTr.materialNode.enableLightChannel(1, this.light);
106 lightTr.materialNode.enableLightChannel(2, this.light2);
107
108 // all added objects are parented to the light node
109 this._rootNode = lightTr;
110
111 // add the light node to the scene
112 this.myScene.addNode(lightTr);
113 */
114 93
115 // load the scene graph data 94 // load the scene graph data
116 this.loadScene(); 95 this.loadScene();
117 96
118 // Add the scene to the engine - necessary if you want the engine to draw for you 97 // Add the scene to the engine - necessary if you want the engine to draw for you
119 //var name = "myScene" + this._canvas.getAttribute( "data-RDGE-id" ); 98 var name = "myScene" + this._canvas.getAttribute( "data-RDGE-id" );
120 //g_Engine.AddScene(name, this.myScene); 99 g_Engine.AddScene(name, this.myScene);
121 100
122 this._initialized = true; 101 this._initialized = true;
123 } 102 }
@@ -139,11 +118,23 @@ function GLRuntime( canvas, importStr )
139 //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)]); 118 //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)]);
140 //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)]); 119 //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)]);
141 120
121 this.updateMaterials();
122
142 // now update all the nodes in the scene 123 // now update all the nodes in the scene
143 this.myScene.update(dt); 124 this.myScene.update(dt);
144 } 125 }
145 } 126 }
146 127
128 this.updateMaterials = function()
129 {
130 var nMats = this._materials.length;
131 for (var i=0; i<nMats; i++)
132 {
133 var mat = this._materials[i];
134 mat.update();
135 }
136 }
137
147 // defining the draw function to control how the scene is rendered 138 // defining the draw function to control how the scene is rendered
148 this.draw = function() 139 this.draw = function()
149 { 140 {
@@ -171,12 +162,139 @@ function GLRuntime( canvas, importStr )
171 } 162 }
172 } 163 }
173 164
165 this.importObjects = function( importStr, parent )
166 {
167 var index = importStr.indexOf( "OBJECT\n", 0 );
168 while (index >= 0)
169 {
170 // update the string to the current object
171 importStr = importStr.substr( index+7 );
172
173 // read the next object
174 var obj = this.importObject( importStr, parent );
175
176 // determine if we have children
177 var endIndex = importStr.indexOf( "ENDOBJECT\n" ),
178 childIndex = importStr.indexOf( "OBJECT\n" );
179 if (endIndex < 0) throw new Error( "ill-formed object data" );
180 if ((childIndex >= 0) && (childIndex < endIndex))
181 {
182 importStr = importStr.substr( childIndex + 7 );
183 importStr = this.importObjects( importStr, obj );
184 endIndex = importStr.indexOf( "ENDOBJECT\n" )
185 }
186
187 // remove the string for the object(s) just created
188 importStr = importStr.substr( endIndex );
189
190 // get the location of the next object
191 index = importStr.indexOf( "OBJECT\n", endIndex );
192 }
193
194 return importStr;
195 }
196
197 this.importObject = function( objStr, parent )
198 {
199 var type = Number( getPropertyFromString( "type: ", objStr ) );
200
201 var obj;
202 switch (type)
203 {
204 case 1:
205 obj = new RuntimeRectangle();
206 obj.import( objStr, parent );
207 break;
208
209 case 2: // circle
210 obj = new RuntimeOval();
211 obj.import( objStr, parent );
212 break;
213
214 case 3: // line
215 obj = new RuntimeLine();
216 obj.import( objStr, parent );
217 break;
218
219 default:
220 throw new Error( "Attempting to load unrecognized object type: " + type );
221 break;
222 }
223
224 if (obj)
225 this.addObject( obj );
226
227 return obj;
228 }
229
230 this.addObject = function( obj, parent )
231 {
232 if (!obj) return;
233
234 if (parent == null)
235 this._geomRoot = obj;
236 else
237 parent.addChild( obj );
238 }
239
240 this.linkMaterials = function( obj )
241 {
242 if (!obj) return;
243
244 // get the array of materials from the object
245 var matArray = obj._materials;
246 var nMats = matArray.length;
247 for (var i=0; i<nMats; i++)
248 {
249 var mat = matArray[i];
250 var nodeName = mat._materialNodeName;
251 var matNode = this.findMaterialNode( nodeName, this.myScene.scene );
252 if (matNode)
253 {
254 mat._materialNode = matNode;
255 mat._shader = matNode.shaderProgram;
256 this._materials.push( mat );
257 }
258 }
259 }
260
261 this.initMaterials = function()
262 {
263 var nMats = this._materials.length;
264 for (var i=0; i<nMats; i++)
265 {
266 var mat = this._materials[i];
267 mat.init();
268 }
269 }