0030748: Visualization - Marker displayed in immediate layer ruins QT Quick view...
[occt.git] / src / OpenGl / OpenGl_View.cxx
index c60c684..a3a388b 100644 (file)
 #include <OpenGl_Texture.hxx>
 #include <OpenGl_Window.hxx>
 #include <OpenGl_Workspace.hxx>
+#include <OSD_Parallel.hxx>
 #include <Standard_CLocaleSentry.hxx>
 
-IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView)
+#include "../Graphic3d/Graphic3d_Structure.pxx"
 
-#ifdef HAVE_GL2PS
-#include <gl2ps.h>
-#endif
+IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView)
 
 // =======================================================================
 // function : Constructor
@@ -46,61 +45,69 @@ IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView)
 OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr,
                           const Handle(OpenGl_GraphicDriver)& theDriver,
                           const Handle(OpenGl_Caps)& theCaps,
-                          Standard_Boolean& theDeviceLostFlag,
                           OpenGl_StateCounter* theCounter)
 : Graphic3d_CView  (theMgr),
   myDriver         (theDriver.operator->()),
   myCaps           (theCaps),
-  myDeviceLostFlag (theDeviceLostFlag),
   myWasRedrawnGL   (Standard_False),
-  myCulling        (Standard_True),
-  myShadingModel   (Graphic3d_TOSM_FACET),
   myBackfacing     (Graphic3d_TOBM_AUTOMATIC),
-  myBgColor        (Quantity_NOC_BLACK),
-  myCamera         (new Graphic3d_Camera()),
   myToShowGradTrihedron  (false),
+  myZLayers        (Structure_MAX_PRIORITY - Structure_MIN_PRIORITY + 1),
   myStateCounter         (theCounter),
+  myCurrLightSourceState (theCounter->Increment()),
+  myLightsRevision       (0),
   myLastLightSourceState (0, 0),
   myFboColorFormat       (GL_RGBA8),
   myFboDepthFormat       (GL_DEPTH24_STENCIL8),
   myToFlipOutput         (Standard_False),
   myFrameCounter         (0),
   myHasFboBlit           (Standard_True),
+  myToDisableOIT         (Standard_False),
+  myToDisableOITMSAA     (Standard_False),
   myToDisableMSAA        (Standard_False),
   myTransientDrawToFront (Standard_True),
   myBackBufferRestored   (Standard_False),
   myIsImmediateDrawn     (Standard_False),
-  myTextureParams   (new OpenGl_AspectFace()),
+  myTextureParams   (new OpenGl_Aspects()),
   myBgGradientArray (new OpenGl_BackgroundArray (Graphic3d_TOB_GRADIENT)),
   myBgTextureArray  (new OpenGl_BackgroundArray (Graphic3d_TOB_TEXTURE)),
   // ray-tracing fields initialization
   myRaytraceInitStatus     (OpenGl_RT_NONE),
   myIsRaytraceDataValid    (Standard_False),
   myIsRaytraceWarnTextures (Standard_False),
+  myRaytraceBVHBuilder (new BVH_BinnedBuilder<Standard_ShortReal, 3, BVH_Constants_NbBinsBest> (BVH_Constants_LeafNodeSizeAverage,
+                                                                                                BVH_Constants_MaxTreeDepth,
+                                                                                                Standard_False,
+                                                                                                OSD_Parallel::NbLogicalProcessors() + 1)),
+  myRaytraceSceneRadius  (0.0f),
+  myRaytraceSceneEpsilon (1.0e-6f),
   myToUpdateEnvironmentMap (Standard_False),
