0027735: Visualization, OpenGl_ShaderManager - fix clipping state management
authorkgv <kgv@opencascade.com>
Fri, 5 Aug 2016 17:58:13 +0000 (20:58 +0300)
committerbugmaster <bugmaster@opencascade.com>
Thu, 11 Aug 2016 09:10:11 +0000 (12:10 +0300)
OpenGl_CappingAlgo::RenderCapping() now updates clipping state in Shader Manager.
OpenGl_View::render() clipping state setup has been moved to OpenGl_View::renderScene().

OpenGl_ShaderManager now converts position to homogeneous coordinates within clipping code GLSL.
This fixes capping plane rendering with clipping planes applied.

Fixed possible Clipping planes misconfiguration when FFP is used;
error-prone OpenGl_Clipping::AddWorldLazy() method has been dropped.

src/OpenGl/OpenGl_CappingAlgo.cxx
src/OpenGl/OpenGl_Clipping.hxx
src/OpenGl/OpenGl_ShaderManager.cxx
src/OpenGl/OpenGl_Structure.cxx
src/OpenGl/OpenGl_View_Redraw.cxx
src/Shaders/PhongShading.fs
tests/bugs/vis/bug24224

index 13f33c4..e2ce3c0 100755 (executable)
 // commercial license or contractual agreement.
 
 #include <OpenGl_CappingAlgo.hxx>
+
 #include <OpenGl_Workspace.hxx>
 #include <OpenGl_Context.hxx>
 #include <OpenGl_PrimitiveArray.hxx>
 #include <OpenGl_CappingPlaneResource.hxx>
 #include <OpenGl_Vec.hxx>
 #include <OpenGl_Structure.hxx>
-#include <Graphic3d_GraphicDriver.hxx>
+#include <OpenGl_ShaderManager.hxx>
 
 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_CappingAlgoFilter,OpenGl_RenderFilter)
 
@@ -98,6 +99,7 @@ void OpenGl_CappingAlgo::RenderCapping (const Handle(OpenGl_Workspace)& theWorks
       const Standard_Boolean isOn = (aPlane == aRenderPlane);
       aContext->ChangeClipping().SetEnabled (aContext, aPlane, isOn);
     }
+    aContext->ShaderManager()->UpdateClippingState();
 
     glClear (GL_STENCIL_BUFFER_BIT);
     glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
@@ -126,6 +128,7 @@ void OpenGl_CappingAlgo::RenderCapping (const Handle(OpenGl_Workspace)& theWorks
       const Standard_Boolean isOn = (aPlane != aRenderPlane);
       aContext->ChangeClipping().SetEnabled (aContext, aPlane, isOn);
     }
+    aContext->ShaderManager()->UpdateClippingState();
 
     // render capping plane using the generated stencil mask
     glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
@@ -135,6 +138,9 @@ void OpenGl_CappingAlgo::RenderCapping (const Handle(OpenGl_Workspace)& theWorks
     glEnable (GL_DEPTH_TEST);
 
     RenderPlane (theWorkspace, aRenderPlane);
+
+    aContext->ShaderManager()->RevertClippingState();
+    aContext->ShaderManager()->RevertClippingState();
   }
 
   // restore previous application state
index b590221..b88979a 100755 (executable)
@@ -173,18 +173,6 @@ public: //! @name Short-cuts
     add (theGlCtx, EquationCoords_World, thePlanes);
   }
 
-  //! Add planes to the context clipping at the world system of coordinates.
-  //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
-  //! are simply ignored.
-  //! @param thePlanes [in/out] the list of planes to be added
-  //! The list then provides information on which planes were really added to clipping state.
-  //! This list then can be used to fall back to previous state.
-  inline void AddWorldLazy (const Handle(OpenGl_Context)&   theGlCtx,
-                            Graphic3d_SequenceOfHClipPlane& thePlanes)
-  {
-    addLazy (theGlCtx, EquationCoords_World, thePlanes);
-  }
-
   //! Remove all of the planes from context state.
   inline void RemoveAll (const Handle(OpenGl_Context)& theGlCtx)
   {
index c999a28..7fb2266 100644 (file)
@@ -228,7 +228,7 @@ const char THE_FRAG_CLIP_PLANES[] =
   EOL"    int  aClipSpace    = occClipPlaneSpaces[aPlaneIter];"
   EOL"    if (aClipSpace == OccEquationCoords_World)"
   EOL"    {"
-  EOL"      if (dot (aClipEquation.xyz, PositionWorld.xyz) + aClipEquation.w < 0.0)"
+  EOL"      if (dot (aClipEquation.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation.w < 0.0)"
   EOL"      {"
   EOL"        discard;"
   EOL"      }"
index f20c254..a0eca72 100644 (file)
@@ -558,10 +558,7 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
     aCtx->ChangeClipping().AddWorld (aCtx, *aUserPlanes);
 
     // Set OCCT state uniform variables
-    if (!aCtx->ShaderManager()->IsEmpty())
-    {
-      aCtx->ShaderManager()->UpdateClippingState();
-    }
+    aCtx->ShaderManager()->UpdateClippingState();
   }
 
   // Render groups
