0029138: Visualization - D3DHost_FrameBuffer should provide software fallback when...
[occt.git] / src / OpenGl / OpenGl_Workspace.cxx
index 61e4d13..e6b2419 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-#ifdef HAVE_CONFIG_H
-  #include <config.h>
-#endif
-
-#include <OpenGl_GlCore15.hxx>
-
-#include <InterfaceGraphic.hxx>
+#include <OpenGl_Workspace.hxx>
 
+#include <OpenGl_ArbFBO.hxx>
 #include <OpenGl_AspectLine.hxx>
 #include <OpenGl_AspectFace.hxx>
 #include <OpenGl_AspectMarker.hxx>
 #include <OpenGl_Context.hxx>
 #include <OpenGl_Element.hxx>
 #include <OpenGl_FrameBuffer.hxx>
+#include <OpenGl_GlCore15.hxx>
+#include <OpenGl_SceneGeometry.hxx>
 #include <OpenGl_Structure.hxx>
+#include <OpenGl_Sampler.hxx>
+#include <OpenGl_ShaderManager.hxx>
 #include <OpenGl_Texture.hxx>
 #include <OpenGl_View.hxx>
-#include <OpenGl_Workspace.hxx>
+#include <OpenGl_Window.hxx>
 
 #include <Graphic3d_TextureParams.hxx>
-
-#if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
-  #include <OpenGl_AVIWriter.hxx>
+#include <Graphic3d_TransformUtils.hxx>
+#include <NCollection_AlignedAllocator.hxx>
+
+IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
+IMPLEMENT_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
+
+#ifdef HAVE_GL2PS
+  #include <gl2ps.h>
+  /* OCC22216 NOTE: linker dependency can be switched off by undefining macro.
+     Pragma comment for gl2ps.lib is defined only here. */
+  #ifdef _MSC_VER
+  #pragma comment( lib, "gl2ps.lib" )
+  #endif
 #endif
 
-IMPLEMENT_STANDARD_HANDLE(OpenGl_Workspace,OpenGl_Window)
-IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,OpenGl_Window)
-
 namespace
 {
-  static const TEL_COLOUR  THE_WHITE_COLOR = { { 1.0f, 1.0f, 1.0f, 1.0f } };
-  static const OpenGl_Vec4 THE_BLACK_COLOR      (0.0f, 0.0f, 0.0f, 1.0f);
+  static const OpenGl_Vec4 THE_WHITE_COLOR (1.0f, 1.0f, 1.0f, 1.0f);
+  static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f);
 
   static const OpenGl_AspectLine myDefaultAspectLine;
   static const OpenGl_AspectFace myDefaultAspectFace;
   static const OpenGl_AspectMarker myDefaultAspectMarker;
   static const OpenGl_AspectText myDefaultAspectText;
 
-  static const OpenGl_TextParam myDefaultTextParam =
-  {
-    16, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM
-  };
-
   static const OpenGl_Matrix myDefaultMatrix =
   {
     {{ 1.0F, 0.0F, 0.0F, 0.0F },
@@ -65,22 +66,24 @@ namespace
      { 0.0F, 0.0F, 0.0F, 1.0F }}
   };
 
-};
+}
 
 // =======================================================================
 // function : Init
 // purpose  :
 // =======================================================================
