0027961: Visualization - remove unused and no more working OpenGl_AVIWriter
[occt.git] / src / OpenGl / OpenGl_ShaderManager.cxx
index c999a28..d94768d 100644 (file)
 
 #include <TCollection_ExtendedString.hxx>
 
-
-
-
 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_ShaderManager,Standard_Transient)
 
 namespace
 {
 
+  //! Clipping planes limit (see the same definition in Declarations.glsl).
+  static const Standard_Size THE_MAX_CLIP_PLANES = 8;
+
 #define EOL "\n"
 
 //! Definition of TexCoord varying.
@@ -83,7 +83,7 @@ const char THE_FUNC_pointLight[] =
   EOL"  vec3 aLight = occLight_Position (theId).xyz;"
   EOL"  if (occLight_IsHeadlight (theId) == 0)"
   EOL"  {"
-  EOL"    aLight = vec3 (occWorldViewMatrix * occModelWorldMatrix * vec4 (aLight, 1.0));"
+  EOL"    aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 1.0));"
   EOL"  }"
   EOL"  aLight -= thePoint;"
   EOL
@@ -121,8 +121,8 @@ const char THE_FUNC_spotLight[] =
   EOL"  vec3 aSpotDir = occLight_SpotDirection (theId).xyz;"
   EOL"  if (occLight_IsHeadlight (theId) == 0)"
   EOL"  {"
-  EOL"    aLight   = vec3 (occWorldViewMatrix * occModelWorldMatrix * vec4 (aLight,   1.0));"
-  EOL"    aSpotDir = vec3 (occWorldViewMatrix * occModelWorldMatrix * vec4 (aSpotDir, 0.0));"
+  EOL"    aLight   = vec3 (occWorldViewMatrix * vec4 (aLight,   1.0));"
+  EOL"    aSpotDir = vec3 (occWorldViewMatrix * vec4 (aSpotDir, 0.0));"
   EOL"  }"
   EOL"  aLight -= thePoint;"
   EOL
@@ -171,7 +171,7 @@ const char THE_FUNC_directionalLight[] =
   EOL"  vec3 aLight = normalize (occLight_Position (theId).xyz);"
   EOL"  if (occLight_IsHeadlight (theId) == 0)"
   EOL"  {"
-  EOL"    aLight = vec3 (occWorldViewMatrix * occModelWorldMatrix * vec4 (aLight, 0.0));"
+  EOL"    aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 0.0));"
   EOL"  }"
   EOL
   EOL"  vec3 aHalf = normalize (aLight + theView);"
@@ -200,7 +200,7 @@ const char THE_FUNC_directionalLightFirst[] =
   EOL"  vec3 aLight = normalize (occLightSources[1].xyz);"
   EOL"  if (occLight_IsHeadlight (0) == 0)"
   EOL"  {"
-  EOL"    aLight = vec3 (occWorldViewMatrix * occModelWorldMatrix * vec4 (aLight, 0.0));"
+  EOL"    aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 0.0));"
   EOL"  }"
   EOL
   EOL"  vec3 aHalf = normalize (aLight + theView);"
@@ -221,27 +221,34 @@ const char THE_FUNC_directionalLightFirst[] =
 
 //! Process clipping planes in Fragment Shader.
 //! Should be added at the beginning of the main() function.
-const char THE_FRAG_CLIP_PLANES[] =
+const char THE_FRAG_CLIP_PLANES_N[] =
   EOL"  for (int aPlaneIter = 0; aPlaneIter < occClipPlaneCount; ++aPlaneIter)"
   EOL"  {"
   EOL"    vec4 aClipEquation = occClipPlaneEquations[aPlaneIter];"
-  EOL"    int  aClipSpace    = occClipPlaneSpaces[aPlaneIter];"
-  EOL"    if (aClipSpace == OccEquationCoords_World)"
+  EOL"    if (dot (aClipEquation.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation.w < 0.0)"
   EOL"    {"
-  EOL"      if (dot (aClipEquation.xyz, PositionWorld.xyz) + aClipEquation.w < 0.0)"
-  EOL"      {"
-  EOL"        discard;"
-  EOL"      }"
-  EOL"    }"
-  EOL"    else if (aClipSpace == OccEquationCoords_View)"
-  EOL"    {"
-  EOL"      if (dot (aClipEquation.xyz, Position.xyz) + aClipEquation.w < 0.0)"
-  EOL"      {"
-  EOL"        discard;"
-  EOL"      }"
+  EOL"      discard;"
   EOL"    }"
   EOL"  }";
 
+//! Process 1 clipping plane in Fragment Shader.
+const char THE_FRAG_CLIP_PLANES_1[] =
+  EOL"  vec4 aClipEquation0 = occClipPlaneEquations[0];"
+  EOL"  if (dot (aClipEquation0.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation0.w < 0.0)"
+  EOL"  {"
+  EOL"    discard;"
+  EOL"  }";
+
+//! Process 2 clipping planes in Fragment Shader.
+const char THE_FRAG_CLIP_PLANES_2[] =
+  EOL"  vec4 aClipEquation0 = occClipPlaneEquations[0];"
+  EOL"  vec4 aClipEquation1 = occClipPlaneEquations[1];"
+  EOL"  if (dot (aClipEquation0.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation0.w < 0.0"
+  EOL"   || dot (aClipEquation1.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation1.w < 0.0)"
+  EOL"  {"
+  EOL"    discard;"
+  EOL"  }";
+
 }
 
 // =======================================================================
