0024717: TKOpenGl - globally defined clipping planes blink when operating with view
[occt.git] / src / OpenGl / OpenGl_View_2.cxx
index 3bd7bd5..507ddc7 100644 (file)
@@ -64,6 +64,7 @@ struct OPENGL_CLIP_PLANE
 * Fonctions privees
 */
 
+#if !defined(GL_ES_VERSION_2_0)
 /*-----------------------------------------------------------------*/
 /*
 *  Set des lumieres
@@ -153,11 +154,13 @@ static void bind_light (const OpenGl_Light& theLight,
 
   glEnable (theLightGlId++);
 }
+#endif
 
 /*----------------------------------------------------------------------*/
 
 void OpenGl_View::DrawBackground (OpenGl_Workspace& theWorkspace)
 {
+#if !defined(GL_ES_VERSION_2_0)
   if ( (theWorkspace.NamedStatus & OPENGL_NS_WHITEBACK) == 0 &&
        ( myBgTexture.TexId != 0 || myBgGradient.type != Aspect_GFM_NONE ) )
   {
@@ -344,6 +347,7 @@ void OpenGl_View::DrawBackground (OpenGl_Workspace& theWorkspace)
       glEnable (GL_DEPTH_TEST);
     }
   }
+#endif
 }
 
 /*----------------------------------------------------------------------*/
@@ -361,27 +365,28 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
 
   const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
 
-  // Store and disable current clipping planes
-  Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
-
-  OPENGL_CLIP_PLANE *aOldPlanes = new OPENGL_CLIP_PLANE[aMaxPlanes];
-  OPENGL_CLIP_PLANE *aPtrPlane = aOldPlanes;
-
-  GLenum aClipPlaneId = GL_CLIP_PLANE0;
-  const GLenum aClipLastId = GL_CLIP_PLANE0 + aMaxPlanes;
-  for (; aClipPlaneId < aClipLastId; aClipPlaneId++, aPtrPlane++)
+#if !defined(GL_ES_VERSION_2_0)
+  // store and disable current clipping planes
+  const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
+  NCollection_Array1<OPENGL_CLIP_PLANE> aOldPlanes (GL_CLIP_PLANE0, GL_CLIP_PLANE0 + aMaxPlanes - 1);
+  if (aContext->core11 != NULL)
   {
-    glGetClipPlane (aClipPlaneId, aPtrPlane->Equation);
-    if (aPtrPlane->isEnabled)
-    {
-      glDisable (aClipPlaneId);
-      aPtrPlane->isEnabled = GL_TRUE;
-    }
-    else
+    for (Standard_Integer aClipPlaneId = aOldPlanes.Lower(); aClipPlaneId <= aOldPlanes.Upper(); ++aClipPlaneId)
     {
-      aPtrPlane->isEnabled = GL_FALSE;
+      OPENGL_CLIP_PLANE& aPlane = aOldPlanes.ChangeValue (aClipPlaneId);
+      aContext->core11->glGetClipPlane (aClipPlaneId, aPlane.Equation);
+      if (aPlane.isEnabled)
+      {
+        aContext->core11fwd->glDisable (aClipPlaneId);
+        aPlane.isEnabled = GL_TRUE;
+      }
+      else
+      {
+        aPlane.isEnabled = GL_FALSE;
+      }
     }
   }
+#endif
 
   Standard_Boolean isProjectionMatUpdateNeeded  = Standard_False;
   Standard_Boolean isOrientationMatUpdateNeeded = Standard_False;
@@ -397,7 +402,8 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
   }
 
   // Set OCCT state uniform variables
-  const Handle(OpenGl_ShaderManager) aManager = aContext->ShaderManager();
+  const Handle(OpenGl_ShaderManager) aManager   = aContext->ShaderManager();
+  const Standard_Boolean             isSameView = aManager->IsSameView (this); // force camera state update when needed
   if (!aManager->IsEmpty())
   {
     if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
@@ -406,13 +412,15 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
       myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
     }
 
