0027961: Visualization - remove unused and no more working OpenGl_AVIWriter
[occt.git] / src / OpenGl / OpenGl_Structure.cxx
index 05d0dd6..608050e 100644 (file)
@@ -73,7 +73,7 @@ public:
     glDisable (GL_LIGHTING);
 
     // Use highlight colors
-    theWorkspace->GetGlContext()->core11->glColor3fv (theWorkspace->LineColor().rgb);
+    theWorkspace->GetGlContext()->core11->glColor3fv (theWorkspace->LineColor().GetData());
 
     glEnableClientState (GL_VERTEX_ARRAY);
     glVertexPointer (3, GL_FLOAT, 0, (GLfloat* )&myVerts);
@@ -119,7 +119,6 @@ public:
 // =======================================================================
 OpenGl_Structure::OpenGl_Structure (const Handle(Graphic3d_StructureManager)& theManager)
 : Graphic3d_CStructure (theManager),
-  myHighlightColor     (NULL),
   myInstancedStructure (NULL),
   myIsRaytracable      (Standard_False),
   myModificationState  (0),
@@ -139,19 +138,21 @@ OpenGl_Structure::~OpenGl_Structure()
 }
 
 // =======================================================================
-// function : UpdateTransformation
+// function : SetTransformation
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::UpdateTransformation()
+void OpenGl_Structure::SetTransformation (const Handle(Geom_Transformation)& theTrsf)
 {
-  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;
+  myTrsf = theTrsf;
+  myIsMirrored = Standard_False;
+  if (!myTrsf.IsNull())
+  {
+    // 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;
+  }
 
   if (IsRaytracable())
   {
@@ -172,33 +173,13 @@ void OpenGl_Structure::clearHighlightBox (const Handle(OpenGl_Context)& theGlCtx
   }
 }
 
-// =======================================================================
-// function : HighlightWithColor
-// purpose  :
-// =======================================================================
-void OpenGl_Structure::HighlightWithColor (const Graphic3d_Vec3&  theColor,
-                                           const Standard_Boolean theToCreate)
-{
-  const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
-  if (theToCreate)
-    setHighlightColor   (aContext, theColor);
-  else
-    clearHighlightColor (aContext);
-}
-
 // =======================================================================
 // function : HighlightWithBndBox
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::HighlightWithBndBox (const Handle(Graphic3d_Structure)& theStruct,
-                                            const Standard_Boolean             theToCreate)
+void OpenGl_Structure::highlightWithBndBox (const Handle(Graphic3d_Structure)& theStruct)
 {
   const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
-  if (!theToCreate)
-  {
-    clearHighlightBox (aContext);
-    return;
-  }
 
   if (!myHighlightBox.IsNull())
   {
@@ -209,45 +190,43 @@ void OpenGl_Structure::HighlightWithBndBox (const Handle(Graphic3d_Structure)& t
     myHighlightBox = new OpenGl_Group (theStruct);
   }
 
-  CALL_DEF_CONTEXTLINE& aContextLine = myHighlightBox->ChangeContextLine();
-  aContextLine.IsDef    = 1;
-  aContextLine.Color    = HighlightColor;
-  aContextLine.LineType = Aspect_TOL_SOLID;
-  aContextLine.Width    = 1.0f;
-  myHighlightBox->UpdateAspectLine (Standard_True);
+  myHighlightBox->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d (myHighlightStyle->Color(), Aspect_TOL_SOLID, 1.0));
 
   OpenGl_BndBoxPrs* aBndBoxPrs = new OpenGl_BndBoxPrs (myBndBox);
   myHighlightBox->AddElement (aBndBoxPrs);
 }
 
 // =======================================================================
-// 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_HighlightStyle)& theStyle,
+                                         const Handle(Graphic3d_Structure)&      theStruct)
 {
-  clearHighlightBox (theGlCtx);
-  if (myHighlightColor == NULL)
+  myHighlightStyle = theStyle;
+
+  highlight = 1;
+  if (myHighlightStyle->Method() == Aspect_TOHM_BOUNDBOX)
   {
-    myHighlightColor = new TEL_COLOUR();
+    highlightWithBndBox (theStruct);
   }
-
-  myHighlightColor->rgb[0] = theColor.r();
-  myHighlightColor->rgb[1] = theColor.g();
-  myHighlightColor->rgb[2] = theColor.b();
-  myHighlightColor->rgb[3] = 1.F;
 }
 
 // =======================================================================
-// 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;
+
+  if (myHighlightStyle->Method() == Aspect_TOHM_BOUNDBOX)
+  {
+    const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
+    clearHighlightBox (aContext);
+  }
+
+  myHighlightStyle.Nullify();
 }
 
 // =======================================================================
