0030931: Visualization, TKOpenGl - do not render into GL_FRONT within "GDI Generic...
[occt.git] / src / OpenGl / OpenGl_Workspace.cxx
index 10e0ac4..c13bcb3 100644 (file)
 #include <OpenGl_Workspace.hxx>
 
 #include <OpenGl_ArbFBO.hxx>
-#include <OpenGl_AspectLine.hxx>
-#include <OpenGl_AspectFace.hxx>
-#include <OpenGl_AspectMarker.hxx>
-#include <OpenGl_AspectText.hxx>
+#include <OpenGl_Aspects.hxx>
 #include <OpenGl_Context.hxx>
 #include <OpenGl_Element.hxx>
 #include <OpenGl_FrameBuffer.hxx>
 #include <NCollection_AlignedAllocator.hxx>
 
 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
-IMPLEMENT_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
-
-#ifdef HAVE_GL2PS
-  #include <gl2ps.h>
-  /* OCC22216 NOTE: linker dependency can be switched off by undefining macro.
-     Pragma comment for gl2ps.lib is defined only here. */
-  #ifdef _MSC_VER
-  #pragma comment( lib, "gl2ps.lib" )
-  #endif
-#endif
 
 namespace
 {
   static const OpenGl_Vec4 THE_WHITE_COLOR (1.0f, 1.0f, 1.0f, 1.0f);
   static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f);
 
-  static const OpenGl_AspectLine myDefaultAspectLine;
-  static const OpenGl_AspectFace myDefaultAspectFace;
-  static const OpenGl_AspectMarker myDefaultAspectMarker;
-  static const OpenGl_AspectText myDefaultAspectText;
-
   static const OpenGl_Matrix myDefaultMatrix =
   {
     {{ 1.0F, 0.0F, 0.0F, 0.0F },
@@ -135,10 +117,10 @@ OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Wi
   myUseZBuffer    (Standard_True),
   myUseDepthWrite (Standard_True),
   //
-  myAspectLineSet (&myDefaultAspectLine),
-  myAspectFaceSet (&myDefaultAspectFace),
-  myAspectMarkerSet (&myDefaultAspectMarker),
-  myAspectTextSet (&myDefaultAspectText),
+  myNbSkippedTranspElems (0),
+  myRenderFilter (OpenGl_RenderFilter_Empty),
+  //
+  myAspectsSet (&myDefaultAspects),
   //
   ViewMatrix_applied (&myDefaultMatrix),
   StructureMatrix_applied (&myDefaultMatrix),
@@ -164,14 +146,15 @@ OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Wi
 
     glHint (GL_LINE_SMOOTH_HINT,    GL_FASTEST);
     glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
+    if (myGlContext->Vendor() == "microsoft corporation"
+    && !myGlContext->IsGlGreaterEqual (1, 2))
+    {
+      // this software implementation causes too slow rendering into GL_FRONT on modern Windows
+      theView->SetImmediateModeDrawToFront (false);
+    }
   #endif
   }
 
-  myDefaultCappingAlgoFilter = new OpenGl_CappingAlgoFilter();
-
-  myFontFaceAspect.Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Mask, 0.285f);
-  myFontFaceAspect.Aspect()->SetShadingModel (Graphic3d_TOSM_UNLIT);
-
   myNoneCulling .Aspect()->SetSuppressBackFaces (false);
   myNoneCulling .Aspect()->SetDrawEdges (false);
   myNoneCulling .Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Opaque);
@@ -220,92 +203,64 @@ void OpenGl_Workspace::ResetAppliedAspect()
 
   myHighlightStyle.Nullify();
   myToAllowFaceCulling  = false;
-  myAspectLineSet       = &myDefaultAspectLine;
-  myAspectFaceSet       = &myDefaultAspectFace;
-  myAspectFaceApplied.Nullify();
-  myAspectMarkerSet     = &myDefaultAspectMarker;
-  myAspectMarkerApplied.Nullify();
-  myAspectTextSet       = &myDefaultAspectText;
+  myAspectsSet = &myDefaultAspects;
+  myAspectsApplied.Nullify();
   myGlContext->SetPolygonOffset (Graphic3d_PolygonOffset());
 
