0030748: Visualization - Marker displayed in immediate layer ruins QT Quick view...
[occt.git] / src / OpenGl / OpenGl_Structure.cxx
index 9d4d15e..927a4a0 100644 (file)
@@ -16,6 +16,7 @@
 #include <OpenGl_CappingAlgo.hxx>
 #include <OpenGl_Context.hxx>
 #include <OpenGl_GlCore11.hxx>
+#include <OpenGl_ClippingIterator.hxx>
 #include <OpenGl_GraphicDriver.hxx>
 #include <OpenGl_ShaderManager.hxx>
 #include <OpenGl_ShaderProgram.hxx>
 #include <OpenGl_View.hxx>
 #include <OpenGl_Workspace.hxx>
 
-#include <Graphic3d_SequenceOfHClipPlane.hxx>
-
-
 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Structure,Graphic3d_CStructure)
 
-//! Auxiliary class for bounding box presentation
-class OpenGl_BndBoxPrs : public OpenGl_Element
+// =======================================================================
+// function : renderBoundingBox
+// purpose  :
+// =======================================================================
+void OpenGl_Structure::renderBoundingBox (const Handle(OpenGl_Workspace)& theWorkspace) const
 {
-
-public:
-
-  //! Main constructor
-  OpenGl_BndBoxPrs (const Graphic3d_BndBox3d& theBndBox)
-  {
-    const float Xm = (float )theBndBox.CornerMin().x();
-    const float Ym = (float)theBndBox.CornerMin().y();
-    const float Zm = (float)theBndBox.CornerMin().z();
-    const float XM = (float)theBndBox.CornerMax().x();
-    const float YM = (float)theBndBox.CornerMax().y();
-    const float ZM = (float)theBndBox.CornerMax().z();
-
-    myVerts[0]  = OpenGl_Vec3 (Xm, Ym, Zm);
-    myVerts[1]  = OpenGl_Vec3 (Xm, Ym, ZM);
-    myVerts[2]  = OpenGl_Vec3 (Xm, YM, ZM);
-    myVerts[3]  = OpenGl_Vec3 (Xm, YM, Zm);
-    myVerts[4]  = OpenGl_Vec3 (Xm, Ym, Zm);
-    myVerts[5]  = OpenGl_Vec3 (XM, Ym, Zm);
-    myVerts[6]  = OpenGl_Vec3 (XM, Ym, ZM);
-    myVerts[7]  = OpenGl_Vec3 (XM, YM, ZM);
-    myVerts[8]  = OpenGl_Vec3 (XM, YM, Zm);
-    myVerts[9]  = OpenGl_Vec3 (XM, Ym, Zm);
-    myVerts[10] = OpenGl_Vec3 (XM, YM, Zm);
-    myVerts[11] = OpenGl_Vec3 (Xm, YM, Zm);
-    myVerts[12] = OpenGl_Vec3 (Xm, YM, ZM);
-    myVerts[13] = OpenGl_Vec3 (XM, YM, ZM);
-    myVerts[14] = OpenGl_Vec3 (XM, Ym, ZM);
-    myVerts[15] = OpenGl_Vec3 (Xm, Ym, ZM);
-  }
-
-  //! Render presentation
-  virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const
+  if (!myBndBox.IsValid())
   {
-  #if !defined(GL_ES_VERSION_2_0)
-    // Apply line aspect
-    const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture();
-
-    theWorkspace->GetGlContext()->BindProgram (Handle(OpenGl_ShaderProgram)());
-    theWorkspace->GetGlContext()->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
-
-    glDisable (GL_LIGHTING);
-
-    // Use highlight colors
-    theWorkspace->GetGlContext()->core11->glColor3fv (theWorkspace->LineColor().GetData());
-
-    glEnableClientState (GL_VERTEX_ARRAY);
-    glVertexPointer (3, GL_FLOAT, 0, (GLfloat* )&myVerts);
-    glDrawArrays (GL_LINE_STRIP, 0, 16);
-    glDisableClientState (GL_VERTEX_ARRAY);
-
-    // restore aspects
-    if (!aPrevTexture.IsNull())
-    {
-      theWorkspace->EnableTexture (aPrevTexture);
-    }
-  #else
-    (void )theWorkspace;
-  #endif
+    return;
   }
 
-  //! Release graphical resources
-  virtual void Release (OpenGl_Context*)
+  const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
+  const Handle(OpenGl_TextureSet) aPrevTexture = aCtx->BindTextures (Handle(OpenGl_TextureSet)());
+  const Graphic3d_ZLayerSettings& aLayer = myGraphicDriver->ZLayerSettings (myZLayer);
+  const Graphic3d_Vec3d aMoveVec = myTrsfPers.IsNull()
+                               && !aLayer.OriginTransformation().IsNull()
+                                 ? -Graphic3d_Vec3d (aLayer.Origin().X(), aLayer.Origin().Y(), aLayer.Origin().Z())
+                                 :  Graphic3d_Vec3d (0.0, 0.0, 0.0);
+  if (aCtx->core20fwd != NULL
+   && aCtx->ShaderManager()->BindBoundBoxProgram())
+  {
+    const Graphic3d_Vec3d aCenter = myBndBox.Center() + aMoveVec;
+    const Graphic3d_Vec3d aSize   = myBndBox.Size();
+    aCtx->ActiveProgram()->SetUniform (aCtx, "occBBoxCenter", Graphic3d_Vec3 ((float )aCenter.x(), (float )aCenter.y(), (float )aCenter.z()));
+    aCtx->ActiveProgram()->SetUniform (aCtx, "occBBoxSize",   Graphic3d_Vec3 ((float )aSize.x(),   (float )aSize.y(),   (float )aSize.z()));
+    aCtx->SetColor4fv (theWorkspace->InteriorColor());
+
+    const Handle(OpenGl_VertexBuffer)& aBoundBoxVertBuffer = aCtx->ShaderManager()->BoundBoxVertBuffer();
+    aBoundBoxVertBuffer->BindAttribute  (aCtx, Graphic3d_TOA_POS);
+    aCtx->core20fwd->glDrawArrays (GL_LINES, 0, aBoundBoxVertBuffer->GetElemsNb());
+    aBoundBoxVertBuffer->UnbindAttribute(aCtx, Graphic3d_TOA_POS);
+  }
+#if !defined(GL_ES_VERSION_2_0)
+  else if (aCtx->core11 != NULL)
   {
-    //
+    const Graphic3d_Vec3d aMind = myBndBox.CornerMin() + aMoveVec;
+    const Graphic3d_Vec3d aMaxd = myBndBox.CornerMax() + aMoveVec;
+    const Graphic3d_Vec3 aMin ((float )aMind.x(), (float )aMind.y(), (float )aMind.z());
+    const Graphic3d_Vec3 aMax ((float )aMaxd.x(), (float )aMaxd.y(), (float )aMaxd.z());
+    const OpenGl_Vec3 aVerts[16] =
+    {
+      OpenGl_Vec3 (aMin.x(), aMin.y(), aMin.z()),
+      OpenGl_Vec3 (aMin.x(), aMin.y(), aMax.z()),
+      OpenGl_Vec3 (aMin.x(), aMax.y(), aMax.z()),
+      OpenGl_Vec3 (aMin.x(), aMax.y(), aMin.z()),
+      OpenGl_Vec3 (aMin.x(), aMin.y(), aMin.z()),
+      OpenGl_Vec3 (aMax.x(), aMin.y(), aMin.z()),
+      OpenGl_Vec3 (aMax.x(), aMin.y(), aMax.z()),
+      OpenGl_Vec3 (aMax.x(), aMax.y(), aMax.z()),
+      OpenGl_Vec3 (aMax.x(), aMax.y(), aMin.z()),
+      OpenGl_Vec3 (aMax.x(), aMin.y(), aMin.z()),
+      OpenGl_Vec3 (aMax.x(), aMax.y(), aMin.z()),
+      OpenGl_Vec3 (aMin.x(), aMax.y(), aMin.z()),
+      OpenGl_Vec3 (aMin.x(), aMax.y(), aMax.z()),
+      OpenGl_Vec3 (aMax.x(), aMax.y(), aMax.z()),
+      OpenGl_Vec3 (aMax.x(), aMin.y(), aMax.z()),
+      OpenGl_Vec3 (aMin.x(), aMin.y(), aMax.z())
+    };
+
+    aCtx->ShaderManager()->BindLineProgram (Handle(OpenGl_TextureSet)(), Aspect_TOL_SOLID, Graphic3d_TOSM_UNLIT, Graphic3d_AlphaMode_Opaque, false, Handle(OpenGl_ShaderProgram)());
+    aCtx->SetColor4fv (theWorkspace->InteriorColor());
+    aCtx->core11fwd->glDisable (GL_LIGHTING);
+    aCtx->core11->glEnableClientState (GL_VERTEX_ARRAY);
+    aCtx->core11->glVertexPointer (3, GL_FLOAT, 0, aVerts[0].GetData());
+    aCtx->core11fwd->glDrawArrays (GL_LINE_STRIP, 0, 16);
+    aCtx->core11->glDisableClientState (GL_VERTEX_ARRAY);
   }
-
-protected:
-
-  //! Protected destructor
-  virtual ~OpenGl_BndBoxPrs() {}
-
-private:
-
-  OpenGl_Vec3 myVerts[16]; //!< vertices array
-
-public:
-
-  DEFINE_STANDARD_ALLOC
-
-};
-
-/*----------------------------------------------------------------------*/
+#endif
+  aCtx->BindTextures (aPrevTexture);
+}
 
 // =======================================================================
 // function : OpenGl_Structure