-  myRaytraceLayerListState (0)
+  myRaytraceLayerListState (0),
+  myPrevCameraApertureRadius(0.f),
+  myPrevCameraFocalPlaneDist(0.f)
 {
   myWorkspace = new OpenGl_Workspace (this, NULL);
 
-  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();
+  Handle(Graphic3d_CLight) aLight = new Graphic3d_CLight (Graphic3d_TOLS_AMBIENT);
+  aLight->SetHeadlight (false);
+  aLight->SetColor (Quantity_NOC_WHITE);
+  myNoShadingLight = new Graphic3d_LightSet();
+  myNoShadingLight->Add (aLight);
+
+  myMainSceneFbos[0]         = new OpenGl_FrameBuffer();
+  myMainSceneFbos[1]         = new OpenGl_FrameBuffer();
+  myMainSceneFbosOit[0]      = new OpenGl_FrameBuffer();
+  myMainSceneFbosOit[1]      = new OpenGl_FrameBuffer();
+  myImmediateSceneFbos[0]    = new OpenGl_FrameBuffer();
+  myImmediateSceneFbos[1]    = new OpenGl_FrameBuffer();
+  myImmediateSceneFbosOit[0] = new OpenGl_FrameBuffer();
+  myImmediateSceneFbosOit[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();
 }
 
 // =======================================================================
@@ -121,35 +128,47 @@ OpenGl_View::~OpenGl_View()
 // =======================================================================
 void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
 {
-  myGraduatedTrihedron.Release (theCtx.operator->());
+  myGraduatedTrihedron.Release (theCtx.get());
+  myFrameStatsPrs.Release (theCtx.get());
 
   if (!myTextureEnv.IsNull())
   {
-    theCtx->DelayedRelease (myTextureEnv);
+    if (!theCtx.IsNull())
+    {
+      for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
+      {
+        theCtx->DelayedRelease (aTextureIter.ChangeValue());
+        aTextureIter.ChangeValue().Nullify();
+      }
+    }
     myTextureEnv.Nullify();
   }
 
   if (myTextureParams != NULL)
   {
-    myTextureParams->Release (theCtx.operator->());
+    myTextureParams->Release (theCtx.get());
   }
   if (myBgGradientArray != NULL)
   {
-    myBgGradientArray->Release (theCtx.operator->());
+    myBgGradientArray->Release (theCtx.get());
   }
   if (myBgTextureArray != NULL)
   {
-    myBgTextureArray->Release (theCtx.operator->());
+    myBgTextureArray->Release (theCtx.get());
   }
 
-  myMainSceneFbos[0]     ->Release (theCtx.operator->());
-  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->());
+  myMainSceneFbos[0]        ->Release (theCtx.get());
+  myMainSceneFbos[1]        ->Release (theCtx.get());
+  myMainSceneFbosOit[0]     ->Release (theCtx.get());
+  myMainSceneFbosOit[1]     ->Release (theCtx.get());
+  myImmediateSceneFbos[0]   ->Release (theCtx.get());
+  myImmediateSceneFbos[1]   ->Release (theCtx.get());
+  myImmediateSceneFbosOit[0]->Release (theCtx.get());
+  myImmediateSceneFbosOit[1]->Release (theCtx.get());
+  myOpenGlFBO               ->Release (theCtx.get());
+  myOpenGlFBO2              ->Release (theCtx.get());
+  myFullScreenQuad           .Release (theCtx.get());
+  myFullScreenQuadFlip       .Release (theCtx.get());
 
   releaseRaytraceResources (theCtx);
 }
@@ -171,15 +190,6 @@ void OpenGl_View::Remove()
   Graphic3d_CView::Remove();
 }
 
-// =======================================================================
-// function : SetTextureEnv
-// purpose  :
-// =======================================================================
-void OpenGl_View::SetCamera(const Handle(Graphic3d_Camera)& theCamera)
-{
-  myCamera = theCamera;
-}
-
 // =======================================================================
 // function : SetLocalOrigin
 // purpose  :
@@ -203,7 +213,11 @@ void OpenGl_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureE
   Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
   if (!aCtx.IsNull() && !myTextureEnv.IsNull())
   {
-    aCtx->DelayedRelease (myTextureEnv);
+    for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
+    {
+      aCtx->DelayedRelease (aTextureIter.ChangeValue());
+      aTextureIter.ChangeValue().Nullify();
+    }
   }
 
   myToUpdateEnvironmentMap = Standard_True;
@@ -225,11 +239,13 @@ void OpenGl_View::initTextureEnv (const Handle(OpenGl_Context)& theContext)
     return;
   }
 