-  ApplyAspectLine();
-  ApplyAspectFace();
-  ApplyAspectMarker();
-  ApplyAspectText();
-
-  myGlContext->SetTypeOfLine (myDefaultAspectLine.Aspect()->Type());
-  myGlContext->SetLineWidth  (myDefaultAspectLine.Aspect()->Width());
-}
-
-// =======================================================================
-// function : SetAspectLine
-// purpose  :
-// =======================================================================
-const OpenGl_AspectLine* OpenGl_Workspace::SetAspectLine (const OpenGl_AspectLine* theAspect)
-{
-  const OpenGl_AspectLine* aPrevAspectLine = myAspectLineSet;
-  myAspectLineSet = theAspect;
-  return aPrevAspectLine;
-}
-
-// =======================================================================
-// function : SetAspectFace
-// purpose  :
-// =======================================================================
-const OpenGl_AspectFace * OpenGl_Workspace::SetAspectFace (const OpenGl_AspectFace* theAspect)
-{
-  const OpenGl_AspectFace* aPrevAspectFace = myAspectFaceSet;
-  myAspectFaceSet = theAspect;
-  return aPrevAspectFace;
+  ApplyAspects();
+  myGlContext->SetTypeOfLine (myDefaultAspects.Aspect()->LineType());
+  myGlContext->SetLineWidth  (myDefaultAspects.Aspect()->LineWidth());
+  if (myGlContext->core15fwd != NULL)
+  {
+    myGlContext->core15fwd->glActiveTexture (GL_TEXTURE0);
+  }
 }
 
 // =======================================================================
-// function : SetAspectMarker
+// function : SetDefaultPolygonOffset
 // purpose  :
 // =======================================================================
-const OpenGl_AspectMarker* OpenGl_Workspace::SetAspectMarker (const OpenGl_AspectMarker* theAspect)
+Graphic3d_PolygonOffset OpenGl_Workspace::SetDefaultPolygonOffset (const Graphic3d_PolygonOffset& theOffset)
 {
-  const OpenGl_AspectMarker* aPrevAspectMarker = myAspectMarkerSet;
-  myAspectMarkerSet = theAspect;
-  return aPrevAspectMarker;
+  Graphic3d_PolygonOffset aPrev = myDefaultAspects.Aspect()->PolygonOffset();
+  myDefaultAspects.Aspect()->SetPolygonOffset (theOffset);
+  if (myAspectsApplied == myDefaultAspects.Aspect()
+   || myAspectsApplied.IsNull()
+   || (myAspectsApplied->PolygonOffset().Mode & Aspect_POM_None) == Aspect_POM_None)
+  {
+    myGlContext->SetPolygonOffset (theOffset);
+  }
+  return aPrev;
 }
 
 // =======================================================================
-// function : SetAspectText
+// function : SetAspects
 // purpose  :
 // =======================================================================
-const OpenGl_AspectText * OpenGl_Workspace::SetAspectText (const OpenGl_AspectText* theAspect)
+const OpenGl_Aspects* OpenGl_Workspace::SetAspects (const OpenGl_Aspects* theAspect)
 {
-  const OpenGl_AspectText* aPrevAspectText = myAspectTextSet;
-  myAspectTextSet = theAspect;
-  return aPrevAspectText;
+  const OpenGl_Aspects* aPrevAspects = myAspectsSet;
+  myAspectsSet = theAspect;
+  return aPrevAspects;
 }
 
 // =======================================================================
-// function : ApplyAspectFace
+// function : ApplyAspects
 // purpose  :
 // =======================================================================
