0027925: Visualization - implement order-independent transparency algorithm within...
[occt.git] / src / Shaders / PhongShading.fs
index 30ccaa6..15311c7 100755 (executable)
@@ -31,7 +31,7 @@ void pointLight (in int  theId,
   vec3 aLight = occLight_Position (theId).xyz;
   if (occLight_IsHeadlight (theId) == 0)
   {
-    aLight = vec3 (occWorldViewMatrix * occModelWorldMatrix * vec4 (aLight, 1.0));
+    aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 1.0));
   }
   aLight -= thePoint;
 
@@ -67,8 +67,8 @@ void spotLight (in int  theId,
   vec3 aSpotDir = occLight_SpotDirection (theId).xyz;
   if (occLight_IsHeadlight (theId) == 0)
   {
-    aLight   = vec3 (occWorldViewMatrix * occModelWorldMatrix * vec4 (aLight,   1.0));
-    aSpotDir = vec3 (occWorldViewMatrix * occModelWorldMatrix * vec4 (aSpotDir, 0.0));
+    aLight   = vec3 (occWorldViewMatrix * vec4 (aLight,   1.0));
+    aSpotDir = vec3 (occWorldViewMatrix * vec4 (aSpotDir, 0.0));
   }
   aLight -= thePoint;
 
@@ -116,7 +116,7 @@ void directionalLight (in int  theId,
   vec3 aLight = normalize (occLight_Position (theId).xyz);
   if (occLight_IsHeadlight (theId) == 0)
   {
-    aLight = vec3 (occWorldViewMatrix * occModelWorldMatrix * vec4 (aLight, 0.0));
+    aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 0.0));
   }
 
   vec3 aHalf = normalize (aLight + theView);
@@ -165,9 +165,12 @@ vec4 computeLighting (in vec3 theNormal,
   vec4 aMaterialAmbient  = gl_FrontFacing ? occFrontMaterial_Ambient()  : occBackMaterial_Ambient();
   vec4 aMaterialDiffuse  = gl_FrontFacing ? occFrontMaterial_Diffuse()  : occBackMaterial_Diffuse();
   vec4 aMaterialSpecular = gl_FrontFacing ? occFrontMaterial_Specular() : occBackMaterial_Specular();
-  return vec4 (Ambient,  1.0) * aMaterialAmbient
-       + vec4 (Diffuse,  1.0) * aMaterialDiffuse
-       + vec4 (Specular, 1.0) * aMaterialSpecular;
+  vec4 aMaterialEmission = gl_FrontFacing ? occFrontMaterial_Emission() : occBackMaterial_Emission();
+  vec3 aColor = Ambient  * aMaterialAmbient.rgb
+              + Diffuse  * aMaterialDiffuse.rgb
+              + Specular * aMaterialSpecular.rgb
+                         + aMaterialEmission.rgb;
+  return vec4 (aColor, aMaterialDiffuse.a);
 }
 
 //! Entry point to the Fragment Shader
@@ -177,24 +180,20 @@ void main()
   for (int anIndex = 0; anIndex < occClipPlaneCount; ++anIndex)
   {
     vec4 aClipEquation = occClipPlaneEquations[anIndex];
-    int  aClipSpace    = occClipPlaneSpaces[anIndex];
-    if (aClipSpace == OccEquationCoords_World)
+    if (dot (aClipEquation.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation.w < 0.0)
     {
-      if (dot (aClipEquation.xyz, PositionWorld.xyz) + aClipEquation.w < 0.0)
-      {
-        discard;
-      }
-    }
-    else if (aClipSpace == OccEquationCoords_View)
-    {
-      if (dot (aClipEquation.xyz, Position.xyz) + aClipEquation.w < 0.0)
-      {
-        discard;
-      }
+      discard;
     }
   }
 
-  gl_FragColor = computeLighting (normalize (Normal),
+  occFragColor = computeLighting (normalize (Normal),
                                   normalize (View),
                                   Position);
+
+  if (occOitOutput != 0)
+  {
+    float aWeight     = occFragColor.a * clamp (1e+2 * pow (1.0 - gl_FragCoord.z * occOitDepthFactor, 3.0), 1e-2, 1e+2);
+    occFragCoverage.r = occFragColor.a * aWeight;
+    occFragColor      = vec4 (occFragColor.rgb * occFragColor.a * aWeight, occFragColor.a);
+  }
 }