-void OpenGl_Material::Init (const OPENGL_SURF_PROP& theProp)
+void OpenGl_Material::Init (const Graphic3d_MaterialAspect& theMat,
+                            const Quantity_Color&           theInteriorColor)
 {
+  const bool isPhysic = theMat.MaterialType (Graphic3d_MATERIAL_PHYSIC);
+  ChangeShine()        = 128.0f * theMat.Shininess();
+  ChangeTransparency() = theMat.Alpha();
+
   // ambient component
-  if (theProp.color_mask & OPENGL_AMBIENT_MASK)
+  if (theMat.ReflectionMode (Graphic3d_TOR_AMBIENT))
   {
-    const float* aSrcAmb = theProp.isphysic ? theProp.ambcol.rgb : theProp.matcol.rgb;
-    Ambient = OpenGl_Vec4 (aSrcAmb[0] * theProp.amb,
-                           aSrcAmb[1] * theProp.amb,
-                           aSrcAmb[2] * theProp.amb,
-                           1.0f);
+    const OpenGl_Vec3& aSrcAmb = isPhysic ? theMat.AmbientColor() : theInteriorColor;
+    Ambient = OpenGl_Vec4 (aSrcAmb * theMat.Ambient(), 1.0f);
   }
   else
   {
@@ -88,13 +91,10 @@ void OpenGl_Material::Init (const OPENGL_SURF_PROP& theProp)
   }
 
   // diffusion component
-  if (theProp.color_mask & OPENGL_DIFFUSE_MASK)
+  if (theMat.ReflectionMode (Graphic3d_TOR_DIFFUSE))
   {
-    const float* aSrcDif = theProp.isphysic ? theProp.difcol.rgb : theProp.matcol.rgb;
-    Diffuse = OpenGl_Vec4 (aSrcDif[0] * theProp.diff,
-                           aSrcDif[1] * theProp.diff,
-                           aSrcDif[2] * theProp.diff,
-                           1.0f);
+    const OpenGl_Vec3& aSrcDif = isPhysic ? theMat.DiffuseColor() : theInteriorColor;
+    Diffuse = OpenGl_Vec4 (aSrcDif * theMat.Diffuse(), 1.0f);
   }
   else
   {
@@ -102,13 +102,10 @@ void OpenGl_Material::Init (const OPENGL_SURF_PROP& theProp)
   }
 
   // specular component
-  if (theProp.color_mask & OPENGL_SPECULAR_MASK)
+  if (theMat.ReflectionMode (Graphic3d_TOR_SPECULAR))
   {
-    const float* aSrcSpe = theProp.isphysic ? theProp.speccol.rgb : THE_WHITE_COLOR.rgb;
-    Specular = OpenGl_Vec4 (aSrcSpe[0] * theProp.spec,
-                            aSrcSpe[1] * theProp.spec,
-                            aSrcSpe[2] * theProp.spec,
-                            1.0f);
+    const OpenGl_Vec3& aSrcSpe = isPhysic ? (const OpenGl_Vec3& )theMat.SpecularColor() : THE_WHITE_COLOR.rgb();
+    Specular = OpenGl_Vec4 (aSrcSpe * theMat.Specular(), 1.0f);
   }
   else
   {
@@ -116,122 +113,66 @@ void OpenGl_Material::Init (const OPENGL_SURF_PROP& theProp)
   }
 
   // emission component
-  if (theProp.color_mask & OPENGL_EMISSIVE_MASK)
+  if (theMat.ReflectionMode (Graphic3d_TOR_EMISSION))
   {
-    const float* aSrcEms = theProp.isphysic ? theProp.emscol.rgb : theProp.matcol.rgb;
-    Emission = OpenGl_Vec4 (aSrcEms[0] * theProp.emsv,
-                            aSrcEms[1] * theProp.emsv,
-                            aSrcEms[2] * theProp.emsv,
-                            1.0f);
+    const OpenGl_Vec3& aSrcEms = isPhysic ? theMat.EmissiveColor() : theInteriorColor;
+    Emission = OpenGl_Vec4 (aSrcEms * theMat.Emissive(), 1.0f);
   }
   else
   {
     Emission = THE_BLACK_COLOR;
   }
-
-  ChangeShine()        = theProp.shine;
-  ChangeTransparency() = theProp.trans;
 }
 
 // =======================================================================
 // function : OpenGl_Workspace
 // purpose  :
 // =======================================================================
-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 (theDisplayConnection, theCWindow, theGContext, theCaps, theShareCtx),
-  NamedStatus (0),
-  HighlightColor (&THE_WHITE_COLOR),
+OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow)
+: myView (theView),
+  myWindow (theWindow),
+  myGlContext (!theWindow.IsNull() ? theWindow->GetGlContext() : NULL),
+  myUseZBuffer    (Standard_True),
+  myUseDepthWrite (Standard_True),
   //
-  myComputeInitStatus (OpenGl_RT_NONE),
-  myIsRaytraceDataValid (Standard_False),
-  myViewModificationStatus (0),
-  myLayersModificationStatus (0),
+  myAspectLineSet (&myDefaultAspectLine),
+  myAspectFaceSet (&myDefaultAspectFace),
+  myAspectMarkerSet (&myDefaultAspectMarker),
+  myAspectTextSet (&myDefaultAspectText),
   //
-  myAntiAliasingMode     (3),
-  myTransientDrawToFront (Standard_True),
-  myBackBufferRestored   (Standard_False),
-  myIsImmediateDrawn     (Standard_False),
-  myUseTransparency (Standard_False),
-  myUseZBuffer (Standard_False),
-  myUseDepthTest (Standard_True),
-  myUseGLLight (Standard_True),
-  myIsCullingEnabled (Standard_False),
-  //
-  AspectLine_set (&myDefaultAspectLine),
-  AspectLine_applied (NULL),
-  AspectFace_set (&myDefaultAspectFace),
-  AspectFace_applied (NULL),
-  AspectMarker_set (&myDefaultAspectMarker),
-  AspectMarker_applied (NULL),
-  AspectText_set (&myDefaultAspectText),
-  AspectText_applied (NULL),
-  TextParam_set (&myDefaultTextParam),
-  TextParam_applied (NULL),
   ViewMatrix_applied (&myDefaultMatrix),
   StructureMatrix_applied (&myDefaultMatrix),