@@ -125,7 +107,6 @@ OpenGl_Structure::OpenGl_Structure (const Handle(Graphic3d_StructureManager)& th
   myInstancedStructure (NULL),
   myIsRaytracable      (Standard_False),
   myModificationState  (0),
-  myIsCulled           (Standard_True),
   myIsMirrored         (Standard_False)
 {
   updateLayerTransformation();
@@ -205,64 +186,14 @@ void OpenGl_Structure::updateLayerTransformation()
   aRenderTrsf.GetMat4 (myRenderTrsf);
 }
 
-// =======================================================================
-// function : clearHighlightBox
-// purpose  :
-// =======================================================================
-void OpenGl_Structure::clearHighlightBox (const Handle(OpenGl_Context)& theGlCtx)
-{
-  if (!myHighlightBox.IsNull())
-  {
-    myHighlightBox->Release (theGlCtx);
-    myHighlightBox.Nullify();
-  }
-}
-
-// =======================================================================
-// function : HighlightWithBndBox
-// purpose  :
-// =======================================================================
-void OpenGl_Structure::highlightWithBndBox (const Handle(Graphic3d_Structure)& theStruct)
-{
-  const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
-
-  if (!myHighlightBox.IsNull())
-  {
-    myHighlightBox->Release (aContext);
-  }
-  else
-  {
-    myHighlightBox = new OpenGl_Group (theStruct);
-  }
-
-  myHighlightBox->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d (myHighlightStyle->Color(), Aspect_TOL_SOLID, 1.0));
-
-  OpenGl_BndBoxPrs* aBndBoxPrs = new OpenGl_BndBoxPrs (myBndBox);
-  myHighlightBox->AddElement (aBndBoxPrs);
-}
-
 // =======================================================================
 // function : GraphicHighlight
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::GraphicHighlight (const Handle(Graphic3d_PresentationAttributes)& theStyle,
-                                         const Handle(Graphic3d_Structure)& theStruct)
+void OpenGl_Structure::GraphicHighlight (const Handle(Graphic3d_PresentationAttributes)& theStyle)
 {
-  if (!myHighlightStyle.IsNull()
-    && myHighlightStyle->Method() == Aspect_TOHM_BOUNDBOX
-    && theStyle->Method() != Aspect_TOHM_BOUNDBOX)
-  {
-    const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
-    clearHighlightBox (aContext);
-  }
-
   myHighlightStyle = theStyle;
-
   highlight = 1;
-  if (myHighlightStyle->Method() == Aspect_TOHM_BOUNDBOX)
-  {
-    highlightWithBndBox (theStruct);
-  }
 }
 
 // =======================================================================
