0027670: Visualization - avoid duplication of structures defining primitive array...
[occt.git] / src / OpenGl / OpenGl_View.cxx
index b572396..39037a7 100644 (file)
@@ -34,6 +34,8 @@
 #include <OpenGl_Workspace.hxx>
 #include <Standard_CLocaleSentry.hxx>
 
+IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView)
+
 #ifdef HAVE_GL2PS
 #include <gl2ps.h>
 #endif
@@ -43,8 +45,7 @@
 namespace
 {
   static const OPENGL_ZCLIP myDefaultZClip = { { Standard_False, 0.F }, { Standard_False, 1.F } };
-  static const OPENGL_FOG   myDefaultFog   = { Standard_False, 0.F, 1.F, { { 0.F, 0.F, 0.F, 1.F } } };
-  static const TEL_COLOUR   myDefaultBg    = { { 0.F, 0.F, 0.F, 1.F } };
+  static const OPENGL_FOG   myDefaultFog   = { Standard_False, 0.F, 1.F, OpenGl_Vec4 (0.0f, 0.0f, 0.0f, 1.0f) };
 }
 
 // =======================================================================
@@ -64,20 +65,22 @@ OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr,
   myAntiAliasing   (Standard_False),
   myCulling        (Standard_True),
   myShadingModel   (Graphic3d_TOSM_FACET),
-  mySurfaceDetail  (Graphic3d_TOD_ALL),
   myBackfacing     (Graphic3d_TOBM_AUTOMATIC),
-  myBgColor        (myDefaultBg),
+  myBgColor        (Quantity_NOC_BLACK),
   myFog            (myDefaultFog),
   myZClip          (myDefaultZClip),
   myCamera         (new Graphic3d_Camera()),
-  myFBO            (NULL),
   myUseGLLight     (Standard_True),
   myToShowTrihedron      (false),
   myToShowGradTrihedron  (false),
   myStateCounter         (theCounter),
   myLastLightSourceState (0, 0),
+  myFboColorFormat       (GL_RGBA8),
+  myFboDepthFormat       (GL_DEPTH24_STENCIL8),
+  myToFlipOutput         (Standard_False),
   myFrameCounter         (0),
   myHasFboBlit           (Standard_True),
+  myToDisableMSAA        (Standard_False),
   myTransientDrawToFront (Standard_True),
   myBackBufferRestored   (Standard_False),
   myIsImmediateDrawn     (Standard_False),
@@ -101,11 +104,25 @@ OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr,
     if (sscanf (anAaEnv, "%d", &v) > 0) myAntiAliasing = v;
   }
 
+  OpenGl_Light       aLight;
+  aLight.Type        = Graphic3d_TOLS_AMBIENT;
+  aLight.IsHeadlight = Standard_False;
+  aLight.Color.r()   = 1.;
+  aLight.Color.g()   = 1.;
+  aLight.Color.b()   = 1.;
+  myNoShadingLight.Append (aLight);
+
   myCurrLightSourceState  = myStateCounter->Increment();
   myMainSceneFbos[0]      = new OpenGl_FrameBuffer();
   myMainSceneFbos[1]      = new OpenGl_FrameBuffer();
   myImmediateSceneFbos[0] = new OpenGl_FrameBuffer();
   myImmediateSceneFbos[1] = new OpenGl_FrameBuffer();
+  myOpenGlFBO             = new OpenGl_FrameBuffer();
+  myOpenGlFBO2            = new OpenGl_FrameBuffer();
+  myRaytraceFBO1[0]       = new OpenGl_FrameBuffer();
+  myRaytraceFBO1[1]       = new OpenGl_FrameBuffer();
+  myRaytraceFBO2[0]       = new OpenGl_FrameBuffer();
+  myRaytraceFBO2[1]       = new OpenGl_FrameBuffer();
 }
 
 // =======================================================================
@@ -152,6 +169,8 @@ void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
   myMainSceneFbos[1]     ->Release (theCtx.operator->());
   myImmediateSceneFbos[0]->Release (theCtx.operator->());
   myImmediateSceneFbos[1]->Release (theCtx.operator->());
+  myOpenGlFBO            ->Release (theCtx.operator->());
+  myOpenGlFBO2           ->Release (theCtx.operator->());
   myFullScreenQuad        .Release (theCtx.operator->());
   myFullScreenQuadFlip    .Release (theCtx.operator->());
 
