0030698: Volume Rendering - Early clipping of volume object's bounding box
[occt.git] / src / OpenGl / OpenGl_Structure.cxx
index 0921824..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_BndBox4f& theBndBox)
-  {
-    const float Xm = theBndBox.CornerMin().x();
-    const float Ym = theBndBox.CornerMin().y();
-    const float Zm = theBndBox.CornerMin().z();
-    const float XM = theBndBox.CornerMax().x();
-    const float YM = theBndBox.CornerMax().y();
-    const float ZM = 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();
-
-    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
@@ -119,14 +104,12 @@ public:
 // =======================================================================
 OpenGl_Structure::OpenGl_Structure (const Handle(Graphic3d_StructureManager)& theManager)
 : Graphic3d_CStructure (theManager),
-  myHighlightColor     (NULL),
   myInstancedStructure (NULL),
   myIsRaytracable      (Standard_False),
   myModificationState  (0),
-  myIsCulled           (Standard_True),
   myIsMirrored         (Standard_False)
 {
-  //
+  updateLayerTransformation();
 }
 
 // =======================================================================
@@ -139,109 +122,88 @@ OpenGl_Structure::~OpenGl_Structure()
 }
 
 // =======================================================================
-// function : UpdateTransformation
+// function : SetZLayer
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::UpdateTransformation()
+void OpenGl_Structure::SetZLayer (const Graphic3d_ZLayerId theLayerIndex)
 {
-  const OpenGl_Mat4& aMat = Graphic3d_CStructure::Transformation;
-  Standard_ShortReal aDet =
-    aMat.GetValue(0, 0) * (aMat.GetValue(1, 1) * aMat.GetValue(2, 2) - aMat.GetValue(2, 1) * aMat.GetValue(1, 2)) -
-    aMat.GetValue(0, 1) * (aMat.GetValue(1, 0) * aMat.GetValue(2, 2) - aMat.GetValue(2, 0) * aMat.GetValue(1, 2)) +
-    aMat.GetValue(0, 2) * (aMat.GetValue(1, 0) * aMat.GetValue(2, 1) - aMat.GetValue(2, 0) * aMat.GetValue(1, 1));
-
-  // Determinant of transform matrix less then 0 means that mirror transform applied.
-  myIsMirrored = aDet < 0.0f;
-
-  if (IsRaytracable())
-  {
-    ++myModificationState;
-  }
+  Graphic3d_CStructure::SetZLayer (theLayerIndex);
+  updateLayerTransformation();
 }
 
 // =======================================================================
-// function : clearHighlightBox
+// function : SetTransformation
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::clearHighlightBox (const Handle(OpenGl_Context)& theGlCtx)
+void OpenGl_Structure::SetTransformation (const Handle(Geom_Transformation)& theTrsf)
 {
-  if (!myHighlightBox.IsNull())
+  myTrsf = theTrsf;
+  myIsMirrored = Standard_False;
+  if (!myTrsf.IsNull())
   {
-    myHighlightBox->Release (theGlCtx);
-    myHighlightBox.Nullify();
+    // Determinant of transform matrix less then 0 means that mirror transform applied.
+    const Standard_Real aDet = myTrsf->Value(1, 1) * (myTrsf->Value (2, 2) * myTrsf->Value (3, 3) - myTrsf->Value (3, 2) * myTrsf->Value (2, 3))
+                             - myTrsf->Value(1, 2) * (myTrsf->Value (2, 1) * myTrsf->Value (3, 3) - myTrsf->Value (3, 1) * myTrsf->Value (2, 3))
+                             + myTrsf->Value(1, 3) * (myTrsf->Value (2, 1) * myTrsf->Value (3, 2) - myTrsf->Value (3, 1) * myTrsf->Value (2, 2));
+    myIsMirrored = aDet < 0.0;
+  }
+
+  updateLayerTransformation();
+  if (IsRaytracable())
+  {
+    ++myModificationState;
   }
 }
 
 // =======================================================================
-// function : HighlightWithColor
+// function : SetTransformPersistence
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::HighlightWithColor (const Graphic3d_Vec3&  theColor,
-                                           const Standard_Boolean theToCreate)
+void OpenGl_Structure::SetTransformPersistence (const Handle(Graphic3d_TransformPers)& theTrsfPers)
 {
-  const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
-  if (theToCreate)
-    setHighlightColor   (aContext, theColor);
-  else
-    clearHighlightColor (aContext);
+  myTrsfPers = theTrsfPers;
+  updateLayerTransformation();
 }
 
 // =======================================================================