-    if (myProjectionState != myCamera->ProjectionState())
+    if (myProjectionState != myCamera->ProjectionState()
+    || !isSameView)
     {
       myProjectionState = myCamera->ProjectionState();
       aManager->UpdateProjectionStateTo ((const Tmatrix3*)myCamera->ProjectionMatrixF().GetData());
     }
 
-    if (myModelViewState != myCamera->ModelViewState())
+    if (myModelViewState != myCamera->ModelViewState()
+    || !isSameView)
     {
       myModelViewState = myCamera->ModelViewState();
       aManager->UpdateWorldViewStateTo ((const Tmatrix3*)myCamera->OrientationMatrixF().GetData());
@@ -429,19 +437,6 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
     }
   }
 
-  if (!aManager.IsNull())
-  {
-    if (!aManager->IsSameView (this))
-    {
-      // Force update camera states
-      myProjectionState = myCamera->ProjectionState();
-      aManager->UpdateProjectionStateTo ((const Tmatrix3*)myCamera->ProjectionMatrixF().GetData());
-
-      myModelViewState = myCamera->ModelViewState();
-      aManager->UpdateWorldViewStateTo ((const Tmatrix3*)myCamera->OrientationMatrixF().GetData());
-    }
-  }
-
   if (isProjectionMatUpdateNeeded
    || isOrientationMatUpdateNeeded)
   {
@@ -458,8 +453,10 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
     DrawBackground (*theWorkspace);
   }
 
+#if !defined(GL_ES_VERSION_2_0)
   // Switch off lighting by default
   glDisable(GL_LIGHTING);
+#endif
 
   // =================================
   //      Step 3: Draw underlayer
@@ -485,6 +482,7 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
       glDisable( GL_CULL_FACE );
   }
 
+#if !defined(GL_ES_VERSION_2_0)
   // if the view is scaled normal vectors are scaled to unit
   // length for correct displaying of shaded objects
   const gp_Pnt anAxialScale = myCamera->AxialScale();
@@ -516,7 +514,7 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
     {
       myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
       myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
-    }    
+    }
 
     glFogi(GL_FOG_MODE, GL_LINEAR);
     glFogf(GL_FOG_START, (Standard_ShortReal )aFogFrontConverted);
@@ -528,7 +526,11 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
     glDisable(GL_FOG);
 
   // Apply InteriorShadingMethod
-  glShadeModel( myIntShadingMethod == TEL_SM_FLAT ? GL_FLAT : GL_SMOOTH );
+  aContext->core11->glShadeModel (myShadingModel == Visual3d_TOM_FACET
+                               || myShadingModel == Visual3d_TOM_NONE ? GL_FLAT : GL_SMOOTH);
+#endif
+
+  aManager->SetShadingModel (myShadingModel);
 
   // Apply AntiAliasing
   if (myAntiAliasing)
@@ -572,7 +574,7 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
     }
     // safely switch to right Eye buffer
     aContext->SetDrawBufferRight();
-    
+
     // redraw right Eye
     RedrawScene (thePrintContext, theWorkspace, aRProj, aOrient);
 
@@ -600,7 +602,7 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
     // We need to disable (unbind) all shaders programs to ensure
     // that all objects without specified aspect will be drawn
     // correctly (such as background)
-    OpenGl_ShaderProgram::Unbind (aContext);
+    aContext->BindProgram (NULL);
   }
 
   // Render trihedron
@@ -636,20 +638,21 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
   //      Step 7: Finalize
   // ===============================
 