-  myCullingMode (TelCullUndefined),
-  myModelViewMatrix (myDefaultMatrix),
-  PolygonOffset_applied (THE_DEFAULT_POFFSET)
+  myToAllowFaceCulling (false),
+  myModelViewMatrix (myDefaultMatrix)
 {
-  myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
-
-  if (!myGlContext->GetResource ("OpenGl_LineAttributes", myLineAttribs))
+  if (!myGlContext.IsNull() && myGlContext->MakeCurrent())
   {
-    // 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
+    myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
 
-  // Eviter d'avoir les faces mal orientees en noir.
-  // Pourrait etre utiliser pour detecter les problemes d'orientation
-  glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
+    // General initialization of the context
+  #if !defined(GL_ES_VERSION_2_0)
+    if (myGlContext->core11 != NULL)
+    {
+      // Eviter d'avoir les faces mal orientees en noir.
+      // Pourrait etre utiliser pour detecter les problemes d'orientation
+      glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
 
-  // Optimisation pour le Fog et l'antialiasing
-  glHint (GL_FOG_HINT,            GL_FASTEST);
-  glHint (GL_POINT_SMOOTH_HINT,   GL_FASTEST);
-  glHint (GL_LINE_SMOOTH_HINT,    GL_FASTEST);
-  glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
+      // Optimisation pour le Fog et l'antialiasing
+      glHint (GL_FOG_HINT,            GL_FASTEST);
+      glHint (GL_POINT_SMOOTH_HINT,   GL_FASTEST);
+    }
 
-  // AA mode
-  const char* anAaEnv = ::getenv ("CALL_OPENGL_ANTIALIASING_MODE");
-  if (anAaEnv != NULL)
-  {
-    int v;
-    if (sscanf (anAaEnv, "%d", &v) > 0) myAntiAliasingMode = v;
+    glHint (GL_LINE_SMOOTH_HINT,    GL_FASTEST);
+    glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
+  #endif
   }
-}
-
-// =======================================================================
-// function : SetImmediateModeDrawToFront
-// purpose  :
-// =======================================================================
-Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
-{
-  const Standard_Boolean aPrevMode = myTransientDrawToFront;
-  myTransientDrawToFront = theDrawToFrontBuffer;
-  return aPrevMode;
-}
 
-// =======================================================================
-// function : ~OpenGl_Workspace
-// purpose  :
-// =======================================================================
-OpenGl_Workspace::~OpenGl_Workspace()
-{
-  if (!myLineAttribs.IsNull())
-  {
-    myLineAttribs.Nullify();
-    myGlContext->ReleaseResource ("OpenGl_LineAttributes", Standard_True);
-  }
+  myDefaultCappingAlgoFilter = new OpenGl_CappingAlgoFilter();
 
-  ReleaseRaytraceResources();
+  myNoneCulling .Aspect()->SetSuppressBackFaces (false);
+  myNoneCulling .Aspect()->SetDrawEdges (false);
+  myFrontCulling.Aspect()->SetSuppressBackFaces (true);
+  myFrontCulling.Aspect()->SetDrawEdges (false);
 }
 
 // =======================================================================
@@ -240,591 +181,367 @@ OpenGl_Workspace::~OpenGl_Workspace()
 // =======================================================================
 Standard_Boolean OpenGl_Workspace::Activate()
 {
-  if (!OpenGl_Window::Activate())
+  if (myWindow.IsNull() || !myWindow->Activate())
+  {
     return Standard_False;
+  }
 
   ViewMatrix_applied      = &myDefaultMatrix;
   StructureMatrix_applied = &myDefaultMatrix;
 
   ResetAppliedAspect();
 
+  // reset state for safety
+  myGlContext->BindProgram (Handle(OpenGl_ShaderProgram)());
+  if (myGlContext->core20fwd != NULL)
+  {
+    myGlContext->core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
+  }
+  myGlContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
   return Standard_True;
 }
 
-// =======================================================================
-// function : UseTransparency
-// purpose  : call_togl_transparency
-// =======================================================================
-void OpenGl_Workspace::UseTransparency (const Standard_Boolean theFlag)
-{
-  myUseTransparency = theFlag;
-}
-
 //=======================================================================
 //function : ResetAppliedAspect
 //purpose  : Sets default values of GL parameters in accordance with default aspects
 //=======================================================================
 void OpenGl_Workspace::ResetAppliedAspect()
 {
-  NamedStatus           = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
-  HighlightColor        = &THE_WHITE_COLOR;
-  AspectLine_set        = &myDefaultAspectLine;
-  AspectLine_applied    = NULL;
-  AspectFace_set        = &myDefaultAspectFace;
-  AspectFace_applied    = NULL;
-  AspectMarker_set      = &myDefaultAspectMarker;
-  AspectMarker_applied  = NULL;
-  AspectText_set        = &myDefaultAspectText;
-  AspectText_applied    = NULL;
-  TextParam_set         = &myDefaultTextParam;
-  TextParam_applied     = NULL;
-  PolygonOffset_applied = THE_DEFAULT_POFFSET;
-  myCullingMode         = TelCullUndefined;
-
-  AspectLine(Standard_True);
-  AspectFace(Standard_True);
-  AspectMarker(Standard_True);
-  AspectText(Standard_True);
+  myGlContext->BindDefaultVao();
+
+  myHighlightStyle.Nullify();
+  myToAllowFaceCulling  = false;
+  myAspectLineSet       = &myDefaultAspectLine;
+  myAspectFaceSet       = &myDefaultAspectFace;
+  myAspectFaceApplied.Nullify();
+  myAspectMarkerSet     = &myDefaultAspectMarker;
+  myAspectMarkerApplied.Nullify();
+  myAspectTextSet       = &myDefaultAspectText;
+  myPolygonOffsetApplied= Graphic3d_PolygonOffset();
+
+  ApplyAspectLine();
+  ApplyAspectFace();
+  ApplyAspectMarker();
+  ApplyAspectText();
+
+  myGlContext->SetTypeOfLine (myDefaultAspectLine.Aspect()->Type());
+  myGlContext->SetLineWidth  (myDefaultAspectLine.Aspect()->Width());
 }
 
 // =======================================================================
-// function : DisableTexture
+// function : SetAspectLine
 // purpose  :
 // =======================================================================
-Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
+const OpenGl_AspectLine* OpenGl_Workspace::SetAspectLine (const OpenGl_AspectLine* theAspect)
 {
-  if (myTextureBound.IsNull())
-  {
-    return myTextureBound;
-  }
+  const OpenGl_AspectLine* aPrevAspectLine = myAspectLineSet;
+  myAspectLineSet = theAspect;
+  return aPrevAspectLine;
+}
 
-  // reset texture matrix because some code may expect it is identity
-  GLint aMatrixMode = GL_TEXTURE;
-  glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
-  glMatrixMode (GL_TEXTURE);
-  glLoadIdentity();
-  glMatrixMode (aMatrixMode);
+// =======================================================================
+// function : SetAspectFace
+// purpose  :
+// =======================================================================
+const OpenGl_AspectFace * OpenGl_Workspace::SetAspectFace (const OpenGl_AspectFace* theAspect)
+{
+  const OpenGl_AspectFace* aPrevAspectFace = myAspectFaceSet;
+  myAspectFaceSet = theAspect;
+  return aPrevAspectFace;
+}
 
-  myTextureBound->Unbind (myGlContext);
-  switch (myTextureBound->GetTarget())
+// =======================================================================
+// function : SetAspectMarker
+// purpose  :
+// =======================================================================
+const OpenGl_AspectMarker* OpenGl_Workspace::SetAspectMarker (const OpenGl_AspectMarker* theAspect)
+{
+  const OpenGl_AspectMarker* aPrevAspectMarker = myAspectMarkerSet;
+  myAspectMarkerSet = theAspect;
+  return aPrevAspectMarker;
+}
+
+// =======================================================================
+// function : SetAspectText
+// purpose  :
+// =======================================================================
+const OpenGl_AspectText * OpenGl_Workspace::SetAspectText (const OpenGl_AspectText* theAspect)
+{
+  const OpenGl_AspectText* aPrevAspectText = myAspectTextSet;
+  myAspectTextSet = theAspect;
+  return aPrevAspectText;
+}
+
+// =======================================================================
+// function : ApplyAspectFace
+// purpose  :
+// =======================================================================
+const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
+{
+  if (myView->BackfacingModel() == Graphic3d_TOBM_AUTOMATIC)
   {
-    case GL_TEXTURE_1D:
+    // manage back face culling mode, disable culling when clipping is enabled
+    bool toSuppressBackFaces = myToAllowFaceCulling
+                            && myAspectFaceSet->Aspect()->ToSuppressBackFaces();
+    if (toSuppressBackFaces)
     {
-      if (myTextureBound->GetParams()->GenMode() != GL_NONE)
+      if (myGlContext->Clipping().IsClippingOrCappingOn()
+       || myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HATCH)
       {
-        glDisable (GL_TEXTURE_GEN_S);
+        toSuppressBackFaces = false;
       }
-      glDisable (GL_TEXTURE_1D);
-      break;
     }
-    case GL_TEXTURE_2D:
+    if (toSuppressBackFaces)
     {
-      if (myTextureBound->GetParams()->GenMode() != GL_NONE)
+      if ((float )myAspectFaceSet->Aspect()->FrontMaterial().Transparency() != 0.0f)
       {
-        glDisable (GL_TEXTURE_GEN_S);
-        glDisable (GL_TEXTURE_GEN_T);
-        if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE)
-        {
-          glDisable (GL_POINT_SPRITE);
-        }
+        // disable culling in case of translucent shading aspect
+        toSuppressBackFaces = false;
       }
-      glDisable (GL_TEXTURE_2D);
-      break;
     }
-    default: break;
+    myGlContext->SetCullBackFaces (toSuppressBackFaces);
   }
 
-  Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
-  myTextureBound.Nullify();
-  return aPrevTexture;
-}
-
-// =======================================================================
-// function : setTextureParams
-// purpose  :
-// =======================================================================
-void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)&                theTexture,
-                                         const Handle(Graphic3d_TextureParams)& theParams)
-{
-  const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
-  if (aParams.IsNull())
+  if (myAspectFaceSet->Aspect() == myAspectFaceApplied
+   && myHighlightStyle == myAspectFaceAppliedWithHL)
   {
-    return;
+    return myAspectFaceSet;
   }
+  myAspectFaceAppliedWithHL = myHighlightStyle;
 
-  GLint aMatrixMode = GL_TEXTURE;
-  glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
-
-  // setup texture matrix
-  glMatrixMode (GL_TEXTURE);
-  glLoadIdentity();
-  const Graphic3d_Vec2& aScale = aParams->Scale();
-  const Graphic3d_Vec2& aTrans = aParams->Translation();
-  glScalef     ( aScale.x(),  aScale.y(), 1.0f);
-  glTranslatef (-aTrans.x(), -aTrans.y(), 0.0f);
-  glRotatef (-aParams->Rotation(), 0.0f, 0.0f, 1.0f);
-
-  // setup generation of texture coordinates
-  switch (aParams->GenMode())
+#if !defined(GL_ES_VERSION_2_0)
+  const Aspect_InteriorStyle anIntstyle = myAspectFaceSet->Aspect()->InteriorStyle();
+  if (myAspectFaceApplied.IsNull()
+   || myAspectFaceApplied->InteriorStyle() != anIntstyle)
   {
-    case Graphic3d_TOTM_OBJECT:
+    switch (anIntstyle)
     {
-      glTexGeni  (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
-      glTexGenfv (GL_S, GL_OBJECT_PLANE,     aParams->GenPlaneS().GetData());
-      if (theTexture->GetTarget() != GL_TEXTURE_1D)
+      case Aspect_IS_EMPTY:
+      case Aspect_IS_HOLLOW:
       {
-        glTexGeni  (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
-        glTexGenfv (GL_T, GL_OBJECT_PLANE,     aParams->GenPlaneT().GetData());
+        myGlContext->SetPolygonMode (GL_LINE);
+        break;
       }
-      break;
-    }
-    case Graphic3d_TOTM_SPHERE:
-    {
-      glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
-      if (theTexture->GetTarget() != GL_TEXTURE_1D)
+      case Aspect_IS_HATCH:
       {
-        glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
+        myGlContext->SetPolygonMode (GL_FILL);
+        myGlContext->SetPolygonHatchEnabled (true);
+        break;
       }
-      break;
-    }
-    case Graphic3d_TOTM_EYE:
-    {
-      glMatrixMode (GL_MODELVIEW);
-      glPushMatrix();
-      glLoadIdentity();
-
-      glTexGeni  (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
-      glTexGenfv (GL_S, GL_EYE_PLANE,        aParams->GenPlaneS().GetData());
-
-      if (theTexture->GetTarget() != GL_TEXTURE_1D)
+      case Aspect_IS_SOLID:
+      case Aspect_IS_HIDDENLINE:
       {
-        glTexGeni  (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
-        glTexGenfv (GL_T, GL_EYE_PLANE,        aParams->GenPlaneT().GetData());
+        myGlContext->SetPolygonMode (GL_FILL);
+        myGlContext->SetPolygonHatchEnabled (false);
+        break;
       }
-      glPopMatrix();
-      break;
-    }
-    case Graphic3d_TOTM_SPRITE:
-    {
-      if (GetGlContext()->core20 != NULL)
+      case Aspect_IS_POINT:
       {
-        glEnable  (GL_POINT_SPRITE);
-        glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
-        glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
-        GetGlContext()->core15->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
+        myGlContext->SetPolygonMode (GL_POINT);
+        break;
       }
-      break;
     }
-    case Graphic3d_TOTM_MANUAL:
-    default: break;
   }
 
-  // setup lighting
-  if (aParams->GenMode() != Graphic3d_TOTM_SPRITE)
+  if (anIntstyle == Aspect_IS_HATCH)
   {
-    glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aParams->IsModulate() ? GL_MODULATE : GL_DECAL);
+    myGlContext->SetPolygonHatchStyle (myAspectFaceSet->Aspect()->HatchStyle());
   }
+#endif
 
-  // setup texture filtering and wrapping
-  //if (theTexture->GetParams() != theParams)
-  const GLenum aFilter   = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
-  const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : GL_CLAMP;
-  switch (theTexture->GetTarget())
+  // Aspect_POM_None means: do not change current settings
+  if ((myAspectFaceSet->Aspect()->PolygonOffset().Mode & Aspect_POM_None) != Aspect_POM_None)
   {
-    case GL_TEXTURE_1D:
-    {
-      glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
-      glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
-      glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S,     aWrapMode);
-      break;
-    }
-    case GL_TEXTURE_2D:
+    if (myPolygonOffsetApplied.Mode   != myAspectFaceSet->Aspect()->PolygonOffset().Mode
+     || myPolygonOffsetApplied.Factor != myAspectFaceSet->Aspect()->PolygonOffset().Factor
+     || myPolygonOffsetApplied.Units  != myAspectFaceSet->Aspect()->PolygonOffset().Units)
     {
-      GLenum aFilterMin = aFilter;
-      if (theTexture->HasMipmaps())
-      {
-        aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
-        if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
-        {
-          aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
-        }
-        else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
-        {
-          aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
-        }
-
-        if (myGlContext->extAnis)
-        {
-          // setup degree of anisotropy filter
-          const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
-          switch (aParams->AnisoFilter())
-          {
-            case Graphic3d_LOTA_QUALITY:
-            {
-              glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aMaxDegree);
-              break;
-            }
-            case Graphic3d_LOTA_MIDDLE:
-            {
-
-              glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2));
-              break;
-            }
-            case Graphic3d_LOTA_FAST:
-            {
-              glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2);
-              break;
-            }
-            case Graphic3d_LOTA_OFF:
-            default:
-            {
-              glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
-              break;
-            }
-          }
-        }
-      }
-      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
-      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
-      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,     aWrapMode);
-      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,     aWrapMode);
-      break;
+      SetPolygonOffset (myAspectFaceSet->Aspect()->PolygonOffset());
     }
