Project

General

Profile

SO3Engine
SO3ShaderGeneratorCG.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
20SShaderGeneratorCG::SShaderGeneratorCG(Ogre::Technique* technique, Ogre::Pass* pass, ShaderType type, bool ignoreSlicePlane) : SShaderGenerator(technique, pass, type, ignoreSlicePlane)
21{
22
23}
24
29
31{
33 BuildVertexShader();
34 else
35 BuildFragmentShader();
36}
37
38void SShaderGeneratorCG::BuildVertexShader()
39{
40 // generate the shader
41 mSource << "void " << GetShaderName() << "(float4 position : POSITION,\n";
42 mSource << "\tfloat3 normal : NORMAL,\n";
43 if( (mConfig & MAP_NORMAL) && !mNotLight)
44 mSource << "\tfloat3 tangent : TANGENT,\n";
45
47 mSource << "\tfloat4 iColor : COLOR,\n";
48
49 for (unsigned int i = 0; i < mSortedTexUnits.size(); i++)
50 {
51 if (mSortedCubeUv[i])
52 mSource << "\tfloat3 uv";
53 else
54 mSource << "\tfloat2 uv";
55
56 mSource << mSortedTexUnits[i] << " : TEXCOORD" << mSortedTexUnits[i] << ",\n";
57 }
58
59 int texCoord = 0;
60 mSource << "\tout float4 oPos : POSITION,\n";
61 mSource << "\tout float4 oNorm : TEXCOORD" << texCoord++ << ",\n";
62
63 if ((mConfig & MAP_NORMAL) && !mNotLight)
64 {
65 mSource << "\tout float3 oTang : TEXCOORD" << texCoord++ << ",\n";
66 mSource << "\tout float3 oBinormal : TEXCOORD" << texCoord++ << ",\n";
67 }
68
70 mSource << "\tout float4 oColor : TEXCOORD" << texCoord++ << ",\n";
71
72 mSource << "\tout float4 oWp : TEXCOORD" << texCoord++ << ",\n";
73
74 int lastAvailableTexCoord = texCoord;
75 int lastUvIndex = -1;
76
77 for (unsigned int i = 0; i < mSortedTexUnits.size() && (texCoord < 16); i++)
78 {
79 if (mSortedCubeUv[i])
80 mSource << "\tout float3 oUv";
81 else
82 mSource << "\tout float2 oUv";
83
84 mSource << mSortedTexUnits[i] << " : TEXCOORD" << texCoord++ << ",\n";
85 lastUvIndex = mSortedTexUnits[i];
86 }
87
88 if (texCoord > 8)
89 mHighProfile = true;
90
91 mSource << "\tuniform float4x4 wMat,\n";
92
93 if (!(mConfig & MAP_NORMAL) || mNotLight)
94 mSource << "\tuniform float4x4 witMat,\n";
95
96 mSource << "\tuniform float4x4 wvpMat";
97
98 for (unsigned int i = 0; i < mSortedTexUnits.size(); i++)
99 {
101 mSource << ",\n\tuniform float4x4 texMat" << mSortedTexUnits[i];
102 }
103
104 mSource << "\n)\n";
105 mSource << "{\n";
106
107 mSource << "\toWp = mul(wMat, position);\n";
108 mSource << "\toPos = mul(wvpMat, position);\n";
109
110 // for fog
111 mSource << "\toWp.w = oPos.z;\n";
112 mSource << "\toNorm.w = oPos.w;\n";
113
114 texCoord = lastAvailableTexCoord;
115 lastUvIndex = -1;
116 for (unsigned int i = 0; i < mSortedTexUnits.size() && (texCoord < 16); i++)
117 {
118 bool aUV = IsUvAnimated(mSortedTexUnits[i]);
119
120 if (!aUV)
121 mSource << "\toUv" << mSortedTexUnits[i] << " = uv" << mSortedTexUnits[i] << ";\n";
122 else
123 mSource << "\toUv" << mSortedTexUnits[i] << " = mul(texMat" << mSortedTexUnits[i] << ", float4(" << "uv" << mSortedTexUnits[i] << ", 0.0, 1.0)).xy;\n";
124
125 lastUvIndex = mSortedTexUnits[i];
126 texCoord++;
127 }
128
129 if (texCoord > 8)
130 mHighProfile = true;
131
132 if ((mConfig & MAP_NORMAL) && !mNotLight)
133 {
134 mSource << "\toTang = tangent;\n";
135 mSource << "\toBinormal = cross(tangent, normal);\n";
136 mSource << "\toNorm.xyz = normal;\n";
137 }
138 else
139 {
140 mSource << "\toNorm.xyz = normalize(mul((float3x3)witMat, normal));\n";
141 }
142
143 if (mUseVertexColor)
144 mSource << "\toColor = iColor;\n";
145
146 mSource << "}\n";
147}
148
149void SShaderGeneratorCG::BuildFragmentShader()
150{
151 int texCoord = 0;
152
153 // generate the shader
154 mSource << "float4 " << GetShaderName() << "(float4 position : POSITION,\n";
155 mSource << "\tfloat4 norm : TEXCOORD" << texCoord++ << ",\n";
156
157 if ((mConfig & MAP_NORMAL) && !mNotLight)
158 {
159 mSource << "\tfloat3 tangent : TEXCOORD" << texCoord++ << ",\n";
160 mSource << "\tfloat3 binormal : TEXCOORD" << texCoord++ << ",\n";
161 }
162
163 if (mUseVertexColor)
164 mSource << "\tfloat4 iColor : TEXCOORD" << texCoord++ << ",\n";
165
166 mSource << "\tfloat4 wp : TEXCOORD" << texCoord++ << ",\n";
167
168 int lastUvIndex = -1;
169 for (unsigned int i = 0; i < mSortedTexUnits.size() && (texCoord < 16); i++)
170 {
171 if (mSortedCubeUv[i])
172 mSource << "\tfloat3 uv" << mSortedTexUnits[i] << " : TEXCOORD" << texCoord++ << ",\n";
173 else
174 mSource << "\tfloat2 uv" << mSortedTexUnits[i] << " : TEXCOORD" << texCoord++ << ",\n";
175
176 lastUvIndex = mSortedTexUnits[i];
177 }
178
179 if (texCoord > 8)
180 mHighProfile = true;
181
183 mSource << "\tuniform float4 slicePlane,\n";
184
185 if (!mNotLight)
186 {
187 //mSource << "\tuniform float4x4 wMat,\n";
188 mSource << "\tuniform float4 ambient,\n";
189
190 mSource << "\tuniform float4 lightDif[" << mNbLights << "],\n";
191 mSource << "\tuniform float4 lightPos[" << mNbLights << "],\n";
192 mSource << "\tuniform float4 lightAtt[" << mNbLights << "],\n";
193 //mSource << "\tuniform float3 lightSpec[" << mNbLights << "],\n";
194 mSource << "\tuniform float4 spotlightParams[" << mNbLights << "],\n";
195 mSource << "\tuniform float4 spotlightDir[" << mNbLights << "],\n";
196 }
197
199 mSource << "\tuniform float3 camPos,\n";
200
201 mSource << "\tuniform float4 fogParams,\n";
202 mSource << "\tuniform float4 fogColor";
203
204 if (!mNotLight)
205 {
206 if (!mUseVertexColor)
207 {
208 mSource << ",\n\tuniform float4 matAmb";
209 mSource << ",\n\tuniform float4 matDif";
210 }
211
212 mSource << ",\n\tuniform float4 matEmissive";
213 mSource << ",\n\tuniform float4 matSpec";
214 mSource << ",\n\tuniform float matShininess";
215 }
216
217 if ((mConfig & MAP_NORMAL) && !mNotLight)
218 {
219 mSource << ",\n\tuniform float4x4 iTWMat";
220 //mSource << ",\n\tuniform float normalMul";
221 }
222
224 {
225 mSource << ",\n\tuniform float reflectivity";
226
227 if (mConfig & MAP_FRESNEL)
228 {
229 mSource << ",\n\tuniform float fresnelMul";
230 mSource << ",\n\tuniform float fresnelPow";
231 }
232 }
233
234 int samplerId = 0;
235 int ambUv = 0;
236 int diffUv = 0;
237 int specUv = 0;
238 int normUv = 0;
239 int illUv = 0;
240 int refMskUv = 0;
241
242 bool bAmbCube = false;
243 bool bDiffCube = false;
244 bool bSpecCube = false;
245 bool bNormCube = false;
246 bool bRefCube = false;
247 bool bIllCube = false;
248 bool bRefMskCube = false;
249
250 for (unsigned int i = 0; i < mTexTypes.size(); i++)
251 {
252 switch (mTexTypes[i])
253 {
254 //could be a light map
255 case MAP_AMBIENT:
256 if (mCubeUv[i])
257 mSource << ",\n\tuniform samplerCUBE ambMap : register(s" << samplerId << ")";
258 else
259 mSource << ",\n\tuniform sampler2D ambMap : register(s" << samplerId << ")";
260 ambUv = mTexUnits[i];
261 bAmbCube = mCubeUv[i];
262 break;
263
264 case MAP_DIFFUSE:
265 if (mCubeUv[i])
266 mSource << ",\n\tuniform samplerCUBE diffuseMap : register(s" << samplerId << ")";
267 else
268 mSource << ",\n\tuniform sampler2D diffuseMap : register(s" << samplerId << ")";
269 diffUv = mTexUnits[i];
270 bDiffCube = mCubeUv[i];
271 break;
272
273 case MAP_SPECULAR:
274 if (mCubeUv[i])
275 mSource << ",\n\tuniform samplerCUBE specMap : register(s" << samplerId << ")";
276 else
277 mSource << ",\n\tuniform sampler2D specMap : register(s" << samplerId << ")";
278 specUv = mTexUnits[i];
279 bSpecCube = mCubeUv[i];
280 break;
281
282 case MAP_NORMAL:
283 if (mCubeUv[i])
284 mSource << ",\n\tuniform samplerCUBE normalMap : register(s" << samplerId << ")";
285 else
286 mSource << ",\n\tuniform sampler2D normalMap : register(s" << samplerId << ")";
287 normUv = mTexUnits[i];
288 bNormCube = mCubeUv[i];
289 break;
290
291 case MAP_EMISSIVE:
292 if (mCubeUv[i])
293 mSource << ",\n\tuniform samplerCUBE illMap : register(s" << samplerId << ")";
294 else
295 mSource << ",\n\tuniform sampler2D illMap : register(s" << samplerId << ")";
296 illUv = mTexUnits[i];
297 bIllCube = mCubeUv[i];
298 break;
299
300 case MAP_REFLECTION:
301 if (mCubeUv[i])
302 mSource << ",\n\tuniform samplerCUBE reflectMap : register(s" << samplerId << ")";
303 else
304 mSource << ",\n\tuniform sampler2D reflectMap : register(s" << samplerId << ")";
305 bRefCube = mCubeUv[i];
306 break;
307
308 case MAP_REFMASK:
309 if (mCubeUv[i])
310 mSource << ",\n\tuniform samplerCUBE reflectMskMap : register(s" << samplerId << ")";
311 else
312 mSource << ",\n\tuniform sampler2D reflectMskMap : register(s" << samplerId << ")";
313 bRefMskCube = mCubeUv[i];
314 break;
315 }
316 samplerId++;
317 }
318
319 mSource << "): COLOR0\n";
320 mSource << "{\n";
321
323 {
324 mSource << "\tif((slicePlane.x + slicePlane.y + slicePlane.z) != 0.0)\n";
325 mSource << "\t\tclip((slicePlane.x * wp.x + slicePlane.y * wp.y + slicePlane.z * wp.z + slicePlane.w > 0.0) ? -1.0 : 1.0);\n";
326 }
327
328 //Fog
329 mSource << "\tfloat fog = 1.0;\n";
330 mSource << "\t if (fogParams.x == 0.0)\n";
331 mSource << "\t{\n";
332 mSource << "\tif (fogParams.w > 0.0)\n";
333 mSource << "\t\tfog = (fogParams.z - wp.w) * fogParams.w;\n";
334 mSource << "\t}\n";
335 mSource << "\telse\n";
336 mSource << "\t\tfog = exp2(-fogParams.x * abs(norm.w));\n";
337 mSource << "\tfog = clamp(fog, 0.0, 1.0);\n";
338
339 if (!(mConfig & MAP_REFLECTION))
340 mSource << "\tif (fog == 0.0) return fogColor;\n";
341
342 if ((mConfig & MAP_NORMAL) && !mNotLight)
343 {
344 if (bNormCube)
345 mSource << "\tfloat3 normalTex = texCUBE(normalMap, uv" << normUv << ").rgb;\n";
346 else
347 mSource << "\tfloat3 normalTex = tex2D(normalMap, uv" << normUv << ".xy).rgb;\n";
348
349// mSource << "\ttangent *= normalMul;\n";
350// mSource << "\tbinormal *= normalMul;\n";
351 mSource << "\tfloat3x3 tbn = float3x3(tangent, binormal, norm.xyz);\n";
352
353 mSource << "\tfloat3 normal = mul(transpose(tbn), (normalTex.xyz -0.5) * 2); // to object space\n";
354 mSource << "\tnormal = normalize(mul((float3x3)iTWMat, normal));\n";
355 }
356 else
357 {
358 mSource << "\tfloat3 normal = normalize(norm.xyz);\n";
359 }
360
362 mSource << "\tfloat3 camDir = normalize(camPos - wp.xyz);\n";
363
364 if (!mNotLight)
365 {
366 mSource << "\tfloat3 diffuseLight = float3(0.0, 0.0, 0.0);\n";
367 mSource << "\tfloat3 specularLight = float3(0.0, 0.0, 0.0);\n";
368 mSource << "\tfloat shincontrib = 0.0;\n";
369 mSource << "\tfloat shinlt = 0.0;\n";
370 mSource << "\tfloat spot = 0.0;\n";
371 mSource << "\thalf ila = 0;\n";
372 mSource << "\thalf la = 0;\n";
373 mSource << "\tfloat3 ld = float3(0.0, 0.0, 0.0);\n";
374 mSource << "\tfloat lc = 0.0;\n";
375 mSource << "\thalf lightDist = 0;\n\n";
376
377 mSource << "\tfor (int i = 0; i < " << mNbLights <<"; i++)\n";
378 mSource << "\t{\n";
379 // direction
380 mSource << "\tld = normalize(lightPos[i].xyz - (lightPos[i].w * wp.xyz));\n";
381
382 mSource << "\t// attenuation\n";
383 mSource << "\tlightDist = length(lightPos[i].xyz - wp.xyz) / (lightAtt[i].r / lightAtt[i].r);\n";
384 mSource << "\tla = 1;\n";
385 mSource << "\tif(lightAtt[i].a > 0.0)\n";
386 mSource << "\t{\n";
387 mSource << "\t\tila = lightDist * lightDist; // quadratic falloff\n";
388 mSource << "\t\tla = 1.0 / (lightAtt[i].g + lightAtt[i].b * lightDist + lightAtt[i].a * ila);\n";
389 mSource << "\t}\n";
390
391 mSource << "\t// calculate the spotlight effect\n";
392 mSource << "\tspot = ((spotlightParams[i].w == 0.0) ? 1.0 : // if so, then it's not a spot light\n";
393 mSource << "\t saturate((dot(-normalize(spotlightDir[i].xyz - (spotlightDir[i].w * wp.xyz)), ld) - spotlightParams[i].y) / (spotlightParams[i].x - spotlightParams[i].y)));\n";
394
395 mSource << "\tlc = max(dot(normal, ld), 0.0) * spot * la;\n";
396 mSource << "\tshinlt = pow(max(dot(normal, normalize(ld + camDir)), 0.0), matShininess + 0.1) * lc;\n";
397
398 mSource << "\tspecularLight += lightDif[i].rgb * float3(shinlt, shinlt, shinlt);\n";
399 mSource << "\tdiffuseLight += lightDif[i].rgb * float3(lc, lc, lc);\n";
400 mSource << "\tshincontrib += shinlt;\n";
401
402 mSource << "\t}\n";
403
404 if (!mUseVertexColor)
405 {
406 mSource << "\tfloat3 ambientColor = ambient.rgb * matAmb.rgb;\n";
407 mSource << "\tfloat3 diffuseContrib = matDif.rgb;\n";
408 }
409 else
410 {
411 mSource << "\tfloat3 ambientColor = ambient.rgb * iColor.rgb;\n";
412 mSource << "\tfloat3 diffuseContrib = iColor.rgb;\n";
413 }
414
415 if (!(mConfig & MAP_EMISSIVE))
416 {
417 mSource << "\tambientColor = max(matEmissive.rgb, ambientColor);\n";
418 }
419 }
420 else
421 {
422 if (!mUseVertexColor)
423 mSource << "\tfloat4 ambientColor = float4(1.0, 1.0, 1.0, 1.0);\n";
424 else
425 mSource << "\tfloat4 ambientColor = iColor;\n";
426 }
427 if (mConfig & MAP_AMBIENT)
428 {
429 if (bAmbCube)
430 mSource << "\tfloat4 ambTex = texCUBE(ambMap, uv" << ambUv << ");\n";
431 else
432 mSource << "\tfloat4 ambTex = tex2D(ambMap, uv" << ambUv << ".xy);\n";
433
435 mSource << "\tif (ambTex.a < 0.05) discard;\n";
436
437 if (!mNotLight)
438 mSource << "\tambientColor *= ambTex.rgb;\n";
439 else
440 mSource << "\tambientColor *= ambTex;\n";
441 }
442
443 if (mConfig & MAP_DIFFUSE)
444 {
445 if (bDiffCube)
446 mSource << "\tfloat4 diffuseTex = texCUBE(diffuseMap, uv" << diffUv << ");\n";
447 else
448 mSource << "\tfloat4 diffuseTex = tex2D(diffuseMap, uv" << diffUv << ".xy);\n";
449
451 mSource << "\tif (diffuseTex.a < 0.05) discard;\n";
452
453 if (!mNotLight)
454 {
455 mSource << "\tambientColor *= diffuseTex.rgb;\n";
456 mSource << "\tdiffuseContrib *= diffuseTex.rgb;\n";
457 }
458 else
459 {
460 mSource << "\tambientColor *= diffuseTex;\n";
461 }
462 }
463
464 if (mConfig & MAP_EMISSIVE)
465 {
466 if (bIllCube)
467 mSource << "\tfloat3 illTex = texCUBE(illMap, uv" << illUv << ").rgb;\n";
468 else
469 mSource << "\tfloat3 illTex = tex2D(illMap, uv" << illUv << ".xy).rgb;\n";
470
471 if (!mNotLight)
472 {
473 mSource << "\tambientColor = max(ambientColor, matEmissive.rgb * illTex);\n";
474 }
475 else
476 {
477 mSource << "\tambientColor += float4(illTex, 0.0);\n";
478 }
479 }
480
481 if (!mNotLight)
482 mSource << "\tfloat4 specularContrib = float4(specularLight, 1.0) * matSpec;\n";
483
484 if ((mConfig & MAP_SPECULAR) && !mNotLight)
485 {
486 if (bSpecCube)
487 mSource << "\tfloat4 specTex = texCUBE(specMap, uv" << specUv << ");\n";
488 else
489 mSource << "\tfloat4 specTex = tex2D(specMap, uv" << specUv << ".xy);\n";
490
491 mSource << "\tspecularContrib.rgb *= specTex.rgb;\n";
492 }
493
494 //if(bAmbient)
495 //mSource << "\tspecularContrib.rgb *= ambTex.rgb;\n";
496
497 if (!mNotLight)
498 {
499 mSource << "\tfloat3 light0C = clamp(ambientColor + (diffuseLight * diffuseContrib) + specularContrib.rgb, 0.0, 1.0);\n";
500
501 if (!mUseVertexColor)
502 mSource << "\tfloat alpha = clamp(matDif.a + (specularContrib.a * shincontrib * matDif.a), 0.0, 1.0);\n";
503 else
504 mSource << "\tfloat alpha = clamp(iColor.a + (specularContrib.a * shincontrib * iColor.a), 0.0, 1.0);\n";
505
506 if (mConfig & MAP_DIFFUSE)
507 mSource << "\talpha *= diffuseTex.a;\n";
508 else if (mConfig & MAP_AMBIENT)
509 mSource << "\talpha *= ambTex.a;\n";
510 }
511
513 {
514 mSource << "\tfloat3 refVec = -reflect(camDir, normal);\n";
515 if (bRefCube)
516 {
517 mSource << "\trefVec.z = -refVec.z;\n";
518 mSource << "\tfloat4 reflecTex = texCUBE(reflectMap, refVec);\n";
519 }
520 else
521 {
522 mSource << "\tfloat mref = 2.0 * sqrt((refVec.x * refVec.x) + (refVec.y * refVec.y) + (refVec.z * refVec.z));\n";
523 mSource << "\tfloat2 pref = float2(0.5 + refVec.x / mref, 0.5 - refVec.y / mref);\n";
524 mSource << "\tfloat4 reflecTex = tex2D(reflectMap, pref);\n";
525 }
526
527 if (mConfig & MAP_FRESNEL && !mNotLight)
528 {
529 mSource << "\tfloat fresnel = fresnelMul * reflectivity * pow(1 + dot(-camDir, normal), fresnelPow - (reflectivity * fresnelMul));\n";
530 mSource << "\tfloat4 reflecVal = clamp(reflecTex * fresnel, 0.0, 1.0);\n";
531 mSource << "\tfloat3 reflectColor = reflecVal.rgb * (1.0 - light0C);\n";
532 }
533 else //metal style
534 {
535 mSource << "\tfloat3 reflectColor = reflecTex.rgb * reflectivity;\n";
536 }
537
538 //mask
539 if (mConfig & MAP_REFMASK)
540 {
541 if (bRefMskCube)
542 mSource << "\tfloat3 refTexMsk = texCUBE(reflectMskMap, uv" << refMskUv << ").rgb;\n";
543 else
544 mSource << "\tfloat3 refTexMsk = tex2D(reflectMskMap, uv" << refMskUv << ".xy).rgb;\n";
545
546 mSource << "\treflectColor *= refTexMsk;\n";
547 }
548
549 if (!mNotLight)
550 {
551 mSource << "\treturn float4((fog * (light0C + reflectColor)) + fogColor.xyz * (1.0 - fog), alpha);\n";
552 }
553 else
554 {
555 mSource << "\treturn float4((fog * (ambientColor.rgb + reflectColor)) + fogColor.xyz * (1.0 - fog), ambientColor.a);\n";
556 }
557 }
558 else
559 {
560 if (!mNotLight)
561 mSource << "\treturn float4((fog * light0C) + (fogColor.xyz * (1.0 - fog)), alpha);\n";
562 else
563 mSource << "\treturn float4((fog * ambientColor.rgb) + (fogColor.xyz * (1.0 - fog)), ambientColor.a);\n";
564 }
565
566 mSource << "}\n";
567}
568
570{
571 std::stringstream out;
572
574 {
575 out << "\t\t\tvertex_program_ref " << GetShaderName() << "\n";
576 out << "\t\t\t{\n";
577
578 for (unsigned int i = 0; i < mSortedTexUnits.size(); i++)
579 {
581 out << "\t\t\t\tparam_named_auto " << "texMat" << mSortedTexUnits[i] << " texture_matrix " << mSortedTexUnits[i] << "\n";
582 }
583
584 out << "\t\t\t}\n";
585 }
586 else
587 {
588 out << "\t\t\tfragment_program_ref " << GetShaderName() << "\n";
589 out << "\t\t\t{\n";
590
591/* if ((mConfig & MAP_NORMAL) && !mNotLight)
592 {
593 //TODO where could I get that kind of value ?
594 out << "\t\t\t\tparam_named normalMul float " << 1.0f << "\n";
595 }
596*/
598 {
599 //TODO where could I get that kind of value ?
600 out << "\t\t\t\tparam_named reflectivity float " << 0.8f << "\n";
601 if (mConfig & MAP_FRESNEL)
602 {
603 out << "\t\t\t\tparam_named fresnelMul float " << 0.8f << "\n";
604 out << "\t\t\t\tparam_named fresnelPow float " << ((mPass->getShininess() / 255.0f) * 2.0f) << "\n";
605 }
606 }
607
608 out << "\t\t\t}\n";
609 }
610 mParams = out.str();
611 return mParams;
612}
613
615{
616 std::stringstream out;
617
619 {
620 out << "vertex_program " << GetShaderName() << "_CG cg\n";
621 out << "{\n";
622
623 out << "\tsource " << GetShaderName() << ".cg\n";
624 if (mHighProfile)
625 out << "\tprofiles vs_4_0 vs_4_0_level_9_1 vs_4_0_level_9_3 vs_3_0 arbvp2\n";
626 else
627 out << "\tprofiles vs_4_0 vs_4_0_level_9_1 vs_4_0_level_9_3 vs_3_0 arbvp1\n";
628 out << "\tentry_point " << GetShaderName() << "\n";
629
630 out << "\tdefault_params\n";
631 out << "\t{\n";
632 out << "\t\tparam_named_auto wMat world_matrix\n";
633
634 if (!(mConfig & MAP_NORMAL) || mNotLight)
635 out << "\t\tparam_named_auto witMat inverse_transpose_world_matrix\n";
636
637 out << "\t\tparam_named_auto wvpMat worldviewproj_matrix\n";
638
639 out << "\t}\n";
640 out << "}\n";
641 }
642 else
643 {
644 //CG
645 out << "fragment_program " << GetShaderName() << "_CG cg\n";
646 out << "{\n";
647
648 out << "\tsource " << GetShaderName() << ".cg\n";
649 if (mHighProfile)
650 out << "\tprofiles ps_4_0 ps_3_x arbfp1\n";
651 else
652 out << "\tprofiles ps_4_0 ps_3_0 arbfp1\n";
653 out << "\tentry_point " << GetShaderName() << "\n";
654
655 out << "\tdefault_params\n";
656 out << "\t{\n";
657
658 out << "\t\tparam_named_auto fogParams fog_params\n";
659 out << "\t\tparam_named_auto fogColor fog_colour\n";
660
661 if (!mNotLight)
662 {
663 //out << "\t\tparam_named_auto wMat world_matrix\n";
664 out << "\t\tparam_named_auto ambient ambient_light_colour\n";
665
666 out << "\t\tparam_named_auto lightDif light_diffuse_colour_array " << mNbLights << "\n";
667 out << "\t\tparam_named_auto lightPos light_position_array " << mNbLights << "\n";
668 out << "\t\tparam_named_auto lightAtt light_attenuation_array " << mNbLights << "\n";
669 //out << "\t\tparam_named_auto lightSpec light_specular_colour_array " << mNbLights << "\n";
670 out << "\t\tparam_named_auto spotlightParams spotlight_params_array " << mNbLights << "\n";
671 out << "\t\tparam_named_auto spotlightDir light_direction_array " << mNbLights << "\n";
672
673 out << "\t\tparam_named_auto camPos camera_position\n";
674
675 if (!mUseVertexColor)
676 {
677 out << "\t\tparam_named_auto matAmb surface_ambient_colour\n";
678 out << "\t\tparam_named_auto matDif surface_diffuse_colour\n";
679 }
680
681 out << "\t\tparam_named_auto matEmissive surface_emissive_colour\n";
682 out << "\t\tparam_named_auto matSpec surface_specular_colour\n";
683 out << "\t\tparam_named_auto matShininess surface_shininess\n";
684 }
685 else
686 {
688 out << "\t\tparam_named_auto camPos camera_position\n";
689 }
690
691 if ((mConfig & MAP_NORMAL) && !mNotLight)
692 {
693 out << "\t\tparam_named_auto iTWMat inverse_transpose_world_matrix\n";
694 //out << "\t\tparam_named normalMul float 1\n";
695 }
696
698 {
699 out << "\t\tparam_named reflectivity float 0.8\n";
700
701 if (mConfig & MAP_FRESNEL)
702 {
703 out << "\t\tparam_named fresnelMul float 1.0\n";
704 out << "\t\tparam_named fresnelPow float 1.0\n";
705 }
706 }
707
708 out << "\t}\n";
709 out << "}\n";
710 }
711 mProgram = out.str();
712 return mProgram;
713}
714
716{
718 {
719 //Try to get the shader if it exist already
720 Ogre::GpuProgramPtr shaderPtr = Ogre::GpuProgramManager::getSingletonPtr()->getByName(GetShaderName(), Ogre::RGN_DEFAULT);
721
722 // otherwise we load it
723 if (!shaderPtr)
724 {
726 shaderPtr = Ogre::GpuProgramManager::getSingletonPtr()->create(GetShaderName(),
727 Ogre::RGN_DEFAULT,
728 Ogre::GPT_VERTEX_PROGRAM,
729 "cg");
730
731 shaderPtr->setSource(mSource.str());
732 shaderPtr->setParameter("entry_point", GetShaderName());
733
734 if (mHighProfile)
735 shaderPtr->setParameter("profiles", "vs_4_0 vs_4_0_level_9_1 vs_4_0_level_9_3 vs_3_0 arbvp2");
736 else
737 shaderPtr->setParameter("profiles", "vs_4_0 vs_4_0_level_9_1 vs_4_0_level_9_3 vs_3_0 arbvp1");
738
739 // Set auto param binding
740 Ogre::GpuProgramParametersSharedPtr vpParams = shaderPtr->getDefaultParameters();
741
742 vpParams->setNamedAutoConstant("wMat", Ogre::GpuProgramParameters::ACT_WORLD_MATRIX);
743
744 if (!(mConfig & MAP_NORMAL) || mNotLight)
745 vpParams->setNamedAutoConstant("witMat", Ogre::GpuProgramParameters::ACT_INVERSE_TRANSPOSE_WORLD_MATRIX);
746
747 vpParams->setNamedAutoConstant("wvpMat", Ogre::GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
748
749 // All defined, load!
750 shaderPtr->load();
751
752 mGeneratedShaders.push_back(shaderPtr);
753 }
754
755 if (shaderPtr->hasCompileError())
756 {
757 shaderPtr.reset();
758 mPass->setVertexProgram("");
759 return;
760 }
761
762 mPass->setVertexProgram(GetShaderName());
763 Ogre::GpuProgramParametersSharedPtr passParams = mPass->getVertexProgramParameters();
764
765 // on pass
766 for (unsigned int i = 0; i < mSortedTexUnits.size(); i++)
767 {
768 std::stringstream pname;
769
771 {
772 pname << "texMat" << mSortedTexUnits[i];
773 passParams->setNamedAutoConstant(pname.str(), Ogre::GpuProgramParameters::ACT_TEXTURE_MATRIX, mSortedTexUnits[i]);
774 }
775 }
776 }
777 else
778 {
779 //Try to get the shader if it exist already
780 Ogre::GpuProgramPtr shaderPtr = Ogre::GpuProgramManager::getSingletonPtr()->getByName(GetShaderName(), Ogre::RGN_DEFAULT);
781
782 // otherwise we load it
783 if (!shaderPtr)
784 {
786 shaderPtr = Ogre::GpuProgramManager::getSingletonPtr()->create(GetShaderName(),
787 Ogre::RGN_DEFAULT,
788 Ogre::GPT_FRAGMENT_PROGRAM,
789 "cg");
790
791 shaderPtr->setSource(mSource.str());
792 shaderPtr->setParameter("entry_point", GetShaderName());
793 if (mHighProfile)
794 shaderPtr->setParameter("profiles", "ps_4_0 ps_4_0_level_9_1 ps_4_0_level_9_3 ps_3_x arbfp1");
795 else
796 shaderPtr->setParameter("profiles", "ps_4_0 ps_4_0_level_9_1 ps_4_0_level_9_3 ps_3_0 arbfp1");
797
798 // Set auto param binding
799 Ogre::GpuProgramParametersSharedPtr vpParams = shaderPtr->getDefaultParameters();
800
802 vpParams->addSharedParameters("SO3SlicePlaneParams");
803
804 vpParams->setNamedAutoConstant("fogParams", Ogre::GpuProgramParameters::ACT_FOG_PARAMS);
805 vpParams->setNamedAutoConstant("fogColor", Ogre::GpuProgramParameters::ACT_FOG_COLOUR);
806
807 //common
809 vpParams->setNamedAutoConstant("camPos", Ogre::GpuProgramParameters::ACT_CAMERA_POSITION);
810
811 if (!mNotLight)
812 {
813 //vpParams->setNamedAutoConstant("wMat", Ogre::GpuProgramParameters::ACT_WORLD_MATRIX);
814 vpParams->setNamedAutoConstant("ambient", Ogre::GpuProgramParameters::ACT_AMBIENT_LIGHT_COLOUR);
815
816 if (!mUseVertexColor)
817 {
818 vpParams->setNamedAutoConstant("matAmb", Ogre::GpuProgramParameters::ACT_SURFACE_AMBIENT_COLOUR);
819 vpParams->setNamedAutoConstant("matDif", Ogre::GpuProgramParameters::ACT_SURFACE_DIFFUSE_COLOUR);
820 }
821
822 vpParams->setNamedAutoConstant("matEmissive", Ogre::GpuProgramParameters::ACT_SURFACE_EMISSIVE_COLOUR);
823 vpParams->setNamedAutoConstant("matSpec", Ogre::GpuProgramParameters::ACT_SURFACE_SPECULAR_COLOUR);
824 vpParams->setNamedAutoConstant("matShininess", Ogre::GpuProgramParameters::ACT_SURFACE_SHININESS);
825
826 vpParams->setNamedAutoConstant("lightDif", Ogre::GpuProgramParameters::ACT_LIGHT_DIFFUSE_COLOUR_ARRAY, mNbLights);
827 vpParams->setNamedAutoConstant("lightPos", Ogre::GpuProgramParameters::ACT_LIGHT_POSITION_ARRAY, mNbLights);
828 vpParams->setNamedAutoConstant("lightAtt", Ogre::GpuProgramParameters::ACT_LIGHT_ATTENUATION_ARRAY, mNbLights);
829 //vpParams->setNamedAutoConstant("lightSpec", Ogre::GpuProgramParameters::ACT_LIGHT_SPECULAR_COLOUR_ARRAY, mNbLights);
830 vpParams->setNamedAutoConstant("spotlightParams", Ogre::GpuProgramParameters::ACT_SPOTLIGHT_PARAMS_ARRAY, mNbLights);
831 vpParams->setNamedAutoConstant("spotlightDir", Ogre::GpuProgramParameters::ACT_LIGHT_DIRECTION_ARRAY, mNbLights);
832 }
833
834 if ((mConfig & MAP_NORMAL) && !mNotLight)
835 {
836 vpParams->setNamedAutoConstant("iTWMat", Ogre::GpuProgramParameters::ACT_INVERSE_TRANSPOSE_WORLD_MATRIX);
837 //vpParams->setNamedConstant("normalMul", 1.0f);
838 }
839
841 {
842 vpParams->setNamedConstant("reflectivity", 1.0f);
843
844 if (mConfig & MAP_FRESNEL)
845 {
846 vpParams->setNamedConstant("fresnelMul", 1.0f);
847 vpParams->setNamedConstant("fresnelPow", 1.0f);
848 }
849 }
850
851 // All defined, load!
852 shaderPtr->load();
853
854 mGeneratedShaders.push_back(shaderPtr);
855 }
856
857 if (shaderPtr->hasCompileError())
858 {
859 shaderPtr.reset();
860 mPass->setFragmentProgram("");
861 return;
862 }
863
864 mPass->setFragmentProgram(GetShaderName());
865 Ogre::GpuProgramParametersSharedPtr passParams = mPass->getFragmentProgramParameters();
866
867 // on pass
868 /*if ((mConfig & MAP_NORMAL) && !mNotLight)
869 {
870 passParams->setNamedConstant("normalMul", 1.0f);
871 }*/
872
874 {
875 passParams->setNamedConstant("reflectivity", mRefTexCoef);
876
877 if (mConfig & MAP_FRESNEL)
878 {
879 passParams->setNamedConstant("fresnelMul", mRefTexCoef * 1.5f);
880 passParams->setNamedConstant("fresnelPow", ((mPass->getShininess() / 255.0f) * (mRefTexCoef * 10.0f)));
881 }
882 }
883 }
884}
885
886}
SShaderGeneratorCG(Ogre::Technique *, Ogre::Pass *pass, ShaderType type, bool ignoreSlicePlane=false)
virtual std::string GetUniformParams()
virtual std::string GetProgram()
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