diff options
Diffstat (limited to 'js/lib/drawing')
-rwxr-xr-x | js/lib/drawing/world.js | 407 |
1 files changed, 178 insertions, 229 deletions
diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js index 4ca738f4..eb06e59d 100755 --- a/js/lib/drawing/world.js +++ b/js/lib/drawing/world.js | |||
@@ -29,14 +29,20 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { | |||
29 | } | 29 | } |
30 | 30 | ||
31 | this._canvas = canvas; | 31 | this._canvas = canvas; |
32 | if (this._useWebGL) { | 32 | if (this._useWebGL) |
33 | { | ||
33 | preserveDrawingBuffer = true; | 34 | preserveDrawingBuffer = true; |
34 | if(preserveDrawingBuffer) { | 35 | if(preserveDrawingBuffer) |
36 | { | ||
35 | this._glContext = canvas.getContext("experimental-webgl", {preserveDrawingBuffer: true}); | 37 | this._glContext = canvas.getContext("experimental-webgl", {preserveDrawingBuffer: true}); |
36 | } else { | 38 | } |
39 | else | ||
40 | { | ||
37 | this._glContext = canvas.getContext("experimental-webgl"); | 41 | this._glContext = canvas.getContext("experimental-webgl"); |
38 | } | 42 | } |
39 | } else { | 43 | } |
44 | else | ||
45 | { | ||
40 | this._2DContext = canvas.getContext( "2d" ); | 46 | this._2DContext = canvas.getContext( "2d" ); |
41 | } | 47 | } |
42 | 48 | ||
@@ -124,6 +130,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { | |||
124 | 130 | ||
125 | // Flag to play/pause animation at authortime | 131 | // Flag to play/pause animation at authortime |
126 | this._previewAnimation = true; | 132 | this._previewAnimation = true; |
133 | |||
127 | //////////////////////////////////////////////////////////////////////////////////// | 134 | //////////////////////////////////////////////////////////////////////////////////// |
128 | // RDGE | 135 | // RDGE |
129 | // local variables | 136 | // local variables |
@@ -148,46 +155,47 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { | |||
148 | this.setCameraMat( camMat ); | 155 | this.setCameraMat( camMat ); |
149 | 156 | ||
150 | // post-load processing of the scene | 157 | // post-load processing of the scene |
151 | this.init = function() { | 158 | this.init = function() |
152 | var ctx1 = g_Engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle), | 159 | { |
153 | ctx2 = g_Engine.getContext(); | 160 | var ctx1 = RDGE.globals.engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle), |
161 | ctx2 = RDGE.globals.engine.getContext(); | ||
154 | if (ctx1 != ctx2) console.log( "***** different contexts *****" ); | 162 | if (ctx1 != ctx2) console.log( "***** different contexts *****" ); |
155 | this.renderer = ctx1.renderer; | 163 | this.renderer = ctx1.renderer; |
156 | this.renderer._world = this; | 164 | this.renderer._world = this; |
157 | 165 | ||
158 | // create a camera, set its perspective, and then point it at the origin | 166 | // create a camera, set its perspective, and then point it at the origin |
159 | var cam = new camera(); | 167 | var cam = new RDGE.camera(); |
160 | this._camera = cam; | 168 | this._camera = cam; |
161 | cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar()); | 169 | cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar()); |
162 | cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], vec3.up()); | 170 | cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], RDGE.vec3.up()); |
163 | 171 | ||
164 | // make this camera the active camera | 172 | // make this camera the active camera |
165 | this.renderer.cameraManager().setActiveCamera(cam); | 173 | this.renderer.cameraManager().setActiveCamera(cam); |
166 | 174 | ||
167 | // change clear color | 175 | // change clear color |
168 | //this.renderer.setClearFlags(g_Engine.getContext().DEPTH_BUFFER_BIT); | 176 | //this.renderer.setClearFlags(RDGE.globals.engine.getContext().DEPTH_BUFFER_BIT); |
169 | this.renderer.setClearColor([0.0, 0.0, 0.0, 0.0]); | 177 | this.renderer.setClearColor([0.0, 0.0, 0.0, 0.0]); |
170 | //this.renderer.NinjaWorld = this; | 178 | //this.renderer.NinjaWorld = this; |
171 | 179 | ||
172 | // create an empty scene graph | 180 | // create an empty scene graph |
173 | this.myScene = new SceneGraph(); | 181 | this.myScene = new RDGE.SceneGraph(); |
174 | 182 | ||
175 | // create some lights | 183 | // create some lights |
176 | // light 1 | 184 | // light 1 |
177 | this.light = createLightNode("myLight"); | 185 | this.light = RDGE.createLightNode("myLight"); |
178 | this.light.setPosition([0,0,1.2]); | 186 | this.light.setPosition([0,0,1.2]); |
179 | this.light.setDiffuseColor([0.75,0.9,1.0,1.0]); | 187 | this.light.setDiffuseColor([0.75,0.9,1.0,1.0]); |
180 | 188 | ||
181 | // light 2 | 189 | // light 2 |
182 | this.light2 = createLightNode("myLight2"); | 190 | this.light2 = RDGE.createLightNode("myLight2"); |
183 | this.light2.setPosition([-0.5,0,1.2]); | 191 | this.light2.setPosition([-0.5,0,1.2]); |
184 | this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]); | 192 | this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]); |
185 | 193 | ||
186 | // create a light transform | 194 | // create a light transform |
187 | var lightTr = createTransformNode("lightTr"); | 195 | var lightTr = RDGE.createTransformNode("lightTr"); |
188 | 196 | ||
189 | // create and attach a material - materials hold the light data | 197 | // create and attach a material - materials hold the light data |
190 | lightTr.attachMaterial(createMaterialNode("lights")); | 198 | lightTr.attachMaterial(RDGE.createMaterialNode("lights")); |
191 | 199 | ||
192 | // enable light channels 1, 2 - channel 0 is used by the default shader | 200 | // enable light channels 1, 2 - channel 0 is used by the default shader |
193 | lightTr.materialNode.enableLightChannel(1, this.light); | 201 | lightTr.materialNode.enableLightChannel(1, this.light); |
@@ -200,21 +208,23 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { | |||
200 | this.myScene.addNode(lightTr); | 208 | this.myScene.addNode(lightTr); |
201 | 209 | ||
202 | // Add the scene to the engine - necessary if you want the engine to draw for you | 210 | // Add the scene to the engine - necessary if you want the engine to draw for you |
203 | //g_Engine.AddScene("myScene" + this._canvas.id, this.myScene); | 211 | //RDGE.globals.engine.AddScene("myScene" + this._canvas.id, this.myScene); |
204 | var name = this._canvas.getAttribute( "data-RDGE-id" ); | 212 | var name = this._canvas.getAttribute( "data-RDGE-id" ); |
205 | g_Engine.AddScene("myScene" + name, this.myScene); | 213 | RDGE.globals.engine.AddScene("myScene" + name, this.myScene); |
206 | }; | 214 | }; |
207 | 215 | ||
208 | // main code for handling user interaction and updating the scene | 216 | // main code for handling user interaction and updating the scene |
209 | this.update = function(dt) { | 217 | this.update = function(dt) |
218 | { | ||
210 | if (!dt) dt = 0.2; | 219 | if (!dt) dt = 0.2; |
211 | 220 | ||
212 | dt = 0.01; // use our own internal throttle | 221 | dt = 0.01; // use our own internal throttle |
213 | this.elapsed += dt; | 222 | this.elapsed += dt; |
214 | 223 | ||
215 | if (this._useWebGL) { | 224 | if (this._useWebGL) |
225 | { | ||
216 | // changed the global position uniform of light 0, another way to change behavior of a light | 226 | // changed the global position uniform of light 0, another way to change behavior of a light |
217 | rdgeGlobalParameters.u_light0Pos.set( [5*Math.cos(this.elapsed), 5*Math.sin(this.elapsed), 20]); | 227 | RDGE.rdgeGlobalParameters.u_light0Pos.set([5 * Math.cos(this.elapsed), 5 * Math.sin(this.elapsed), 20]); |
218 | 228 | ||
219 | // orbit the light nodes around the boxes | 229 | // orbit the light nodes around the boxes |
220 | 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)]); | 230 | 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)]); |
@@ -229,36 +239,48 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { | |||
229 | }; | 239 | }; |
230 | 240 | ||
231 | // defining the draw function to control how the scene is rendered | 241 | // defining the draw function to control how the scene is rendered |
232 | this.draw = function() { | 242 | this.draw = function() |
233 | if (this._useWebGL) { | 243 | { |
234 | g_Engine.setContext( this._canvas.rdgeid ); | 244 | if (this._useWebGL) |
235 | var ctx = g_Engine.getContext(); | 245 | { |
246 | RDGE.globals.engine.setContext( this._canvas.rdgeid ); | ||
247 | var ctx = RDGE.globals.engine.getContext(); | ||
236 | var renderer = ctx.renderer; | 248 | var renderer = ctx.renderer; |
237 | if (renderer.unloadedTextureCount <= 0) { | 249 | if (renderer.unloadedTextureCount <= 0) |
250 | { | ||
238 | renderer.disableCulling(); | 251 | renderer.disableCulling(); |
239 | renderer._clear(); | 252 | renderer._clear(); |
240 | this.myScene.render(); | 253 | this.myScene.render(); |
241 | 254 | ||
242 | if (this._firstRender) { | 255 | if (this._firstRender) |
256 | { | ||
243 | this._notifier.sendNotification( this._notifier.FIRST_RENDER ); | 257 | this._notifier.sendNotification( this._notifier.FIRST_RENDER ); |
244 | if (this._canvas.task) { | 258 | if (this._canvas.task) |
259 | { | ||
245 | this._firstRender = false; | 260 | this._firstRender = false; |
246 | 261 | ||
247 | if (!this.hasAnimatedMaterials() || !this._previewAnimation) { | 262 | if (!this.hasAnimatedMaterials() || !this._previewAnimation) |
263 | { | ||
248 | this._canvas.task.stop(); | 264 | this._canvas.task.stop(); |
249 | //this._renderCount = 10; | 265 | //this._renderCount = 10; |
250 | } | 266 | } |
251 | } | 267 | } |
252 | } else if (this._renderCount >= 0) { | 268 | } |
253 | if (this._canvas.task) { | 269 | else if (this._renderCount >= 0) |
270 | { | ||
271 | if (this._canvas.task) | ||
272 | { | ||
254 | this._renderCount--; | 273 | this._renderCount--; |
255 | if (this._renderCount <= 0) { | 274 | if (this._renderCount <= 0) |
275 | { | ||
256 | this._canvas.task.stop(); | 276 | this._canvas.task.stop(); |
257 | } | 277 | } |
258 | } | 278 | } |
259 | } | 279 | } |
260 | } | 280 | } |
261 | } else { | 281 | } |
282 | else | ||
283 | { | ||
262 | this.render(); | 284 | this.render(); |
263 | } | 285 | } |
264 | }; | 286 | }; |
@@ -272,8 +294,10 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { | |||
272 | // console.log( "GLWorld.onLoadState" ); | 294 | // console.log( "GLWorld.onLoadState" ); |
273 | }; | 295 | }; |
274 | 296 | ||
275 | this.textureToLoad = function( texture ) { | 297 | this.textureToLoad = function( texture ) |
276 | if (!texture.previouslyReferenced) { | 298 | { |
299 | if (!texture.previouslyReferenced) | ||
300 | { | ||
277 | var name = texture.lookUpName; | 301 | var name = texture.lookUpName; |
278 | texture._world = this; | 302 | texture._world = this; |
279 | texture.callback = this.textureMapLoaded; | 303 | texture.callback = this.textureMapLoaded; |
@@ -285,7 +309, |