@@ -272,13 +203,6 @@ void OpenGl_Structure::GraphicHighlight (const Handle(Graphic3d_PresentationAttr
 void OpenGl_Structure::GraphicUnhighlight()
 {
   highlight = 0;
-
-  if (myHighlightStyle->Method() == Aspect_TOHM_BOUNDBOX)
-  {
-    const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
-    clearHighlightBox (aContext);
-  }
-
   myHighlightStyle.Nullify();
 }
 
@@ -486,7 +410,7 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
   const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
 
   // Render named status
-  if (highlight && myHighlightBox.IsNull())
+  if (highlight && !myHighlightStyle.IsNull() && myHighlightStyle->Method() != Aspect_TOHM_BOUNDBOX)
   {
     theWorkspace->SetHighlightStyle (myHighlightStyle);
   }
@@ -535,10 +459,7 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
   aCtx->ApplyModelViewMatrix();
 
   // remember aspects
-  const OpenGl_AspectLine*   aPrevAspectLine   = theWorkspace->AspectLine();
-  const OpenGl_AspectFace*   aPrevAspectFace   = theWorkspace->AspectFace();
-  const OpenGl_AspectMarker* aPrevAspectMarker = theWorkspace->AspectMarker();
-  const OpenGl_AspectText*   aPrevAspectText   = theWorkspace->AspectText();
+  const OpenGl_Aspects* aPrevAspectFace = theWorkspace->Aspects();
 
   // Apply correction for mirror transform
   if (myIsMirrored)
@@ -547,7 +468,7 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
   }
 
   // Collect clipping planes of structure scope
-  aCtx->ChangeClipping().SetLocalPlanes (aCtx, myClipPlanes);
+  aCtx->ChangeClipping().SetLocalPlanes (myClipPlanes);
 
   // True if structure is fully clipped
   bool isClipped = false;
@@ -558,7 +479,7 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
     if (!myClipPlanes.IsNull()
       && myClipPlanes->ToOverrideGlobal())
     {
-      aCtx->ChangeClipping().DisableGlobal (aCtx);
+      aCtx->ChangeClipping().DisableGlobal();
       hasDisabled = aCtx->Clipping().HasDisabled();
     }
     else if (!myTrsfPers.IsNull())
@@ -579,9 +500,8 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
           }
 
           // check for clipping