-    default: break;
   }
 
-  switch (theTexture->GetTarget())
+  // Case of hidden line
+  if (myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
   {
-    case GL_TEXTURE_1D:
-    {
-      if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
-      {
-        glEnable (GL_TEXTURE_GEN_S);
-      }
-      glEnable (GL_TEXTURE_1D);
-      break;
-    }
-    case GL_TEXTURE_2D:
-    {
-      if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
-      {
-        glEnable (GL_TEXTURE_GEN_S);
-        glEnable (GL_TEXTURE_GEN_T);
-      }
-      glEnable (GL_TEXTURE_2D);
-      break;
-    }
-    default: break;
+    // copy all values including line edge aspect
+    *myAspectFaceHl.Aspect().operator->() = *myAspectFaceSet->Aspect();
+    myAspectFaceHl.SetAspectEdge (myAspectFaceSet->AspectEdge());
+    myAspectFaceHl.Aspect()->SetInteriorColor (myView->BackgroundColor().GetRGB());
+    myAspectFaceHl.SetNoLighting (true);
+    myAspectFaceSet = &myAspectFaceHl;
   }
-
-  glMatrixMode (aMatrixMode); // turn back active matrix
-  theTexture->SetParams (aParams);
-}
-
-// =======================================================================
-// function : EnableTexture
-// purpose  :
-// =======================================================================
-Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)&          theTexture,
-                                                        const Handle(Graphic3d_TextureParams)& theParams)
-{
-  if (theTexture.IsNull() || !theTexture->IsValid())
+  else
   {
-    return DisableTexture();
+    myGlContext->SetShadingMaterial (myAspectFaceSet, myHighlightStyle);
   }
 
-  if (myTextureBound == theTexture
-   && (theParams.IsNull() || theParams == theTexture->GetParams()))
+  if (myAspectFaceSet->Aspect()->ToMapTexture())
   {
-    // already bound
-    return myTextureBound;
+    myGlContext->BindTextures (myAspectFaceSet->TextureSet (myGlContext));
+  }
+  else
+  {
+    myGlContext->BindTextures (myEnvironmentTexture);
   }
 
-  Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
-  myTextureBound = theTexture;
-  myTextureBound->Bind (myGlContext);
-  setTextureParams (myTextureBound, theParams);
-
-  return aPrevTexture;
+  myAspectFaceApplied = myAspectFaceSet->Aspect();
+  return myAspectFaceSet;
 }
 
