diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/rendercontext.js')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/rendercontext.js | 268 |
1 files changed, 268 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/rendercontext.js b/js/helper-classes/RDGE/src/core/script/rendercontext.js new file mode 100644 index 00000000..6bc8665b --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/rendercontext.js | |||
@@ -0,0 +1,268 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No 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 | // render flags, handles, states | ||
8 | |||
9 | var g_whiteTex = null; | ||
10 | var g_blackTex = null; | ||
11 | var g_blueTex = null; | ||
12 | |||
13 | function RDGEWebTexture() | ||
14 | { | ||
15 | this.handle; // gl handle | ||
16 | this.unit; // the texture slot/unit | ||
17 | this.type = UNIFORMTYPE.TEXTURE2D; | ||
18 | } | ||
19 | |||
20 | function RDGEWebUniformPair(uniform, value, type) { | ||
21 | this.uniform = uniform; | ||
22 | this.value = value; | ||
23 | this.type = type; | ||
24 | } | ||
25 | |||
26 | |||
27 | // pass a function that takes no parameters | ||
28 | function defaultState() | ||
29 | { | ||
30 | return [ function(){gl.disable(gl.CULL_FACE)}, | ||
31 | function(){gl.enable(gl.DEPTH_TEST)}] | ||
32 | } | ||
33 | // this chunk of data contains uniform storage references set the data with RDGEUniform.set (ie RDGEUniform.set("u_name", UNIFORMTYPE.FLOAT4, myData); ) | ||
34 | function CreateMasterList() { | ||
35 | _MasterUniformList = | ||
36 | [ | ||
37 | { 'uniform': projMatrixUniform = 0, 'name': "u_projMatrix", 'type': UNIFORMTYPE.MATRIX4, 'value': new Float32Array(16) }, | ||
38 | { 'uniform': mvMatrixUniform = 0, 'name': "u_mvMatrix", 'type': UNIFORMTYPE.MATRIX4, 'value': new Float32Array(16) }, | ||
39 | { 'uniform': normalMatrixUniform = 0, 'name': "u_normalMatrix", 'type': UNIFORMTYPE.MATRIX4, 'value': new Float32Array(16) }, | ||
40 | { 'uniform': worldMatrixUniform = 0, 'name': "u_worldMatrix", 'type': UNIFORMTYPE.MATRIX4, 'value': new Float32Array(16) }, | ||
41 | { 'uniform': lightPos = 0, 'name': "u_lightPos", 'type': UNIFORMTYPE.FLOAT3, 'value': new Float32Array(3) }, | ||
42 | { 'uniform': lightDiff = 0, 'name': "u_lightDiff", 'type': UNIFORMTYPE.FLOAT4, 'value': new Float32Array(4) }, | ||
43 | { 'uniform': lightAmb = 0, 'name': "u_lightAmb", 'type': UNIFORMTYPE.FLOAT4, 'value': new Float32Array(4) }, | ||
44 | { 'uniform': colMap = 0, 'name': "colMap", 'type': UNIFORMTYPE.TEXTURE2D, 'value': new Int32Array(1) }, | ||
45 | { 'uniform': envMap = 0, 'name': "envMap", 'type': UNIFORMTYPE.TEXTURE2D, 'value': new Int32Array(1) }, | ||
46 | { 'uniform': normalMap = 0, 'name': "normalMap", 'type': UNIFORMTYPE.TEXTURE2D, 'value': new Int32Array(1) }, | ||
47 | { 'uniform': glowMap = 0, 'name': "glowMap", 'type': UNIFORMTYPE.TEXTURE2D, 'value': new Int32Array(1) }, | ||
48 | { 'uniform': matAmbient = 0, 'name': "u_matAmbient", 'type': UNIFORMTYPE.FLOAT4, 'value': new Float32Array(4) }, | ||
49 | { 'uniform': matDiffuse = 0, 'name': "u_matDiffuse", 'type': UNIFORMTYPE.FLOAT4, 'value': new Float32Array(4) }, | ||
50 | { 'uniform': matSpecular = 0, 'name': "u_matSpecular", 'type': UNIFORMTYPE.FLOAT4, 'value': new Float32Array(4) }, | ||
51 | { 'uniform': matShininess = 0, 'name': "u_matShininess", 'type': UNIFORMTYPE.FLOAT, 'value': new Float32Array(1) }, | ||
52 | { 'uniform': matEmission = 0, 'name': "u_matEmission", 'type': UNIFORMTYPE.FLOAT, 'value': new Float32Array(4) }, | ||
53 | { 'uniform': frustumFarZ = 0, 'name': "u_frustumFarZ", 'type': UNIFORMTYPE.FLOAT, 'value': new Float32Array(1) }, | ||
54 | { 'uniform': shadowLightWorld = 0, 'name': "u_shadowLightWorld", 'type': UNIFORMTYPE.MATRIX4, 'value': new Float32Array(16) }, | ||
55 | { 'uniform': shadowBiasMatrix = 0, 'name': "u_shadowBiasMatrix", 'type': UNIFORMTYPE.MATRIX4, 'value': new Float32Array(16) }, | ||
56 | { 'uniform': vShadowLight = 0, 'name': "u_vShadowLight", 'type': UNIFORMTYPE.MATRIX4, 'value': new Float32Array(16) }, | ||
57 | { 'uniform': depthMap = 0, 'name': "depthMap", 'type': UNIFORMTYPE.TEXTURE2D, 'value': new Int32Array(1) } | ||
58 | ]; | ||
59 | |||
60 | return _MasterUniformList; | ||
61 | } | ||
62 | |||
63 | function RDGEUniformInit() | ||
64 | { | ||
65 | this.uniformList = CreateMasterList(); | ||
66 | this.uniformMap=[]; | ||
67 | for(var idx = 0; idx < this.uniformList.length; ++idx) | ||
68 | { | ||
69 | this.uniformMap[this.uniformList[idx].name] = this.uniformList[idx]; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | RDGEUniformInit.prototype.SetUni = function(name, _value) | ||
74 | { | ||
75 | //this.uniformMap[name].value.set(_value); | ||
76 | |||
77 | this.uniformMap[name].value = _value; | ||
78 | } | ||
79 | |||
80 | // Globally available uniform storage | ||
81 | RDGEUniform = new RDGEUniformInit(); | ||
82 | |||
83 | |||
84 | function RenderContext() | ||
85 | { | ||
86 | |||
87 | this.shaderProg = null; | ||
88 | this.uniforms =[]; | ||
89 | this.textureList =[]; | ||
90 | this.curRenderProc = null; | ||
91 | this.light0Color = [1, 1, 1, 0]; | ||
92 | this.parentID = 0; | ||
93 | this.world = mat4.identity(); | ||
94 | this.hide = false; | ||
95 | enableNormalMapping=true; | ||
96 | this.lights = [ null, null, null, null]; | ||
97 | |||
98 | // the camera | ||
99 | this.cam; | ||
100 | |||
101 | // state settings - set functions in the array that set states or 'other' so long as it makes since | ||
102 | this.stateSettings = []; | ||
103 | |||
104 | } | ||
105 | |||
106 | RenderContext.prototype.Load = function(ctx) | ||
107 | { | ||
108 | this.shaderProg = ctx.shaderProg; | ||
109 | this.uniforms = ctx.uniforms.slice(); | ||
110 | this.textureList = ctx.textureList.slice(); | ||
111 | this.stateSettings = ctx.stateSettings.slice(); | ||
112 | this.curRenderProc = ctx.curRenderProc; | ||
113 | this.light0Color = ctx.light0Color.slice(); | ||
114 | this.parentID = ctx.parentID; | ||
115 | this.world = mat4.copy(ctx.world); | ||
116 | this.lights = ctx.lights.slice(); | ||
117 | |||
118 | // the camera | ||
119 | this.cam = this.cam; | ||
120 | |||
121 | // state settings - set functions in the array that set states or 'other' so long as it makes since | ||
122 | this.stateSettings = this.stateSettings.slice(); | ||
123 | } | ||
124 | |||
125 | |||
126 | __lastInited = []; // keep list of initialized shaders | ||
127 | |||
128 | function _SetShader(program) | ||
129 | { | ||
130 | gl.useProgram(program); | ||
131 | |||
132 | if(__lastInited[program.shaderID] === undefined) | ||
133 | { | ||
134 | __lastInited[program.shaderID] = true; | ||
135 | // init the uniforms | ||
136 | gl.enableVertexAttribArray(0); | ||
137 | gl.enableVertexAttribArray(1); | ||
138 | gl.enableVertexAttribArray(2); | ||
139 | |||
140 | var deleteMe = []; | ||
141 | for (var uniIndex = 0; uniIndex < program.RDGEUniform.uniformList.length; uniIndex++) { | ||
142 | |||
143 | var handle = gl.getUniformLocation(program, program.RDGEUniform.uniformList[uniIndex].name); | ||
144 | if (handle) | ||
145 | program.RDGEUniform.uniformList[uniIndex].uniform = handle; | ||
146 | else { | ||
147 | program.RDGEUniform.uniformList.splice(uniIndex, 1); | ||
148 | uniIndex = Math.max(uniIndex - 1, 0); | ||
149 | } | ||
150 | |||
151 | } | ||
152 | |||
153 | } | ||
154 | |||
155 | } | ||
156 | |||
157 | // take a parameterless function | ||
158 | RenderContext.prototype.AddStateSetting = function(functor) | ||
159 | { | ||
160 | this.stateSettings.push(functor); | ||
161 | } | ||
162 | |||
163 | // take a parameterless function | ||
164 | RenderContext.prototype.Apply = function() { | ||
165 | |||
166 | // set shader program to use | ||
167 | shaderProg = null; | ||
168 | if (this.shaderProg != null) { | ||
169 | _SetShader(this.shaderProg); | ||
170 | shaderProg = this.shaderProg; | ||
171 | } | ||
172 | else { | ||
173 | _SetShader(g_Engine.defaultContext.shaderProg); | ||
174 | shaderProg = g_Engine.defaultContext.shaderProg; | ||
175 | } | ||
176 | |||
177 | // set shader values (name/uniform pair must be in the master list) | ||
178 | |||
179 | if (this.uniforms.length > 0) { | ||
180 | for (var idx = 0; idx < this.uniforms.length; ++idx) { | ||
181 | shaderProg.RDGEUniform.SetUni(this.uniforms[idx].name, this.uniforms[idx].value); | ||
182 | } | ||
183 | } | ||
184 | |||
185 | // set the diffuse color | ||
186 | shaderProg.RDGEUniform.SetUni("u_lightDiff", this.light0Color); | ||
187 | |||
188 | |||
189 | for (var setIdx = 0; setIdx < this.stateSettings.length; ++setIdx) { | ||
190 | // call state funcs | ||
191 | this.stateSettings[setIdx](); | ||
192 | } | ||
193 | |||
194 | this.bindTextures(); | ||
195 | |||
196 | |||
197 | } | ||
198 | |||
199 | RenderContext.prototype.bindTextures = function() { | ||
200 | |||
201 | var ctx = g_Engine.getContext().renderer.ctx; | ||
202 | // gl.activeTexture(gl.TEXTURE0); | ||
203 | // gl.bindTexture(gl.TEXTURE_CUBE_MAP, g_whiteTex); | ||
204 | // gl.activeTexture(gl.TEXTURE0); | ||
205 | // gl.bindTexture(gl.TEXTURE_2D, g_whiteTex); | ||
206 | |||
207 | // gl.activeTexture(gl.TEXTURE2); | ||
208 | // gl.bindTexture(gl.TEXTURE_CUBE_MAP, g_blueTex); | ||
209 | // gl.activeTexture(gl.TEXTURE2); | ||
210 | // gl.bindTexture(gl.TEXTURE_2D, g_blueTex); | ||
211 | |||
212 | // gl.activeTexture(gl.TEXTURE3); | ||
213 | // gl.bindTexture(gl.TEXTURE_CUBE_MAP, g_blackTex); | ||
214 | // gl.activeTexture(gl.TEXTURE3); | ||
215 | // gl.bindTexture(gl.TEXTURE_2D, g_blackTex); | ||
216 | |||