-  myTextureEnv = new OpenGl_Texture (myTextureEnvData->GetParams());
+  myTextureEnv = new OpenGl_TextureSet (1);
+  Handle(OpenGl_Texture)& aTextureEnv = myTextureEnv->ChangeFirst();
+  aTextureEnv = new OpenGl_Texture (myTextureEnvData->GetId(), myTextureEnvData->GetParams());
   Handle(Image_PixMap) anImage = myTextureEnvData->GetImage();
   if (!anImage.IsNull())
   {
-    myTextureEnv->Init (theContext, *anImage.operator->(), myTextureEnvData->Type());
+    aTextureEnv->Init (theContext, *anImage.operator->(), myTextureEnvData->Type());
   }
 }
 
@@ -339,25 +355,58 @@ void OpenGl_View::GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, c
 // =======================================================================
 Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType)
 {
-  return myWorkspace->BufferDump (myFBO, theImage, theBufferType);
-}
+  if (theBufferType != Graphic3d_BT_RGB_RayTraceHdrLeft)
+  {
+    return myWorkspace->BufferDump(myFBO, theImage, theBufferType);
+  }
 
-// =======================================================================
-// function : Background
-// purpose  :
-// =======================================================================
-Aspect_Background OpenGl_View::Background() const
-{
-  return Aspect_Background (myBgColor.GetRGB());
-}
+  if (!myRaytraceParameters.AdaptiveScreenSampling)
+  {
+    return myWorkspace->BufferDump(myAccumFrames % 2 ? myRaytraceFBO2[0] : myRaytraceFBO1[0], theImage, theBufferType);
+  }
 
-// =======================================================================
-// function : SetBackground
-// purpose  :
-// =======================================================================
-void OpenGl_View::SetBackground (const Aspect_Background& theBackground)
-{
-  myBgColor.SetRGB (theBackground.Color());
+#if defined(GL_ES_VERSION_2_0)
+  return false;
+#else
+  if (theImage.Format() != Image_Format_RGBF)
+  {
+    return false;
+  }
+
+  const GLuint aW = myRaytraceOutputTexture[0]->SizeX();
+  const GLuint aH = myRaytraceOutputTexture[0]->SizeY();
+  if (aW / 3 != theImage.SizeX() || aH / 2 != theImage.SizeY())
+  {
+    return false;
+  }
+
+  std::vector<GLfloat> aValues;
+  try
+  {
+    aValues.resize (aW * aH);
+  }
+  catch (const std::bad_alloc&)
+  {
+    return false;
+  }
+
+  glBindTexture (GL_TEXTURE_RECTANGLE, myRaytraceOutputTexture[0]->TextureId());
+  glGetTexImage (GL_TEXTURE_RECTANGLE, 0, OpenGl_TextureFormat::Create<GLfloat, 1>().Format(), GL_FLOAT, &aValues[0]);
+  glBindTexture (GL_TEXTURE_RECTANGLE, 0);
+  for (unsigned int aRow = 0; aRow < aH; aRow += 2)
+  {
+    for (unsigned int aCol = 0; aCol < aW; aCol += 3)
+    {
+      float* anImageValue = theImage.ChangeValue<float[3]> ((aH - aRow) / 2 - 1, aCol / 3);
+      float aInvNbSamples = 1.f / aValues[aRow * aW + aCol + aW];
+      anImageValue[0] = aValues[aRow * aW + aCol] * aInvNbSamples;
+      anImageValue[1] = aValues[aRow * aW + aCol + 1] * aInvNbSamples;
+      anImageValue[2] = aValues[aRow * aW + aCol + 1 + aW] * aInvNbSamples;
+    }
+  }
+
+  return true;
+#endif
 }
 
 // =======================================================================
@@ -661,112 +710,6 @@ void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
   aFrameBuffer->ChangeViewport (theWidth, theHeight);
 }
 
