diff options
Diffstat (limited to 'js/helper-classes/RDGE/runtime/GLRuntime.js')
-rw-r--r-- | js/helper-classes/RDGE/runtime/GLRuntime.js | 303 |
1 files changed, 246 insertions, 57 deletions
diff --git a/js/helper-classes/RDGE/runtime/GLRuntime.js b/js/helper-classes/RDGE/runtime/GLRuntime.js index 5c99be02..58cb4e33 100644 --- a/js/helper-classes/RDGE/runtime/GLRuntime.js +++ b/js/helper-classes/RDGE/runtime/GLRuntime.js | |||
@@ -16,6 +16,7 @@ function GLRuntime( canvas, importStr ) | |||
16 | // Instance variables | 16 | // Instance variables |
17 | /////////////////////////////////////////////////////////////////////// | 17 | /////////////////////////////////////////////////////////////////////// |
18 | this._canvas = canvas; | 18 | this._canvas = canvas; |
19 | this._context = null; | ||
19 | this._importStr = importStr; | 20 | this._importStr = importStr; |
20 | 21 | ||
21 | this.renderer = null; | 22 | this.renderer = null; |
@@ -25,15 +26,40 @@ function GLRuntime( canvas, importStr ) | |||
25 | this._rootNode = null; | 26 | this._rootNode = null; |
26 | 27 | ||
27 | this._firstRender = true; | 28 | this._firstRender = true; |
29 | this._initialized = false; | ||
30 | |||
31 | this._useWebGL = false; | ||
32 | |||
33 | // view parameters | ||
34 | this._fov = 45.0; | ||
35 | this._zNear = 0.1; | ||
36 | this._zFar = 100.0; | ||
37 | this._viewDist = 5.0; | ||
38 | |||
39 | this._aspect = canvas.width/canvas.height; | ||
40 | |||
41 | this._geomRoot; | ||
42 | |||
43 | // all "live" materials | ||
44 | this._materials = []; | ||
28 | 45 | ||
29 | /////////////////////////////////////////////////////////////////////// | 46 | /////////////////////////////////////////////////////////////////////// |
30 | // initialization code | 47 | // accessors |
31 | /////////////////////////////////////////////////////////////////////// | 48 | /////////////////////////////////////////////////////////////////////// |
32 | var id = canvas.getAttribute( "data-RDGE-id" ); | 49 | this.getZNear = function() { return this._zNear; } |
33 | canvas.rdgeid = id; | 50 | this.getZFar = function() { return this._zFar; } |
34 | g_Engine.registerCanvas(canvas, this); | 51 | this.getFOV = function() { return this._fov; } |
35 | RDGEStart( canvas ); | 52 | this.getAspect = function() { return this._aspect; } |
53 | this.getViewDistance = function() { return this._viewDist; } | ||
54 | |||
55 | this.get2DContext = function() { return this._context; } | ||
36 | 56 | ||
57 | this.getViewportWidth = function() { return this._canvas.width; } | ||
58 | this.getViewportHeight = function() { return this._canvas.height; } | ||
59 | |||
60 | /////////////////////////////////////////////////////////////////////// | ||
61 | // accessors | ||
62 | /////////////////////////////////////////////////////////////////////// | ||
37 | this.loadScene = function() | 63 | this.loadScene = function() |
38 | { | 64 | { |
39 | // parse the data | 65 | // parse the data |
@@ -41,6 +67,8 @@ function GLRuntime( canvas, importStr ) | |||
41 | var index = importStr.indexOf( "scenedata: " ); | 67 | var index = importStr.indexOf( "scenedata: " ); |
42 | if (index >= 0) | 68 | if (index >= 0) |
43 | { | 69 | { |
70 | this._useWebGL = true; | ||
71 | |||
44 | var rdgeStr = importStr.substr( index+11 ); | 72 | var rdgeStr = importStr.substr( index+11 ); |
45 | var endIndex = rdgeStr.indexOf( "endscene\n" ); | 73 | var endIndex = rdgeStr.indexOf( "endscene\n" ); |
46 | if (endIndex < 0) throw new Error( "ill-formed WebGL data" ); | 74 | if (endIndex < 0) throw new Error( "ill-formed WebGL data" ); |
@@ -48,6 +76,15 @@ function GLRuntime( canvas, importStr ) | |||
48 | rdgeStr = rdgeStr.substr( 0, endIndex ); | 76 | rdgeStr = rdgeStr.substr( 0, endIndex ); |
49 | 77 | ||
50 | this.myScene.importJSON( rdgeStr ); | 78 | this.myScene.importJSON( rdgeStr ); |
79 | this.importObjects( importStr ); | ||
80 | this.linkMaterials( this._geomRoot ); | ||
81 | this.initMaterials(); | ||
82 | } | ||
83 | else | ||
84 | { | ||
85 | this._context = this._canvas.getContext( "2d" ); | ||
86 | this.importObjects( importStr ); | ||
87 | this.render(); | ||
51 | } | 88 | } |
52 | } | 89 | } |
53 | 90 | ||
@@ -72,86 +109,238 @@ function GLRuntime( canvas, importStr ) | |||
72 | 109 | ||
73 | // create an empty scene graph | 110 | // create an empty scene graph |
74 | this.myScene = new SceneGraph(); | 111 | 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 | 112 | ||
106 | // load the scene graph data | 113 | // load the scene graph data |
114 | this.loadScene(); | ||
107 | 115 | ||
108 | // Add the scene to the engine - necessary if you want the engine to draw for you | 116 | // 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" ); | 117 | var name = "myScene" + this._canvas.getAttribute( "data-RDGE-id" ); |
110 | g_Engine.AddScene(name, this.myScene); | 118 | g_Engine.AddScene(name, this.myScene); |
119 | |||
120 | this._initialized = true; | ||
111 | } | 121 | } |
112 | 122 | ||
113 | // main code for handling user interaction and updating the scene | 123 | // main code for handling user interaction and updating the scene |
114 | this.update = function(dt) | 124 | this.update = function(dt) |
115 | { | 125 | { |
116 | if (!dt) dt = 0.2; | 126 | if (this._initialized) |
127 | { | ||
128 | if (!dt) dt = 0.2; | ||
117 | 129 | ||
118 | dt = 0.01; // use our own internal throttle | 130 | dt = 0.01; // use our own internal throttle |
119 | this.elapsed += dt; | 131 | this.elapsed += dt; |
120 | 132 | ||
121 | // changed the global position uniform of light 0, another way to change behavior of a light | 133 | // 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]); | 134 | rdgeGlobalParameters.u_light0Pos.set( [5*Math.cos(this.elapsed), 5*Math.sin(this.elapsed), 20]); |
123 | 135 | ||
124 | // orbit the light nodes around the boxes | 136 | // 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)]); | 137 | //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)]); | 138 | //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)]); |
139 | |||
140 | this.updateMaterials(); | ||
127 | 141 | ||
128 | // now update all the nodes in the scene | 142 | // now update all the nodes in the scene |
129 | this.myScene.update(dt); | 143 | this.myScene.update(dt); |
144 | } | ||
130 | } | 145 | } |
131 | 146 | ||
147 | this.updateMaterials = function() | ||
148 | { | ||
149 | var nMats = this._materials.length; | ||
150 | for (var i=0; i<nMats; i++) | ||
151 | { | ||
152 | var mat = this._materials[i]; | ||
153 | mat.update(); | ||
154 | } | ||
155 | } | ||
156 | |||
132 | // defining the draw function to control how the scene is rendered | 157 | // defining the draw function to control how the scene is rendered |
133 | this.draw = function() | 158 | this.draw = function() |
134 | { | 159 | { |
135 | g_Engine.setContext( this._canvas.rdgeid ); | 160 | if (this._initialized) |
136 | |||
137 | var ctx = g_Engine.getContext(); | ||
138 | var renderer = ctx.renderer; | ||
139 | if (renderer.unloadedTextureCount <= 0) | ||
140 | { | 161 | { |
141 | renderer.disableCulling(); | 162 | g_Engine.setContext( this._canvas.rdgeid ); |
142 | renderer._clear(); | ||
143 | this.myScene.render(); | ||
144 | 163 | ||
145 | if (this._firstRender) | 164 | var ctx = g_Engine.getContext(); |
165 | var renderer = ctx.renderer; | ||
166 | if (renderer.unloadedTextureCount <= 0) | ||
146 | { | 167 | { |
147 | if (this._canvas.task) | 168 | renderer.disableCulling(); |
169 | renderer._clear(); | ||
170 | this.myScene.render(); | ||
171 | |||
172 | if (this._firstRender) | ||
148 | { | 173 | { |
149 | this._firstRender = false; | 174 | if (this._canvas.task) |
150 |