0026025: Visualization, TKOpenGl - stereoscopic output does not work
[occt.git] / src / OpenGl / OpenGl_View_2.cxx
index 24b374a..3abea51 100644 (file)
@@ -55,15 +55,6 @@ namespace
 extern void InitLayerProp (const int theListId); //szvgl: defined in OpenGl_GraphicDriver_Layer.cxx
 
 /*----------------------------------------------------------------------*/
-
-struct OPENGL_CLIP_PLANE
-{
-  GLboolean isEnabled;
-  GLdouble Equation[4];
-  DEFINE_STANDARD_ALLOC
-};
-
-/*----------------------------------------------------------------------*/
 /*
 * Fonctions privees
 */
@@ -241,6 +232,7 @@ void OpenGl_View::DrawBackground (const Handle(OpenGl_Workspace)& theWorkspace)
   aCtx->WorldViewState.Pop();
   aCtx->ProjectionState.Pop();
   aCtx->ApplyProjectionMatrix();
+  aCtx->ApplyWorldViewMatrix();
 
   if (theWorkspace->UseZBuffer())
   {
@@ -253,6 +245,8 @@ void OpenGl_View::DrawBackground (const Handle(OpenGl_Workspace)& theWorkspace)
 //call_func_redraw_all_structs_proc
 void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
                           const Handle(OpenGl_Workspace)&      theWorkspace,
+                          OpenGl_FrameBuffer*                  theOutputFBO,
+                          Graphic3d_Camera::Projection         theProjection,
                           const Graphic3d_CView&               theCView,
                           const Aspect_CLayer2d&               theCUnderLayer,
                           const Aspect_CLayer2d&               theCOverLayer,
@@ -265,24 +259,13 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
   const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
 
 #if !defined(GL_ES_VERSION_2_0)
-  // store and disable current clipping planes
-  const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
-  NCollection_Array1<OPENGL_CLIP_PLANE> aOldPlanes (GL_CLIP_PLANE0, GL_CLIP_PLANE0 + aMaxPlanes - 1);
+  // Disable current clipping planes
   if (aContext->core11 != NULL)
   {
-    for (Standard_Integer aClipPlaneId = aOldPlanes.Lower(); aClipPlaneId <= aOldPlanes.Upper(); ++aClipPlaneId)
+    const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
+    for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId)
     {
-      OPENGL_CLIP_PLANE& aPlane = aOldPlanes.ChangeValue (aClipPlaneId);
-      aContext->core11->glGetClipPlane (aClipPlaneId, aPlane.Equation);
-      if (aPlane.isEnabled)
-      {
-        aContext->core11fwd->glDisable (aClipPlaneId);
-        aPlane.isEnabled = GL_TRUE;
-      }
-      else
-      {
-        aPlane.isEnabled = GL_FALSE;
-      }
+      aContext->core11fwd->glDisable (aClipPlaneId);
     }
   }
 #endif
@@ -452,42 +435,17 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
   }
 
   // Redraw 3d scene
-  if (!myCamera->IsStereo() || !aContext->HasStereoBuffers())
+  if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
   {
-    // single-pass monographic rendering
-    // redraw scene with normal orientation and projection
-    RedrawScene (thePrintContext, theWorkspace, theCView, theToDrawImmediate);
-  }
-  else
-  {
-    // two stereographic passes
-
-    // safely switch to left Eye buffer
-    aContext->SetDrawBufferLeft();
-
     aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF());
     aContext->ApplyProjectionMatrix();
-
-    // redraw left Eye
-    RedrawScene (thePrintContext, theWorkspace, theCView, theToDrawImmediate);
-
-    // reset depth buffer of first rendering pass
-    if (theWorkspace->UseDepthTest())
-    {
-      glClear (GL_DEPTH_BUFFER_BIT);
-    }
-    // safely switch to right Eye buffer
-    aContext->SetDrawBufferRight();
-
+  }
+  else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye)
+  {
     aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF());
     aContext->ApplyProjectionMatrix();
-
-    // redraw right Eye
-    RedrawScene (thePrintContext, theWorkspace, theCView, theToDrawImmediate);
-
-    // switch back to monographic rendering
-    aContext->SetDrawBufferMono();
   }
+  RedrawScene (thePrintContext, theWorkspace, theOutputFBO, theCView, theToDrawImmediate);
 
   // ===============================
   //      Step 5: Trihedron
@@ -543,28 +501,8 @@ void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
     theWorkspace->DisplayCallback (theCView, aMode);
   }
 
-  // ===============================
-  //      Step 7: Finalize
-  // ===============================
-
-#if !defined(GL_ES_VERSION_2_0)
-  // restore clipping planes
-  if (aContext->core11 != NULL)
-  {
-    for (Standard_Integer aClipPlaneId = aOldPlanes.Lower(); aClipPlaneId <= aOldPlanes.Upper(); ++aClipPlaneId)
-    {
-      const OPENGL_CLIP_PLANE& aPlane = aOldPlanes.ChangeValue (aClipPlaneId);
-      aContext->core11->glClipPlane (aClipPlaneId, aPlane.Equation);
-      if (aPlane.isEnabled)
-        aContext->core11fwd->glEnable (aClipPlaneId);
-      else
-        aContext->core11fwd->glDisable (aClipPlaneId);
-    }
-  }
-#endif
-
   // ==============================================================
-  //      Step 8: Keep shader manager informed about last View
+  //      Step 7: Keep shader manager informed about last View
   // ==============================================================
 
   if (!aManager.IsNull())
@@ -586,6 +524,7 @@ void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
 
 //ExecuteViewDisplay
 void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace)& theWorkspace,