-// =======================================================================
-// function : Redraw
-// purpose  :
-// =======================================================================
-void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
-                               const Aspect_CLayer2d& theCUnderLayer,
-                               const Aspect_CLayer2d& theCOverLayer)
+//=======================================================================
+//function : SetPolygonOffset
+//purpose  :
+//=======================================================================
+void OpenGl_Workspace::SetPolygonOffset (const Graphic3d_PolygonOffset& theParams)
 {
-  if (!Activate())
+  myPolygonOffsetApplied = theParams;
+
+  if ((theParams.Mode & Aspect_POM_Fill) == Aspect_POM_Fill)
   {
-    return;
+    glEnable (GL_POLYGON_OFFSET_FILL);
   }
-
-  myIsCullingEnabled = theCView.IsCullingEnabled;
-
-  // release pending GL resources
-  Handle(OpenGl_Context) aGlCtx = GetGlContext();
-  aGlCtx->ReleaseDelayed();
-
-  // fetch OpenGl context state
-  aGlCtx->FetchState();
-
-  Tint toSwap = (aGlCtx->IsRender()); // swap buffers
-  GLint aViewPortBack[4];
-  OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
-  if (aFrameBuffer != NULL)
+  else
   {
-    glGetIntegerv (GL_VIEWPORT, aViewPortBack);
-    aFrameBuffer->SetupViewport (aGlCtx);
-    aFrameBuffer->BindBuffer    (aGlCtx);
-    toSwap = 0; // no need to swap buffers
+    glDisable (GL_POLYGON_OFFSET_FILL);
   }
 
-  if (theCView.RenderParams.Method != Graphic3d_RM_RAYTRACING || myComputeInitStatus == OpenGl_RT_FAIL)
+#if !defined(GL_ES_VERSION_2_0)
+  if ((theParams.Mode & Aspect_POM_Line) == Aspect_POM_Line)
   {
-    const Standard_Boolean isImmediate = !myView->ImmediateStructures().IsEmpty();
-    redraw1 (theCView, theCUnderLayer, theCOverLayer, isImmediate ? 0 : toSwap);
-    if (isImmediate)
-    {
-      RedrawImmediate (theCView, theCUnderLayer, theCOverLayer, Standard_True);
-    }
-
-    theCView.WasRedrawnGL = Standard_True;
+    glEnable (GL_POLYGON_OFFSET_LINE);
   }
   else
   {
-    int aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWidth;
-    int aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myHeight;
-
-    Raytrace (theCView, aSizeX, aSizeY, toSwap, aFrameBuffer);
-
-    theCView.WasRedrawnGL = Standard_False;
+    glDisable (GL_POLYGON_OFFSET_LINE);
   }
 
-  if (aFrameBuffer != NULL)
+  if ((theParams.Mode & Aspect_POM_Point) == Aspect_POM_Point)
   {
-    aFrameBuffer->UnbindBuffer (aGlCtx);
-    // move back original viewport
-    glViewport (aViewPortBack[0], aViewPortBack[1], aViewPortBack[2], aViewPortBack[3]);
+    glEnable (GL_POLYGON_OFFSET_POINT);
   }
-
-#if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
-  if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow))
+  else
   {
-    GLint params[4];
-    glGetIntegerv (GL_VIEWPORT, params);
-    int nWidth  = params[2] & ~0x7;
-    int nHeight = params[3] & ~0x7;
-
-    const int nBitsPerPixel = 24;
-    GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
-
-    glPixelStorei (GL_PACK_ALIGNMENT, 1);
-    glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData);
-    OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel);
-    delete[] aDumpData;
+    glDisable (GL_POLYGON_OFFSET_POINT);
   }
 #endif
