aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/engine.js
blob: 919a1e9c49af6e971a0bfe2115bbd32fd4ed1343 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* <copyright>
Copyright (c) 2012, Motorola Mobility, Inc
All Rights Reserved.
BSD License.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  - Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
  - Neither the name of Motorola Mobility nor the names of its contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</copyright> */

var RDGE = RDGE || {};

/*
 *  Manage state instances
 */
RDGE.stateManager = function () {
    // a stack of states
    this.stateStack = [];

    // number of states on the stack
    this.stateTop       = undefined;

    // the states of the context
    this.RDGEInitState  = null;
    this.RDGERunState   = null;

    this.currentState = function () {
        if(this.stateTop != undefined)
            return this.stateStack[this.stateTop];

        return null;
    };

    /*
     *  Push new IRuntime state - engine executes the new state
     */
    this.PushState = function (state, flags) {
        if (state != null && typeof state.Init == 'function') {
            if(this.stateTop != undefined)
                this.stateStack[this.stateTop].LeaveState();

            if(flags == undefined || flags != "noInit")
                state.Init();

            this.stateTop = this.stateStack.push(state) - 1;
        }
    };

    /*
     *  Remove IRuntime state from stack, engine executes previous state
     */
    this.PopState = function () {
        state = this.stateStack.pop();
        if (state != null) {
            state.Shutdown();
        }

        this.stateTop = this.stateTop > 0 ? this.stateTop - 1 : 0;

        if (this.stateStack[this.stateTop]) {
            this.stateStack[this.stateTop].ReInit();
        }
    };

    /*
     *  Remove all states from the stack
     */
    this.PopAll = function () {
        while (this.stateStack[this.stateTop] != null) {
            this.PopState();
        }
    };

    this.tick = function (dt) {
        if (this.stateStack[this.stateTop] != null) {
            this.stateStack[this.stateTop].Update(dt);
            this.stateStack[this.stateTop].Resize();
            this.stateStack[this.stateTop].Draw();
        }
    };
};

RDGE.Engine = function () {
    this._assetPath = "assets/";

    // map of scene graphs to names
    this.sceneMap = [];

    // number of states on the stack
    this.stateTop = undefined;

    // size of the browser window
    this.lastWindowWidth = window.innerWidth;
    this.lastWindowHeight = window.innerHeight;

    this.defaultContext = null;

    this.lightManager = null;

    clearColor = [0.0, 0.0, 0.0, 0.0];

    this.initializeComplete = false;

    this.RDGECanvas = null;

    /*
    *   a map of canvas names to renderer
    */
    this.canvasToRendererMap = {};

    /*
    *   states to canvas map - maps a state stack to the canvas context it belongs to
    */
    this.canvasNameToStateStack = {};

    /*
    *   the list of context's that are active
    */
    this.canvasCtxList = [];

    /*
    *   regex object to verify runtime object is not some sort of exploit
    */
    invalidObj = new RegExp("([()]|function)");

    isValidObj = function (name) {
        // do a quick test make sure user isn't trying to execute a function
        if (invalidObj.test(name)) {
            window.console.error("invalid object name passed to RDGE, " + name + " - looks like a function");
            return false;
        }

        return true;
    };

    /*
    *   The context definition - every context shares these parameters
    */
    contextDef = function () {
        this.id = null;
        this.renderer = null;
        this.ctxStateManager = null;
        this.startUpState = null;
        this.sceneGraphMap = [];
        this.currentScene = null;
        this.getScene = function () {
            return this.sceneGraphMap[this.currentScene];
        }
        this.debug =
        {
            'frameCounter': 0,
            'mat4CallCount': 0
        }
    };

    // maintains the contexts
    contextManager = new RDGE.objectManager();
    this.ctxMan = contextManager;

    // the context currently being updated
    contextManager.currentCtx = null;

    contextManager._addObject = contextManager.addObject;
    contextManager.contextMap = {};

    contextManager.addObject = function (context) {
        this.contextMap[context.id] = context;
        return this._addObject(context);
    };

    contextManager.start = function () {
        var len = this.objects.length;
        for (var i = 0; i < len; ++i) {
            // set the current context
            contextManager.currentCtx = this.objects[i];
            this.objects[i].ctxStateManager.PushState(this.objects[i].startUpState);
        }
    };

    contextManager.forEach = function (cb) {
        var len = this.objects.length;
        for (var i = 0; i < len; ++i) {
            cb(this.objects[i]);
        }
    };

    this.getContext = function (optCanvasID) {
        if (!optCanvasID) {
            return contextManager.currentCtx;
        }
        else {
            return contextManager.contextMap[optCanvasID];
        }
    };

    this.clearContext = function (canvasID) {
        contextManager.contextMap[canvasID] = undefined;
    };

    /*
    *   give the contextID (canvas id) of the context to set
    */
    this.setContext = function (contextID) {
        contextManager.currentCtx = contextManager.contextMap[contextID];
    };

    this.tickContext = function (contextID) {
        var savedCtx = contextManager.currentCtx;
        contextManager.currentCtx = contextManager.contextMap[contextID];
        this.objects[i].ctxStateManager.tick(dt);
        contextManager.currentCtx = savedCtx;
    };

    this.setAssetPath = function (path) {
        this._assetPath = path.slice();
    };

    this.remapAssetFolder = function (url) {
        var searchStr = "assets/";
        var index = url.indexOf(searchStr);
        var rtnPath = url;
        if (index >= 0) {
            rtnPath = url.substr(index + searchStr.length);
            rtnPath = this._assetPath + rtnPath;
        }
        return rtnPath;
    };
};