@@ -268,16 +247,14 @@ void OpenGl_Structure::OnVisibilityChanged()
 // =======================================================================
 Standard_Boolean OpenGl_Structure::IsRaytracable() const
 {
-  if (!myGroups.IsEmpty())
+  if (!myGroups.IsEmpty()
+    && myIsRaytracable)
   {
-    return myIsRaytracable; // geometry structure
-  }
-  else if (myInstancedStructure != NULL)
-  {
-    return myInstancedStructure->IsRaytracable(); // instance structure
+    return Standard_True;
   }
 
-  return Standard_False; // has no any groups or structures
+  return myInstancedStructure != NULL
+     &&  myInstancedStructure->IsRaytracable();
 }
 
 // =======================================================================
@@ -286,7 +263,18 @@ Standard_Boolean OpenGl_Structure::IsRaytracable() const
 // =======================================================================
 void OpenGl_Structure::UpdateStateIfRaytracable (const Standard_Boolean toCheck) const
 {
-  myIsRaytracable = !toCheck || OpenGl_Raytrace::IsRaytracedStructure (this);
+  myIsRaytracable = !toCheck;
+  if (!myIsRaytracable)
+  {
+    for (OpenGl_Structure::GroupIterator anIter (myGroups); anIter.More(); anIter.Next())
+    {
+      if (anIter.Value()->IsRaytracable())
+      {
+        myIsRaytracable = Standard_True;
+        break;
+      }
+    }
+  }
 
   if (IsRaytracable())
   {
@@ -430,26 +418,6 @@ void OpenGl_Structure::renderGeometry (const Handle(OpenGl_Workspace)& theWorksp
   }
 }
 
-// =======================================================================
-// function : renderClosedGeometry
-// purpose  :
-// =======================================================================
-void OpenGl_Structure::renderClosedGeometry (const Handle(OpenGl_Workspace)& theWorkspace) const
-{
-  if (myInstancedStructure != NULL)
-  {
-    myInstancedStructure->renderClosedGeometry (theWorkspace);
-  }
-
-  for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next())
-  {
-    if (aGroupIter.Value()->IsClosed())
-    {
-      aGroupIter.Value()->Render (theWorkspace);
-    }
-  }
-}
-
 // =======================================================================
 // function : Render
 // purpose  :
@@ -465,40 +433,46 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
   const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
 
   // Render named status
-  if (highlight)
+  if (highlight && myHighlightBox.IsNull())
   {
     theWorkspace->SetHighlight (true);
   }
 
   // Apply local transformation
   aCtx->ModelWorldState.Push();
-  aCtx->ModelWorldState.SetCurrent (Transformation);
+  OpenGl_Mat4& aModelWorld = aCtx->ModelWorldState.ChangeCurrent();
+  if (!myTrsf.IsNull())
+  {
+    myTrsf->Trsf().GetMat4 (aModelWorld);
+  }
+  else
+  {
+    aModelWorld.InitIdentity();
+  }
 
   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);
     }
   }
 #endif
 