-// function : HighlightWithBndBox
+// function : updateLayerTransformation
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::HighlightWithBndBox (const Handle(Graphic3d_Structure)& theStruct,
-                                            const Standard_Boolean             theToCreate)
+void OpenGl_Structure::updateLayerTransformation()
 {
-  const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
-  if (!theToCreate)
+  gp_Trsf aRenderTrsf;
+  if (!myTrsf.IsNull())
   {
-    clearHighlightBox (aContext);
-    return;
+    aRenderTrsf = myTrsf->Trsf();
   }
 
-  if (!myHighlightBox.IsNull())
-  {
-    myHighlightBox->Release (aContext);
-  }
-  else
+  const Graphic3d_ZLayerSettings& aLayer = myGraphicDriver->ZLayerSettings (myZLayer);
+  if (!aLayer.OriginTransformation().IsNull()
+    && myTrsfPers.IsNull())
   {
-    myHighlightBox = new OpenGl_Group (theStruct);
+    aRenderTrsf.SetTranslationPart (aRenderTrsf.TranslationPart() - aLayer.Origin());
   }
-
-  myHighlightBox->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d (HighlightColor, Aspect_TOL_SOLID, 1.0));
-
-  OpenGl_BndBoxPrs* aBndBoxPrs = new OpenGl_BndBoxPrs (myBndBox);
-  myHighlightBox->AddElement (aBndBoxPrs);
+  aRenderTrsf.GetMat4 (myRenderTrsf);
 }
 
 // =======================================================================
-// function : setHighlightColor
+// function : GraphicHighlight
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::setHighlightColor (const Handle(OpenGl_Context)& theGlCtx,
-                                          const Graphic3d_Vec3&         theColor)
+void OpenGl_Structure::GraphicHighlight (const Handle(Graphic3d_PresentationAttributes)& theStyle)
 {
-  clearHighlightBox (theGlCtx);
-  if (myHighlightColor == NULL)
-  {
-    myHighlightColor = new OpenGl_Vec4 (theColor, 1.0f);
-  }
-  else
-  {
-    myHighlightColor->xyz() = theColor;
-  }
+  myHighlightStyle = theStyle;
+  highlight = 1;
 }
 
 // =======================================================================
-// function : clearHighlightColor
+// function : GraphicUnhighlight
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::clearHighlightColor (const Handle(OpenGl_Context)& theGlCtx)
+void OpenGl_Structure::GraphicUnhighlight()
 {
-  clearHighlightBox(theGlCtx);
-  delete myHighlightColor;
-  myHighlightColor = NULL;
+  highlight = 0;
+  myHighlightStyle.Nullify();
 }
 
 // =======================================================================
@@ -448,23 +410,24 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
   const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
 
   // Render named status
-  if (highlight)
+  if (highlight && !myHighlightStyle.IsNull() && myHighlightStyle->Method() != Aspect_TOHM_BOUNDBOX)
   {
-    theWorkspace->SetHighlight (true);
+    theWorkspace->SetHighlightStyle (myHighlightStyle);
   }
 
   // Apply local transformation
   aCtx->ModelWorldState.Push();
-  aCtx->ModelWorldState.SetCurrent (Transformation);
+  OpenGl_Mat4& aModelWorld = aCtx->ModelWorldState.ChangeCurrent();
+  aModelWorld = myRenderTrsf;
 
   const Standard_Boolean anOldGlNormalize = aCtx->IsGlNormalizeEnabled();
-
 #if !defined(GL_ES_VERSION_2_0)
   // detect scale transform