@@ -755,60 +762,46 @@ void OpenGl_ShaderManager::PushClippingState (const Handle(OpenGl_ShaderProgram)
 
   theProgram->UpdateState (OpenGl_CLIP_PLANES_STATE, myClippingState.Index());
   const GLint aLocEquations = theProgram->GetStateLocation (OpenGl_OCC_CLIP_PLANE_EQUATIONS);
-  const GLint aLocSpaces    = theProgram->GetStateLocation (OpenGl_OCC_CLIP_PLANE_SPACES);
-  if (aLocEquations == OpenGl_ShaderProgram::INVALID_LOCATION
-   && aLocSpaces    == OpenGl_ShaderProgram::INVALID_LOCATION)
+  if (aLocEquations == OpenGl_ShaderProgram::INVALID_LOCATION)
   {
     return;
   }
 
-  GLint aPlanesNb = 0;
-  for (Graphic3d_SequenceOfHClipPlane::Iterator anIter (myContext->Clipping().Planes());
-       anIter.More(); anIter.Next())
-  {
-    const Handle(Graphic3d_ClipPlane)& aPlane = anIter.Value();
-    if (!myContext->Clipping().IsEnabled (aPlane))
-    {
-      continue;
-    }
-
-    ++aPlanesNb;
-  }
-  if (aPlanesNb < 1)
+  const GLint aNbPlanes = Min (myContext->Clipping().NbClippingOrCappingOn(), THE_MAX_CLIP_PLANES);
+  theProgram->SetUniform (myContext,
+                          theProgram->GetStateLocation (OpenGl_OCC_CLIP_PLANE_COUNT),
+                          aNbPlanes);
+  if (aNbPlanes < 1)
   {
     return;
   }
 
-  const Standard_Size MAX_CLIP_PLANES = 8;
-  OpenGl_Vec4* anEquations = new OpenGl_Vec4[MAX_CLIP_PLANES];
-  GLint*       aSpaces     = new GLint      [MAX_CLIP_PLANES];
+  OpenGl_Vec4 anEquations[THE_MAX_CLIP_PLANES];
   GLuint aPlaneId = 0;
-  for (Graphic3d_SequenceOfHClipPlane::Iterator anIter (myContext->Clipping().Planes());
-       anIter.More(); anIter.Next())
+  for (OpenGl_ClippingIterator aPlaneIter (myContext->Clipping()); aPlaneIter.More(); aPlaneIter.Next())
   {
-    const Handle(Graphic3d_ClipPlane)& aPlane = anIter.Value();
-    if (!myContext->Clipping().IsEnabled (aPlane))
+    const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIter.Value();
+    if (aPlaneIter.IsDisabled())
     {
       continue;
     }
+    else if (aPlaneId >= THE_MAX_CLIP_PLANES)
+    {
+      myContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
+        GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_MEDIUM,
+        "Warning: clipping planes limit (8) has been exceeded.");
+      break;
+    }
 
     const Graphic3d_ClipPlane::Equation& anEquation = aPlane->GetEquation();
     anEquations[aPlaneId] = OpenGl_Vec4 ((float) anEquation.x(),
                                          (float) anEquation.y(),
                                          (float) anEquation.z(),
                                          (float) anEquation.w());
-    aSpaces[aPlaneId] = myContext->Clipping().GetEquationSpace (aPlane);
     ++aPlaneId;
   }
 
-  theProgram->SetUniform (myContext,
-                          theProgram->GetStateLocation (OpenGl_OCC_CLIP_PLANE_COUNT),
-                          aPlanesNb);
-  theProgram->SetUniform (myContext, aLocEquations, MAX_CLIP_PLANES, anEquations);
-  theProgram->SetUniform (myContext, aLocSpaces,    MAX_CLIP_PLANES, aSpaces);
-
-  delete[] anEquations;
-  delete[] aSpaces;
+  theProgram->SetUniform (myContext, aLocEquations, THE_MAX_CLIP_PLANES, anEquations);
 }
 
 // =======================================================================
@@ -908,6 +901,11 @@ Standard_Boolean OpenGl_ShaderManager::prepareStdProgramFboBlit()
   {
     aProgramSrc->SetHeader ("#version 300 es");
   }