-  if (TransformPersistence.Flags)
+  if (!myTrsfPers.IsNull())
   {
-    OpenGl_Mat4 aProjection = aCtx->ProjectionState.Current();
-    OpenGl_Mat4 aWorldView  = aCtx->WorldViewState.Current();
-    TransformPersistence.Apply (aProjection, aWorldView, theWorkspace->Width(), theWorkspace->Height());
+    OpenGl_Mat4 aWorldView = aCtx->WorldViewState.Current();
+    myTrsfPers->Apply (theWorkspace->View()->Camera(), aCtx->ProjectionState.Current(), aWorldView,
+                       aCtx->Viewport()[2], aCtx->Viewport()[3]);
 
-    aCtx->ProjectionState.Push();
     aCtx->WorldViewState.Push();
-    aCtx->ProjectionState.SetCurrent (aProjection);
     aCtx->WorldViewState.SetCurrent (aWorldView);
-    aCtx->ApplyProjectionMatrix();
 
   #if !defined(GL_ES_VERSION_2_0)
     if (!aCtx->IsGlNormalizeEnabled()
@@ -529,50 +503,109 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
   }
 
   // Apply highlight color
-  const TEL_COLOUR *aHighlightColor = theWorkspace->HighlightColor;
-  if (myHighlightColor)
-    theWorkspace->HighlightColor = myHighlightColor;
-
-  // Set up plane equations for non-structure transformed global model-view matrix
-  // List of planes to be applied to context state
-  NCollection_Handle<Graphic3d_SequenceOfHClipPlane> aUserPlanes;
+  const OpenGl_Vec4* aHighlightColor = theWorkspace->HighlightColor;
+  if (!myHighlightStyle.IsNull())
+    theWorkspace->HighlightColor = myHighlightStyle->ColorFltPtr();
 
   // Collect clipping planes of structure scope
-  if (!myClipPlanes.IsEmpty())
+  aCtx->ChangeClipping().SetLocalPlanes (aCtx, myClipPlanes);
+
+  // True if structure is fully clipped
+  bool isClipped = false;
+  bool hasDisabled = false;
+  if (aCtx->Clipping().IsClippingOrCappingOn())
   {
-    Graphic3d_SequenceOfHClipPlane::Iterator aClippingIter (myClipPlanes);
-    for (; aClippingIter.More(); aClippingIter.Next())
+    const Graphic3d_BndBox4f& aBBox = BoundingBox();
+    if (!myClipPlanes.IsNull()
+      && myClipPlanes->ToOverrideGlobal())
     {
-      const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIter.Value();
-      if (!aClipPlane->IsOn())
+      aCtx->ChangeClipping().DisableGlobal (aCtx);
+      hasDisabled = aCtx->Clipping().HasDisabled();
+    }
+    else if (!myTrsfPers.IsNull())
+    {
+      if (myTrsfPers->IsZoomOrRotate())
       {
-        continue;
+        // 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& 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
+          {
+            isClipped = true;
+            break;
+          }
+        }
       }
 
-      if (aUserPlanes.IsNull())
+      aCtx->ChangeClipping().DisableGlobal (aCtx);
+      hasDisabled = aCtx->Clipping().HasDisabled();
+    }
+
+    // Set of clipping planes that do not intersect the structure,
+    // and thus can be disabled to improve rendering performance
+    if (aBBox.IsValid()
+     && myTrsfPers.IsNull())
+    {
+      for (OpenGl_ClippingIterator aPlaneIt (aCtx->Clipping()); aPlaneIt.More(); aPlaneIt.Next())
       {
-        aUserPlanes = new Graphic3d_SequenceOfHClipPlane();
+        const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
+        if (!aPlane->IsOn())
+        {
+          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
+        {
+          isClipped = true;
+          break;
+        }
+
+        // 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
+        {
+          aCtx->ChangeClipping().SetEnabled (aCtx, aPlaneIt, Standard_False);
+          hasDisabled = true;
+        }
       }
-
-      aUserPlanes->Append (aClipPlane);
     }
-  }
-
-  if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty())
-  {
-    // add planes at loaded view matrix state
-    aCtx->ChangeClipping().AddWorld (aCtx, *aUserPlanes);
 
-    // Set OCCT state uniform variables
-    if (!aCtx->ShaderManager()->IsEmpty())
+    if ((!myClipPlanes.IsNull() && !myClipPlanes->IsEmpty())
+     || hasDisabled)
     {
+      // Set OCCT state uniform variables
       aCtx->ShaderManager()->UpdateClippingState();
     }
   }
 
   // Render groups
   bool hasClosedPrims = false;
-  renderGeometry (theWorkspace, hasClosedPrims);
+  if (!isClipped)
+  {
+    renderGeometry (theWorkspace, hasClosedPrims);
+  }
 
   // Reset correction for mirror transform
   if (myIsMirrored)
@@ -582,31 +615,31 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) con
 
   // Render capping for structure groups
   if (hasClosedPrims
-  && !aCtx->Clipping().Planes().IsEmpty())
+   && aCtx->Clipping().IsCappingOn())
   {
     OpenGl_CappingAlgo::RenderCapping (theWorkspace, *this);
   }
 
   // Revert structure clippings
-  if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty())
+  if (hasDisabled)
+  {
+    // enable planes that were previously disabled
+    aCtx->ChangeClipping().RestoreDisabled (aCtx);
+  }
+  aCtx->ChangeClipping().SetLocalPlanes (aCtx, Handle(Graphic3d_SequenceOfHClipPlane)());
+  if ((!myClipPlanes.IsNull() && !myClipPlanes->IsEmpty())
+    || hasDisabled)
   {
-    aCtx->ChangeClipping().Remove (aCtx, *aUserPlanes);
-
     // Set OCCT state uniform variables
-    if (!aCtx->ShaderManager()->IsEmpty())
-    {
-      aCtx->ShaderManager()->RevertClippingState();
-    }
+    aCtx->ShaderManager()->RevertClippingState();
   }
 
   // Restore local transformation
   aCtx->ModelWorldState.Pop();
   aCtx->SetGlNormalizeEnabled (anOldGlNormalize);
-  if (TransformPersistence.Flags)
+  if (!myTrsfPers.IsNull())
   {
-    aCtx->ProjectionState.Pop();
     aCtx->WorldViewState.Pop();
-    aCtx->ApplyProjectionMatrix();
   }
 
   // Restore highlight color
@@ -636,7 +669,8 @@ void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx)
 {
   // Release groups
   Clear (theGlCtx);
-  clearHighlightColor (theGlCtx);
+  clearHighlightBox (theGlCtx);
+  myHighlightStyle.Nullify();
 }
 
 // =======================================================================