@@ -381,7 +400,7 @@ Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3
 // =======================================================================
 Aspect_Background OpenGl_View::Background() const
 {
-  return Aspect_Background (Quantity_Color (myBgColor.rgb[0], myBgColor.rgb[1], myBgColor.rgb[2], Quantity_TOC_RGB));
+  return Aspect_Background (myBgColor.GetRGB());
 }
 
 // =======================================================================
@@ -390,11 +409,8 @@ Aspect_Background OpenGl_View::Background() const
 // =======================================================================
 void OpenGl_View::SetBackground (const Aspect_Background& theBackground)
 {
-  Quantity_Color aBgColor = theBackground.Color();
-  myBgColor.rgb[0] = static_cast<float> (aBgColor.Red());
-  myBgColor.rgb[1] = static_cast<float> (aBgColor.Green());
-  myBgColor.rgb[2] = static_cast<float> (aBgColor.Blue());
-  myFog.Color      = myBgColor;
+  myBgColor.SetRGB (theBackground.Color());
+  myFog.Color = myBgColor;
 }
 
 // =======================================================================
@@ -441,6 +457,7 @@ void OpenGl_View::SetBackgroundImage (const TCollection_AsciiString& theFilePath
                                         Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
   anAspect->SetTextureMap (aTextureMap);
   anAspect->SetInteriorStyle (Aspect_IS_SOLID);
+  anAspect->SetSuppressBackFaces (false);
   // Enable texture mapping
   if (aTextureMap->IsDone())
   {
@@ -502,30 +519,115 @@ void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId        theLayerId,
   myZLayers.SetLayerSettings (theLayerId, theSettings);
 }
 
+//=======================================================================
+//function : ZLayerMax
+//purpose  :
+//=======================================================================
+Standard_Integer OpenGl_View::ZLayerMax() const
+{
+  Standard_Integer aLayerMax = Graphic3d_ZLayerId_Default;
+  for (OpenGl_LayerSeqIds::Iterator aMapIt(myZLayers.LayerIDs()); aMapIt.More(); aMapIt.Next())
+  {
+    aLayerMax = Max (aLayerMax, aMapIt.Value());
+  }
+
+  return aLayerMax;
+}
+
+//=======================================================================
+//function : InvalidateZLayerBoundingBox
+//purpose  :
+//=======================================================================
+void OpenGl_View::InvalidateZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId) const
+{
+  if (myZLayers.LayerIDs().IsBound (theLayerId))
+  {
+    myZLayers.Layer (theLayerId).InvalidateBoundingBox();
+  }
+  else
+  {
+    for (Standard_Integer aLayerId = Graphic3d_ZLayerId_Default; aLayerId < ZLayerMax(); ++aLayerId)
+    {
+      if (myZLayers.LayerIDs().IsBound (aLayerId))
+      {
+        const OpenGl_Layer& aLayer = myZLayers.Layer (aLayerId);
+        if (aLayer.NbOfTransformPersistenceObjects() > 0)
+        {
+          aLayer.InvalidateBoundingBox();
+        }
+      }
+    }
+  }
+}
+
+//=======================================================================
+//function : ZLayerBoundingBox
+//purpose  :
+//=======================================================================
+Graphic3d_BndBox4f OpenGl_View::ZLayerBoundingBox (const Graphic3d_ZLayerId        theLayerId,
+                                                   const Handle(Graphic3d_Camera)& theCamera,
+                                                   const Standard_Integer          theWindowWidth,
+                                                   const Standard_Integer          theWindowHeight,
+                                                   const Standard_Boolean          theToIgnoreInfiniteFlag) const
+{
+  if (myZLayers.LayerIDs().IsBound (theLayerId))
+  {
+    return myZLayers.Layer (theLayerId).BoundingBox (Identification(),
+                                                     theCamera,
+                                                     theWindowWidth,
+                                                     theWindowHeight,
+                                                     theToIgnoreInfiniteFlag);
+  }
+
+  return Graphic3d_BndBox4f();
+}
+
+//=======================================================================
+//function : considerZoomPersistenceObjects
+//purpose  :
+//=======================================================================
+Standard_Real OpenGl_View::considerZoomPersistenceObjects (const Graphic3d_ZLayerId        theLayerId,
+                                                           const Handle(Graphic3d_Camera)& theCamera,
+                                                           const Standard_Integer          theWindowWidth,
+                                                           const Standard_Integer          theWindowHeight,
+                                                           const Standard_Boolean          theToIgnoreInfiniteFlag) const
+{
+  if (myZLayers.LayerIDs().IsBound (theLayerId))
+  {
+    return myZLayers.Layer (theLayerId).considerZoomPersistenceObjects (Identification(),
+                                                                        theCamera,
+                                                                        theWindowWidth,
+                                                                        theWindowHeight,
+                                                                        theToIgnoreInfiniteFlag);
+  }
+
+  return 1.0;
+}
+
 //=======================================================================
 //function : FBO
 //purpose  :
 //=======================================================================