-  if (aCtx->core11 != NULL)
+  if (aCtx->core11 != NULL
+  && !myTrsf.IsNull())
   {
-    const Standard_ShortReal aScaleX = Transformation.GetRow (0).xyz().SquareModulus();
-    if (Abs (aScaleX - 1.f) > Precision::Confusion())
+    const Standard_Real aScale = myTrsf->ScaleFactor();
+    if (Abs (aScale - 1.0) > Precision::Confusion())
     {
       aCtx->SetGlNormalizeEnabled (Standard_True);
     }
@@ -473,19 +436,18 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
 
   if (!myTrsfPers.IsNull())
   {
-    OpenGl_Mat4 aWorldView = aCtx->WorldViewState.Current();
-    myTrsfPers->Apply (theWorkspace->View()->Camera(), aCtx->ProjectionState.Current(), aWorldView,
-                       aCtx->Viewport()[2], aCtx->Viewport()[3]);
-
     aCtx->WorldViewState.Push();
-    aCtx->WorldViewState.SetCurrent (aWorldView);
+    OpenGl_Mat4& aWorldView = aCtx->WorldViewState.ChangeCurrent();
+    myTrsfPers->Apply (theWorkspace->View()->Camera(),
+                       aCtx->ProjectionState.Current(), aWorldView,
+                       aCtx->VirtualViewport()[2], aCtx->VirtualViewport()[3]);
 
   #if !defined(GL_ES_VERSION_2_0)
     if (!aCtx->IsGlNormalizeEnabled()
       && aCtx->core11 != NULL)
     {
       const Standard_Real aScale = Graphic3d_TransformUtils::ScaleFactor<Standard_ShortReal> (aWorldView);
-      if (Abs (aScale - 1.0f) > Precision::Confusion())
+      if (Abs (aScale - 1.0) > Precision::Confusion())
       {
         aCtx->SetGlNormalizeEnabled (Standard_True);
       }
@@ -497,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)
@@ -508,24 +467,49 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
     aCtx->core11fwd->glFrontFace (GL_CW);
   }
 
-  // Apply highlight color
-  const OpenGl_Vec4* aHighlightColor = theWorkspace->HighlightColor;
-  if (myHighlightColor)
-    theWorkspace->HighlightColor = myHighlightColor;
-
   // Collect clipping planes of structure scope
-  aCtx->ChangeClipping().SetLocalPlanes (aCtx, myClipPlanes);
+  aCtx->ChangeClipping().SetLocalPlanes (myClipPlanes);
 
   // True if structure is fully clipped
   bool isClipped = false;
   bool hasDisabled = false;
   if (aCtx->Clipping().IsClippingOrCappingOn())
   {
-    const Graphic3d_BndBox4f& aBBox = BoundingBox();
-    if ((!myTrsfPers.IsNull() && myTrsfPers->IsTrihedronOr2d())
-     || (!myClipPlanes.IsNull() && myClipPlanes->ToOverrideGlobal()))
+    const Graphic3d_BndBox3d& aBBox = BoundingBox();
+    if (!myClipPlanes.IsNull()
+      && myClipPlanes->ToOverrideGlobal())
+    {
+      aCtx->ChangeClipping().DisableGlobal();
+      hasDisabled = aCtx->Clipping().HasDisabled();
+    }
+    else if (!myTrsfPers.IsNull())
     {
-      aCtx->ChangeClipping().DisableGlobal (aCtx);
+      if (myTrsfPers->IsZoomOrRotate())
+      {
+        // Zoom/rotate persistence object lives in two worlds at the same time.
+        // Global clipping planes can not be trivially applied without being converted
+        // into local space of transformation persistence object.
+        // As more simple alternative - just clip entire object by its anchor point defined in the world space.
+        const gp_Pnt anAnchor = myTrsfPers->AnchorPoint();
+        for (OpenGl_ClippingIterator aPlaneIt (aCtx->Clipping()); aPlaneIt.More() && aPlaneIt.IsGlobal(); aPlaneIt.Next())
+        {
+          const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
+          if (!aPlane->IsOn())
+          {
+            continue;
+          }
+
+          // check for clipping
+          const Graphic3d_Vec4d aCheckPnt (anAnchor.X(), anAnchor.Y(), anAnchor.Z(), 1.0);
+          if (aPlane->ProbePoint (aCheckPnt) == Graphic3d_ClipState_Out)
+          {
+            isClipped = true;
+            break;
+          }
+        }
+      }
+
+      aCtx->ChangeClipping().DisableGlobal();
       hasDisabled = aCtx->Clipping().HasDisabled();
     }
 
@@ -537,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;
         }
       }
     }
@@ -599,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)
   {
@@ -612,28 +585,27 @@ 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 highlight color
-  theWorkspace->HighlightColor = aHighlightColor;
 
   // 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)
   {
-    myHighlightBox->Render (theWorkspace);
+    aCtx->ApplyModelViewMatrix();
+    theWorkspace->SetHighlightStyle (myHighlightStyle);
+    renderBoundingBox (theWorkspace);
+  }
+
+  if (!myTrsfPers.IsNull())
+  {
+    aCtx->WorldViewState.Pop();
   }
 
   // Restore named status
-  theWorkspace->SetHighlight (false);
+  theWorkspace->SetHighlightStyle (Handle(Graphic3d_PresentationAttributes)());
 }
 
 // =======================================================================
@@ -644,7 +616,7 @@ void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx)
 {
   // Release groups
   Clear (theGlCtx);
-  clearHighlightColor (theGlCtx);
+  myHighlightStyle.Nullify();
 }
 
 // =======================================================================
@@ -657,10 +629,6 @@ void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCt
   {
     aGroupIter.ChangeValue()->Release (theGlCtx);
   }
-  if (!myHighlightBox.IsNull())
-  {
-    myHighlightBox->Release (theGlCtx.operator->());
-  }
 }
 
 //=======================================================================