diff options
Diffstat (limited to 'js/helper-classes/RDGE/GLMaterial.js')
-rw-r--r-- | js/helper-classes/RDGE/GLMaterial.js | 268 |
1 files changed, 268 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/GLMaterial.js b/js/helper-classes/RDGE/GLMaterial.js new file mode 100644 index 00000000..51c27ace --- /dev/null +++ b/js/helper-classes/RDGE/GLMaterial.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 | /////////////////////////////////////////////////////////////////////// | ||
8 | // Class GLMaterial | ||
9 | // GL representation of a material. | ||
10 | /////////////////////////////////////////////////////////////////////// | ||
11 | function GLMaterial( world ) | ||
12 | { | ||
13 | /////////////////////////////////////////////////////////////////////// | ||
14 | // Instance variables | ||
15 | /////////////////////////////////////////////////////////////////////// | ||
16 | this._name = "GLMaterial"; | ||
17 | this._shaderName = "undefined"; | ||
18 | |||
19 | // keep a reference to the owning GLWorld | ||
20 | this._world = null; | ||
21 | if(world) | ||
22 | { | ||
23 | this._world = world; | ||
24 | } | ||
25 | |||
26 | this._shininess = 60; | ||
27 | |||
28 | this._ambient = [0.0, 0.0, 0.0, 1.0]; | ||
29 | this._diffuse = [0.0, 0.0, 0.0, 1.0]; | ||
30 | this._specular = [0.0, 0.0, 0.0, 1.0]; | ||
31 | |||
32 | this._texture; | ||
33 | |||
34 | // RDGE variables | ||
35 | this._shader; | ||
36 | this._materialNode; | ||
37 | |||
38 | /////////////////////////////////////////////////////////////////////// | ||
39 | // Property Accessors | ||
40 | /////////////////////////////////////////////////////////////////////// | ||
41 | this.getShininess = function() { return this._shininess; } | ||
42 | this.setShininess = function(s) { this._shininess = s; } | ||
43 | |||
44 | this.setName = function(n) { this._name = n; } | ||
45 | this.getName = function() { return this._name; } | ||
46 | |||
47 | this.setShaderName = function(n) { this._shaderName = n; } | ||
48 | this.getShaderName = function() { return this._shaderName; } | ||
49 | |||
50 | this.setWorld = function(world) { this._world = world; } | ||
51 | this.getWorld = function() { return this._world; } | ||
52 | |||
53 | this.setAmbient = function(r, g, b, a) { this._ambient = [r, g, b, a]; } | ||
54 | this.getAmbient = function() { return [this._ambient[0], this._ambient[1], this._ambient[2], this._ambient[3]]; } | ||
55 | |||
56 | this.setDiffuse = function(r, g, b, a) { this._diffuse = [r, g, b, a]; } | ||
57 | this.getDiffuse = function() { return [this._diffuse[0], this._diffuse[1], this._diffuse[2], this._diffuse[3]]; } | ||
58 | |||
59 | this.setSpecular = function(r, g, b, a) { this._specular = [r, g, b, a]; } | ||
60 | this.getSpecular = function() { return [this._specular[0], this._specular[1], this._specular[2], this._specular[3]]; } | ||
61 | |||
62 | this.getShader = function() { return this._shader; } | ||
63 | this.getMaterialNode = function() { return this._materialNode; } | ||
64 | |||
65 | |||
66 | /////////////////////////////////////////////////////////////////////// | ||
67 | // Common Material Methods | ||
68 | /////////////////////////////////////////////////////////////////////// | ||
69 | this.getProperty = function( propName ) | ||
70 | { | ||
71 | return this._propValues[propName]; | ||
72 | } | ||
73 | |||
74 | this.getPropertyCount = function() | ||
75 | { | ||
76 | return this._propNames.length; | ||
77 | } | ||
78 | |||
79 | this.getPropertyAtIndex = function( index ) | ||
80 | { | ||
81 | var rtnArr = []; | ||
82 | if ((index < 0) || (index >= this.getPropertyCount())) | ||
83 | throw new Error( "property index " + index + " is out of range for material" ); | ||
84 | |||
85 | var rtnArr = [ this._propNames[index], this._propLabels[index], this._propTypes[index], this._propValues[index] ]; | ||
86 | return rtnArr; | ||
87 | } | ||
88 | |||
89 | this.getAllProperties = function( propNames, propValues, propTypes, propLabels) | ||
90 | { | ||
91 | // clear all the input arrays if there is junk in them | ||
92 | propNames.length = 0; | ||
93 | propValues.length = 0; | ||
94 | propTypes.length = 0; | ||
95 | propLabels.length = 0; | ||
96 | |||
97 | var nProps = this._propNames.length; | ||
98 | for (var i=0; i<nProps; i++) | ||
99 | { | ||
100 | propNames[i] = this._propNames[i]; | ||
101 | propValues[i] = this._propValues[this._propNames[i]]; | ||
102 | propTypes[i] = this._propTypes[i]; | ||
103 | propLabels[i] = this._propLabels[i]; | ||
104 | } | ||
105 | } | ||
106 | |||
107 | this.validateProperty = function( prop, value ) | ||
108 | { | ||
109 | var rtnVal = false; | ||
110 | try | ||
111 | { | ||
112 | //if (!this._propValues[prop]) return false; | ||
113 | |||
114 | // find the index of the property | ||
115 | var n = this._propNames.length; | ||
116 | var valType = typeof value; | ||
117 | for (var i=0; i<n; i++) | ||
118 | { | ||
119 | if (this._propNames[i] == prop) | ||
120 | { | ||
121 | switch (this._propTypes[i]) | ||
122 | { | ||
123 | case "color": | ||
124 | rtnVal = ((valType == "object") && (value.length >= 4)); | ||
125 | break; | ||
126 | |||
127 | case "vector2d": | ||
128 | rtnVal = ((valType == "object") && (value.length >= 2)); | ||
129 | break; | ||
130 | |||
131 | case "vector3d": | ||
132 | rtnVal = ((valType == "object") && (value.length >= 3)); | ||
133 | break; | ||
134 | |||
135 | case "float": | ||
136 | rtnVal = (valType == "number"); | ||
137 | break; | ||
138 | |||
139 | case "file": | ||
140 | rtnVal = ((valType == "string") || !value); | ||
141 | break; | ||
142 | } | ||
143 | break; | ||
144 | } | ||
145 | } | ||
146 | } | ||
147 | catch(e) { | ||
148 | console.log( "setting invalid material property: " + prop + ", value: " + value ); | ||
149 | } | ||
150 | |||
151 | if (!rtnVal) | ||
152 | console.log( "invalid material property: " + prop + " : " + value ); | ||
153 | |||
154 | return rtnVal; | ||
155 | } | ||
156 | /////////////////////////////////////////////////////////////////////// | ||
157 | |||
158 | /////////////////////////////////////////////////////////////////////// | ||
159 | // Methods | ||
160 | /////////////////////////////////////////////////////////////////////// | ||
161 | // duplcate method required by sub class | ||
162 | this.dup = function() | ||
163 | { | ||
164 | throw new Error( "Material.dup() must be overridden by subclass" ); | ||
165 | } | ||
166 | |||
167 | this.init = function( world ) | ||
168 | { | ||
169 | throw new Error( "Material.init() must be overridden by subclass" ); | ||
170 | } | ||
171 | |||
172 | this.update = function( time ) | ||
173 | { | ||
174 | // animated materials should implement the update method | ||
175 | } | ||
176 | |||
177 | this.export = function() | ||
178 | { | ||
179 | // this function should be overridden by subclasses | ||
180 | var exportStr = "material: " + this.getShaderName() + "\n" + "endMaterial\n"; | ||
181 | return exportStr; | ||
182 | } | ||
183 | |||
184 | this.import = function( importStr ) | ||
185 | { | ||
186 | var endKey = "endMaterial\n"; | ||
187 | var index = importStr.indexOf( endKey ); | ||
188 | index += endKey.length; | ||
189 | rtnStr = importStr.substr( index ); | ||
190 | |||
191 | return rtnStr; | ||
192 | } | ||
193 | |||
194 | /* | ||
195 | this.setRenderProperties = function( glContext, shaderProgram ) | ||
196 | { | ||
197 | glContext.uniform1f( shaderProgram.materialShininessUniform, this._shininess ); | ||
198 | |||
199 | if (this._texture) | ||
200 | this.prepareTextureForRender( 0 ); | ||
201 | else | ||
202 | glContext.uniform1i( shaderProgram.useTextureUniform, false ); | ||
203 | |||
204 | var amb = this._ambient, diff = this._diffuse, spec = this._specular; | ||
205 | glContext.uniform4f( shaderProgram.materialAmbientUniform, amb[0], amb[1], amb[2], amb[3]); | ||
206 | glContext.uniform4f( shaderProgram.materialDiffuseUniform, diff[0], diff[1], diff[2], diff[3]); | ||
207 | glContext.uniform4f( shaderProgram.materialSpecularUniform, spec[0], spec[1], spec[2], spec[3]); | ||
208 | } | ||
209 | |||
210 | |||
211 | |||
212 | this.prepareTextureForRender = function ( index ) | ||
213 | { | ||
214 | // we will need to be able to handle multiple textures. | ||
215 | // currently only dealing with 1. | ||
216 | index = 0; | ||
217 | var texture = this._texture; | ||
218 | |||
219 | var gl = this.getWorld().getGLContext(); | ||
220 | var shaderProgram = this.getWorld().getShaderProgram(); | ||
221 | |||
222 | gl.activeTexture(gl.TEXTURE0); | ||
223 | gl.bindTexture(gl.TEXTURE_2D, texture); | ||
224 | gl.uniform1i(shaderProgram.samplerUniform, 0); | ||
225 | gl.uniform1i( shaderProgram.useTextureUniform, true ); | ||