-  // Restore clipping planes
-  aClipPlaneId = GL_CLIP_PLANE0;
-  aPtrPlane = aOldPlanes;
-
-  for (; aClipPlaneId < aClipLastId; aClipPlaneId++, aPtrPlane++)
+#if !defined(GL_ES_VERSION_2_0)
+  // restore clipping planes
+  if (aContext->core11 != NULL)
   {
-    glClipPlane (aClipPlaneId, aPtrPlane->Equation);
-    if (aPtrPlane->isEnabled)
-      glEnable (aClipPlaneId);
-    else
-      glDisable (aClipPlaneId);
+    for (Standard_Integer aClipPlaneId = aOldPlanes.Lower(); aClipPlaneId <= aOldPlanes.Upper(); ++aClipPlaneId)
+    {
+      const OPENGL_CLIP_PLANE& aPlane = aOldPlanes.ChangeValue (aClipPlaneId);
+      aContext->core11->glClipPlane (aClipPlaneId, aPlane.Equation);
+      if (aPlane.isEnabled)
+        aContext->core11fwd->glEnable (aClipPlaneId);
+      else
+        aContext->core11fwd->glDisable (aClipPlaneId);
+    }
   }
-
-  delete[] aOldPlanes;
+#endif
 
   // ==============================================================
   //      Step 8: Keep shader manager informed about last View
@@ -678,27 +681,35 @@ void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace) &AWorkspace)
   if ( myZLayers.NbStructures() <= 0 )
     return;
 
+#if !defined(GL_ES_VERSION_2_0)
   glPushAttrib ( GL_DEPTH_BUFFER_BIT );
+#endif
 
   //TsmPushAttri(); /* save previous graphics context */
 
   if ( (AWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) == 0 )
   {
+  #if !defined(GL_ES_VERSION_2_0)
     const int antiAliasingMode = AWorkspace->AntiAliasingMode();
+  #endif
 
     if ( !myAntiAliasing )
     {
+    #if !defined(GL_ES_VERSION_2_0)
       glDisable(GL_POINT_SMOOTH);
       glDisable(GL_LINE_SMOOTH);
       if( antiAliasingMode & 2 ) glDisable(GL_POLYGON_SMOOTH);
+    #endif
       glBlendFunc (GL_ONE, GL_ZERO);
       glDisable (GL_BLEND);
     }
     else
     {
+    #if !defined(GL_ES_VERSION_2_0)
       glEnable(GL_POINT_SMOOTH);
       glEnable(GL_LINE_SMOOTH);
       if( antiAliasingMode & 2 ) glEnable(GL_POLYGON_SMOOTH);
+    #endif
       glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
       glEnable (GL_BLEND);
     }
@@ -706,8 +717,10 @@ void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace) &AWorkspace)
 
   myZLayers.Render (AWorkspace);
 
+#if !defined(GL_ES_VERSION_2_0)
   //TsmPopAttri(); /* restore previous graphics context; before update lights */
   glPopAttrib();
+#endif
 }
 
 /*----------------------------------------------------------------------*/
@@ -717,6 +730,7 @@ void OpenGl_View::RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintCo
                                  const Graphic3d_CView&               ACView,
                                  const Aspect_CLayer2d&               ACLayer)
 {
+#if !defined(GL_ES_VERSION_2_0)
   if (&ACLayer == NULL
    || ACLayer.ptrLayer == NULL
    || ACLayer.ptrLayer->listIndex == 0) return;
@@ -841,6 +855,7 @@ void OpenGl_View::RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintCo
     glViewport (0, 0, (GLsizei) ACView.DefWindow.dx, (GLsizei) ACView.DefWindow.dy);
 
   glFlush ();
+#endif
 }
 
 /*----------------------------------------------------------------------*/
@@ -924,9 +939,12 @@ void OpenGl_View::CreateBackgroundTexture (const Standard_CString  theFilePath,
   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
 
   const GLenum aDataFormat = (anImage.Format() == Image_PixMap::ImgRGB) ? GL_RGB : GL_RGBA;
+
+#if !defined(GL_ES_VERSION_2_0)
   gluBuild2DMipmaps (GL_TEXTURE_2D, 3/*4*/,
                      GLint(anImage.SizeX()), GLint(anImage.SizeY()),
                      aDataFormat, GL_UNSIGNED_BYTE, anImage.Data());
+#endif
 
   myBgTexture.TexId  = aTextureId;
   myBgTexture.Width  = (Standard_Integer )anImage.SizeX();
@@ -1127,7 +1145,7 @@ void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintCont
       aPlaneFront = new Graphic3d_ClipPlane (aFrontEquation);
     }
 
