diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/RenderProcs.js')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/RenderProcs.js | 536 |
1 files changed, 536 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/RenderProcs.js b/js/helper-classes/RDGE/src/core/script/RenderProcs.js new file mode 100644 index 00000000..6d3b02df --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/RenderProcs.js | |||
@@ -0,0 +1,536 @@ | |||
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 | /** | ||
8 | * supported uniform types | ||
9 | */ | ||
10 | function __UNIFORMTYPE() | ||
11 | { | ||
12 | this.INT = 0x3F0; | ||
13 | this.FLOAT = 0x3E8; | ||
14 | this.FLOAT2 = 0x3E9; | ||
15 | this.FLOAT3 = 0x3EA; | ||
16 | this.FLOAT4 = 0x3EB; | ||
17 | this.MATRIX3 = 0x3EC; | ||
18 | this.MATRIX4 = 0x3ED; | ||
19 | this.TEXTURE2D = 0x3EE; | ||
20 | this.TEXTURECUBE = 0x3EF; | ||
21 | } | ||
22 | UNIFORMTYPE = new __UNIFORMTYPE(); | ||
23 | |||
24 | /** | ||
25 | * RenderObject - contains references to all the data need to render, including vertex buffers, uniform handles, and matrices | ||
26 | * @param shaderHandle | ||
27 | */ | ||
28 | function RenderObject(shaderHandle) | ||
29 | { | ||
30 | this.shader = shaderHandle; | ||
31 | this.world = null; | ||
32 | this.bindings = new ShaderData(); | ||
33 | this.initRenderProc = null; | ||
34 | this.renderProc = null; | ||
35 | this.postRenderProc = null; | ||
36 | } | ||
37 | |||
38 | /** | ||
39 | * Adds a uniform to the render object to bound during render | ||
40 | * @param name - name of the uniform | ||
41 | * @param value - reference to value that will get bound (will be referenced from now on, don't delete the ref) | ||
42 | * @param type - type of uniform, use UNIFORMTYPE | ||
43 | */ | ||
44 | RenderObject.prototype.addUniform = function(name, value, type) | ||
45 | { | ||
46 | var uniform = gl.getUniformLocation(this.shader, name); | ||
47 | if(uniform) | ||
48 | { | ||
49 | uniform.debugName = name; | ||
50 | this.bindings.uniforms.push( new UniformPair(uniform, value, type)); | ||
51 | } | ||
52 | /* | ||
53 | else | ||
54 | { | ||
55 | gl.console.log("ERROR: uniform - " + name + " not found!"); | ||
56 | } | ||
57 | */ | ||
58 | }; | ||
59 | |||
60 | /** | ||
61 | * Adds a uniform to the render object to bound during render | ||
62 | * @param name - name of the uniform | ||
63 | * @param value - reference to value that will get bound (will be referenced from now on, don't delete the ref) | ||
64 | * @param type - type of uniform, use UNIFORMTYPE | ||
65 | */ | ||
66 | RenderObject.prototype.addUniformArray = function(name, value, type, size) | ||
67 | { | ||
68 | var uniform = gl.getUniformLocation(this.shader, name); | ||
69 | if (uniform) | ||
70 | { | ||
71 | for (var index=0; index<size; index) | ||
72 | { | ||
73 | uniform.debugName = name+index; | ||
74 | this.bindings.uniforms.push( new UniformPair(uniform, value[index], type)); | ||
75 | uniform+=value[index].length; | ||
76 | value++; | ||
77 | } | ||
78 | } | ||
79 | /* | ||
80 | else | ||
81 | { | ||
82 | gl.console.log("ERROR: uniform - " + name + " not found!"); | ||
83 | }*/ | ||
84 | }; | ||
85 | /** | ||
86 | * Add texture to uniform | ||
87 | * @param name - handle to the texture | ||
88 | * @param unit - texture slot to use | ||
89 | * @param type - UNIFORMTYPE.TEXTURE2D or TEXTURE2D.TEXTURECUBE | ||
90 | */ | ||
91 | RenderObject.prototype.addTexture = function(name, unit, type) | ||
92 | { | ||
93 | var uniform = gl.getUniformLocation(this.shader, name); | ||
94 | if(uniform) | ||
95 | { | ||
96 | this.bindings.textures.push( new TexUniform(uniform, unit, type)); | ||
97 | } | ||
98 | /* | ||
99 | else | ||
100 | { | ||
101 | gl.console.log("ERROR: texture uniform - " + name + " not found!"); | ||
102 | } | ||
103 | */ | ||
104 | }; | ||
105 | |||
106 | /** | ||
107 | * Adds a vertex buffer to the render object | ||
108 | * @param buffer - buffer to use | ||
109 | * @param glBufferType - type of buffer i.e. gl.ARRAY_BUFFER | ||
110 | * @param attribSize - if using attrib the size of an element (3 for vec3) | ||
111 | * @param attribIndex - the index slot the attrib goes in | ||
112 | * @param glAttribType - type of the attrib i.e. gl.FLOAT | ||
113 | */ | ||
114 | RenderObject.prototype.addBuffers = function(buffer, glBufferType, attribSize, attribIndex, glAttribType) | ||
115 | { | ||
116 | //gl.useProgram(this.shader); | ||
117 | if( attribSize == undefined || attribIndex == undefined || glAttribType == undefined || | ||
118 | attribSize == null || attribIndex == null || glAttribType == null ) | ||
119 | { | ||
120 | this.bindings.buffers.push( new BufferAttrib(buffer, glBufferType, null, null, null)); | ||
121 | } | ||
122 | else | ||
123 | { | ||
124 | this.bindings.buffers.push( new BufferAttrib(buffer, glBufferType, attribSize, attribIndex, glAttribType)); | ||
125 | } | ||
126 | //gl.useProgram(null); | ||
127 | }; | ||
128 | |||
129 | /** | ||
130 | * bind the matrices, vertices and floats to shader uniforms | ||
131 | */ | ||
132 | RenderObject.prototype.bindUniforms = function() | ||
133 | { | ||
134 | for(var uniIndex = 0; uniIndex < this.bindings.uniforms.length; uniIndex++) | ||
135 | { | ||
136 | var bind = this.bindings.uniforms[uniIndex]; | ||
137 | switch(bind.type) | ||
138 | { | ||
139 | case UNIFORMTYPE.INT: | ||
140 | gl.uniform1i(bind.uniform, bind.value); | ||
141 | break; | ||
142 | case UNIFORMTYPE.FLOAT: | ||
143 | gl.uniform1f(bind.uniform, bind.value); | ||
144 | break; | ||
145 | case UNIFORMTYPE.FLOAT2: | ||
146 | gl.uniform2fv(bind.uniform, bind.value); | ||
147 | break; | ||
148 | case UNIFORMTYPE.FLOAT3: | ||
149 | gl.uniform3fv(bind.uniform, bind.value); | ||
150 | break; | ||
151 | case UNIFORMTYPE.FLOAT4: | ||
152 | gl.uniform4fv(bind.uniform, bind.value); | ||
153 | break; | ||
154 | case UNIFORMTYPE.MATRIX3: | ||
155 | gl.uniformMatrix3fv(bind.uniform, false, bind.value); | ||
156 | break; | ||
157 | case UNIFORMTYPE.MATRIX4: | ||
158 | gl.uniformMatrix4fv(bind.uniform, false, bind.value); | ||
159 | break; | ||
160 | default: | ||
161 | // gl.console.log("RenderObject: trying to bind unknown texture type"); | ||
162 | break; | ||
163 | } | ||
164 | } | ||
165 | }; | ||
166 | |||
167 | /** | ||
168 | * binds the texture uniform to texture slots | ||
169 | */ | ||
170 | RenderObject.prototype.bindTextures = function() | ||
171 | { | ||
172 | for(var uniIndex = 0; uniIndex < this.bindings.textures.length; uniIndex++) | ||
173 | { | ||
174 | var bind = this.bindings.textures[uniIndex]; | ||
175 | var error = 0; | ||
176 | switch(bind.type) | ||
177 | { | ||
178 | case UNIFORMTYPE.TEXTURE2D: | ||
179 | gl.activeTexture(gl.TEXTURE0 + bind.unit); | ||
180 | gl.uniform1i(bind.uniform, bind.unit); | ||
181 | break; | ||
182 | case UNIFORMTYPE.TEXTURECUBE: | ||
183 | gl.activeTexture(gl.TEXTURE0 + bind.unit); | ||
184 | gl.uniform1i(bind.uniform, bind.unit); | ||
185 | break; | ||
186 | default: | ||
187 | // gl.console.log("RenderObject: trying to bind unknown texture type"); | ||
188 | break; | ||
189 | } | ||
190 | } | ||
191 | }; | ||
192 | /** | ||
193 | * Binds all buffers and enables any vertexAttribs | ||
194 | */ | ||
195 | RenderObject.prototype.bindBuffers = function() { | ||
196 | for (var bufIndex = 0; bufIndex < this.bindings.buffers.length; bufIndex++) | ||
197 | { | ||
198 | var bind = this.bindings.buffers[bufIndex]; | ||
199 | gl.bindBuffer(bind.glBufferType, bind.buffer); | ||
200 | |||
201 | if (bind.glAttribType != null) { | ||
202 | // enable the attribute and point buffer to it | ||
203 | gl.enableVertexAttribArray(bind.attribIndex); | ||
204 | |||
205 | gl.vertexAttribPointer(bind.attribIndex, bind.attribSize, bind.glAttribType, false, 0, 0); | ||
206 | } | ||
207 | } | ||
208 | }; | ||
209 | |||
210 | RenderObject.prototype.unBindBuffers=function() | ||
211 | { | ||
212 | for(var bufIndex=0;bufIndex<this.bindings.buffers.length;bufIndex++) | ||
213 | { | ||
214 | var bind=this.bindings.buffers[bufIndex]; | ||
215 | |||
216 | if(bind.glAttribType!=null) | ||
217 | { | ||
218 | // enable the attribute and point buffer |