00001
00002 #include "MercuryString.h"
00003 #include "ShaderComposer.h"
00004
00005 unsigned int ShaderDescription (bool ambient, bool diffuse, bool specular, bool color, bool transparent, bool texture, bool detailTex, bool normalMap, bool cellShade)
00006 {
00007 return ( ((ambient&0x0001)<<0) |
00008 ((diffuse&0x0001)<<1) |
00009 ((specular&0x0001)<<2) |
00010 ((color&0x0001)<<3) |
00011 ((transparent&0x0001)<<4) |
00012 ((texture&0x0001)<<5) |
00013 ((detailTex&0x0001)<<6) |
00014 ((normalMap&0x0001)<<7) |
00015 ((cellShade&0x0001)<<8) );
00016 }
00017
00018 bool isShaderAmbient (unsigned int shaderDescription)
00019 {
00020 return (shaderDescription>>0)&0x0001;
00021 }
00022
00023 bool isShaderDiffuse (unsigned int shaderDescription)
00024 {
00025 return (shaderDescription>>1)&0x0001;
00026 }
00027
00028 bool isShaderSpecular (unsigned int shaderDescription)
00029 {
00030 return (shaderDescription>>2)&0x0001;
00031 }
00032
00033 bool isShaderColor (unsigned int shaderDescription)
00034 {
00035 return (shaderDescription>>3)&0x0001;
00036 }
00037
00038 bool isShaderTransparent (unsigned int shaderDescription)
00039 {
00040 return (shaderDescription>>4)&0x0001;
00041 }
00042
00043 bool isShaderTexture (unsigned int shaderDescription)
00044 {
00045 return (shaderDescription>>5)&0x0001;
00046 }
00047
00048 bool isShaderDetailTexture (unsigned int shaderDescription)
00049 {
00050 return (shaderDescription>>6)&0x0001;
00051 }
00052
00053 bool isShaderNormalMap (unsigned int shaderDescription)
00054 {
00055 return (shaderDescription>>7)&0x0001;
00056 }
00057
00058 bool isShaderCellShade (unsigned int shaderDescription)
00059 {
00060 return (shaderDescription>>8)&0x0001;
00061 }
00062
00063 MString ComposerGLSLangVertexshader (unsigned int shaderDescription)
00064 {
00065 return "Vertex shader composer not implemented yet\n\n";
00066 }
00067
00068 MString ComposeGLSLangVertexshader (unsigned int shaderDescription)
00069 {
00070 MString vertexshader;
00071
00072 if (isShaderColor (shaderDescription))
00073 {
00074 vertexshader += "\n\
00075 varying vec4 color;\n";
00076 }
00077
00078 if (isShaderAmbient (shaderDescription))
00079 {
00080 vertexshader += "\n\
00081 varying vec4 ambient;\n";
00082 }
00083
00084 if (isShaderDiffuse (shaderDescription))
00085 {
00086 vertexshader += "\n\
00087 varying vec4 diffuse;\n";
00088 }
00089
00090 if (isShaderDiffuse (shaderDescription) || isShaderSpecular (shaderDescription))
00091 {
00092 vertexshader += "\n\
00093 varying vec3 normalEyeSpace;\n\
00094 varying vec3 L;\n";
00095 }
00096
00097 if (isShaderSpecular (shaderDescription))
00098 {
00099 vertexshader += "\n\
00100 varying vec3 halfVector;\n";
00101 }
00102
00103 if (isShaderTexture (shaderDescription))
00104 {
00105 vertexshader += "\n\
00106 varying vec2 texCoord;\n";
00107 }
00108
00109 vertexshader += "\n\
00110 void main ( void )\n\
00111 {\n\n";
00112
00113 vertexshader += "\n\
00114 \tgl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n";
00115
00116 if (isShaderDiffuse (shaderDescription) || isShaderSpecular (shaderDescription))
00117 {
00118 vertexshader += "\n\
00119 \tnormalEyeSpace = normalize(gl_NormalMatrix * gl_Normal);\n\
00120 \tL = normalize(vec3(gl_LightSource[0].position));\n";
00121 }
00122 if (isShaderSpecular (shaderDescription))
00123 {
00124 vertexshader += "\n\
00125 \thalfVector = normalize(gl_LightSource[0].halfVector.xyz);\n";
00126 }
00127
00128 if (isShaderTexture (shaderDescription))
00129 {
00130 vertexshader += "\n\
00131 \ttexCoord = gl_MultiTexCoord0.xy;\n";
00132 }
00133
00134 if (isShaderColor (shaderDescription))
00135 {
00136 vertexshader += "\n\
00137 \tcolor = gl_Color;\n";
00138 }
00139
00140 if (isShaderAmbient (shaderDescription))
00141 {
00142 vertexshader += "\n\
00143 \tambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;\n\
00144 \tambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;\n";
00145 }
00146
00147 if (isShaderDiffuse (shaderDescription))
00148 {
00149 vertexshader += "\n\
00150 \tdiffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;\n";
00151 }
00152
00153 vertexshader += "\n\
00154 }\n\n";
00155
00156 return vertexshader;
00157 }
00158
00159 MString ComposeGLSLangPixelshader (unsigned int shaderDescription)
00160 {
00161 MString pixelshader;
00162
00163 if (isShaderTexture (shaderDescription))
00164 {
00165 pixelshader += "\n\
00166 uniform sampler2D regTexSampler2D;\n";
00167 pixelshader += "\n\
00168 varying vec2 texCoord;\n";
00169 }
00170
00171 if (isShaderColor (shaderDescription))
00172 {
00173 pixelshader += "\n\
00174 varying vec4 color;\n";
00175 }
00176
00177 if (isShaderColor (shaderDescription))
00178 {
00179 pixelshader += "\n\
00180 varying vec3 halfVector;\n";
00181 }
00182
00183 if (isShaderAmbient (shaderDescription))
00184 {
00185 pixelshader += "\n\
00186 varying vec4 ambient;\n";
00187 }
00188
00189 if (isShaderDiffuse (shaderDescription))
00190 {
00191 pixelshader += "\n\
00192 varying vec4 diffuse;\n";
00193 }
00194
00195 if (isShaderDiffuse (shaderDescription) || isShaderSpecular (shaderDescription))
00196 {
00197 pixelshader += "\n\
00198 varying vec3 normalEyeSpace;\n\
00199 varying vec3 L;\n";
00200 }
00201
00202 pixelshader += "\n\
00203 void main ( void )\n\
00204 {\n\
00205 \n";
00206
00207 if (isShaderDiffuse (shaderDescription) || isShaderSpecular (shaderDescription))
00208 {
00209 pixelshader += "\n\
00210 \tvec3 N = normalize (normalEyeSpace);\n\
00211 \tL = normalize(vec3(gl_LightSource[0].position));\n";
00212 }
00213
00214 if (isShaderDiffuse (shaderDescription) || isShaderSpecular (shaderDescription))
00215 {
00216 pixelshader += "\n\
00217 \tfloat dpd = max(dot(N, L),0.0);\n";
00218 }
00219
00220 if (!isShaderColor (shaderDescription))
00221 {
00222 pixelshader += "\n\
00223 \tvec4 color = {1.0, 1.0, 1.0, 1.0};\n";
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 if (isShaderSpecular (shaderDescription))
00236 {
00237 pixelshader += "\n\
00238 \tvec3 half = normalize(halfVector);\n\
00239 \tfloat NdotHV = max(dot(N, half),0.0001);\n\
00240 \tvec4 specularColor = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV, gl_FrontMaterial.shininess);\n";
00241 }
00242
00243 if (isShaderTexture (shaderDescription))
00244 {
00245 pixelshader += "\n\
00246 \tvec4 textureColor = texture2D( regTexSampler2D, texCoord) ;\n";
00247
00248 }
00249 else
00250 {
00251 pixelshader += "\n\
00252 \tvec4 textureColor = {1.0, 1.0, 1.0, 1.0};\n";
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 if (!isShaderAmbient (shaderDescription) && !isShaderDiffuse (shaderDescription) && !isShaderSpecular (shaderDescription))
00289 {
00290 if (isShaderColor (shaderDescription))
00291 {
00292 pixelshader += "\n\
00293 \tvec4 outColor = color;\n";
00294 }
00295
00296 else if (!isShaderColor (shaderDescription))
00297 {
00298 pixelshader += "\n\
00299 \tvec4 outColor = vec4 (1.0, 1.0, 1.0, 1.0);\n";
00300 }
00301 }
00302 else
00303 {
00304 if (isShaderAmbient (shaderDescription))
00305 {
00306 pixelshader += "\n\
00307 \tvec4 outColor = ambient;\n";
00308 }
00309 else
00310 {
00311 pixelshader += "\n\
00312 \tvec4 outColor = vec4 (0.0, 0.0, 0.0, 0.0);\n";
00313 }
00314
00315 if (isShaderDiffuse (shaderDescription))
00316 {
00317 if (isShaderColor (shaderDescription))
00318 {
00319 pixelshader += "\n\
00320 \toutColor += diffuse*color*dpd;\n";
00321 }
00322 else
00323 {
00324 pixelshader += "\n\
00325 \toutColor += diffuse*dpd;\n";
00326 }
00327 }
00328
00329 if (isShaderSpecular (shaderDescription))
00330 {
00331 pixelshader += "\n\
00332 \toutColor += specularColor;\n";
00333 }
00334 }
00335
00336 if (isShaderTexture (shaderDescription))
00337 {
00338 pixelshader += "\n\
00339 \toutColor *= textureColor;\n";
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 pixelshader += "\n\
00351 \tgl_FragColor = outColor;\n";
00352
00353 pixelshader += "\n\
00354 }\n";
00355
00356
00357 return pixelshader;
00358 }
00359