0024887: Visualization - revise and extend Raytracing controls
[occt.git] / src / OpenGl / OpenGl_Workspace.cxx
index ecedc6a..b6dd166 100644 (file)
@@ -4,8 +4,8 @@
 //
 // This file is part of Open CASCADE Technology software library.
 //
-// This library is free software; you can redistribute it and / or modify it
-// under the terms of the GNU Lesser General Public version 2.1 as published
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
 // by the Free Software Foundation, with special exception defined in the file
 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
 // distribution for complete text of the license and disclaimer of any warranty.
 #include <OpenGl_AspectMarker.hxx>
 #include <OpenGl_AspectText.hxx>
 #include <OpenGl_Context.hxx>
+#include <OpenGl_Element.hxx>
 #include <OpenGl_FrameBuffer.hxx>
+#include <OpenGl_Structure.hxx>
 #include <OpenGl_Texture.hxx>
 #include <OpenGl_View.hxx>
 #include <OpenGl_Workspace.hxx>
-#include <OpenGl_Element.hxx>
 
 #include <Graphic3d_TextureParams.hxx>
 
@@ -136,23 +137,28 @@ void OpenGl_Material::Init (const OPENGL_SURF_PROP& theProp)
 // function : OpenGl_Workspace
 // purpose  :
 // =======================================================================
-OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
+OpenGl_Workspace::OpenGl_Workspace (const Handle(Aspect_DisplayConnection)& theDisplayConnection,
                                     const CALL_DEF_WINDOW&        theCWindow,
                                     Aspect_RenderingContext       theGContext,
                                     const Handle(OpenGl_Caps)&    theCaps,
                                     const Handle(OpenGl_Context)& theShareCtx)
-: OpenGl_Window (theDisplay, theCWindow, theGContext, theCaps, theShareCtx),
+: OpenGl_Window (theDisplayConnection, theCWindow, theGContext, theCaps, theShareCtx),
   NamedStatus (0),
   HighlightColor (&THE_WHITE_COLOR),
   //
-  myIsTransientOpen (Standard_False),
-  myRetainMode (Standard_False),
+  myComputeInitStatus (OpenGl_RT_NONE),
+  myIsRaytraceDataValid (Standard_False),
+  myViewModificationStatus (0),
+  myLayersModificationStatus (0),
+  //
+  myAntiAliasingMode     (3),
   myTransientDrawToFront (Standard_True),
+  myBackBufferRestored   (Standard_False),
+  myIsImmediateDrawn     (Standard_False),
   myUseTransparency (Standard_False),
   myUseZBuffer (Standard_False),
   myUseDepthTest (Standard_True),
   myUseGLLight (Standard_True),
-  myBackBufferRestored (Standard_False),
   //
   AspectLine_set (&myDefaultAspectLine),
   AspectLine_applied (NULL),
@@ -168,9 +174,17 @@ OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
   StructureMatrix_applied (&myDefaultMatrix),
   myCullingMode (TelCullUndefined),
   myModelViewMatrix (myDefaultMatrix),
-  PolygonOffset_applied (NULL)
+  PolygonOffset_applied (THE_DEFAULT_POFFSET)
 {
-  theDisplay->InitAttributes();
+  myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
+
+  if (!myGlContext->GetResource ("OpenGl_LineAttributes", myLineAttribs))
+  {
+    // share and register for release once the resource is no longer used
+    myLineAttribs = new OpenGl_LineAttributes();
+    myGlContext->ShareResource ("OpenGl_LineAttributes", myLineAttribs);
+    myLineAttribs->Init (myGlContext);
+  }
 
   // General initialization of the context
 
@@ -184,20 +198,13 @@ OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
   glHint (GL_LINE_SMOOTH_HINT,    GL_FASTEST);
   glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
 
-  // Polygon Offset
-  EnablePolygonOffset();
-
-#ifdef HAVE_OPENCL
-
-  myComputeInitStatus = OpenGl_CLIS_NONE;
-
-  myViewModificationStatus = 0;
-  myLayersModificationStatus = 0;
-
-  myIsRaytraceDataValid = Standard_False;
-  myToUpdateRaytraceData = Standard_False;
-
-#endif
+  // AA mode
+  const char* anAaEnv = ::getenv ("CALL_OPENGL_ANTIALIASING_MODE");
+  if (anAaEnv != NULL)
+  {
+    int v;
+    if (sscanf (anAaEnv, "%d", &v) > 0) myAntiAliasingMode = v;
+  }
 }
 
 // =======================================================================
