0031096: Visualization, TKOpenGl - support metallic-roughness texture mapping
[occt.git] / src / Shaders / DeclarationsImpl.glsl
1
2 //! @file DeclarationsImpl.glsl includes implementation of common functions and properties accessors
3 #if defined(FRAGMENT_SHADER)
4 //! Output color (and coverage for accumulation by OIT algorithm).
5 void occSetFragColor (in vec4 theColor)
6 {
7 #if defined(OCC_ALPHA_TEST)
8   if (theColor.a < occAlphaCutoff) discard;
9 #endif
10 #if defined(OCC_WRITE_WEIGHT_OIT_COVERAGE)
11   float aWeight     = theColor.a * clamp (1e+2 * pow (1.0 - gl_FragCoord.z * occOitDepthFactor, 3.0), 1e-2, 1e+2);
12   occFragCoverage.r = theColor.a * aWeight;
13   occFragColor      = vec4 (theColor.rgb * theColor.a * aWeight, theColor.a);
14 #else
15   occFragColor = theColor;
16 #endif
17 }
18 #endif
19
20 #if defined(THE_MAX_LIGHTS) && (THE_MAX_LIGHTS > 0)
21 // arrays of light sources
22 uniform               vec4  occLightSources[THE_MAX_LIGHTS * 4];  //!< packed light sources parameters
23 uniform THE_PREC_ENUM ivec2 occLightSourcesTypes[THE_MAX_LIGHTS]; //!< packed light sources types
24 #endif
25
26 #if defined(THE_IS_PBR)
27 vec3 occDiffIBLMap (in vec3 theNormal)
28 {
29   vec3 aSHCoeffs[9];
30   for (int i = 0; i < 9; ++i)
31   {
32     aSHCoeffs[i] = occTexture2D (occDiffIBLMapSHCoeffs, vec2 ((float(i) + 0.5) / 9.0, 0.0)).rgb;
33   }
34   return aSHCoeffs[0]
35
36        + aSHCoeffs[1] * theNormal.x
37            + aSHCoeffs[2] * theNormal.y
38            + aSHCoeffs[3] * theNormal.z
39
40            + aSHCoeffs[4] * theNormal.x * theNormal.z
41            + aSHCoeffs[5] * theNormal.y * theNormal.z
42            + aSHCoeffs[6] * theNormal.x * theNormal.y
43
44            + aSHCoeffs[7] * (3.0 * theNormal.z * theNormal.z - 1.0)
45            + aSHCoeffs[8] * (theNormal.x * theNormal.x - theNormal.y * theNormal.y);
46 }
47 #endif
48
49 // front and back material properties accessors
50 #if defined(THE_IS_PBR)
51 uniform vec4 occPbrFrontMaterial[3];
52 uniform vec4 occPbrBackMaterial[3];
53
54 #define MIN_ROUGHNESS 0.01
55 float occRoughness (in float theNormalizedRoughness) { return theNormalizedRoughness * (1.0 - MIN_ROUGHNESS) + MIN_ROUGHNESS; }
56 vec4  occPBRMaterial_Color(in bool theIsFront)     { return theIsFront ? occPbrFrontMaterial[0]     : occPbrBackMaterial[0]; }
57 vec3  occPBRMaterial_Emission(in bool theIsFront)  { return theIsFront ? occPbrFrontMaterial[1].rgb : occPbrBackMaterial[1].rgb; }
58 float occPBRMaterial_IOR(in bool theIsFront)       { return theIsFront ? occPbrFrontMaterial[1].w   : occPbrBackMaterial[1].w; }
59 float occPBRMaterial_Metallic(in bool theIsFront)  { return theIsFront ? occPbrFrontMaterial[2].b   : occPbrBackMaterial[2].b; }
60 float occPBRMaterial_NormalizedRoughness(in bool theIsFront) { return theIsFront ? occPbrFrontMaterial[2].g : occPbrBackMaterial[2].g; }
61 #else
62 uniform vec4 occFrontMaterial[5];
63 uniform vec4 occBackMaterial[5];
64
65 vec4  occFrontMaterial_Ambient(void)      { return occFrontMaterial[0]; }
66 vec4  occFrontMaterial_Diffuse(void)      { return occFrontMaterial[1]; }
67 vec4  occFrontMaterial_Specular(void)     { return occFrontMaterial[2]; }
68 vec4  occFrontMaterial_Emission(void)     { return occFrontMaterial[3]; }
69 float occFrontMaterial_Shininess(void)    { return occFrontMaterial[4].x; }
70 float occFrontMaterial_Transparency(void) { return occFrontMaterial[4].y; }
71
72 vec4  occBackMaterial_Ambient(void)       { return occBackMaterial[0]; }
73 vec4  occBackMaterial_Diffuse(void)       { return occBackMaterial[1]; }
74 vec4  occBackMaterial_Specular(void)      { return occBackMaterial[2]; }
75 vec4  occBackMaterial_Emission(void)      { return occBackMaterial[3]; }
76 float occBackMaterial_Shininess(void)     { return occBackMaterial[4].x; }
77 float occBackMaterial_Transparency(void)  { return occBackMaterial[4].y; }
78 #endif
79
80 // 2D texture coordinates transformation
81 vec2  occTextureTrsf_Translation(void) { return occTexTrsf2d[0].xy; }
82 vec2  occTextureTrsf_Scale(void)       { return occTexTrsf2d[0].zw; }
83 float occTextureTrsf_RotationSin(void) { return occTexTrsf2d[1].x; }
84 float occTextureTrsf_RotationCos(void) { return occTexTrsf2d[1].y; }
85 //! @endfile DeclarationsImpl.glsl