@@ -587,10 +584,7 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
     aCtx->ChangeClipping().Remove (aCtx, *aUserPlanes);
 
     // Set OCCT state uniform variables
-    if (!aCtx->ShaderManager()->IsEmpty())
-    {
-      aCtx->ShaderManager()->RevertClippingState();
-    }
+    aCtx->ShaderManager()->RevertClippingState();
   }
 
   // Restore local transformation
index 78af8c7..c5f4876 100644 (file)
@@ -922,10 +922,6 @@ void OpenGl_View::render (Graphic3d_Camera::Projection theProjection,
 #endif
 
   aManager->SetShadingModel (myShadingModel);
-  if (!aManager->IsEmpty())
-  {
-    aManager->UpdateClippingState();
-  }
 
   // Redraw 3d scene
   if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
@@ -954,17 +950,11 @@ void OpenGl_View::render (Graphic3d_Camera::Projection theProjection,
   // before drawing auxiliary stuff (trihedrons, overlayer)
   myWorkspace->ResetAppliedAspect();
 
-  aContext->ChangeClipping().RemoveAll (aContext);
 
-  if (!aManager->IsEmpty())
-  {
-    aManager->RevertClippingState();
-
-    // We need to disable (unbind) all shaders programs to ensure
-    // that all objects without specified aspect will be drawn
-    // correctly (such as background)
-    aContext->BindProgram (NULL);
-  }
+  // We need to disable (unbind) all shaders programs to ensure
+  // that all objects without specified aspect will be drawn
+  // correctly (such as background)
+  aContext->BindProgram (NULL);
 
   // Render trihedron
   if (!theToDrawImmediate)
@@ -1192,6 +1182,8 @@ void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
       // add planes at loaded view matrix state
       aContext->ChangeClipping().AddView (aContext, aSlicingPlanes);
     }
+
+    aContext->ShaderManager()->UpdateClippingState();
   }
 
 #ifdef _WIN32
@@ -1221,13 +1213,10 @@ void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
 
     if (!aUserPlanes.IsEmpty())
     {
-      aContext->ChangeClipping().AddWorldLazy (aContext, aUserPlanes);
+      aContext->ChangeClipping().AddWorld (aContext, aUserPlanes);
     }
 
-    if (!aContext->ShaderManager()->IsEmpty())
-    {
-      aContext->ShaderManager()->UpdateClippingState();
-    }
+    aContext->ShaderManager()->UpdateClippingState();
   }
 
 #if !defined(GL_ES_VERSION_2_0)
@@ -1314,6 +1303,16 @@ void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
   // Apply restored view matrix.
   aContext->ApplyWorldViewMatrix();
 
+  aContext->ChangeClipping().RemoveAll (aContext);
+  if (!myClipPlanes.IsEmpty())
+  {
+    aContext->ShaderManager()->RevertClippingState();
+  }
+  if (myZClip.Back.IsOn || myZClip.Front.IsOn)
+  {
+    aContext->ShaderManager()->RevertClippingState();
+  }
+
 #ifdef _WIN32
   // set printing scale/tiling transformation
   if (!aPrintContext.IsNull())
index bdcd58c..d7fb9b2 100755 (executable)
@@ -182,7 +182,7 @@ void main()
     int  aClipSpace    = occClipPlaneSpaces[anIndex];
     if (aClipSpace == OccEquationCoords_World)
     {
-      if (dot (aClipEquation.xyz, PositionWorld.xyz) + aClipEquation.w < 0.0)
+      if (dot (aClipEquation.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation.w < 0.0)
       {
         discard;
       }
index 05b78e3..8ba33fe 100644 (file)
@@ -18,10 +18,8 @@ set y2_coord 320
 set x3_coord 73
 set y3_coord 150
 
-# enable FFP until bug-fix for GLSL
-vcaps -ffp 1
-
-vinit
+vclear
+vinit View1
 box b1 0 0 0 10 10 10
 box b2 30 0 0 10 40 10
 box b3 -30 0 0 20 20 20
@@ -53,6 +51,3 @@ vclipplane set pln2 object b3
 checkcolor $x3_coord $y3_coord 0.5 0.5 0.9
 
 checkview -screenshot -3d -path ${imagedir}/${test_image}.png
-
-
-