aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/GLWorld.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/helper-classes/RDGE/GLWorld.js')
-rw-r--r--js/helper-classes/RDGE/GLWorld.js834
1 files changed, 834 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/GLWorld.js b/js/helper-classes/RDGE/GLWorld.js
new file mode 100644
index 00000000..cc44da50
--- /dev/null
+++ b/js/helper-classes/RDGE/GLWorld.js
@@ -0,0 +1,834 @@
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// globals
8var shaderProgramArray = new Array;
9var glContextArray = new Array;
10
11var vertexShaderSource = "";
12var fragmentShaderSource = "";
13
14var rdgeStarted = false;
15
16var nodeCounter = 0;
17
18
19///////////////////////////////////////////////////////////////////////
20// Class GLWorld
21// Manages display in a canvas
22///////////////////////////////////////////////////////////////////////
23function GLWorld( canvas, use3D )
24{
25 ///////////////////////////////////////////////////////////////////////
26 // Instance variables
27 ///////////////////////////////////////////////////////////////////////
28
29 // flag to do the drawing with WebGL
30 this._useWebGL = false;
31 if(use3D)
32 this._useWebGL = use3D;
33
34 this._canvas = canvas;
35 if (this._useWebGL)
36 this._glContext = canvas.getContext("experimental-webgl");
37 else
38 this._2DContext = canvas.getContext( "2d" );
39
40 this._viewportWidth = canvas.width;
41 this._viewportHeight = canvas.height;
42
43 // view parameters
44 this._fov = 45.0;
45 this._zNear = 0.1;
46 this._zFar = 100.0;
47 this._viewDist = 5.0;
48
49 // default light parameters
50 this._ambientLightColor = [0.1, 0.1, 0.1, 1.0];
51 this._diffuseLightColor = [0.1, 0.1, 0.1, 1.0];
52 this._specularLightColor = [0.6, 0.6, 0.6, 1.0];
53 this._pointLightLoc = [0.0, 0.0, 0.05];
54
55 // default material properties. Material properties should be overridden
56 // by the materials used by the objects
57 this._materialShininess = 20.0;
58
59 this._geomRoot = undefined;
60
61 this._cameraMat = Matrix.I(4);
62 this._cameraMat[14] = 5.0;
63 this._cameraMatInv = Matrix.I(4);
64 this._cameraMatInv[14] = -5.0;
65
66 this._camera;
67
68 ///////////////////////////////////////////////////////////////////////
69 // Property accessors
70 ///////////////////////////////////////////////////////////////////////
71 this.getGLContext = function() { return this._glContext; }
72 this.setGLContext = function(gl) { this._glContext = gl; }
73
74 this.get2DContext = function() { return this._2DContext; }
75 this.set2DContext = function(c) { this._2DContext = c; }
76
77 this.getCanvas = function() { return this._canvas; }
78 this.setCanvas = function(c) { this._canvas = c; }
79
80 this.getShaderProgram = function() { return this._shaderProgram; }
81
82 this.getViewportWidth = function() { return this._viewportWidth; }
83 this.getViewportHeight = function() { return this._viewportHeight; }
84
85 this.getAspect = function() { return this._viewportWidth/this._viewportHeight; }
86
87 this.getGeomRoot = function() { return this._geomRoot; }
88 this.getZNear = function() { return this._zNear; }
89 this.getZFar = function() { return this._zFar; }
90 this.getFOV = function() { return this._fov; }
91
92 this.getCamera = function() { return this._camera; }
93
94 this.getCameraMat = function() { return this._cameraMat.slice(0); }
95 this.setCameraMat = function(c) { this._cameraMat = c.slice(0); this._cameraMatInv = glmat4.inverse(c, []); }
96
97 this.getCameraMatInverse = function() { return this._cameraMatInv.slice(0); }
98
99 this.getViewDistance = function() { return this._viewDist; }
100
101 this.getRootNode = function() { return this._rootNode; }
102 this.setRootNode = function(r) { this._rootNode = r; }
103
104 this.isWebGL = function() { return this._useWebGL; }
105
106 ////////////////////////////////////////////////////////////////////////////////////
107 // RDGE
108 // local variables
109 this.myScene = null;
110 this.elapsed = 0;
111 this.light = null;
112 this.light2 = null;
113 this.fillShader = null;
114 this.strokeShader = null;
115 this.renderer = null;
116
117 // this is the node to which objects get hung
118 this._rootNode;
119
120 // set up the camera matrix
121 var camMat = Matrix.I(4);
122 camMat[14] = this.getViewDistance();
123 this.setCameraMat( camMat );
124
125 //////////////////////////////////////////
126 // test call to SVG importer
127// console.log( "***** SVG TEST *****" );
128// var svgImporter = new SVGParse( this );
129// svgImporter.importSVG();
130 //////////////////////////////////////////
131
132 // post-load processing of the scene
133 this.init = function()
134 {
135 var ctx1 = g_Engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle),
136 ctx2 = g_Engine.getContext();
137 if (ctx1 != ctx2) console.log( "***** different contexts *****" );
138 this.renderer = ctx1.renderer;
139
140 // create a camera, set its perspective, and then point it at the origin
141 var cam = new camera();
142 this._camera = cam;
143 cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar());
144 cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], vec3.up());
145
146 // make this camera the active camera
147 this.renderer.cameraManager().setActiveCamera(cam);
148
149 // change clear color
150 this.renderer.setClearFlags(g_Engine.getContext().DEPTH_BUFFER_BIT);
151 this.renderer.setClearColor([1.0, 1.0, 1.0, 0.0]);
152 //this.renderer.NinjaWorld = this;
153
154 // create an empty scene graph
155 this.myScene = new SceneGraph();
156
157 // create some lights
158 // light 1
159 this.light = createLightNode("myLight");
160 this.light.setPosition([0,0,1.2]);
161 this.light.setDiffuseColor([0.75,0.9,1.0,1.0]);
162
163 // light 2
164 this.light2 = createLightNode("myLight2");
165 this.light2.setPosition([-0.5,0,1.2]);
166 this.light2.setDiffuseColor([1.0,0.9,0.75,1.0]);
167
168 // create a light transform
169 var lightTr = createTransformNode("lightTr");
170
171 // create and attach a material - materials hold the light data
172 lightTr.attachMaterial(createMaterialNode("lights"));
173
174 // enable light channels 1, 2 - channel 0 is used by the default shader
175 lightTr.materialNode.enableLightChannel(1, this.light);
176 lightTr.materialNode.enableLightChannel(2, this.light2);
177
178 // all added objects are parented to the light node
179 this._rootNode = lightTr;
180
181 // add the light node to the scene
182 this.myScene.addNode(lightTr);
183
184 // Add the scene to the engine - necessary if you want the engine to draw for you
185 g_Engine.AddScene("myScene" + this._canvas.id, this.myScene);
186 }
187
188 // main code for handling user interaction and updating the scene
189 this.update = function(dt)
190 {
191 if (!dt) dt = 0.2;
192
193 this.elapsed += dt;
194
195 if (this._useWebGL)
196 {
197 // changed the global position uniform of light 0, another way to change behavior of a light
198 rdgeGlobalParameters.u_light0Pos.set( [5*Math.cos(this.elapsed), 5*Math.sin(this.elapsed), 20]);
199
200 // orbit the light nodes around the boxes
201 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)]);
202 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)]);
203 }
204
205 this.updateMaterials( this.getGeomRoot(), this.elapsed );
206
207 // now update all the nodes in the scene
208 if (this._useWebGL)
209 this.myScene.update(dt);
210 }
211
212 // defining the draw function to control how the scene is rendered
213 this.draw = function()
214 {
215 if (this._useWebGL)
216 {
217 var ctx = g_Engine.getContext();
218 //console.log( "RDGE state: " + ctx.ctxStateManager.currentState().name);
219
220 var renderer = ctx.renderer;
221 renderer.disableCulling();
222 this.myScene.render();
223 }
224 else
225 {
226 this.render();
227 }
228 }
229
230