-const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
+const OpenGl_Aspects* OpenGl_Workspace::ApplyAspects()
 {
   if (myView->BackfacingModel() == Graphic3d_TOBM_AUTOMATIC)
   {
-    // manage back face culling mode, disable culling when clipping is enabled
     bool toSuppressBackFaces = myToAllowFaceCulling
-                            && myAspectFaceSet->Aspect()->ToSuppressBackFaces();
+                            && myAspectsSet->Aspect()->ToSuppressBackFaces();
     if (toSuppressBackFaces)
     {
-      if (myGlContext->Clipping().IsClippingOrCappingOn()
-       || myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HATCH)
-      {
-        toSuppressBackFaces = false;
-      }
-    }
-    if (toSuppressBackFaces)
-    {
-      if (myAspectFaceSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_Blend
-       || myAspectFaceSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_Mask
-       || (myAspectFaceSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_BlendAuto
-        && myAspectFaceSet->Aspect()->FrontMaterial().Transparency() != 0.0f))
+      if (myAspectsSet->Aspect()->InteriorStyle() == Aspect_IS_HATCH
+       || myAspectsSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_Blend
+       || myAspectsSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_Mask
+       || (myAspectsSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_BlendAuto
+        && myAspectsSet->Aspect()->FrontMaterial().Transparency() != 0.0f))
       {
         // disable culling in case of translucent shading aspect
         toSuppressBackFaces = false;
@@ -314,109 +269,65 @@ const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
     myGlContext->SetCullBackFaces (toSuppressBackFaces);
   }
 
-  if (myAspectFaceSet->Aspect() == myAspectFaceApplied
+  if (myAspectsSet->Aspect() == myAspectsApplied
    && myHighlightStyle == myAspectFaceAppliedWithHL)
   {
-    return myAspectFaceSet;
+    return myAspectsSet;
   }
   myAspectFaceAppliedWithHL = myHighlightStyle;
 
-#if !defined(GL_ES_VERSION_2_0)
-  const Aspect_InteriorStyle anIntstyle = myAspectFaceSet->Aspect()->InteriorStyle();
-  if (myAspectFaceApplied.IsNull()
-   || myAspectFaceApplied->InteriorStyle() != anIntstyle)
+  // Aspect_POM_None means: do not change current settings
+  if ((myAspectsSet->Aspect()->PolygonOffset().Mode & Aspect_POM_None) != Aspect_POM_None)
   {
-    switch (anIntstyle)
-    {
-      case Aspect_IS_EMPTY:
-      case Aspect_IS_HOLLOW:
-      {
-        myGlContext->SetPolygonMode (GL_LINE);
-        break;
-      }
-      case Aspect_IS_HATCH:
-      {
-        myGlContext->SetPolygonMode (GL_FILL);
-        myGlContext->SetPolygonHatchEnabled (true);
-        break;
-      }
-      case Aspect_IS_SOLID:
-      case Aspect_IS_HIDDENLINE:
-      {
-        myGlContext->SetPolygonMode (GL_FILL);
-        myGlContext->SetPolygonHatchEnabled (false);
-        break;
-      }
-      case Aspect_IS_POINT:
-      {
-        myGlContext->SetPolygonMode (GL_POINT);
-        break;
-      }
-    }
+    myGlContext->SetPolygonOffset (myAspectsSet->Aspect()->PolygonOffset());
   }
 
-  if (anIntstyle == Aspect_IS_HATCH)
+  const Aspect_InteriorStyle anIntstyle = myAspectsSet->Aspect()->InteriorStyle();
+  if (myAspectsApplied.IsNull()
+   || myAspectsApplied->InteriorStyle() != anIntstyle)
   {
-    myGlContext->SetPolygonHatchStyle (myAspectFaceSet->Aspect()->HatchStyle());
+  #if !defined(GL_ES_VERSION_2_0)
+    myGlContext->SetPolygonMode (anIntstyle == Aspect_IS_POINT ? GL_POINT : GL_FILL);
+    myGlContext->SetPolygonHatchEnabled (anIntstyle == Aspect_IS_HATCH);
+  #endif
   }
-#endif
 
-  // Aspect_POM_None means: do not change current settings
-  if ((myAspectFaceSet->Aspect()->PolygonOffset().Mode & Aspect_POM_None) != Aspect_POM_None)
+#if !defined(GL_ES_VERSION_2_0)
+  if (anIntstyle == Aspect_IS_HATCH)
   {
-    myGlContext->SetPolygonOffset (myAspectFaceSet->Aspect()->PolygonOffset());
+    myGlContext->SetPolygonHatchStyle (myAspectsSet->Aspect()->HatchStyle());
   }
+#endif
 
   // Case of hidden line
-  if (myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
+  if (anIntstyle == Aspect_IS_HIDDENLINE)
   {
     // copy all values including line edge aspect
-    *myAspectFaceHl.Aspect() = *myAspectFaceSet->Aspect();
-    myAspectFaceHl.SetAspectEdge (myAspectFaceSet->AspectEdge());
+    *myAspectFaceHl.Aspect() = *myAspectsSet->Aspect();
     myAspectFaceHl.Aspect()->SetShadingModel (Graphic3d_TOSM_UNLIT);
     myAspectFaceHl.Aspect()->SetInteriorColor (myView->BackgroundColor().GetRGB());
+    myAspectFaceHl.Aspect()->SetDistinguish (false);
     myAspectFaceHl.SetNoLighting();
-    myAspectFaceSet = &myAspectFaceHl;
+    myAspectsSet = &myAspectFaceHl;
   }
   else
   {
-    myGlContext->SetShadingMaterial (myAspectFaceSet, myHighlightStyle);
+    myGlContext->SetShadingMaterial (myAspectsSet, myHighlightStyle);
   }
 
-  if (myAspectFaceSet->Aspect()->ToMapTexture())
+  const Handle(OpenGl_TextureSet)& aTextureSet = myAspectsSet->TextureSet (myGlContext, ToHighlight());
+  if (!aTextureSet.IsNull()
+   || myAspectsSet->Aspect()->ToMapTexture())
   {
-    myGlContext->BindTextures (myAspectFaceSet->TextureSet (myGlContext));
+    myGlContext->BindTextures (aTextureSet);
   }
   else
   {
     myGlContext->BindTextures (myEnvironmentTexture);
   }
 
-  myAspectFaceApplied = myAspectFaceSet->Aspect();
-  return myAspectFaceSet;
-}
-
-// =======================================================================
-// function : ApplyAspectMarker
-// purpose  :
-// =======================================================================
-const OpenGl_AspectMarker* OpenGl_Workspace::ApplyAspectMarker()
-{
-  if (myAspectMarkerSet->Aspect() != myAspectMarkerApplied)
-  {
-    if (myAspectMarkerApplied.IsNull()
-    || (myAspectMarkerSet->Aspect()->Scale() != myAspectMarkerApplied->Scale()))
-    {
-    #if !defined(GL_ES_VERSION_2_0)
-      glPointSize (myAspectMarkerSet->Aspect()->Scale());
-    #ifdef HAVE_GL2PS
-      gl2psPointSize (myAspectMarkerSet->Aspect()->Scale());
-    #endif
-    #endif
-    }
-    myAspectMarkerApplied = myAspectMarkerSet->Aspect();
-  }
-  return myAspectMarkerSet;
+  myAspectsApplied = myAspectsSet->Aspect();
+  return myAspectsSet;
 }
 
 // =======================================================================
@@ -437,15 +348,6 @@ Standard_Integer OpenGl_Workspace::Height() const
   return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0;
 }
 
-// =======================================================================
-// function : IsCullingEnabled
-// purpose  :
-// =======================================================================
-Standard_Boolean OpenGl_Workspace::IsCullingEnabled() const
-{
-  return myView->IsCullingEnabled();
-}
-
 // =======================================================================
 // function : FBOCreate
 // purpose  :
@@ -503,14 +405,51 @@ Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)&
 // function : ShouldRender
 // purpose  :
 // =======================================================================
-Standard_Boolean OpenGl_RaytraceFilter::ShouldRender (const Handle(OpenGl_Workspace)& theWorkspace,
-                                                      const OpenGl_Element*           theElement)
+bool OpenGl_Workspace::ShouldRender (const OpenGl_Element* theElement)
 {
-  Standard_Boolean aPrevFilterResult = Standard_True;
-  if (!myPrevRenderFilter.IsNull())
+  // render only non-raytracable elements when RayTracing is enabled
+  if ((myRenderFilter & OpenGl_RenderFilter_NonRaytraceableOnly) != 0)
   {
-    aPrevFilterResult = myPrevRenderFilter->ShouldRender (theWorkspace, theElement);
+    if (OpenGl_Raytrace::IsRaytracedElement (theElement))
+    {
+      return false;
+    }
+  }
+  else if ((myRenderFilter & OpenGl_RenderFilter_FillModeOnly) != 0)
+  {
+    if (!theElement->IsFillDrawMode())
+    {
+      return false;
+    }
+  }
+
+  // handle opaque/transparency render passes
+  if ((myRenderFilter & OpenGl_RenderFilter_OpaqueOnly) != 0)
+  {
+    if (!theElement->IsFillDrawMode())
+    {
+      return true;
+    }
+
+    if (OpenGl_Context::CheckIsTransparent (myAspectsSet, myHighlightStyle))
+    {
+      ++myNbSkippedTranspElems;
+      return false;
+    }
+  }
+  else if ((myRenderFilter & OpenGl_RenderFilter_TransparentOnly) != 0)
+  {
+    if (!theElement->IsFillDrawMode())
+    {
+      if (dynamic_cast<const OpenGl_Aspects*> (theElement) == NULL)
+      {
+        return false;
+      }
+    }
+    else if (!OpenGl_Context::CheckIsTransparent (myAspectsSet, myHighlightStyle))
+    {
+      return false;
+    }
   }
-  return aPrevFilterResult &&
-    !OpenGl_Raytrace::IsRaytracedElement (theElement);
+  return true;
 }