0030700: Visualization, TKOpenGl - support PBR Metallic-Roughness shading model
[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 // Converts roughness value from range [0, 1] to real value for calculations
56 float occRoughness (in float theNormalizedRoughness) { return theNormalizedRoughness * (1.0 - MIN_ROUGHNESS) + MIN_ROUGHNESS; }
57
58 vec4  occPBRFrontMaterial_Color(void)     { return occPbrFrontMaterial[0]; }
59 vec3  occPBRFrontMaterial_Emission(void)  { return occPbrFrontMaterial[1].rgb; }
60 float occPBRFrontMaterial_IOR(void)       { return occPbrFrontMaterial[1].w; }
61 float occPBRFrontMaterial_Metallic(void)  { return occPbrFrontMaterial[2].b; }
62 float occPBRFrontMaterial_Roughness(void) { return occRoughness (occPbrFrontMaterial[2].g); }
63 float occPBRFrontMaterial_NormalizedRoughness(void) { return occPbrFrontMaterial[2].g; }
64
65 vec4  occPBRBackMaterial_Color(void)     { return occPbrBackMaterial[0]; }
66 vec3  occPBRBackMaterial_Emission(void)  { return occPbrBackMaterial[1].rgb; }
67 float occPBRBackMaterial_IOR(void)       { return occPbrBackMaterial[1].w; }
68 float occPBRBackMaterial_Metallic(void)  { return occPbrBackMaterial[2].b; }
69 float occPBRBackMaterial_Roughness(void) { return occRoughness (occPbrBackMaterial[2].g); }
70 float occPBRBackMaterial_NormalizedRoughness(void) { return occPbrBackMaterial[2].g; }
71 #else
72 uniform vec4 occFrontMaterial[5];
73 uniform vec4 occBackMaterial[5];
74
75 vec4  occFrontMaterial_Ambient(void)      { return occFrontMaterial[0]; }
76 vec4  occFrontMaterial_Diffuse(void)      { return occFrontMaterial[1]; }
77 vec4  occFrontMaterial_Specular(void)     { return occFrontMaterial[2]; }
78 vec4  occFrontMaterial_Emission(void)     { return occFrontMaterial[3]; }
79 float occFrontMaterial_Shininess(void)    { return occFrontMaterial[4].x; }
80 float occFrontMaterial_Transparency(void) { return occFrontMaterial[4].y; }
81
82 vec4  occBackMaterial_Ambient(void)       { return occBackMaterial[0]; }
83 vec4  occBackMaterial_Diffuse(void)       { return occBackMaterial[1]; }
84 vec4  occBackMaterial_Specular(void)      { return occBackMaterial[2]; }
85 vec4  occBackMaterial_Emission(void)      { return occBackMaterial[3]; }
86 float occBackMaterial_Shininess(void)     { return occBackMaterial[4].x; }
87 float occBackMaterial_Transparency(void)  { return occBackMaterial[4].y; }
88 #endif
89
90 // 2D texture coordinates transformation
91 vec2  occTextureTrsf_Translation(void) { return occTexTrsf2d[0].xy; }
92 vec2  occTextureTrsf_Scale(void)       { return occTexTrsf2d[0].zw; }
93 float occTextureTrsf_RotationSin(void) { return occTexTrsf2d[1].x; }
94 float occTextureTrsf_RotationCos(void) { return occTexTrsf2d[1].y; }
95 //! @endfile DeclarationsImpl.glsl