-          const Graphic3d_Vec4d& aPlaneEquation = aPlane->GetEquation();
           const Graphic3d_Vec4d aCheckPnt (anAnchor.X(), anAnchor.Y(), anAnchor.Z(), 1.0);
-          if (aPlaneEquation.Dot (aCheckPnt) < 0.0) // vertex is outside the half-space
+          if (aPlane->ProbePoint (aCheckPnt) == Graphic3d_ClipState_Out)
           {
             isClipped = true;
             break;
@@ -589,7 +509,7 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
         }
       }
 
-      aCtx->ChangeClipping().DisableGlobal (aCtx);
+      aCtx->ChangeClipping().DisableGlobal();
       hasDisabled = aCtx->Clipping().HasDisabled();
     }
 
@@ -601,32 +521,21 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
       for (OpenGl_ClippingIterator aPlaneIt (aCtx->Clipping()); aPlaneIt.More(); aPlaneIt.Next())
       {
         const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
-        if (!aPlane->IsOn())
+        if (aPlaneIt.IsDisabled())
         {
           continue;
         }
 
-        // check for clipping
-        const Graphic3d_Vec4d& aPlaneEquation = aPlane->GetEquation();
-        const Graphic3d_Vec4d aMaxPnt (aPlaneEquation.x() > 0.0 ? aBBox.CornerMax().x() : aBBox.CornerMin().x(),
-                                       aPlaneEquation.y() > 0.0 ? aBBox.CornerMax().y() : aBBox.CornerMin().y(),
-                                       aPlaneEquation.z() > 0.0 ? aBBox.CornerMax().z() : aBBox.CornerMin().z(),
-                                       1.0);
-        if (aPlaneEquation.Dot (aMaxPnt) < 0.0) // max vertex is outside the half-space
+        const Graphic3d_ClipState aBoxState = aPlane->ProbeBox (aBBox);
+        if (aBoxState == Graphic3d_ClipState_In)
         {
-          isClipped = true;
-          break;
+          aCtx->ChangeClipping().SetEnabled (aPlaneIt, false);
+          hasDisabled = true;
         }
-
-        // check for no intersection (e.g. object is "entirely not clipped")
-        const Graphic3d_Vec4d aMinPnt (aPlaneEquation.x() > 0.0 ? aBBox.CornerMin().x() : aBBox.CornerMax().x(),
-                                       aPlaneEquation.y() > 0.0 ? aBBox.CornerMin().y() : aBBox.CornerMax().y(),
-                                       aPlaneEquation.z() > 0.0 ? aBBox.CornerMin().z() : aBBox.CornerMax().z(),
-                                       1.0);
-        if (aPlaneEquation.Dot (aMinPnt) > 0.0) // min vertex is inside the half-space
+        else if (aBoxState == Graphic3d_ClipState_Out && myBndBoxClipCheck)
         {
-          aCtx->ChangeClipping().SetEnabled (aCtx, aPlaneIt, Standard_False);
-          hasDisabled = true;
+          isClipped = true;
+          break;
         }
       }
     }
