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.js159
1 files changed, 159 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/runtime/GLRuntime.js b/js/helper-classes/RDGE/runtime/GLRuntime.js
new file mode 100644
index 00000000..5c99be02
--- /dev/null
+++ b/js/helper-classes/RDGE/runtime/GLRuntime.js
@@ -0,0 +1,159 @@
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._importStr = importStr;
20
21 this.renderer = null;
22 this.myScene = null;
23 this.light = null;
24 this.light2 = null;
25 this._rootNode = null;
26
27 this._firstRender = true;
28
29 ///////////////////////////////////////////////////////////////////////
30 // initialization code
31 ///////////////////////////////////////////////////////////////////////
32 var id = canvas.getAttribute( "data-RDGE-id" );
33 canvas.rdgeid = id;
34 g_Engine.registerCanvas(canvas, this);
35 RDGEStart( canvas );
36
37 this.loadScene = function()
38 {
39 // parse the data
40 // the GL runtime must start with a "sceneData: "
41 var index = importStr.indexOf( "scenedata: " );
42 if (index >= 0)
43 {
44 var rdgeStr = importStr.substr( index+11 );
45 var endIndex = rdgeStr.indexOf( "endscene\n" );
46 if (endIndex < 0) throw new Error( "ill-formed WebGL data" );
47 var len = endIndex - index + 11;
48 rdgeStr = rdgeStr.substr( 0, endIndex );
49
50 this.myScene.importJSON( rdgeStr );
51 }
52 }
53
54 this.init = function()
55 {
56 var ctx1 = g_Engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle),
57 ctx2 = g_Engine.getContext();
58 if (ctx1 != ctx2) console.log( "***** different contexts *****" );
59 this.renderer = ctx1.renderer;
60
61 // create a camera, set its perspective, and then point it at the origin
62 var cam = new camera();
63 this._camera = cam;
64 cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar());
65 cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], vec3.up());
66
67 // make this camera the active camera
68 this.renderer.cameraManager().setActiveCamera(cam);
69
70 // change clear color
71 this.renderer.setClearColor([1.0, 1.0, 1.0, 0.0]);
72
73 // create an empty scene graph
74 this.myScene = new SceneGraph();
75 this.loadScene();
76
77 /*
78 // create some lights
79 // light 1
80 this.light = createLightNode("myLight");
81 this.light.setPosition([0,0,1.2]);
82 this.light.setDiffuseColor([0.75,0.9,1.0,1.0]);
83
84 // light 2
85 this.light2 = createLightNode("myLight2");
86 this.light2.setPosition([-0.5,0,1.2]);
87 this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]);
88
89 // create a light transform
90 var lightTr = createTransformNode("lightTr");
91
92 // create and attach a material - materials hold the light data
93 lightTr.attachMaterial(createMaterialNode("lights"));
94
95 // enable light channels 1, 2 - channel 0 is used by the default shader
96 lightTr.materialNode.enableLightChannel(1, this.light);
97 lightTr.materialNode.enableLightChannel(2, this.light2);
98
99 // all added objects are parented to the light node
100 this._rootNode = lightTr;
101
102 // add the light node to the scene
103 this.myScene.addNode(lightTr);
104 */
105
106 // load the scene graph data
107
108 // Add the scene to the engine - necessary if you want the engine to draw for you
109 var name = "myScene" + this._canvas.getAttribute( "data-RDGE-id" );
110 g_Engine.AddScene(name, this.myScene);
111 }
112
113 // main code for handling user interaction and updating the scene
114 this.update = function(dt)
115 {
116 if (!dt) dt = 0.2;
117
118 dt = 0.01; // use our own internal throttle
119 this.elapsed += dt;
120
121 // changed the global position uniform of light 0, another way to change behavior of a light
122 rdgeGlobalParameters.u_light0Pos.set( [5*Math.cos(this.elapsed), 5*Math.sin(this.elapsed), 20]);
123
124 // orbit the light nodes around the boxes
125 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)]);
126 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)]);
127
128 // now update all the nodes in the scene
129 this.myScene.update(dt);
130 }
131
132 // defining the draw function to control how the scene is rendered
133 this.draw = function()
134 {
135 g_Engine.setContext( this._canvas.rdgeid );
136
137 var ctx = g_Engine.getContext();
138 var renderer = ctx.renderer;
139 if (renderer.unloadedTextureCount <= 0)
140 {
141 renderer.disableCulling();
142 renderer._clear();
143 this.myScene.render();
144
145 if (this._firstRender)
146 {
147 if (this._canvas.task)
148 {
149 this._firstRender = false;
150 this._canvas.task.stop();
151 }
152 }
153 }
154 }
155}
156
157
158
159