-Graphic3d_PtrFrameBuffer OpenGl_View::FBO() const
+Handle(Standard_Transient) OpenGl_View::FBO() const
 {
-  return reinterpret_cast<Graphic3d_PtrFrameBuffer> (myFBO);
+  return Handle(Standard_Transient)(myFBO);
 }
 
 //=======================================================================
 //function : SetFBO
 //purpose  :
 //=======================================================================
-void OpenGl_View::SetFBO (const Graphic3d_PtrFrameBuffer theFBO)
+void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo)
 {
-  myFBO = reinterpret_cast<OpenGl_FrameBuffer*> (theFBO);
+  myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
 }
 
 //=======================================================================
 //function : FBOCreate
 //purpose  :
 //=======================================================================
-Graphic3d_PtrFrameBuffer OpenGl_View::FBOCreate (const Standard_Integer theWidth,
-                                                 const Standard_Integer theHeight)
+Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth,
+                                                   const Standard_Integer theHeight)
 {
   return myWorkspace->FBOCreate (theWidth, theHeight);
 }
@@ -534,22 +636,34 @@ Graphic3d_PtrFrameBuffer OpenGl_View::FBOCreate (const Standard_Integer theWidth
 //function : FBORelease
 //purpose  :
 //=======================================================================
-void OpenGl_View::FBORelease (Graphic3d_PtrFrameBuffer& theFBOPtr)
+void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo)
 {
-  myWorkspace->FBORelease (theFBOPtr);
+  Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
+  if (aFrameBuffer.IsNull())
+  {
+    return;
+  }
+
+  myWorkspace->FBORelease (aFrameBuffer);
+  theFbo.Nullify();
 }
 
 //=======================================================================
 //function : FBOGetDimensions
 //purpose  :
 //=======================================================================
-void OpenGl_View::FBOGetDimensions (const Graphic3d_PtrFrameBuffer theFBOPtr,
+void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
                                     Standard_Integer& theWidth,
                                     Standard_Integer& theHeight,
                                     Standard_Integer& theWidthMax,
                                     Standard_Integer& theHeightMax)
 {
-  const OpenGl_FrameBuffer* aFrameBuffer = (const OpenGl_FrameBuffer* )theFBOPtr;
+  const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
+  if (aFrameBuffer.IsNull())
+  {
+    return;
+  }
+
   theWidth     = aFrameBuffer->GetVPSizeX(); // current viewport size
   theHeight    = aFrameBuffer->GetVPSizeY();
   theWidthMax  = aFrameBuffer->GetSizeX(); // texture size
@@ -560,11 +674,16 @@ void OpenGl_View::FBOGetDimensions (const Graphic3d_PtrFrameBuffer theFBOPtr,
 //function : FBOChangeViewport
 //purpose  :
 //=======================================================================
-void OpenGl_View::FBOChangeViewport (Graphic3d_PtrFrameBuffer& theFBOPtr,
+void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
                                      const Standard_Integer theWidth,
                                      const Standard_Integer theHeight)
 {
-  OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theFBOPtr;
+  const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
+  if (aFrameBuffer.IsNull())
+  {
+    return;
+  }
+
   aFrameBuffer->ChangeViewport (theWidth, theHeight);
 }
 
@@ -706,6 +825,8 @@ void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure
   const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
   const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
   myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId);
+  Update (Aspect_TOU_WAIT, anOldLayer);
+  Update (Aspect_TOU_WAIT, theNewLayerId);
 }
 
 //=======================================================================