/*
 *   Initialize the RDGE web engine
 */
RDGE.Engine.prototype.init = function (userInitState, userRunState, canvasObject) {
    this.GlInit(canvasObject);

    globalParamFuncSet = function (param) {
        this.data = param.data;
        this.type =param.type;

        this.set = function (v) {
            var len = this.data ? this.data.length : 0;
            for(var i=0;i<len;++i)
                this.data[i]=v[i];
        }
        this.get = function () {
            if (this.data.length == undefined) {
                return this.data;
            }
            else {
                return this.data.slice();
            }
        }
    };

    // light manager init before global parameters structure is reconfigured
    this.lightManager = new RDGE.LightManager(RDGE.rdgeGlobalParameters.rdge_lights);

    // added getter and setter to global uniforms
    for (var p in RDGE.rdgeGlobalParameters) {
        if (p != "rdge_lights") {
            RDGE.rdgeGlobalParameters[p] = new globalParamFuncSet(RDGE.rdgeGlobalParameters[p]);
        }
        else {
            var lights = RDGE.rdgeGlobalParameters[p];
            for (var l in lights) {
                RDGE.rdgeGlobalParameters[l] = new globalParamFuncSet(lights[l]);
            }
        }
    }

    // initial window
    this.lastWindowWidth = window.innerWidth;
    this.lastWindowHeight = window.innerHeight;

    // setup default render context
    this.defaultContext = new RDGE.RenderContext();

    this.defaultContext.uniforms = [
        { 'name': "u_matAmbient", 'value': [0.02,0.02,0.02, 1.0] },
        { 'name': "u_matDiffuse", 'value': [1.0, 1.0, 1.0, 1.0] },
        { 'name': "u_matSpecular", 'value': [1.0, 1.0, 1.0, 1.0] },
        { 'name': "u_matShininess", 'value': [128.0] },
        { 'name': "u_matEmission", 'value': [0.0, 0.0, 0.0, 1.0] }
    ];

    // startup the contexts
    contextManager.start();

    this.initializeComplete = true;
};

// shutdown the engine clears all states
RDGE.Engine.prototype.Shutdown = function () {
    this.PopAll();
};

// initialize WebGL
RDGE.Engine.prototype.GlInit = function (canvasObject) {
    // Initialize
    var canvases = document.getElementsByTagName("canvas");

    // transverse the canvases and create the contexts
    var numCv = canvases.length;
    for (var cvIdx = 0; cvIdx < numCv; ++cvIdx) {
        var canvas;

        // if this canvas has a rdge attribute initialize the render context
        var rdgeAttr = canvases[cvIdx].getAttribute("rdge");
        if (rdgeAttr == "true") {
            // hack ~ while implementing multi-context
            canvas = canvases[cvIdx];
            this.registerCanvas(canvas);
        }

    }
/*
    canvas.addEventListener("webglcontextlost", contextLostHandler, false);
    canvas.addEventListener("webglcontextrestored", contextRestoredHandler, false);
*/
};