-
-  // reset render mode state
-  aGlCtx->FetchState();
+  glPolygonOffset (theParams.Factor, theParams.Units);
 }
 
 // =======================================================================
-// function : redraw1
+// function : ApplyAspectMarker
 // purpose  :
 // =======================================================================
-void OpenGl_Workspace::redraw1 (const Graphic3d_CView& theCView,
-                                const Aspect_CLayer2d& theCUnderLayer,
-                                const Aspect_CLayer2d& theCOverLayer,
-                                const int              theToSwap)
+const OpenGl_AspectMarker* OpenGl_Workspace::ApplyAspectMarker()
 {
-  if (myView.IsNull())
-  {
-    return;
-  }
-
-  // request reset of material
-  NamedStatus |= OPENGL_NS_RESMAT;
-
-  GLbitfield toClear = GL_COLOR_BUFFER_BIT;
-  if (myUseZBuffer)
+  if (myAspectMarkerSet->Aspect() != myAspectMarkerApplied)
   {
-    glDepthFunc (GL_LEQUAL);
-    glDepthMask (GL_TRUE);
-    if (myUseDepthTest)
+    if (myAspectMarkerApplied.IsNull()
+    || (myAspectMarkerSet->Aspect()->Scale() != myAspectMarkerApplied->Scale()))
     {
-      glEnable (GL_DEPTH_TEST);
+    #if !defined(GL_ES_VERSION_2_0)
+      glPointSize (myAspectMarkerSet->Aspect()->Scale());
+    #ifdef HAVE_GL2PS
+      gl2psPointSize (myAspectMarkerSet->Aspect()->Scale());
+    #endif
+    #endif
     }
-    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;
+    myAspectMarkerApplied = myAspectMarkerSet->Aspect();
   }
+  return myAspectMarkerSet;
 }
 
 // =======================================================================