@@ -217,9 +224,13 @@ Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_B
 // =======================================================================
 OpenGl_Workspace::~OpenGl_Workspace()
 {
-#ifdef HAVE_OPENCL
-  ReleaseOpenCL();
-#endif
+  if (!myLineAttribs.IsNull())
+  {
+    myLineAttribs.Nullify();
+    myGlContext->ReleaseResource ("OpenGl_LineAttributes", Standard_True);
+  }
+
+  ReleaseRaytraceResources();
 }
 
 // =======================================================================
@@ -266,7 +277,7 @@ void OpenGl_Workspace::ResetAppliedAspect()
   AspectText_applied    = NULL;
   TextParam_set         = &myDefaultTextParam;
   TextParam_applied     = NULL;
-  PolygonOffset_applied = NULL;
+  PolygonOffset_applied = THE_DEFAULT_POFFSET;
   myCullingMode         = TelCullUndefined;
 
   AspectLine(Standard_True);
@@ -567,29 +578,26 @@ void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
     toSwap = 0; // no need to swap buffers
   }
 
-#ifdef HAVE_OPENCL
-  if (!theCView.IsRaytracing || myComputeInitStatus == OpenGl_CLIS_FAIL)
+  if (theCView.RenderParams.Method != Graphic3d_RM_RAYTRACING || myComputeInitStatus == OpenGl_RT_FAIL)
   {
-#endif
-    Redraw1 (theCView, theCUnderLayer, theCOverLayer, toSwap);
-    if (aFrameBuffer == NULL || !myTransientDrawToFront)
+    const Standard_Boolean isImmediate = !myView->ImmediateStructures().IsEmpty();
+    redraw1 (theCView, theCUnderLayer, theCOverLayer, isImmediate ? 0 : toSwap);
+    if (isImmediate)
     {
-      RedrawImmediatMode();
+      RedrawImmediate (theCView, theCUnderLayer, theCOverLayer, Standard_True);
     }
 
     theCView.WasRedrawnGL = Standard_True;
-#ifdef HAVE_OPENCL
   }
   else
   {
     int aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWidth;
     int aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myHeight;
 
-    Raytrace (theCView, aSizeX, aSizeY, toSwap);
+    Raytrace (theCView, aSizeX, aSizeY, toSwap, aFrameBuffer);
 
     theCView.WasRedrawnGL = Standard_False;
   }
-#endif
 
   if (aFrameBuffer != NULL)
   {
@@ -598,7 +606,7 @@ void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
     glViewport (aViewPortBack[0], aViewPortBack[1], aViewPortBack[2], aViewPortBack[3]);
   }
 
-#if (defined(_WIN32) || defined(__WIN32__)) && defined(HAVE_VIDEOCAPTURE)
+#if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
   if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow))
   {
     GLint params[4];
@@ -619,3 +627,201 @@ void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
   // reset render mode state
   aGlCtx->FetchState();
 }