RDGE.Engine.prototype.loadScene = function (name) {
    var url = "assets_web/mesh/" + name + ".json"

    // if we are not in the load state than push it on again
    if (contextManager.currentCtx.stateMan.currentState().name == "RunState") {
        contextManager.currentCtx.stateMan.PushState(contextManager.currentCtx.stateMan.RDGEInitState);
        contextManager.currentCtx.loadScene(url, name);
    }
};

RDGE.Engine.prototype.getScene = function (name) {
    return contextManager.currentCtx.sceneGraphMap[name];
};

RDGE.Engine.prototype.AddScene = function (name, sceneGraph) {
    contextManager.currentCtx.sceneGraphMap[name] = sceneGraph;
    contextManager.currentCtx.currentScene = name;
};

RDGE.Engine.prototype.registerCanvas = function (canvas, runState) {
    if (canvas && this.getContext(canvas.rdgeid))
        return;

    canvas.renderer = new RDGE._renderer(canvas);   // create the renderer for the context
    this.canvasToRendererMap[canvas.rdgeid] = canvas; // store the canvas in the context map
    canvas.renderer.id = canvas.rdgeid;

    // configure the state manager for this context
    var stateMan = new RDGE.stateManager();

    // add this context to the contextManager and attach the handle to DOM canvas for user retrieval
    var context = new contextDef();

    context.id = canvas.rdgeid;
    context.renderer = canvas.renderer;
    context.ctxStateManager = stateMan;

    context.renderer.mvMatrix = RDGE.mat4.identity();
    context.renderer.invMvMatrix = RDGE.mat4.identity();
    context.renderer.projectionMatrix = RDGE.mat4.identity();
    context.renderer.normalMatrix = RDGE.mat4.identity();

    canvas.rdgeCtxHandle = contextManager.addObject(context);

    // set new context as the current context so that when the runtime object is instantiated
    // it can use the context during construction
    var oldCtx = contextManager.currentCtx;
    contextManager.currentCtx = context;

    var _runState;

    // check for runtime handlers
    if (runState) {
        _runState = runState;
    }
    else {
        var runAttr = canvas.getAttribute("rdgerun");

        if (runAttr) {
            // make sure attribute is valid
            if (!isValidObj(runAttr))
                return;
            try {
                var state = eval(runAttr);
                _runState = new state();
            }
            catch (err) {
                window.console.error("The provided RDGE state object \"" + runAttr + "\" is not defined");
            }
        }
        else {
            _runState = {};
            RDGE.utilities.validateUserState(_runState);
        }
    }

    // check for a scene
    var sceneName = canvas.getAttribute("rdgescene");

    // setup the RDGE states passing the user state or undefined
    stateMan.RDGEInitState = new RDGE.LoadState(_runState, context);
    stateMan.RDGERunState = new RDGE.RunState(_runState, context);

    // fill out any user state missing methods with dummy methods
    RDGE.utilities.validateUserState(_runState);

    if (sceneName) {
        stateMan.RDGEInitState.sceneName = sceneName;

        // run is now always the bottom state, loading can happen at any time
        stateMan.PushState(stateMan.RDGERunState, "noInit");

        context.startUpState = stateMan.RDGEInitState;
    }
    else {
        context.startUpState = stateMan.RDGERunState;
    }

    if (this.initializeComplete) {
        context.ctxStateManager.PushState(context.startUpState);
    }

    // restore previous context
    // NOTE: Ninja requires this to be commented out!
    //  if (oldCtx)
    //  {
    //      contextManager.currentCtx = oldCtx;
    //  }
};

RDGE.Engine.prototype.unregisterCanvas = function (canvas) {
     contextManager.removeObject(canvas.rdgeCtxHandle);
     this.clearContext( canvas.rdgeid );
};


RDGE.Engine.prototype.getCanvas = function (id)
{
    return this.canvasToRendererMap[id];
};