-// function : copyBackToFront
+// function : Width
 // purpose  :
 // =======================================================================
-void OpenGl_Workspace::copyBackToFront()
+Standard_Integer OpenGl_Workspace::Width()  const
 {
-  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;
+  return !myView->GlWindow().IsNull() ? myView->GlWindow()->Width() : 0;
 }
 
 // =======================================================================
-// function : DisplayCallback
+// function : Height
 // purpose  :
 // =======================================================================
-void OpenGl_Workspace::DisplayCallback (const Graphic3d_CView& theCView,
-                                        Standard_Integer       theReason)
+Standard_Integer OpenGl_Workspace::Height() const
 {
-  if (theCView.GDisplayCB == NULL)
-  {
-    return;
-  }
+  return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0;
+}
 
-  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 : IsCullingEnabled
+// purpose  :
+// =======================================================================
+Standard_Boolean OpenGl_Workspace::IsCullingEnabled() const
+{
+  return myView->IsCullingEnabled();
 }
 
 // =======================================================================
-// function : RedrawImmediate
+// function : FBOCreate
 // purpose  :
 // =======================================================================
-void OpenGl_Workspace::RedrawImmediate (const Graphic3d_CView& theCView,
-                                        const Aspect_CLayer2d& theCUnderLayer,
-                                        const Aspect_CLayer2d& theCOverLayer,
-                                        const Standard_Boolean theToForce)
+Handle(OpenGl_FrameBuffer) OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth,
+                                                        const Standard_Integer theHeight)
 {
+  // activate OpenGL context
   if (!Activate())
-  {
-    return;
-  }
+    return Handle(OpenGl_FrameBuffer)();
 
-  GLboolean isDoubleBuffer = GL_FALSE;
-  glGetBooleanv (GL_DOUBLEBUFFER, &isDoubleBuffer);
-  if (myView->ImmediateStructures().IsEmpty())
+  // create the FBO
+  const Handle(OpenGl_Context)& aCtx = GetGlContext();
+  aCtx->BindTextures (Handle(OpenGl_TextureSet)());
+  Handle(OpenGl_FrameBuffer) aFrameBuffer = new OpenGl_FrameBuffer();
+  if (!aFrameBuffer->Init (aCtx, theWidth, theHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, 0))
   {
-    if (theToForce
-     || !myIsImmediateDrawn)
-    {
-      myIsImmediateDrawn = Standard_False;
-      return;
-    }
-
-    if (myBackBufferRestored
-     && isDoubleBuffer)
-    {
-      copyBackToFront();
-      glFlush();
-    }
-    else
-    {
-      Redraw (theCView, theCUnderLayer, theCOverLayer);
-    }
-    return;
+    aFrameBuffer->Release (aCtx.operator->());
+    return Handle(OpenGl_FrameBuffer)();
   }
+  return aFrameBuffer;
+}
 