+
+// =======================================================================
+// function : redraw1
+// purpose  :
+// =======================================================================
+void OpenGl_Workspace::redraw1 (const Graphic3d_CView& theCView,
+                                const Aspect_CLayer2d& theCUnderLayer,
+                                const Aspect_CLayer2d& theCOverLayer,
+                                const int              theToSwap)
+{
+  if (myView.IsNull())
+  {
+    return;
+  }
+
+  // request reset of material
+  NamedStatus |= OPENGL_NS_RESMAT;
+
+  GLbitfield toClear = GL_COLOR_BUFFER_BIT;
+  if (myUseZBuffer)
+  {
+    glDepthFunc (GL_LEQUAL);
+    glDepthMask (GL_TRUE);
+    if (myUseDepthTest)
+    {
+      glEnable (GL_DEPTH_TEST);
+    }
+    else
+    {
+      glDisable (GL_DEPTH_TEST);
+    }
+
+    glClearDepth (1.0);
+    toClear |= GL_DEPTH_BUFFER_BIT;
+  }
+  else
+  {
+    glDisable (GL_DEPTH_TEST);
+  }
+
+  if (NamedStatus & OPENGL_NS_WHITEBACK)
+  {
+    // set background to white
+    glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
+    toClear |= GL_DEPTH_BUFFER_BIT;
+  }
+  else
+  {
+    glClearColor (myBgColor.rgb[0], myBgColor.rgb[1], myBgColor.rgb[2], 0.0f);
+  }
+  glClear (toClear);
+
+  Handle(OpenGl_Workspace) aWS (this);
+  myView->Render (myPrintContext, aWS, theCView, theCUnderLayer, theCOverLayer);
+
+  // swap the buffers
+  if (theToSwap)
+  {
+    GetGlContext()->SwapBuffers();
+    myBackBufferRestored = Standard_False;
+    myIsImmediateDrawn   = Standard_False;
+  }
+  else
+  {
+    glFlush();
+    myBackBufferRestored = Standard_True;
+    myIsImmediateDrawn   = Standard_False;
+  }
+}
+
+// =======================================================================
+// function : copyBackToFront
+// purpose  :
+// =======================================================================
+void OpenGl_Workspace::copyBackToFront()
+{
+  glMatrixMode (GL_PROJECTION);
+  glPushMatrix();
+  glLoadIdentity();
+  gluOrtho2D (0.0, (GLdouble )myWidth, 0.0, (GLdouble )myHeight);
+  glMatrixMode (GL_MODELVIEW);
+  glPushMatrix();
+  glLoadIdentity();
+
+  DisableFeatures();
+
+  glDrawBuffer (GL_FRONT);
+  glReadBuffer (GL_BACK);
+
+  glRasterPos2i (0, 0);
+  glCopyPixels  (0, 0, myWidth + 1, myHeight + 1, GL_COLOR);
+
+  EnableFeatures();
+
+  glMatrixMode (GL_PROJECTION);
+  glPopMatrix();
+  glMatrixMode (GL_MODELVIEW);
+  glPopMatrix();
+
+  glDrawBuffer (GL_BACK);
+
+  myIsImmediateDrawn = Standard_False;
+}
+
+// =======================================================================
+// function : DisplayCallback
+// purpose  :
+// =======================================================================
+void OpenGl_Workspace::DisplayCallback (const Graphic3d_CView& theCView,
+                                        Standard_Integer       theReason)
+{
+  if (theCView.GDisplayCB == NULL)
+  {
+    return;
+  }
+
+  Aspect_GraphicCallbackStruct aCallData;
+  aCallData.reason    = theReason;
+  aCallData.glContext = GetGlContext();
+  aCallData.wsID      = theCView.WsId;
+  aCallData.viewID    = theCView.ViewId;
+  theCView.GDisplayCB (theCView.DefWindow.XWindow, theCView.GClientData, &aCallData);
+}
+
+// =======================================================================
+// function : RedrawImmediate
+// purpose  :
+// =======================================================================
+void OpenGl_Workspace::RedrawImmediate (const Graphic3d_CView& theCView,
+                                        const Aspect_CLayer2d& theCUnderLayer,
+                                        const Aspect_CLayer2d& theCOverLayer,
+                                        const Standard_Boolean theToForce)
+{
+  if (!Activate())
+  {
+    return;
+  }
+
+  GLboolean isDoubleBuffer = GL_FALSE;
+  glGetBooleanv (GL_DOUBLEBUFFER, &isDoubleBuffer);
+  if (myView->ImmediateStructures().IsEmpty())
+  {
+    if (theToForce
+     || !myIsImmediateDrawn)
+    {
+      myIsImmediateDrawn = Standard_False;
+      return;
+    }
+
+    if (myBackBufferRestored
+     && isDoubleBuffer)
+    {
+      copyBackToFront();
+      glFlush();
+    }
+    else
+    {
+      Redraw (theCView, theCUnderLayer, theCOverLayer);
+    }
+    return;
+  }
+
+  if (isDoubleBuffer && myTransientDrawToFront)
+  {
+    if (!myBackBufferRestored)
+    {
+      Redraw (theCView, theCUnderLayer, theCOverLayer);
+      return;
+    }
+    copyBackToFront();
+    MakeFrontBufCurrent();
+  }
+  else
+  {
+    myBackBufferRestored = Standard_False;
+  }
+  myIsImmediateDrawn = Standard_True;
+
+  NamedStatus |= OPENGL_NS_IMMEDIATE;
+  ///glDisable (GL_LIGHTING);
+  glDisable (GL_DEPTH_TEST);
+
+  Handle(OpenGl_Workspace) aWS (this);
+  for (OpenGl_SequenceOfStructure::Iterator anIter (myView->ImmediateStructures());
+       anIter.More(); anIter.Next())
+  {
+    const OpenGl_Structure* aStructure = anIter.Value();
+    aStructure->Render (aWS);
+  }
+
+  NamedStatus &= ~OPENGL_NS_IMMEDIATE;
+
+  if (isDoubleBuffer && myTransientDrawToFront)
+  {
+    glFlush();
+    MakeBackBufCurrent();
+  }
+}