-    // do some "memory allocation"-wise optimization
+    // Specify slicing planes with identity transformation
     if (!aPlaneBack.IsNull() || !aPlaneFront.IsNull())
     {
       Graphic3d_SequenceOfHClipPlane aSlicingPlanes;
@@ -1146,32 +1164,7 @@ void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintCont
     }
   }
 
-  // Apply user clipping planes
-  if (!myClipPlanes.IsEmpty())
-  {
-    Graphic3d_SequenceOfHClipPlane aUserPlanes;
-    Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes);
-    for (; aClippingIt.More(); aClippingIt.Next())
-    {
-      const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value();
-      if (aClipPlane->IsOn())
-      {
-        aUserPlanes.Append (aClipPlane);
-      }
-    }
-
-    if (!aUserPlanes.IsEmpty())
-    {
-      // add planes at actual matrix state.
-      aContext->ChangeClipping().AddWorld (aUserPlanes);
-    }
-
-    if (!aContext->ShaderManager()->IsEmpty())
-    {
-      aContext->ShaderManager()->UpdateClippingState();
-    }
-  }
-
+#if !defined(GL_ES_VERSION_2_0)
   // Setup view projection
   glMatrixMode (GL_PROJECTION);
 
@@ -1195,12 +1188,39 @@ void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintCont
     aContext->ShaderManager()->UpdateProjectionStateTo (&aResultProjection);
 
     // force shader uniform restore on next frame
-    myProjectionState = 0; 
+    myProjectionState = 0;
   }
+#endif
 
   // Setup view orientation
   theWorkspace->SetViewMatrix (theOrientation);
 
+  // Specify clipping planes in view transformation space
+  if (!myClipPlanes.IsEmpty())
+  {
+    Graphic3d_SequenceOfHClipPlane aUserPlanes;
+    Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes);
+    for (; aClippingIt.More(); aClippingIt.Next())
+    {
+      const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value();
+      if (aClipPlane->IsOn())
+      {
+        aUserPlanes.Append (aClipPlane);
+      }
+    }
+
+    if (!aUserPlanes.IsEmpty())
+    {
+      aContext->ChangeClipping().AddWorld (aUserPlanes);
+    }
+
+    if (!aContext->ShaderManager()->IsEmpty())
+    {
+      aContext->ShaderManager()->UpdateClippingState();
+    }
+  }
+
+#if !defined(GL_ES_VERSION_2_0)
   // Apply Lights
   {
     // setup lights
@@ -1229,6 +1249,7 @@ void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintCont
       glDisable (aLightGlId);
     }
   }
+#endif
 
   // Clear status bitfields
   theWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO);
@@ -1265,13 +1286,15 @@ void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintCont
         theWorkspace->EnableTexture (myTextureEnv);
 
         // Remember OpenGl properties
-        GLint aSaveBlendDst, aSaveBlendSrc;
+        GLint aSaveBlendDst = GL_ONE_MINUS_SRC_ALPHA, aSaveBlendSrc = GL_SRC_ALPHA;
         GLint aSaveZbuffFunc;
         GLboolean aSaveZbuffWrite;
         glGetBooleanv (GL_DEPTH_WRITEMASK, &aSaveZbuffWrite);
         glGetIntegerv (GL_DEPTH_FUNC, &aSaveZbuffFunc);
+      #if !defined(GL_ES_VERSION_2_0)
         glGetIntegerv (GL_BLEND_DST, &aSaveBlendDst);
         glGetIntegerv (GL_BLEND_SRC, &aSaveBlendSrc);
+      #endif
         GLboolean wasZbuffEnabled = glIsEnabled (GL_DEPTH_TEST);
         GLboolean wasBlendEnabled = glIsEnabled (GL_BLEND);