+                                 OpenGl_FrameBuffer*             theReadDrawFbo,
                                  const Graphic3d_CView&          theCView,
                                  const Standard_Boolean          theToDrawImmediate)
 {
@@ -636,27 +575,14 @@ void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace)& theWorkspace,
     toRenderGL = !initRaytraceResources (theCView, aCtx) ||
       !updateRaytraceGeometry (OpenGl_GUM_CHECK, theWorkspace->ActiveViewId(), aCtx);
 
-    OpenGl_FrameBuffer* anOutputFBO = NULL;
-
-    if (theWorkspace->ResultFBO()->IsValid())
-    {
-      anOutputFBO = theWorkspace->ResultFBO().operator->();
-    }
-    else if (theCView.ptrFBO != NULL)
-    {
-      anOutputFBO = (OpenGl_FrameBuffer* )theCView.ptrFBO;
-    }
-    else
-    {
-      //toRenderGL = Standard_True; // failed to get valid FBO
-    }
+    toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL
 
-    if (!toRenderGL && myIsRaytraceDataValid)
+    if (!toRenderGL)
     {
-      const Standard_Integer aSizeX = anOutputFBO != NULL ?
-        anOutputFBO->GetVPSizeX() : theWorkspace->Width();
-      const Standard_Integer aSizeY = anOutputFBO != NULL ?
-        anOutputFBO->GetVPSizeY() : theWorkspace->Height();
+      const Standard_Integer aSizeX = theReadDrawFbo != NULL ?
+        theReadDrawFbo->GetVPSizeX() : theWorkspace->Width();
+      const Standard_Integer aSizeY = theReadDrawFbo != NULL ?
+        theReadDrawFbo->GetVPSizeY() : theWorkspace->Height();
 
       if (myOpenGlFBO.IsNull())
         myOpenGlFBO = new OpenGl_FrameBuffer;
@@ -672,8 +598,8 @@ void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace)& theWorkspace,
 
       myRaytraceFilter->SetPrevRenderFilter (theWorkspace->GetRenderFilter());
 
-      if (anOutputFBO != NULL)
-        anOutputFBO->UnbindBuffer (aCtx);
+      if (theReadDrawFbo != NULL)
+        theReadDrawFbo->UnbindBuffer (aCtx);
 
       // Prepare preliminary OpenGL output
       if (aCtx->arbFBOBlit != NULL)
@@ -683,9 +609,9 @@ void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace)& theWorkspace,
 
         theWorkspace->SetRenderFilter (myRaytraceFilter);
         {
-          if (anOutputFBO != NULL)
+          if (theReadDrawFbo != NULL)
           {
-            anOutputFBO->BindReadBuffer (aCtx);
+            theReadDrawFbo->BindReadBuffer (aCtx);
           }
           else
           {
@@ -705,9 +631,9 @@ void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace)& theWorkspace,
         theWorkspace->SetRenderFilter (myRaytraceFilter->PrevRenderFilter());
       }
 
-      if (anOutputFBO != NULL)
+      if (theReadDrawFbo != NULL)
       {
-        anOutputFBO->BindBuffer (aCtx);
+        theReadDrawFbo->BindBuffer (aCtx);
       }
       else
       {
@@ -715,7 +641,7 @@ void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace)& theWorkspace,
       }
 
       // Ray-tracing polygonal primitive arrays
-      raytrace (theCView, aSizeX, aSizeY, anOutputFBO, aCtx);
+      raytrace (theCView, aSizeX, aSizeY, theReadDrawFbo, aCtx);
 
       // Render upper (top and topmost) OpenGL layers
       myZLayers.Render (theWorkspace, theToDrawImmediate, OpenGl_LF_Upper);
@@ -1073,6 +999,7 @@ void OpenGl_View::ChangePriority (const OpenGl_Structure *theStructure,
 
 void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintContext,
                                const Handle(OpenGl_Workspace)&      theWorkspace,
+                               OpenGl_FrameBuffer*                  theReadDrawFbo,
                                const Graphic3d_CView&               theCView,
                                const Standard_Boolean               theToDrawImmediate)
 {
@@ -1206,14 +1133,14 @@ void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintCont
       theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
       theWorkspace->DisableTexture();
       // Render the view
-      RenderStructs (theWorkspace, theCView, theToDrawImmediate);
+      RenderStructs (theWorkspace, theReadDrawFbo, theCView, theToDrawImmediate);
       break;
 
     case Visual3d_TOD_ENVIRONMENT:
       theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
       theWorkspace->EnableTexture (myTextureEnv);
       // Render the view
-      RenderStructs (theWorkspace, theCView, theToDrawImmediate);
+      RenderStructs (theWorkspace, theReadDrawFbo, theCView, theToDrawImmediate);
       theWorkspace->DisableTexture();
       break;
 
@@ -1221,7 +1148,7 @@ void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintCont
       // First pass
       theWorkspace->NamedStatus &= ~OPENGL_NS_FORBIDSETTEX;
       // Render the view
-      RenderStructs (theWorkspace, theCView, theToDrawImmediate);
+      RenderStructs (theWorkspace, theReadDrawFbo, theCView, theToDrawImmediate);
       theWorkspace->DisableTexture();
 
       // Second pass
@@ -1254,7 +1181,7 @@ void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintCont
         theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
 
         // Render the view
-        RenderStructs (theWorkspace, theCView, theToDrawImmediate);
+        RenderStructs (theWorkspace, theReadDrawFbo, theCView, theToDrawImmediate);
         theWorkspace->DisableTexture();
 
         // Restore properties back