-  if (isDoubleBuffer && myTransientDrawToFront)
-  {
-    if (!myBackBufferRestored)
-    {
-      Redraw (theCView, theCUnderLayer, theCOverLayer);
-      return;
-    }
-    copyBackToFront();
-    MakeFrontBufCurrent();
-  }
-  else
+// =======================================================================
+// function : FBORelease
+// purpose  :
+// =======================================================================
+void OpenGl_Workspace::FBORelease (Handle(OpenGl_FrameBuffer)& theFbo)
+{
+  // activate OpenGL context
+  if (!Activate()
+   || theFbo.IsNull())
   {
-    myBackBufferRestored = Standard_False;
+    return;
   }
-  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);
-  }
+  theFbo->Release (GetGlContext().operator->());
+  theFbo.Nullify();
+}
 
-  NamedStatus &= ~OPENGL_NS_IMMEDIATE;
+// =======================================================================
+// function : BufferDump
+// purpose  :
+// =======================================================================
+Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
+                                               Image_PixMap&                     theImage,
+                                               const Graphic3d_BufferType&       theBufferType)
+{
+  return !theImage.IsEmpty()
+      && Activate()
+      && OpenGl_FrameBuffer::BufferDump (GetGlContext(), theFbo, theImage, theBufferType);
+}
 
-  if (isDoubleBuffer && myTransientDrawToFront)
+// =======================================================================
+// function : ShouldRender
+// purpose  :
+// =======================================================================
+Standard_Boolean OpenGl_RaytraceFilter::ShouldRender (const Handle(OpenGl_Workspace)& theWorkspace,
+                                                      const OpenGl_Element*           theElement)
+{
+  Standard_Boolean aPrevFilterResult = Standard_True;
+  if (!myPrevRenderFilter.IsNull())
   {
-    glFlush();
-    MakeBackBufCurrent();
+    aPrevFilterResult = myPrevRenderFilter->ShouldRender (theWorkspace, theElement);
   }
+  return aPrevFilterResult &&
+    !OpenGl_Raytrace::IsRaytracedElement (theElement);
 }