-// =======================================================================
-// function : Export
-// purpose  :
-// =======================================================================
-#ifdef HAVE_GL2PS
-Standard_Boolean OpenGl_View::Export (const Standard_CString theFileName,
-                                      const Graphic3d_ExportFormat theFormat,
-                                      const Graphic3d_SortType theSortType)
-{
-  // gl2psBeginPage() will call OpenGL functions
-  // so we should activate correct GL context before redraw scene call
-  if (!myWorkspace->Activate())
-  {
-    return Standard_False;
-  }
-
-  Standard_Integer aFormat = -1;
-  Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
-  switch (theFormat)
-  {
-    case Graphic3d_EF_PostScript:
-      aFormat = GL2PS_PS;
-      break;
-    case Graphic3d_EF_EnhPostScript:
-      aFormat = GL2PS_EPS;
-      break;
-    case Graphic3d_EF_TEX:
-      aFormat = GL2PS_TEX;
-      break;
-    case Graphic3d_EF_PDF:
-      aFormat = GL2PS_PDF;
-      break;
-    case Graphic3d_EF_SVG:
-      aFormat = GL2PS_SVG;
-      break;
-    case Graphic3d_EF_PGF:
-      aFormat = GL2PS_PGF;
-      break;
-    case Graphic3d_EF_EMF:
-      //aFormat = GL2PS_EMF;
-      aFormat = GL2PS_PGF + 1; // 6
-      break;
-    default:
-      // unsupported format
-      return Standard_False;
-  }
-
-  switch (theSortType)
-  {
-    case Graphic3d_ST_Simple:
-      aSortType = GL2PS_SIMPLE_SORT;
-      break;
-    case Graphic3d_ST_BSP_Tree:
-      aSortType = GL2PS_BSP_SORT;
-      break;
-  }
-
-  GLint aViewport[4];
-  aViewport[0] = 0;
-  aViewport[1] = 0;
-  aViewport[2] = myWindow->Width();
-  aViewport[3] = myWindow->Height();
-
-  GLint aBufferSize = 1024 * 1024;
-  GLint anErrCode = GL2PS_SUCCESS;
-
-  // gl2ps uses standard write functions and do not check locale
-  Standard_CLocaleSentry aLocaleSentry;
-
-  while (aBufferSize > 0)
-  {
-    // current patch for EMF support in gl2ps uses WinAPI functions to create file
-    FILE* aFileH = (theFormat != Graphic3d_EF_EMF) ? fopen (theFileName, "wb") : NULL;
-    anErrCode = gl2psBeginPage ("", "", aViewport, aFormat, aSortType,
-                    GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
-                    GL_RGBA, 0, NULL,
-                    0, 0, 0, aBufferSize, aFileH, theFileName);
-    if (anErrCode != GL2PS_SUCCESS)
-    {
-      // initialization failed
-      if (aFileH != NULL)
-        fclose (aFileH);
-      break;
-    }
-    Redraw();
-
-    anErrCode = gl2psEndPage();
-    if (aFileH != NULL)
-      fclose (aFileH);
-
-    if (anErrCode == GL2PS_OVERFLOW)
-      aBufferSize *= 2;
-    else
-      break;
-  }
-  return anErrCode == GL2PS_SUCCESS;
-}
-#else
-Standard_Boolean OpenGl_View::Export (const Standard_CString /*theFileName*/,
-                                      const Graphic3d_ExportFormat /*theFormat*/,
-                                      const Graphic3d_SortType /*theSortType*/)
-{
-    return Standard_False;
-}
-#endif
-
 //=======================================================================
 //function : displayStructure
 //purpose  :
@@ -799,8 +742,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);
+  Update (anOldLayer);
+  Update (theNewLayerId);
 }
 
 //=======================================================================
@@ -836,3 +779,32 @@ void OpenGl_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& t
     theDict.ChangeFromIndex (theDict.Add ("ResolutionRatio", aResRatio)) = aResRatio;
   }
 }
+
+//=======================================================================
+//function : StatisticInformation
+//purpose  :
+//=======================================================================
+void OpenGl_View::StatisticInformation (TColStd_IndexedDataMapOfStringString& theDict) const
+{
+  if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext())
+  {
+    const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats();
+    const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams();
+    aStats->FormatStats (theDict, aRendParams.CollectedStats);
+  }
+}
+
+//=======================================================================
+//function : StatisticInformation
+//purpose  :
+//=======================================================================
+TCollection_AsciiString OpenGl_View::StatisticInformation() const
+{
+  if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext())
+  {
+    const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats();
+    const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams();
+    return aStats->FormatStats (aRendParams.CollectedStats);
+  }
+  return TCollection_AsciiString();
+}