+  else if (myContext->extFragDepth)
+  {
+    aProgramSrc->SetHeader ("#extension GL_EXT_frag_depth : enable"
+                         EOL"#define gl_FragDepth gl_FragDepthEXT");
+  }
   else
   {
     // there is no way to draw into depth buffer
@@ -1061,7 +1059,7 @@ Standard_Boolean OpenGl_ShaderManager::prepareStdProgramFlat (Handle(OpenGl_Shad
     aSrcFragExtraOut  += EOL"THE_SHADER_IN  vec4 VertColor;";
     aSrcFragGetColor  =  EOL"vec4 getColor(void) { return VertColor; }";
   }
-  if ((theBits & OpenGl_PO_ClipPlanes) != 0)
+  if ((theBits & OpenGl_PO_ClipPlanesN) != 0)
   {
     aSrcVertExtraOut +=
       EOL"THE_SHADER_OUT vec4 PositionWorld;"
@@ -1072,7 +1070,19 @@ Standard_Boolean OpenGl_ShaderManager::prepareStdProgramFlat (Handle(OpenGl_Shad
     aSrcVertExtraMain +=
       EOL"  PositionWorld = occModelWorldMatrix * occVertex;"
       EOL"  Position      = occWorldViewMatrix * PositionWorld;";
-    aSrcFragExtraMain += THE_FRAG_CLIP_PLANES;
+
+    if ((theBits & OpenGl_PO_ClipPlanes1) != 0)
+    {
+      aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_1;
+    }
+    else if ((theBits & OpenGl_PO_ClipPlanes2) != 0)
+    {
+      aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_2;
+    }
+    else
+    {
+      aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_N;
+    }
   }
 
   TCollection_AsciiString aSrcVertEndMain;
@@ -1338,7 +1348,7 @@ Standard_Boolean OpenGl_ShaderManager::prepareStdProgramGouraud (Handle(OpenGl_S
     aSrcVertColor = EOL"vec4 getVertColor(void) { return occVertColor; }";
   }
 
-  if ((theBits & OpenGl_PO_ClipPlanes) != 0)
+  if ((theBits & OpenGl_PO_ClipPlanesN) != 0)
   {
     aSrcVertExtraOut +=
       EOL"THE_SHADER_OUT vec4 PositionWorld;"
@@ -1349,7 +1359,19 @@ Standard_Boolean OpenGl_ShaderManager::prepareStdProgramGouraud (Handle(OpenGl_S
     aSrcVertExtraMain +=
       EOL"  PositionWorld = aPositionWorld;"
       EOL"  Position      = aPosition;";
-    aSrcFragExtraMain += THE_FRAG_CLIP_PLANES;
+
+    if ((theBits & OpenGl_PO_ClipPlanes1) != 0)
+    {
+      aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_1;
+    }
+    else if ((theBits & OpenGl_PO_ClipPlanes2) != 0)
+    {
+      aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_2;
+    }
+    else
+    {
+      aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_N;
+    }
   }
 
   const TCollection_AsciiString aLights = stdComputeLighting ((theBits & OpenGl_PO_VertColor) != 0);
@@ -1459,9 +1481,20 @@ Standard_Boolean OpenGl_ShaderManager::prepareStdProgramPhong (Handle(OpenGl_Sha
                            EOL"vec4 getVertColor(void) { return VertColor; }";
   }
 
-  if ((theBits & OpenGl_PO_ClipPlanes) != 0)
+  if ((theBits & OpenGl_PO_ClipPlanesN) != 0)
   {
-    aSrcFragExtraMain += THE_FRAG_CLIP_PLANES;
+    if ((theBits & OpenGl_PO_ClipPlanes1) != 0)
+    {
+      aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_1;
+    }
+    else if ((theBits & OpenGl_PO_ClipPlanes2) != 0)
+    {
+      aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_2;
+    }
+    else
+    {
+      aSrcFragExtraMain += THE_FRAG_CLIP_PLANES_N;
+    }
   }
 
   aSrcVert = TCollection_AsciiString()
@@ -1546,8 +1579,8 @@ Standard_Boolean OpenGl_ShaderManager::prepareStdProgramStereo (Handle(OpenGl_Sh
           EOL"uniform mat4 uMultL;"
           EOL"uniform mat4 uMultR;"
           EOL
-          EOL"vec4 THE_POW_UP   = vec4 (2.2, 2.2, 2.2, 1.0);"
-          EOL"vec4 THE_POW_DOWN = 1.0 / THE_POW_UP;"
+          EOL"const vec4 THE_POW_UP   =       vec4 (2.2, 2.2, 2.2, 1.0);"
+          EOL"const vec4 THE_POW_DOWN = 1.0 / vec4 (2.2, 2.2, 2.2, 1.0);"
           EOL
           EOL"THE_SHADER_IN vec2 TexCoord;"
           EOL