@@ -663,9 +572,9 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
   if (hasDisabled)
   {
     // enable planes that were previously disabled
-    aCtx->ChangeClipping().RestoreDisabled (aCtx);
+    aCtx->ChangeClipping().RestoreDisabled();
   }
-  aCtx->ChangeClipping().SetLocalPlanes (aCtx, Handle(Graphic3d_SequenceOfHClipPlane)());
+  aCtx->ChangeClipping().SetLocalPlanes (Handle(Graphic3d_SequenceOfHClipPlane)());
   if ((!myClipPlanes.IsNull() && !myClipPlanes->IsEmpty())
     || hasDisabled)
   {
@@ -676,22 +585,23 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
   // Restore local transformation
   aCtx->ModelWorldState.Pop();
   aCtx->SetGlNormalizeEnabled (anOldGlNormalize);
-  if (!myTrsfPers.IsNull())
-  {
-    aCtx->WorldViewState.Pop();
-  }
 
   // Restore aspects
-  theWorkspace->SetAspectLine   (aPrevAspectLine);
-  theWorkspace->SetAspectFace   (aPrevAspectFace);
-  theWorkspace->SetAspectMarker (aPrevAspectMarker);
-  theWorkspace->SetAspectText   (aPrevAspectText);
+  theWorkspace->SetAspects (aPrevAspectFace);
 
   // Apply highlight box
-  if (!myHighlightBox.IsNull())
+  if (!isClipped
+   && !myHighlightStyle.IsNull()
+   &&  myHighlightStyle->Method() == Aspect_TOHM_BOUNDBOX)
   {
+    aCtx->ApplyModelViewMatrix();
     theWorkspace->SetHighlightStyle (myHighlightStyle);
-    myHighlightBox->Render (theWorkspace);
+    renderBoundingBox (theWorkspace);
+  }
+
+  if (!myTrsfPers.IsNull())
+  {
+    aCtx->WorldViewState.Pop();
   }
 
   // Restore named status
@@ -706,7 +616,6 @@ void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx)
 {
   // Release groups
   Clear (theGlCtx);
-  clearHighlightBox (theGlCtx);
   myHighlightStyle.Nullify();
 }
 
@@ -720,10 +629,6 @@ void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCt
   {
     aGroupIter.ChangeValue()->Release (theGlCtx);
   }
-  if (!myHighlightBox.IsNull())
-  {
-    myHighlightBox->Release (theGlCtx.operator->());
-  }
 }
 
 //=======================================================================