Project

General

Profile

SO3Engine
SO3ShaderGeneratorUnified.cpp
Go to the documentation of this file.
1
8#include <boost/format.hpp>
9#include <boost/lexical_cast.hpp>
10#include <sstream>
11
12#include "SO3Renderer/SO3Root.h"
16
17namespace SO3
18{
19
20 SShaderGeneratorUnified::SShaderGeneratorUnified(Ogre::Technique* technique, Ogre::Pass* pass, ShaderType type, bool ignoreSlicePlane) : SShaderGenerator(technique, pass, type, ignoreSlicePlane)
21 {
22 }
23
28
30 {
32 BuildVertexShader();
33 else
34 BuildFragmentShader();
35 }
36
37 void SShaderGeneratorUnified::BuildVertexShader()
38 {
39 mSource << "\n#ifdef GL_ES\n";
40 mSource << " precision mediump int;\n";
41 mSource << " precision highp float;\n";
42 mSource << "#endif\n\n";
43
44 mSource << "#define USE_OGRE_FROM_FUTURE\n";
45 mSource << "#include <OgreUnifiedShader.h>\n";
46
47 //uniform
48 mSource << "OGRE_UNIFORMS(\n";
49 mSource << "uniform mat4 wMat;\n";
50
51 if (!((mConfig & MAP_NORMAL) && !mNotLight) && (!mNotLight || (mConfig & MAP_REFLECTION)))
52 mSource << "uniform mat4 witMat;\n";
53
54 mSource << "uniform mat4 wvpMat;\n";
55
56 for (unsigned int i = 0; i < mSortedTexUnits.size(); i++)
57 {
59 mSource << "uniform mat4 texMat" << mSortedTexUnits[i] << ";\n";
60 }
61
62 mSource << ")\n\n";
63
64 mSource << "MAIN_PARAMETERS\n";
65
66 // attributes
67 mSource << "IN(vec4 vertex, POSITION)\n";
68
70 mSource << "IN(vec4 colour, COLOR)\n";
71
73 mSource << "IN(vec3 normal, NORMAL)\n";
74
75 if ((mConfig & MAP_NORMAL) && !mNotLight)
76 mSource << "IN(vec4 tangent, TANGENT)\n";
77
78 for (unsigned int i = 0; i < mSortedTexUnits.size(); i++)
79 {
80 if (mSortedCubeUv[i])
81 mSource << "IN(vec3 uv";
82 else
83 mSource << "IN(vec2 uv";
84
86 mSource << ", TEXCOORD";
88 mSource << ")\n";
89 }
90
91 // varying
92 int texCoord = 0;
93
94 mSource << "OUT(vec4 oNorm, TEXCOORD";
95 mSource << texCoord;
96 mSource << ")\n";
97 texCoord++;
98
99 if ((mConfig & MAP_NORMAL) && !mNotLight)
100 {
101 mSource << "OUT(vec3 oTang, TEXCOORD";
102 mSource << texCoord;
103 mSource << ")\n";
104 texCoord++;
105
106 mSource << "OUT(vec3 oBinormal, TEXCOORD";
107 mSource << texCoord;
108 mSource << ")\n";
109 texCoord++;
110 }
111 mSource << "OUT(vec4 oWp, TEXCOORD";
112 mSource << texCoord;
113 mSource << ")\n";
114 texCoord++;
115
116 if (mUseVertexColor)
117 {
118 mSource << "OUT(vec4 oColor, TEXCOORD";
119 mSource << texCoord;
120 mSource << ")\n";
121 texCoord++;
122 }
123
124 int lastAvailableTexCoord = texCoord;
125 int lastUvIndex = -1;
126
127 for (unsigned int i = 0; i < mSortedTexUnits.size() && (texCoord < 8); i++)
128 {
129 if (mSortedCubeUv[i])
130 mSource << "OUT(vec3 oUv";
131 else
132 mSource << "OUT(vec2 oUv";
133
135 mSource << ", TEXCOORD";
136 mSource << texCoord;
137 mSource << ")\n";
138
139 lastUvIndex = mSortedTexUnits[i];
140 texCoord++;
141 }
142
143 // main start
144 mSource << "MAIN_DECLARATION\n";
145 mSource << "{\n";
146
147 mSource << "\toWp = mul(wMat, vertex);\n";
148 mSource << "\tvec4 vPos = mul(wvpMat, vertex);\n";
149 mSource << "\tgl_Position = vPos;\n";
150
151 // for fog
152 mSource << "\toWp.w = vPos.z;\n";
153 mSource << "\toNorm.w = vPos.w;\n";
154
155 texCoord = lastAvailableTexCoord;
156 lastUvIndex = -1;
157 for (unsigned int i = 0; i < mSortedTexUnits.size() && (texCoord < 8); i++)
158 {
159 bool aUV = IsUvAnimated(mSortedTexUnits[i]);
160
161 if (!aUV)
162 mSource << "\toUv" << mSortedTexUnits[i] << " = uv" << mSortedTexUnits[i] << ";\n";
163 else
164 mSource << "\toUv" << mSortedTexUnits[i] << " = mul(texMat" << mSortedTexUnits[i] << ", vec4(" << "uv" << mSortedTexUnits[i] << ", 0.0, 1.0)).xy;\n";
165
166 lastUvIndex = mSortedTexUnits[i];
167 texCoord++;
168 }
169
170 if ((mConfig & MAP_NORMAL) && !mNotLight)
171 {
172 mSource << "\toTang = tangent.xyz;\n";
173 mSource << "\toBinormal = cross(tangent.xyz, normal);\n";
174 mSource << "\toNorm.xyz = normal;\n";
175 }
176 else if (!mNotLight || (mConfig & MAP_REFLECTION))
177 {
178 mSource << "\toNorm.xyz = normalize(mul(witMat, vec4(normal, 1.0)).xyz);\n";
179 }
180 else
181 {
182 mSource << "\toNorm.xyz = vec3(0.0, 1.0, 0.0);\n";
183 }
184
185 if (mUseVertexColor)
186 mSource << "\toColor = colour;\n";
187
188 mSource << "}\n";
189 }
190
191 void SShaderGeneratorUnified::BuildFragmentShader()
192 {
193 // generate the shader
194 mSource << "\n#ifdef GL_ES\n";
195 mSource << " precision mediump int;\n";
196 mSource << " precision highp float;\n";
197 mSource << "#endif\n\n";
198
199 mSource << "#define USE_OGRE_FROM_FUTURE\n";
200 mSource << "#include <OgreUnifiedShader.h>\n";
201
202 //uniform
203 mSource << "OGRE_UNIFORMS(\n";
204
205 mSource << "uniform vec4 fogParams;\n";
206 mSource << "uniform vec4 fogColor;\n";
207
209 mSource << "\tuniform vec4 slicePlane;\n";
210
211 if (!mNotLight)
212 {
213 mSource << "uniform vec3 ambient;\n";
214
215 mSource << "uniform vec4 lightDif[" << mNbLights << "];\n";
216 mSource << "uniform vec4 lightPos[" << mNbLights << "];\n";
217 mSource << "uniform vec4 lightAtt[" << mNbLights << "];\n";
218 //mSource << "uniform vec3 lightSpec[" << mNbLights << "];\n";
219 mSource << "uniform vec4 spotlightParams[" << mNbLights << "];\n";
220 mSource << "uniform vec4 spotlightDir[" << mNbLights << "];\n";
221 }
222
223 if (!mNotLight || (mConfig & MAP_REFLECTION))
224 mSource << "uniform vec3 camPos;\n";
225
226 if (!mNotLight)
227 {
228 if (!mUseVertexColor)
229 {
230 mSource << "uniform vec4 matAmb;\n";
231 mSource << "uniform vec4 matDif;\n";
232 }
233
234 mSource << "uniform vec4 matEmissive;\n";
235 mSource << "uniform vec4 matSpec;\n";
236 mSource << "uniform float matShininess;\n";
237 }
238
239 if ((mConfig & MAP_NORMAL) && !mNotLight)
240 {
241 mSource << "uniform mat4 iTWMat;\n";
242 mSource << "uniform float normalMul;\n";
243 }
244
246 {
247 mSource << "uniform float reflectivity;\n";
248
249 if (mConfig & MAP_FRESNEL)
250 {
251 mSource << "uniform float fresnelMul;\n";
252 mSource << "uniform float fresnelPow;\n";
253 }
254 }
255
256 mSource << ")\n\n";
257
258 //SAMPLER
259 int texCoord = 0;
260 int samplerId = 0;
261 int ambUv = 0;
262 int diffUv = 0;
263 int specUv = 0;
264 int normUv = 0;
265 int illUv = 0;
266 int refMskUv = 0;
267
268 bool bAmbCube = false;
269 bool bDiffCube = false;
270 bool bSpecCube = false;
271 bool bNormCube = false;
272 bool bRefCube = false;
273 bool bIllCube = false;
274 bool bRefMskCube = false;
275
276 for (unsigned int i = 0; i < mTexTypes.size(); i++)
277 {
278 switch (mTexTypes[i])
279 {
280 //could be a light map
281 case MAP_AMBIENT:
282 if (mCubeUv[i])
283 mSource << "SAMPLERCUBE(ambMap, ";
284 else
285 mSource << "SAMPLER2D(ambMap, ";
286
287 mSource << samplerId;
288 mSource << ");\n";
289
290 ambUv = mTexUnits[i];
291 bAmbCube = mCubeUv[i];
292 break;
293
294 case MAP_DIFFUSE:
295 if (mCubeUv[i])
296 mSource << "SAMPLERCUBE(diffuseMap, ";
297 else
298 mSource << "SAMPLER2D(diffuseMap, ";
299
300 mSource << samplerId;
301 mSource << ");\n";
302
303 diffUv = mTexUnits[i];
304 bDiffCube = mCubeUv[i];
305 break;
306
307 case MAP_SPECULAR:
308 if (mCubeUv[i])
309 mSource << "SAMPLERCUBE(specMap, ";
310 else
311 mSource << "SAMPLER2D(specMap, ";
312
313 mSource << samplerId;
314 mSource << ");\n";
315
316 specUv = mTexUnits[i];
317 bSpecCube = mCubeUv[i];
318 break;
319
320 case MAP_NORMAL:
321 if (mCubeUv[i])
322 mSource << "SAMPLERCUBE(normalMap, ";
323 else
324 mSource << "SAMPLER2D(normalMap, ";
325
326 mSource << samplerId;
327 mSource << ");\n";
328
329 normUv = mTexUnits[i];
330 bNormCube = mCubeUv[i];
331 break;
332
333 case MAP_EMISSIVE:
334 if (mCubeUv[i])
335 mSource << "SAMPLERCUBE(illMap, ";
336 else
337 mSource << "SAMPLER2D(illMap, ";
338
339 mSource << samplerId;
340 mSource << ");\n";
341
342 illUv = mTexUnits[i];
343 bIllCube = mCubeUv[i];
344 break;
345
346 case MAP_REFLECTION:
347 if (mCubeUv[i])
348 mSource << "SAMPLERCUBE(reflectMap, ";
349 else
350 mSource << "SAMPLER2D(reflectMap, ";
351
352 mSource << samplerId;
353 mSource << ");\n";
354
355 bRefCube = mCubeUv[i];
356 break;
357
358 case MAP_REFMASK:
359 if (mCubeUv[i])
360 mSource << "SAMPLERCUBE(reflectMskMap, ";
361 else
362 mSource << "SAMPLER2D(reflectMskMap, ";
363
364 mSource << samplerId;
365 mSource << ");\n";
366
367 bRefMskCube = mCubeUv[i];
368 break;
369
370 default:
371 break;
372 }
373 samplerId++;
374 }
375
376 mSource << "\n";
377
378 mSource << "MAIN_PARAMETERS\n";
379
380 // varying
381 mSource << "IN(vec4 oNorm, TEXCOORD";
382 mSource << texCoord;
383 mSource << ")\n";
384 texCoord++;
385
386 if ((mConfig & MAP_NORMAL) && !mNotLight)
387 {
388 mSource << "IN(vec3 oTang, TEXCOORD";
389 mSource << texCoord;
390 mSource << ")\n";
391 texCoord++;
392
393 mSource << "IN(vec3 oBinormal, TEXCOORD";
394 mSource << texCoord;
395 mSource << ")\n";
396 texCoord++;
397 }
398
399 mSource << "IN(vec4 oWp, TEXCOORD";
400 mSource << texCoord;
401 mSource << ")\n";
402 texCoord++;
403
404 if (mUseVertexColor)
405 {
406 mSource << "IN(vec4 oColor, TEXCOORD";
407 mSource << texCoord;
408 mSource << ")\n";
409 texCoord++;
410 }
411
412 int lastAvailableTexCoord = texCoord;
413 int lastUvIndex = -1;
414
415 for (unsigned int i = 0; i < mSortedTexUnits.size() && (texCoord < 7); i++)
416 {
417 if (mSortedCubeUv[i])
418 mSource << "IN(vec3 oUv";
419 else
420 mSource << "IN(vec2 oUv";
421
423 mSource << ", TEXCOORD";
424 mSource << texCoord;
425 mSource << ")\n";
426
427 lastUvIndex = mSortedTexUnits[i];
428 texCoord++;
429 }
430
431 // generate the shader
432 // main start
433 mSource << "MAIN_DECLARATION\n";
434 mSource << "{\n";
435
437 {
438 mSource << "\tif (((slicePlane.x + slicePlane.y + slicePlane.z) != 0.0) && (slicePlane.x * oWp.x + slicePlane.y * oWp.y + slicePlane.z * oWp.z + slicePlane.w > 0.0))\n";
439 mSource << "\t\tdiscard;\n";
440 }
441
442 mSource << "\tfloat fog = 1.0;\n";
443 mSource << "\tif(fogParams.x == 0.0)\n";
444 mSource << "\t{\n";
445 mSource << "\t\tif (fogParams.w > 0.0)\n";
446 mSource << "\t\t\tfog = (fogParams.z - oWp.w) * fogParams.w;\n";
447 mSource << "\t}\n";
448 mSource << "\telse\n";
449 mSource << "\t\tfog = exp2(-fogParams.x * abs(oNorm.w));\n";
450 mSource << "\tfog = saturate(fog);\n";
451
452 // very slow on some devices !
453 //mSource << "\tif (fog == 0.0) {gl_FragColor = fogColor; return;}\n";
454
455 if ((mConfig & MAP_NORMAL) && !mNotLight)
456 {
457 if (bNormCube)
458 mSource << "\tvec3 normalTex = textureCube(normalMap, oUv" << normUv << ").rgb;\n";
459 else
460 mSource << "\tvec3 normalTex = texture2D(normalMap, oUv" << normUv << ".xy" << ").rgb;\n";
461
462 mSource << "\tmat3 tbn = mtxFromRows(oTang * vec3_splat(normalMul), oBinormal * vec3_splat(normalMul), oNorm.xyz);\n";
463 mSource << "\tvec3 normal = mul(transposeM(tbn), (normalTex.xyz - vec3_splat(0.5)) * vec3_splat(2.0)); // to object space\n";
464 mSource << "\tnormal = normalize(mul(iTWMat, vec4(normal, 1.0)).xyz);\n";
465 }
466 else
467 mSource << "\tvec3 normal = normalize(oNorm.xyz);\n";
468
469 if (!mNotLight || (mConfig & MAP_REFLECTION))
470 mSource << "\tvec3 camDir = normalize(camPos - oWp.xyz);\n";
471
472 if (!mNotLight)
473 {
474 mSource << "\tvec3 diffuseLight = vec3_splat(0.0);\n";
475 mSource << "\tvec3 specularLight = vec3_splat(0.0);\n";
476 mSource << "\tfloat shincontrib = 0.0;\n";
477 mSource << "\tfloat shinlt = 0.0;\n";
478 mSource << "\tfloat la = 0.0;\n";
479 mSource << "\tfloat spot = 0.0;\n";
480 mSource << "\tvec3 ld = vec3_splat(0.0);\n";
481 mSource << "\tfloat lc = 0.0;\n";
482 mSource << "\tfloat lightDist = 0.0;\n";
483
484 mSource << "\tfor (int i = 0; i < " << mNbLights << "; i++)\n";
485 mSource << "\t{\n";
486
487 // direction
488 mSource << "\tld = normalize(lightPos[i].xyz - (vec3_splat(lightPos[i].w) * oWp.xyz));\n";
489
490 mSource << "\t// attenuation\n";
491 mSource << "\tlightDist = length(lightPos[i].xyz - oWp.xyz);\n";
492 mSource << "\tla = 1.0;\n";
493 mSource << "\tif(lightAtt[i].w > 0.0)\n";
494 mSource << "\t{\n";
495 mSource << "\t\tif(lightDist > lightAtt[i].x) continue;\n";
496 mSource << "\t\tla = 1.0 / (lightAtt[i].y + lightAtt[i].z * lightDist + lightAtt[i].w * lightDist * lightDist);\n";
497 mSource << "\t\t// calculate the spotlight effect\n";
498 mSource << "\t\tfloat spot = ((spotlightParams[i].w == 0.0) ? 1.0 : // if so, then it's not a spot light\n";
499 mSource << "\t\t saturate(((dot(-normalize(spotlightDir[i].xyz - (vec3_splat(spotlightDir[i].w) * oWp.xyz)), ld) - spotlightParams[i].y) / (spotlightParams[i].x - spotlightParams[i].y))));\n";
500 mSource << "\t\tlc = saturate(dot(normal, ld)) * spot * la;\n";
501 mSource << "\t}\n";
502 mSource << "\telse\n";
503 mSource << "\t{\n";
504 mSource << "\t\tlc = saturate(dot(normal, ld));\n";
505 mSource << "\t}\n";
506 mSource << "\tshinlt = pow(max(dot(normal, normalize(ld + camDir)), 0.0), matShininess + 0.1) * lc;\n";
507
508 mSource << "\tdiffuseLight += lightDif[i].rgb * vec3_splat(lc);\n";
509 mSource << "\tspecularLight += lightDif[i].rgb * vec3_splat(shinlt);\n";
510 mSource << "\tshincontrib += shinlt;\n";
511
512 mSource << "\t}\n";
513
514 if (!mUseVertexColor)
515 {
516 mSource << "\tvec3 ambientColor = ambient * matAmb.rgb;\n";
517 mSource << "\tvec3 diffuseContrib = matDif.rgb;\n";
518 }
519 else
520 {
521 mSource << "\tvec3 ambientColor = ambient * oColor.rgb;\n";
522 mSource << "\tvec3 diffuseContrib = oColor.rgb;\n";
523 }
524
525 if (!(mConfig & MAP_EMISSIVE))
526 {
527 mSource << "\tambientColor = max(matEmissive.rgb, ambientColor);\n";
528 }
529 }
530 else
531 {
532 if (!mUseVertexColor)
533 mSource << "\tvec4 ambientColor = vec4_splat(1.0);\n";
534 else
535 mSource << "\tvec4 ambientColor = oColor;\n";
536 }
537 if (mConfig & MAP_AMBIENT)
538 {
539 if (bAmbCube)
540 mSource << "\tvec4 ambTex = textureCube(ambMap, oUv" << ambUv << ");\n";
541 else
542 mSource << "\tvec4 ambTex = texture2D(ambMap, oUv" << ambUv << ".xy" << ");\n";
543
545 mSource << "\tif (ambTex.a < 0.05) discard;\n";
546
547 if (!mNotLight)
548 mSource << "\tambientColor *= ambTex.rgb;\n";
549 else
550 mSource << "\tambientColor *= ambTex;\n";
551 }
552
553 if (mConfig & MAP_DIFFUSE)
554 {
555 if (bDiffCube)
556 mSource << "\tvec4 diffuseTex = textureCube(diffuseMap, oUv" << diffUv << ");\n";
557 else
558 mSource << "\tvec4 diffuseTex = texture2D(diffuseMap, oUv" << diffUv << ".xy" << ");\n";
559
561 mSource << "\tif (diffuseTex.a < 0.05) discard;\n";
562
563 if (!mNotLight)
564 {
565 mSource << "\tambientColor *= diffuseTex.rgb;\n";
566 mSource << "\tdiffuseContrib *= diffuseTex.rgb;\n";
567 }
568 else
569 {
570 mSource << "\tambientColor *= diffuseTex;\n";
571 }
572 }
573
574 if (mConfig & MAP_EMISSIVE)
575 {
576 if (bIllCube)
577 mSource << "\tvec3 illTex = textureCube(illMap, oUv" << illUv << ").rgb;\n";
578 else
579 mSource << "\tvec3 illTex = texture2D(illMap, oUv" << illUv << ".xy" << ").rgb;\n";
580
581 if (!mNotLight)
582 mSource << "\tambientColor = max(ambientColor, matEmissive.rgb * illTex);\n";
583 else
584 mSource << "\tambientColor += vec4(illTex, 0.0);\n";
585 }
586
587 if (!mNotLight)
588 mSource << "\tvec4 specularContrib = vec4(specularLight, 1.0) * matSpec;\n";
589
590 if (mConfig & MAP_SPECULAR)
591 {
592 if (bSpecCube)
593 mSource << "\tvec3 specTex = textureCube(specMap, oUv" << specUv << ").rgb;\n";
594 else
595 mSource << "\tvec3 specTex = texture2D(specMap, oUv" << specUv << ".xy" << ").rgb;\n";
596
597 mSource << "\tspecularContrib.rgb *= specTex;\n";
598 }
599
600 //if(bAmbient)
601 //mSource << "\tspecularContrib.rgb *= ambTex.rgb;\n";
602
603 if (!mNotLight)
604 {
605 mSource << "\tvec3 light0C = saturate(ambientColor + (diffuseLight * diffuseContrib) + specularContrib.rgb);\n";
606
607 if (!mUseVertexColor)
608 mSource << "\tfloat alpha = saturate(matDif.a + (specularContrib.a * shincontrib * matDif.a));\n";
609 else
610 mSource << "\tfloat alpha = saturate(oColor.a + (specularContrib.a * shincontrib * oColor.a));\n";
611
612 if (mConfig & MAP_DIFFUSE)
613 mSource << "\talpha *= diffuseTex.a;\n";
614 else if (mConfig & MAP_AMBIENT)
615 mSource << "\talpha *= ambTex.a;\n";
616
617 //mSource << "\tif (alpha < 0.01) discard;\n";
618 }
619
621 {
622 mSource << "\tvec3 refVec = -reflect(camDir, normal);\n";
623
624 if (bRefCube)
625 {
626 mSource << "\trefVec.z = -refVec.z;\n";
627 mSource << "\tvec4 reflecTex = textureCube(reflectMap, refVec);\n";
628 }
629 else
630 {
631 mSource << "\tfloat mref = 2.0 * sqrt((refVec.x * refVec.x) + (refVec.y * refVec.y) + (refVec.z * refVec.z));\n";
632 mSource << "\tvec2 pref = vec2(0.5 + refVec.x / mref, 0.5 - refVec.y / mref);\n";
633 mSource << "\tvec4 reflecTex = texture2D(reflectMap, pref);\n";
634 }
635
636 if ((mConfig & MAP_FRESNEL) && !mNotLight)
637 {
638 mSource << "\tfloat fresnel = fresnelMul * reflectivity * pow(1.0 + dot(-camDir, normal), fresnelPow - (reflectivity * fresnelMul));\n";
639 mSource << "\tvec4 reflecVal = saturate(reflecTex * fresnel);\n";
640 mSource << "\tvec3 reflectColor = reflecVal.rgb * (vec3_splat(1.0) - light0C);\n";
641 }
642 else //metal style
643 {
644 mSource << "\tvec3 reflectColor = reflecTex.rgb * reflectivity;\n";
645 }
646
647 if (mConfig & MAP_REFMASK)
648 {
649 if (bRefMskCube)
650 mSource << "\tvec3 refTexMsk = textureCube(reflectMskMap, oUv" << refMskUv << ").rgb;\n";
651 else
652 mSource << "\tvec3 refTexMsk = texture2D(reflectMskMap, oUv" << refMskUv << ".xy" << ").rgb;\n";
653
654 mSource << "\treflectColor *= refTexMsk;\n";
655 }
656
657 if (!mNotLight)
658 {
659 mSource << "\tgl_FragColor = vec4((vec3_splat(fog) * (light0C + reflectColor)) + (fogColor.rgb * vec3_splat(1.0 - fog)), alpha);\n";
660 }
661 else
662 {
663 mSource << "\tgl_FragColor = vec4((vec3_splat(fog) * (ambientColor.rgb + reflectColor)) + (fogColor.rgb * vec3_splat(1.0 - fog)), ambientColor.w);\n";
664 }
665 }
666 else
667 {
668 if (!mNotLight)
669 mSource << "\tgl_FragColor = vec4((vec3_splat(fog) * light0C) + (fogColor.rgb * vec3_splat(1.0 - fog)), alpha);\n";
670 else
671 mSource << "\tgl_FragColor = vec4((vec3_splat(fog) * ambientColor.rgb) + (fogColor.rgb * vec3_splat(1.0 - fog)), ambientColor.w);\n";
672 }
673
674 mSource << "}\n";
675 }
676
678 {
679 std::stringstream out;
680
682 {
683 out << "\t\t\tvertex_program_ref " << GetShaderName() << "\n";
684 out << "\t\t\t{\n";
685
686 for (unsigned int i = 0; i < mSortedTexUnits.size(); i++)
687 {
689 out << "\t\t\t\tparam_named_auto " << "texMat" << mSortedTexUnits[i] << " texture_matrix " << mSortedTexUnits[i] << "\n";
690 }
691
692 out << "\t\t\t}\n";
693 }
694 else
695 {
696 out << "\t\t\tfragment_program_ref " << GetShaderName() << "\n";
697 out << "\t\t\t{\n";
698
699 if ((mConfig & MAP_NORMAL) && !mNotLight)
700 {
701 //TODO where could I get that kind of value ?
702 out << "\t\t\t\tparam_named normalMul float " << 1.0f << "\n";
703 }
704
706 {
707 //TODO where could I get that kind of value ?
708 out << "\t\t\t\tparam_named reflectivity float " << 0.8f << "\n";
709 if (mConfig & MAP_FRESNEL)
710 {
711 out << "\t\t\t\tparam_named fresnelMul float " << 0.8f << "\n";
712 out << "\t\t\t\tparam_named fresnelPow float " << ((mPass->getShininess() / 255.0f) * 2.0f) << "\n";
713 }
714 }
715
716 out << "\t\t\t}\n";
717 }
718 mParams = out.str();
719 return mParams;
720 }
721
723 {
724 std::stringstream out;
725
727 {
728 //HLSL
729 out << "vertex_program " << GetShaderName() << "_HLSL hlsl\n";
730 out << "{\n";
731 out << "\tsource " << GetShaderName() << "VP.glsl\n";
732 out << "\ttarget vs_3_0\n";
733 out << "}\n";
734
735 //GL
736 out << "vertex_program " << GetShaderName() << "_GL glsl glsles glslang\n";
737 out << "{\n";
738 out << "\tsource " << GetShaderName() << "VP.glsl\n";
739 out << "}\n";
740
741 //UNIFIED
742 out << "vertex_program " << GetShaderName() << " unified\n";
743 out << "{\n";
744
745 out << "\tdelegate " << GetShaderName() << "_HLSL\n";
746 out << "\tdelegate " << GetShaderName() << "_GL\n";
747
748 out << "\tdefault_params\n";
749 out << "\t{\n";
750 out << "\t\tparam_named_auto wMat world_matrix\n";
751
752 if (!((mConfig & MAP_NORMAL) && !mNotLight) && (!mNotLight || (mConfig & MAP_REFLECTION)))
753 out << "\t\tparam_named_auto witMat inverse_transpose_world_matrix\n";
754
755 out << "\t\tparam_named_auto wvpMat worldviewproj_matrix\n";
756
757 out << "\t}\n";
758 out << "}\n";
759 }
760 else
761 {
762 //HLSL
763 out << "fragment_program " << GetShaderName() << "_HLSL hlsl\n";
764 out << "{\n";
765
766 out << "\tsource " << GetShaderName() << "FP.glsl\n";
767 out << "\ttarget ps_3_0\n";
768
769 out << "}\n";
770
771 //GL
772 out << "fragment_program " << GetShaderName() << "_GL glsl glsles glslang\n";
773 out << "{\n";
774
775 out << "\tsource " << GetShaderName() << "FP.glsl\n";
776
777 out << "\tdefault_params\n";
778 out << "\t{\n";
779
780 //samplers
781 int samplerId = 0;
782 bool isMask = false;
783 for (unsigned int i = 0; i < mTexTypes.size(); i++)
784 {
785 switch (mTexTypes[i])
786 {
787 case MAP_AMBIENT:
788 out << "\t\tparam_named ambMap int " << samplerId << "\n";
789 break;
790
791 case MAP_DIFFUSE:
792 out << "\t\tparam_named diffuseMap int " << samplerId << "\n";
793 break;
794
795 case MAP_SPECULAR:
796 out << "\t\tparam_named specMap int " << samplerId << "\n";
797 break;
798
799 case MAP_NORMAL:
800 out << "\t\tparam_named normalMap int " << samplerId << "\n";
801 break;
802
803 case MAP_EMISSIVE:
804 out << "\t\tparam_named illMap int " << samplerId << "\n";
805 break;
806
807 case MAP_REFLECTION:
808 out << "\t\tparam_named reflectMap int " << samplerId << "\n";
809 break;
810
811 case MAP_REFMASK:
812 out << "\t\tparam_named reflectMskMap int " << samplerId << "\n";
813 break;
814 }
815
816 samplerId++;
817 }
818
819 out << "\t}\n";
820 out << "}\n";
821
822 //UNIFIED
823 out << "fragment_program " << GetShaderName() << " unified\n";
824 out << "{\n";
825
826 out << "\tdelegate " << GetShaderName() << "_HLSL\n";
827 out << "\tdelegate " << GetShaderName() << "_GL\n";
828
829 out << "\tdefault_params\n";
830 out << "\t{\n";
831
832 out << "\t\tparam_named_auto fogParams fog_params\n";
833 out << "\t\tparam_named_auto fogColor fog_colour\n";
834
835 if (!mNotLight)
836 {
837 out << "\t\tparam_named_auto ambient ambient_light_colour\n";
838
839 out << "\t\tparam_named_auto lightDif light_diffuse_colour_power_scaled_array " << mNbLights << "\n";
840 out << "\t\tparam_named_auto lightPos light_position_array " << mNbLights << "\n";
841 out << "\t\tparam_named_auto lightAtt light_attenuation_array " << mNbLights << "\n";
842 //out << "\t\tparam_named_auto lightSpec light_specular_colour_array " << mNbLights << "\n";
843 out << "\t\tparam_named_auto spotlightParams spotlight_params_array " << mNbLights << "\n";
844 out << "\t\tparam_named_auto spotlightDir light_direction_array " << mNbLights << "\n";
845
846 out << "\t\tparam_named_auto camPos camera_position\n";
847
848 if (!mUseVertexColor)
849 {
850 out << "\t\tparam_named_auto matAmb surface_ambient_colour\n";
851 out << "\t\tparam_named_auto matDif surface_diffuse_colour\n";
852 }
853
854 out << "\t\tparam_named_auto matEmissive surface_emissive_colour\n";
855 out << "\t\tparam_named_auto matSpec surface_specular_colour\n";
856 out << "\t\tparam_named_auto matShininess surface_shininess\n";
857 }
858 else
859 {
861 out << "\t\tparam_named_auto camPos camera_position\n";
862 }
863
864 if ((mConfig & MAP_NORMAL) && !mNotLight)
865 {
866 out << "\t\tparam_named_auto iTWMat inverse_transpose_world_matrix\n";
867 out << "\t\tparam_named normalMul float 1\n";
868 }
869
871 {
872 out << "\t\tparam_named reflectivity float 1.0\n";
873
874 if (mConfig & MAP_FRESNEL)
875 {
876 out << "\t\tparam_named fresnelMul float 1.0\n";
877 out << "\t\tparam_named fresnelPow float 1.0\n";
878 }
879 }
880
881 out << "\t}\n";
882 out << "}\n";
883 }
884 mProgram = out.str();
885 return mProgram;
886 }
887
889 {
890 Ogre::String rendererName = SRoot::getSingletonPtr()->GetOgreRenderSystem()->getName();
891 bool isGlSL = (rendererName == "Direct3D9 Rendering Subsystem" || rendererName == "Direct3D11 Rendering Subsystem") ? false : true;
892 bool isGlSLang = (rendererName == "Vulkan Rendering Subsystem") ? true : false;
893 Ogre::String language = ((rendererName == "OpenGL ES 2.x Rendering Subsystem") || (rendererName == "Metal Rendering Subsystem")) ? "glsles" : (rendererName == "Vulkan Rendering Subsystem") ? "glslang" : isGlSL ? "glsl" : "hlsl";
894
896 {
897 //Try to get the shader if it exist already
898 Ogre::GpuProgramPtr shaderPtr = Ogre::GpuProgramManager::getSingletonPtr()->getByName(GetShaderName(), Ogre::RGN_DEFAULT);
899
900 // otherwise we load it
901 if (!shaderPtr)
902 {
904 shaderPtr = Ogre::GpuProgramManager::getSingletonPtr()->create(GetShaderName(),
905 Ogre::RGN_DEFAULT,
906 Ogre::GPT_VERTEX_PROGRAM,
907 language);
908
909 shaderPtr->setSource(mSource.str());
910 if (!isGlSL)
911 shaderPtr->setParameter("target", "vs_3_0");
912
913 //Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(">>>> Generated shader - " + GetShaderName() + " - :\n" + mSource.str() + "\n>>>>>\n");
914
915 // Set auto param binding
916 Ogre::GpuProgramParametersSharedPtr vpParams = shaderPtr->getDefaultParameters();
917
918 vpParams->setNamedAutoConstant("wMat", Ogre::GpuProgramParameters::ACT_WORLD_MATRIX);
919
920 if (!((mConfig & MAP_NORMAL) && !mNotLight) && (!mNotLight || (mConfig & MAP_REFLECTION)))
921 vpParams->setNamedAutoConstant("witMat", Ogre::GpuProgramParameters::ACT_INVERSE_TRANSPOSE_WORLD_MATRIX);
922
923 vpParams->setNamedAutoConstant("wvpMat", Ogre::GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
924
925 // All defined, load!
926 shaderPtr->load();
927
928 mGeneratedShaders.push_back(shaderPtr);
929 }
930
931 if (shaderPtr->hasCompileError())
932 {
933 shaderPtr.reset();
934 mPass->setVertexProgram("");
935 return;
936 }
937
938 mPass->setVertexProgram(GetShaderName());
939 Ogre::GpuProgramParametersSharedPtr passParams = mPass->getVertexProgramParameters();
940
941 // on pass
942 for (unsigned int i = 0; i < mSortedTexUnits.size(); i++)
943 {
944 std::stringstream pname;
945
947 {
948 pname << "texMat" << mSortedTexUnits[i];
949 passParams->setNamedAutoConstant(pname.str(), Ogre::GpuProgramParameters::ACT_TEXTURE_MATRIX, mSortedTexUnits[i]);
950 }
951 }
952 }
953 else
954 {
955 //Try to get the shader if it exist already
956 Ogre::GpuProgramPtr shaderPtr = Ogre::GpuProgramManager::getSingletonPtr()->getByName(GetShaderName(), Ogre::RGN_DEFAULT);
957
958 // otherwise we load it
959 if (!shaderPtr)
960 {
962 shaderPtr = Ogre::GpuProgramManager::getSingletonPtr()->create(GetShaderName(),
963 Ogre::RGN_DEFAULT,
964 Ogre::GPT_FRAGMENT_PROGRAM,
965 language);
966
967 shaderPtr->setSource(mSource.str());
968 if (!isGlSL)
969 shaderPtr->setParameter("target", "ps_3_0");
970
971 //Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(">>>> Generated shader - " + GetShaderName() + " - :\n" + mSource.str() + "\n>>>>>\n");
972
973 // Set auto param binding
974 Ogre::GpuProgramParametersSharedPtr vpParams = shaderPtr->getDefaultParameters();
975
976 vpParams->setNamedAutoConstant("fogParams", Ogre::GpuProgramParameters::ACT_FOG_PARAMS);
977 vpParams->setNamedAutoConstant("fogColor", Ogre::GpuProgramParameters::ACT_FOG_COLOUR);
978
980 vpParams->addSharedParameters("SO3SlicePlaneParams");
981
982 //common
984 vpParams->setNamedAutoConstant("camPos", Ogre::GpuProgramParameters::ACT_CAMERA_POSITION);
985
986 if (!mNotLight)
987 {
988 vpParams->setNamedAutoConstant("ambient", Ogre::GpuProgramParameters::ACT_AMBIENT_LIGHT_COLOUR);
989
990 if (!mUseVertexColor)
991 {
992 vpParams->setNamedAutoConstant("matAmb", Ogre::GpuProgramParameters::ACT_SURFACE_AMBIENT_COLOUR);
993 vpParams->setNamedAutoConstant("matDif", Ogre::GpuProgramParameters::ACT_SURFACE_DIFFUSE_COLOUR);
994 }
995
996 vpParams->setNamedAutoConstant("matEmissive", Ogre::GpuProgramParameters::ACT_SURFACE_EMISSIVE_COLOUR);
997 vpParams->setNamedAutoConstant("matSpec", Ogre::GpuProgramParameters::ACT_SURFACE_SPECULAR_COLOUR);
998 vpParams->setNamedAutoConstant("matShininess", Ogre::GpuProgramParameters::ACT_SURFACE_SHININESS);
999
1000 vpParams->setNamedAutoConstant("lightDif", Ogre::GpuProgramParameters::ACT_LIGHT_DIFFUSE_COLOUR_POWER_SCALED_ARRAY, mNbLights);
1001 vpParams->setNamedAutoConstant("lightPos", Ogre::GpuProgramParameters::ACT_LIGHT_POSITION_ARRAY, mNbLights);
1002 vpParams->setNamedAutoConstant("lightAtt", Ogre::GpuProgramParameters::ACT_LIGHT_ATTENUATION_ARRAY, mNbLights);
1003 //vpParams->setNamedAutoConstant("lightSpec", Ogre::GpuProgramParameters::ACT_LIGHT_SPECULAR_COLOUR_ARRAY, mNbLights);
1004 vpParams->setNamedAutoConstant("spotlightParams", Ogre::GpuProgramParameters::ACT_SPOTLIGHT_PARAMS_ARRAY, mNbLights);
1005 vpParams->setNamedAutoConstant("spotlightDir", Ogre::GpuProgramParameters::ACT_LIGHT_DIRECTION_ARRAY, mNbLights);
1006 }
1007
1008 if ((mConfig & MAP_NORMAL) && !mNotLight)
1009 {
1010 vpParams->setNamedAutoConstant("iTWMat", Ogre::GpuProgramParameters::ACT_INVERSE_TRANSPOSE_WORLD_MATRIX);
1011 vpParams->setNamedConstant("normalMul", 1.0f);
1012 }
1013
1014 if (mConfig & MAP_REFLECTION)
1015 {
1016 vpParams->setNamedConstant("reflectivity", 1.0f);
1017
1018 if (mConfig & MAP_FRESNEL)
1019 {
1020 vpParams->setNamedConstant("fresnelMul", 1.0f);
1021 vpParams->setNamedConstant("fresnelPow", 1.0f);
1022 }
1023 }
1024
1025 int samplerId = 0;
1026 bool isMask = false;
1027 if (isGlSL && !isGlSLang)
1028 for (unsigned int i = 0; i < mTexTypes.size(); i++)
1029 {
1030 switch (mTexTypes[i])
1031 {
1032 case MAP_AMBIENT:
1033 vpParams->setNamedConstant("ambMap", samplerId);
1034 break;
1035
1036 case MAP_DIFFUSE:
1037 vpParams->setNamedConstant("diffuseMap", samplerId);
1038 break;
1039
1040 case MAP_SPECULAR:
1041 vpParams->setNamedConstant("specMap", samplerId);
1042 break;
1043
1044 case MAP_NORMAL:
1045 vpParams->setNamedConstant("normalMap", samplerId);
1046 break;
1047
1048 case MAP_EMISSIVE:
1049 vpParams->setNamedConstant("illMap", samplerId);
1050 break;
1051
1052 case MAP_REFLECTION:
1053 vpParams->setNamedConstant("reflectMap", samplerId);
1054 break;
1055
1056 case MAP_REFMASK:
1057 vpParams->setNamedConstant("reflectMskMap", samplerId);
1058 break;
1059 }
1060
1061 samplerId++;
1062 }
1063
1064 // All defined, load!
1065 shaderPtr->load();
1066
1067 mGeneratedShaders.push_back(shaderPtr);
1068 }
1069
1070 if (shaderPtr->hasCompileError())
1071 {
1072 shaderPtr.reset();
1073 mPass->setFragmentProgram("");
1074 return;
1075 }
1076
1077 mPass->setFragmentProgram(GetShaderName());
1078 Ogre::GpuProgramParametersSharedPtr passParams = mPass->getFragmentProgramParameters();
1079
1080 // on pass
1081 if ((mConfig & MAP_NORMAL) && !mNotLight)
1082 {
1083 passParams->setNamedConstant("normalMul", 1.0f);
1084 }
1085
1086 if (mConfig & MAP_REFLECTION)
1087 {
1088 passParams->setNamedConstant("reflectivity", mRefTexCoef);
1089
1090 if (mConfig & MAP_FRESNEL)
1091 {
1092 passParams->setNamedConstant("fresnelMul", mRefTexCoef * 1.5f);
1093 passParams->setNamedConstant("fresnelPow", ((mPass->getShininess() / 255.0f) * (mRefTexCoef * 10.0f)));
1094 }
1095 }
1096 }
1097 }
1098
1099}
Ogre::RenderSystem * GetOgreRenderSystem()
Definition SO3Root.cpp:865
static SRoot * getSingletonPtr()
Definition SO3Root.cpp:111
std::vector< unsigned int > mSortedTexUnits
std::vector< ShaderMapType > mTexTypes
std::vector< bool > mSortedCubeUv
bool IsUvAnimated(int uvindex)
std::stringstream mSource
std::vector< unsigned int > mTexUnits
std::vector< bool > mCubeUv
static GeneratedShaders mGeneratedShaders
SShaderGeneratorUnified(Ogre::Technique *, Ogre::Pass *pass, ShaderType type, bool ignoreSlicePlane=false)