0027925: Visualization - implement order-independent transparency algorithm within...
[occt.git] / src / OpenGl / OpenGl_Workspace.cxx
index 640e17e..7ec727d 100644 (file)
@@ -15,8 +15,6 @@
 
 #include <OpenGl_Workspace.hxx>
 
-#include <InterfaceGraphic.hxx>
-
 #include <OpenGl_ArbFBO.hxx>
 #include <OpenGl_AspectLine.hxx>
 #include <OpenGl_AspectFace.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_Window.hxx>
 
 #include <Graphic3d_TextureParams.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>
 
 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_Matrix myDefaultMatrix =
   {
@@ -62,22 +66,22 @@ 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);
+
   // 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
   {
@@ -85,13 +89,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
   {
@@ -99,13 +100,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
   {
@@ -113,21 +111,18 @@ 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;
+  ChangeShine()        = 128.0f * theMat.Shininess();
+  ChangeTransparency() = 1.0f - theMat.Transparency();
 }
 
 // =======================================================================
@@ -135,41 +130,27 @@ void OpenGl_Material::Init (const OPENGL_SURF_PROP& theProp)
 // purpose  :
 // =======================================================================
 OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow)
-: NamedStatus (0),
-  HighlightColor (&THE_WHITE_COLOR),
-  myView (theView),
+: myView (theView),
   myWindow (theWindow),
   myGlContext (!theWindow.IsNull() ? theWindow->GetGlContext() : NULL),
   myUseZBuffer    (Standard_True),
   myUseDepthWrite (Standard_True),
-  myUseGLLight (Standard_True),
   //
-  AspectLine_set (&myDefaultAspectLine),
-  AspectLine_applied (NULL),
-  AspectFace_set (&myDefaultAspectFace),
-  AspectFace_applied (NULL),
-  AspectMarker_set (&myDefaultAspectMarker),
-  AspectMarker_applied (NULL),
+  myAspectLineSet (&myDefaultAspectLine),
+  myAspectFaceSet (&myDefaultAspectFace),
+  myAspectMarkerSet (&myDefaultAspectMarker),
+  myAspectTextSet (&myDefaultAspectText),
+  //
   ViewMatrix_applied (&myDefaultMatrix),
   StructureMatrix_applied (&myDefaultMatrix),
-  myCullingMode (TelCullUndefined),
-  myModelViewMatrix (myDefaultMatrix),
-  PolygonOffset_applied (THE_DEFAULT_POFFSET)
+  myToAllowFaceCulling (false),
+  myModelViewMatrix (myDefaultMatrix)
 {
   if (!myGlContext.IsNull() && myGlContext->MakeCurrent())
   {
     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
-
   #if !defined(GL_ES_VERSION_2_0)
     if (myGlContext->core11 != NULL)
     {
@@ -187,24 +168,12 @@ OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Wi
   #endif
   }
 
-  myDefaultCappingAlgoFilter         = new OpenGl_CappingAlgoFilter();
-  myNoneCulling.ChangeCullingMode()  = TelCullNone;
-  myNoneCulling.ChangeEdge()         = 0;
-  myFrontCulling.ChangeCullingMode() = TelCullBack;
-  myFrontCulling.ChangeEdge()        = 0;
-}
+  myDefaultCappingAlgoFilter = new OpenGl_CappingAlgoFilter();
 
-// =======================================================================
-// function : ~OpenGl_Workspace
-// purpose  :
-// =======================================================================
-OpenGl_Workspace::~OpenGl_Workspace()
-{
-  if (!myLineAttribs.IsNull())
-  {
-    myLineAttribs.Nullify();
-    myGlContext->ReleaseResource ("OpenGl_LineAttributes", Standard_True);
-  }
+  myNoneCulling .Aspect()->SetSuppressBackFaces (false);
+  myNoneCulling .Aspect()->SetDrawEdges (false);
+  myFrontCulling.Aspect()->SetSuppressBackFaces (true);
+  myFrontCulling.Aspect()->SetDrawEdges (false);
 }
 
 // =======================================================================
@@ -223,6 +192,13 @@ Standard_Boolean OpenGl_Workspace::Activate()
 
   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;
 }
 
@@ -234,24 +210,23 @@ void OpenGl_Workspace::ResetAppliedAspect()
 {
   myGlContext->BindDefaultVao();
 
-  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;
-  PolygonOffset_applied = THE_DEFAULT_POFFSET;
-  myCullingMode         = TelCullUndefined;
-
-  AspectLine(Standard_True);
-  AspectFace(Standard_True);
-  AspectMarker(Standard_True);
-  AspectText(Standard_True);
-
-  myGlContext->SetTypeOfLine (myDefaultAspectLine.Type());
-  myGlContext->SetLineWidth  (myDefaultAspectLine.Width());
+  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());
 }
 
 // =======================================================================
@@ -404,7 +379,6 @@ void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)&
           glEnable  (GL_POINT_SPRITE);
           glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
           anEnvMode = GL_REPLACE;
-          myGlContext->core15->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
         }
         break;
       }
@@ -594,345 +568,116 @@ Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Text
   return aPrevTexture;
 }
 
-// =======================================================================
-// function : TelUpdatePolygonOffsets
-// purpose  :
-// =======================================================================
-static void TelUpdatePolygonOffsets (const TEL_POFFSET_PARAM& theOffsetData)
-{
-  if ((theOffsetData.mode & Aspect_POM_Fill) == Aspect_POM_Fill)
-  {
-    glEnable (GL_POLYGON_OFFSET_FILL);
-  }
-  else
-  {
-    glDisable (GL_POLYGON_OFFSET_FILL);
-  }
-
-#if !defined(GL_ES_VERSION_2_0)
-  if ((theOffsetData.mode & Aspect_POM_Line) == Aspect_POM_Line)
-  {
-    glEnable (GL_POLYGON_OFFSET_LINE);
-  }
-  else
-  {
-    glDisable (GL_POLYGON_OFFSET_LINE);
-  }
-
-  if ((theOffsetData.mode & Aspect_POM_Point) == Aspect_POM_Point)
-  {
-    glEnable (GL_POLYGON_OFFSET_POINT);
-  }
-  else
-  {
-    glDisable (GL_POLYGON_OFFSET_POINT);
-  }
-#endif
-
-  glPolygonOffset (theOffsetData.factor, theOffsetData.units);
-}
-
-// =======================================================================
-// function : updateMaterial
-// purpose  :
-// =======================================================================
-void OpenGl_Workspace::updateMaterial (const int theFlag)
-{
-  // Case of hidden line
-  if (AspectFace_set->InteriorStyle() == Aspect_IS_HIDDENLINE)
-  {
-    myAspectFaceHl = *AspectFace_set; // copy all values including line edge aspect
-    myAspectFaceHl.ChangeIntFront().matcol     = myView->BackgroundColor();
-    myAspectFaceHl.ChangeIntFront().color_mask = 0;
-    myAspectFaceHl.ChangeIntFront().color_mask = 0;
-
-    AspectFace_set = &myAspectFaceHl;
-    return;
-  }
-
-  const OPENGL_SURF_PROP* aProps = &AspectFace_set->IntFront();
-  GLenum aFace = GL_FRONT_AND_BACK;
-  if (theFlag == TEL_BACK_MATERIAL)
-  {
-    aFace  = GL_BACK;
-    aProps = &AspectFace_set->IntBack();
-  }
-  else if (AspectFace_set->DistinguishingMode() == TOn
-        && !(NamedStatus & OPENGL_NS_RESMAT))
-  {
-    aFace = GL_FRONT;
-  }
-
-  myMatTmp.Init (*aProps);
-
-  // handling transparency
-  if (NamedStatus & OPENGL_NS_2NDPASSDO)
-  {
-    // second pass
-    myMatTmp.Diffuse.a() = aProps->env_reflexion;
-  }
-  else
-  {
-    if (aProps->env_reflexion != 0.0f)
-    {
-      // if the material reflects the environment scene, the second pass is needed
-      NamedStatus |= OPENGL_NS_2NDPASSNEED;
-    }
-
-    if (aProps->trans != 1.0f)
-    {
-      // render transparent
-      myMatTmp.Diffuse.a() = aProps->trans;
-      glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-      glEnable    (GL_BLEND);
-      if (myUseDepthWrite)
-      {
-        glDepthMask (GL_FALSE);
-      }
-    }
-    else
-    {
-      // render opaque
-      if ((NamedStatus & OPENGL_NS_ANTIALIASING) == 0)
-      {
-        glBlendFunc (GL_ONE, GL_ZERO);
-        glDisable   (GL_BLEND);
-      }
-      if (myUseDepthWrite)
-      {
-        glDepthMask (GL_TRUE);
-      }
-    }
-  }
-
-  // do not update material properties in case of zero reflection mode,
-  // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway.
-  if (aProps->color_mask == 0)
-  {
-    return;
-  }
-
-  // reset material
-  if (NamedStatus & OPENGL_NS_RESMAT)
-  {
-  #if !defined(GL_ES_VERSION_2_0)
-    if (myGlContext->core11 != NULL)
-    {
-      myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT,   myMatTmp.Ambient.GetData());
-      myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE,   myMatTmp.Diffuse.GetData());
-      myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR,  myMatTmp.Specular.GetData());
-      myGlContext->core11->glMaterialfv (aFace, GL_EMISSION,  myMatTmp.Emission.GetData());
-      myGlContext->core11->glMaterialf  (aFace, GL_SHININESS, myMatTmp.Shine());
-    }
-  #endif
-
-    if (theFlag == TEL_FRONT_MATERIAL)
-    {
-      myMatFront = myMatTmp;
-      myMatBack  = myMatTmp;
-    }
-    else
-    {
-      myMatBack = myMatTmp;
-    }
-
-    NamedStatus &= ~OPENGL_NS_RESMAT;
-    return;
-  }
-
-  // reduce updates
-  OpenGl_Material& anOld = (theFlag == TEL_FRONT_MATERIAL)
-                         ? myMatFront
-                         : myMatBack;
-#if !defined(GL_ES_VERSION_2_0)
-  if (myGlContext->core11 != NULL)
-  {
-    if (myMatTmp.Ambient.r() != anOld.Ambient.r()
-     || myMatTmp.Ambient.g() != anOld.Ambient.g()
-     || myMatTmp.Ambient.b() != anOld.Ambient.b())
-    {
-      myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT, myMatTmp.Ambient.GetData());
-    }
-    if (myMatTmp.Diffuse.r() != anOld.Diffuse.r()
-     || myMatTmp.Diffuse.g() != anOld.Diffuse.g()
-     || myMatTmp.Diffuse.b() != anOld.Diffuse.b()
-     || fabs (myMatTmp.Diffuse.a() - anOld.Diffuse.a()) > 0.01f)
-    {
-      myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE, myMatTmp.Diffuse.GetData());
-    }
-    if (myMatTmp.Specular.r() != anOld.Specular.r()
-     || myMatTmp.Specular.g() != anOld.Specular.g()
-     || myMatTmp.Specular.b() != anOld.Specular.b())
-    {
-      myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR, myMatTmp.Specular.GetData());
-    }
-    if (myMatTmp.Emission.r() != anOld.Emission.r()
-     || myMatTmp.Emission.g() != anOld.Emission.g()
-     || myMatTmp.Emission.b() != anOld.Emission.b())
-    {
-      myGlContext->core11->glMaterialfv (aFace, GL_EMISSION, myMatTmp.Emission.GetData());
-    }
-    if (myMatTmp.Shine() != anOld.Shine())
-    {
-      myGlContext->core11->glMaterialf (aFace, GL_SHININESS, myMatTmp.Shine());
-    }
-  }
-#endif
-  anOld = myMatTmp;
-  if (aFace == GL_FRONT_AND_BACK)
-  {
-    myMatBack = myMatTmp;
-  }
-}
-
 // =======================================================================
 // function : SetAspectLine
 // purpose  :
 // =======================================================================
-const OpenGl_AspectLine * OpenGl_Workspace::SetAspectLine(const OpenGl_AspectLine *AnAspect)
+const OpenGl_AspectLine* OpenGl_Workspace::SetAspectLine (const OpenGl_AspectLine* theAspect)
 {
-  const OpenGl_AspectLine *AspectLine_old = AspectLine_set;
-  AspectLine_set = AnAspect;
-  return AspectLine_old;
+  const OpenGl_AspectLine* aPrevAspectLine = myAspectLineSet;
+  myAspectLineSet = theAspect;
+  return aPrevAspectLine;
 }
 
 // =======================================================================
 // function : SetAspectFace
 // purpose  :
 // =======================================================================
-const OpenGl_AspectFace * OpenGl_Workspace::SetAspectFace(const OpenGl_AspectFace *AnAspect)
+const OpenGl_AspectFace * OpenGl_Workspace::SetAspectFace (const OpenGl_AspectFace* theAspect)
 {
-  const OpenGl_AspectFace *AspectFace_old = AspectFace_set;
-  AspectFace_set = AnAspect;
-  return AspectFace_old;
+  const OpenGl_AspectFace* aPrevAspectFace = myAspectFaceSet;
+  myAspectFaceSet = theAspect;
+  return aPrevAspectFace;
 }
 
 // =======================================================================
 // function : SetAspectMarker
 // purpose  :
 // =======================================================================
-const OpenGl_AspectMarker * OpenGl_Workspace::SetAspectMarker(const OpenGl_AspectMarker *AnAspect)
+const OpenGl_AspectMarker* OpenGl_Workspace::SetAspectMarker (const OpenGl_AspectMarker* theAspect)
 {
-  const OpenGl_AspectMarker *AspectMarker_old = AspectMarker_set;
-  AspectMarker_set = AnAspect;
-  return AspectMarker_old;
+  const OpenGl_AspectMarker* aPrevAspectMarker = myAspectMarkerSet;
+  myAspectMarkerSet = theAspect;
+  return aPrevAspectMarker;
 }
 
 // =======================================================================
 // function : SetAspectText
 // purpose  :
 // =======================================================================
-const OpenGl_AspectText * OpenGl_Workspace::SetAspectText(const OpenGl_AspectText *AnAspect)
-{
-  const OpenGl_AspectText *AspectText_old = AspectText_set;
-  AspectText_set = AnAspect;
-  return AspectText_old;
-}
-
-// =======================================================================
-// function : AspectLine
-// purpose  :
-// =======================================================================
-const OpenGl_AspectLine * OpenGl_Workspace::AspectLine(const Standard_Boolean theWithApply)
+const OpenGl_AspectText * OpenGl_Workspace::SetAspectText (const OpenGl_AspectText* theAspect)
 {
-  if (theWithApply)
-  {
-    AspectLine_applied = AspectLine_set;
-  }
-
-  return AspectLine_set;
+  const OpenGl_AspectText* aPrevAspectText = myAspectTextSet;
+  myAspectTextSet = theAspect;
+  return aPrevAspectText;
 }
 
 // =======================================================================
-// function : AspectFace
+// function : ApplyAspectFace
 // purpose  :
 // =======================================================================
-const OpenGl_AspectFace* OpenGl_Workspace::AspectFace (const Standard_Boolean theToApply)
+const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
 {
-  if (!theToApply)
-  {
-    return AspectFace_set;
-  }
-
   if (myView->BackfacingModel() == Graphic3d_TOBM_AUTOMATIC)
   {
     // manage back face culling mode, disable culling when clipping is enabled
-    TelCullMode aCullingMode = (myGlContext->Clipping().IsClippingOrCappingOn()
-                             || AspectFace_set->InteriorStyle() == Aspect_IS_HATCH)
-                             ? TelCullNone
-                             : (TelCullMode )AspectFace_set->CullingMode();
-    if (aCullingMode != TelCullNone
-     && !(NamedStatus & OPENGL_NS_2NDPASSDO))
+    bool toSuppressBackFaces = myToAllowFaceCulling
+                            && myAspectFaceSet->Aspect()->ToSuppressBackFaces();
+    if (toSuppressBackFaces)
     {
-      // disable culling in case of translucent shading aspect
-      if (AspectFace_set->IntFront().trans != 1.0f)
+      if (myGlContext->Clipping().IsClippingOrCappingOn()
+       || myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HATCH)
       {
-        aCullingMode = TelCullNone;
+        toSuppressBackFaces = false;
       }
     }
-    if (myCullingMode != aCullingMode)
+    if (toSuppressBackFaces)
     {
-      myCullingMode = aCullingMode;
-      switch (myCullingMode)
+      if ((float )myAspectFaceSet->Aspect()->FrontMaterial().Transparency() != 0.0f)
       {
-        case TelCullNone:
-        case TelCullUndefined:
-        {
-          glDisable (GL_CULL_FACE);
-          break;
-        }
-        case TelCullFront:
-        {
-          glCullFace (GL_FRONT);
-          glEnable (GL_CULL_FACE);
-          break;
-        }
-        case TelCullBack:
-        {
-          glCullFace (GL_BACK);
-          glEnable (GL_CULL_FACE);
-          break;
-        }
+        // disable culling in case of translucent shading aspect
+        toSuppressBackFaces = false;
       }
     }
+    myGlContext->SetCullBackFaces (toSuppressBackFaces);
   }
 
-  if (AspectFace_set == AspectFace_applied)
+  if (myAspectFaceSet->Aspect() == myAspectFaceApplied
+   && myHighlightStyle == myAspectFaceAppliedWithHL)
   {
-    return AspectFace_set;
+    return myAspectFaceSet;
   }
+  myAspectFaceAppliedWithHL = myHighlightStyle;
 
 #if !defined(GL_ES_VERSION_2_0)
-  const Aspect_InteriorStyle anIntstyle = AspectFace_set->InteriorStyle();
-  if (AspectFace_applied == NULL || AspectFace_applied->InteriorStyle() != anIntstyle)
+  const Aspect_InteriorStyle anIntstyle = myAspectFaceSet->Aspect()->InteriorStyle();
+  if (myAspectFaceApplied.IsNull()
+   || myAspectFaceApplied->InteriorStyle() != anIntstyle)
   {
     switch (anIntstyle)
     {
       case Aspect_IS_EMPTY:
       case Aspect_IS_HOLLOW:
       {
-        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
+        myGlContext->SetPolygonMode (GL_LINE);
         break;
       }
       case Aspect_IS_HATCH:
       {
-        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
-        myLineAttribs->SetTypeOfHatch (AspectFace_applied != NULL ? AspectFace_applied->Hatch() : TEL_HS_SOLID);
+        myGlContext->SetPolygonMode (GL_FILL);
+        myGlContext->SetPolygonHatchEnabled (true);
         break;
       }
       case Aspect_IS_SOLID:
       case Aspect_IS_HIDDENLINE:
       {
-        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
-        if (myGlContext->core11 != NULL)
-        {
-          glDisable (GL_POLYGON_STIPPLE);
-        }
+        myGlContext->SetPolygonMode (GL_FILL);
+        myGlContext->SetPolygonHatchEnabled (false);
         break;
       }
       case Aspect_IS_POINT:
       {
-        glPolygonMode (GL_FRONT_AND_BACK, GL_POINT);
+        myGlContext->SetPolygonMode (GL_POINT);
         break;
       }
     }
@@ -940,39 +685,47 @@ const OpenGl_AspectFace* OpenGl_Workspace::AspectFace (const Standard_Boolean th
 
   if (anIntstyle == Aspect_IS_HATCH)
   {
-    const Tint hatchstyle = AspectFace_set->Hatch();
-    if (AspectFace_applied == NULL || AspectFace_applied->Hatch() != hatchstyle)
-    {
-      myLineAttribs->SetTypeOfHatch (hatchstyle);
-    }
+    myGlContext->SetPolygonHatchStyle (myAspectFaceSet->Aspect()->HatchStyle());
   }
 #endif
 
   // Aspect_POM_None means: do not change current settings
-  if ((AspectFace_set->PolygonOffset().mode & Aspect_POM_None) != Aspect_POM_None)
+  if ((myAspectFaceSet->Aspect()->PolygonOffset().Mode & Aspect_POM_None) != Aspect_POM_None)
   {
-    if (PolygonOffset_applied.mode   != AspectFace_set->PolygonOffset().mode
-     || PolygonOffset_applied.factor != AspectFace_set->PolygonOffset().factor
-     || PolygonOffset_applied.units  != AspectFace_set->PolygonOffset().units)
+    if (myPolygonOffsetApplied.Mode   != myAspectFaceSet->Aspect()->PolygonOffset().Mode
+     || myPolygonOffsetApplied.Factor != myAspectFaceSet->Aspect()->PolygonOffset().Factor
+     || myPolygonOffsetApplied.Units  != myAspectFaceSet->Aspect()->PolygonOffset().Units)
     {
-      SetPolygonOffset (AspectFace_set->PolygonOffset().mode,
-                        AspectFace_set->PolygonOffset().factor,
-                        AspectFace_set->PolygonOffset().units);
+      SetPolygonOffset (myAspectFaceSet->Aspect()->PolygonOffset());
     }
   }
 
-  updateMaterial (TEL_FRONT_MATERIAL);
-  if (AspectFace_set->DistinguishingMode() == TOn)
+  // Case of hidden line
+  if (myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
+  {
+    // 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;
+  }
+  else
   {
-    updateMaterial (TEL_BACK_MATERIAL);
+    myGlContext->SetShadingMaterial (myAspectFaceSet, myHighlightStyle);
   }
 
-  if ((NamedStatus & OPENGL_NS_FORBIDSETTEX) == 0)
+  if (myAspectFaceSet->Aspect()->ToMapTexture())
+  {
+    EnableTexture (myAspectFaceSet->TextureRes (myGlContext),
+                   myAspectFaceSet->TextureParams());
+  }
+  else
   {
-    if (AspectFace_set->DoTextureMap())
+    if (!myEnvironmentTexture.IsNull())
     {
-      EnableTexture (AspectFace_set->TextureRes (myGlContext),
-                     AspectFace_set->TextureParams());
+      EnableTexture (myEnvironmentTexture,
+                     myEnvironmentTexture->GetParams());
     }
     else
     {
@@ -980,59 +733,70 @@ const OpenGl_AspectFace* OpenGl_Workspace::AspectFace (const Standard_Boolean th
     }
   }
 
-  AspectFace_applied = AspectFace_set;
-  return AspectFace_set;
+  myAspectFaceApplied = myAspectFaceSet->Aspect();
+  return myAspectFaceSet;
 }
 
 //=======================================================================
 //function : SetPolygonOffset
 //purpose  :
 //=======================================================================
-void OpenGl_Workspace::SetPolygonOffset (int theMode,
-                                         Standard_ShortReal theFactor,
-                                         Standard_ShortReal theUnits)
+void OpenGl_Workspace::SetPolygonOffset (const Graphic3d_PolygonOffset& theParams)
 {
-  PolygonOffset_applied.mode   = theMode;
-  PolygonOffset_applied.factor = theFactor;
-  PolygonOffset_applied.units  = theUnits;
+  myPolygonOffsetApplied = theParams;
+
+  if ((theParams.Mode & Aspect_POM_Fill) == Aspect_POM_Fill)
+  {
+    glEnable (GL_POLYGON_OFFSET_FILL);
+  }
+  else
+  {
+    glDisable (GL_POLYGON_OFFSET_FILL);
+  }
+
+#if !defined(GL_ES_VERSION_2_0)
+  if ((theParams.Mode & Aspect_POM_Line) == Aspect_POM_Line)
+  {
+    glEnable (GL_POLYGON_OFFSET_LINE);
+  }
+  else
+  {
+    glDisable (GL_POLYGON_OFFSET_LINE);
+  }
 
-  TelUpdatePolygonOffsets (PolygonOffset_applied);
+  if ((theParams.Mode & Aspect_POM_Point) == Aspect_POM_Point)
+  {
+    glEnable (GL_POLYGON_OFFSET_POINT);
+  }
+  else
+  {
+    glDisable (GL_POLYGON_OFFSET_POINT);
+  }
+#endif
+  glPolygonOffset (theParams.Factor, theParams.Units);
 }
 
 // =======================================================================
-// function : AspectMarker
+// function : ApplyAspectMarker
 // purpose  :
 // =======================================================================
-const OpenGl_AspectMarker* OpenGl_Workspace::AspectMarker (const Standard_Boolean theToApply)
+const OpenGl_AspectMarker* OpenGl_Workspace::ApplyAspectMarker()
 {
-  if (theToApply && (AspectMarker_set != AspectMarker_applied))
+  if (myAspectMarkerSet->Aspect() != myAspectMarkerApplied)
   {
-    if (!AspectMarker_applied || (AspectMarker_set->Scale() != AspectMarker_applied->Scale()))
+    if (myAspectMarkerApplied.IsNull()
+    || (myAspectMarkerSet->Aspect()->Scale() != myAspectMarkerApplied->Scale()))
     {
     #if !defined(GL_ES_VERSION_2_0)
-      glPointSize (AspectMarker_set->Scale());
+      glPointSize (myAspectMarkerSet->Aspect()->Scale());
     #ifdef HAVE_GL2PS
-      gl2psPointSize (AspectMarker_set->Scale());
+      gl2psPointSize (myAspectMarkerSet->Aspect()->Scale());
     #endif
     #endif
     }
-    AspectMarker_applied = AspectMarker_set;
-  }
-  return AspectMarker_set;
-}
-
-// =======================================================================
-// function : AspectText
-// purpose  :
-// =======================================================================
-const OpenGl_AspectText* OpenGl_Workspace::AspectText (const Standard_Boolean theWithApply)
-{
-  if (theWithApply)
-  {
-    AspectText_applied = AspectText_set;
+    myAspectMarkerApplied = myAspectMarkerSet->Aspect();
   }
-
-  return AspectText_set;
+  return myAspectMarkerSet;
 }
 
 // =======================================================================
@@ -1053,24 +817,6 @@ Standard_Integer OpenGl_Workspace::Height() const
   return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0;
 }
 
-// =======================================================================
-// function : UseGLLight
-// purpose  :
-// =======================================================================
-Standard_Boolean OpenGl_Workspace::UseGLLight() const
-{
-  return myView->IsGLLightEnabled();
-}
-
-// =======================================================================
-// function : AntiAliasingMode
-// purpose  :
-// =======================================================================
-Standard_Integer OpenGl_Workspace::AntiAliasingMode() const
-{
-  return myView->IsAntialiasingEnabled();
-}
-
 // =======================================================================
 // function : IsCullingEnabled
 // purpose  :
@@ -1091,6 +837,8 @@ Handle(OpenGl_FrameBuffer) OpenGl_Workspace::FBOCreate (const Standard_Integer t
   if (!Activate())
     return Handle(OpenGl_FrameBuffer)();
 
+  DisableTexture();
+
   // create the FBO
   const Handle(OpenGl_Context)& aCtx = GetGlContext();
   Handle(OpenGl_FrameBuffer) aFrameBuffer = new OpenGl_FrameBuffer();
@@ -1119,76 +867,6 @@ void OpenGl_Workspace::FBORelease (Handle(OpenGl_FrameBuffer)& theFbo)
   theFbo.Nullify();
 }
 
-inline bool getDataFormat (const Image_PixMap& theData,
-                           GLenum&             thePixelFormat,
-                           GLenum&             theDataType)
-{
-  thePixelFormat = GL_RGB;
-  theDataType    = GL_UNSIGNED_BYTE;
-  switch (theData.Format())
-  {
-  #if !defined(GL_ES_VERSION_2_0)
-    case Image_PixMap::ImgGray:
-      thePixelFormat = GL_DEPTH_COMPONENT;
-      theDataType    = GL_UNSIGNED_BYTE;
-      return true;
-    case Image_PixMap::ImgGrayF:
-      thePixelFormat = GL_DEPTH_COMPONENT;
-      theDataType    = GL_FLOAT;
-      return true;
-    case Image_PixMap::ImgBGR:
-      thePixelFormat = GL_BGR;
-      theDataType    = GL_UNSIGNED_BYTE;
-      return true;
-    case Image_PixMap::ImgBGRA:
-    case Image_PixMap::ImgBGR32:
-      thePixelFormat = GL_BGRA;
-      theDataType    = GL_UNSIGNED_BYTE;
-      return true;
-    case Image_PixMap::ImgBGRF:
-      thePixelFormat = GL_BGR;
-      theDataType    = GL_FLOAT;
-      return true;
-    case Image_PixMap::ImgBGRAF:
-      thePixelFormat = GL_BGRA;
-      theDataType    = GL_FLOAT;
-      return true;
-  #else
-    case Image_PixMap::ImgGray:
-    case Image_PixMap::ImgGrayF:
-    case Image_PixMap::ImgBGR:
-    case Image_PixMap::ImgBGRA:
-    case Image_PixMap::ImgBGR32:
-    case Image_PixMap::ImgBGRF:
-    case Image_PixMap::ImgBGRAF:
-      return false;
-  #endif
-    case Image_PixMap::ImgRGB:
-      thePixelFormat = GL_RGB;
-      theDataType    = GL_UNSIGNED_BYTE;
-      return true;
-    case Image_PixMap::ImgRGBA:
-    case Image_PixMap::ImgRGB32:
-      thePixelFormat = GL_RGBA;
-      theDataType    = GL_UNSIGNED_BYTE;
-      return true;
-    case Image_PixMap::ImgRGBF:
-      thePixelFormat = GL_RGB;
-      theDataType    = GL_FLOAT;
-      return true;
-    case Image_PixMap::ImgRGBAF:
-      thePixelFormat = GL_RGBA;
-      theDataType    = GL_FLOAT;
-      return true;
-    case Image_PixMap::ImgAlpha:
-    case Image_PixMap::ImgAlphaF:
-      return false; // GL_ALPHA is no more supported in core context
-    case Image_PixMap::ImgUNKNOWN:
-      return false;
-  }
-  return false;
-}
-
 // =======================================================================
 // function : getAligned
 // purpose  :
@@ -1199,6 +877,21 @@ inline Standard_Size getAligned (const Standard_Size theNumber,
   return theNumber + theAlignment - 1 - (theNumber - 1) % theAlignment;
 }
 
+template<typename T>
+inline void convertRowFromRgba (T* theRgbRow,
+                                const Image_ColorRGBA* theRgbaRow,
+                                const Standard_Size theWidth)
+{
+  for (Standard_Size aCol = 0; aCol < theWidth; ++aCol)
+  {
+    const Image_ColorRGBA& anRgba = theRgbaRow[aCol];
+    T& anRgb = theRgbRow[aCol];
+    anRgb.r() = anRgba.r();
+    anRgb.g() = anRgba.g();
+    anRgb.b() = anRgba.b();
+  }
+}
+
 // =======================================================================
 // function : BufferDump
 // purpose  :
@@ -1207,13 +900,92 @@ Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)&
                                                Image_PixMap&                     theImage,
                                                const Graphic3d_BufferType&       theBufferType)
 {
-  GLenum aFormat, aType;
   if (theImage.IsEmpty()
-   || !getDataFormat (theImage, aFormat, aType)
-   || !Activate())
+  || !Activate())
+  {
+    return Standard_False;
+  }
+
+  GLenum aFormat = 0;
+  GLenum aType   = 0;
+  bool toSwapRgbaBgra = false;
+  bool toConvRgba2Rgb = false;
+  switch (theImage.Format())
+  {
+  #if !defined(GL_ES_VERSION_2_0)
+    case Image_Format_Gray:
+      aFormat = GL_DEPTH_COMPONENT;
+      aType   = GL_UNSIGNED_BYTE;
+      break;
+    case Image_Format_GrayF:
+      aFormat = GL_DEPTH_COMPONENT;
+      aType   = GL_FLOAT;
+      break;
+    case Image_Format_RGB:
+      aFormat = GL_RGB;
+      aType   = GL_UNSIGNED_BYTE;
+      break;
+    case Image_Format_BGR:
+      aFormat = GL_BGR;
+      aType   = GL_UNSIGNED_BYTE;
+      break;
+    case Image_Format_BGRA:
+    case Image_Format_BGR32:
+      aFormat = GL_BGRA;
+      aType   = GL_UNSIGNED_BYTE;
+      break;
+    case Image_Format_BGRF:
+      aFormat = GL_BGR;
+      aType   = GL_FLOAT;
+      break;
+    case Image_Format_BGRAF:
+      aFormat = GL_BGRA;
+      aType   = GL_FLOAT;
+      break;
+  #else
+    case Image_Format_Gray:
+    case Image_Format_GrayF:
+    case Image_Format_BGRF:
+    case Image_Format_BGRAF:
+      return Standard_False;
+    case Image_Format_BGRA:
+    case Image_Format_BGR32:
+      aFormat = GL_RGBA;
+      aType   = GL_UNSIGNED_BYTE;
+      toSwapRgbaBgra = true;
+      break;
+    case Image_Format_BGR:
+    case Image_Format_RGB:
+      aFormat = GL_RGBA;
+      aType   = GL_UNSIGNED_BYTE;
+      toConvRgba2Rgb = true;
+      break;
+  #endif
+    case Image_Format_RGBA:
+    case Image_Format_RGB32:
+      aFormat = GL_RGBA;
+      aType   = GL_UNSIGNED_BYTE;
+      break;
+    case Image_Format_RGBF:
+      aFormat = GL_RGB;
+      aType   = GL_FLOAT;
+      break;
+    case Image_Format_RGBAF:
+      aFormat = GL_RGBA;
+      aType   = GL_FLOAT;
+      break;
+    case Image_Format_Alpha:
+    case Image_Format_AlphaF:
+      return Standard_False; // GL_ALPHA is no more supported in core context
+    case Image_Format_UNKNOWN:
+      return Standard_False;
+  }
+
+  if (aFormat == 0)
   {
     return Standard_False;
   }
+
 #if !defined(GL_ES_VERSION_2_0)
   GLint aReadBufferPrev = GL_BACK;
   if (theBufferType == Graphic3d_BT_Depth
@@ -1221,6 +993,8 @@ Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)&
   {
     return Standard_False;
   }
+#else
+  (void )theBufferType;
 #endif
 
   // bind FBO if used
@@ -1263,8 +1037,33 @@ Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)&
     isBatchCopy = false;
   }
 #endif
+  if (toConvRgba2Rgb)
+  {
+    Handle(NCollection_BaseAllocator) anAlloc = new NCollection_AlignedAllocator (16);
+    const Standard_Size aRowSize = theImage.SizeX() * 4;
+    NCollection_Buffer aRowBuffer (anAlloc);
+    if (!aRowBuffer.Allocate (aRowSize))
+    {
+      return Standard_False;
+    }
 
-  if (!isBatchCopy)
+    for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
+    {
+      // Image_PixMap rows indexation always starts from the upper corner
+      // while order in memory depends on the flag and processed by ChangeRow() method
+      glReadPixels (0, GLint(theImage.SizeY() - aRow - 1), GLsizei (theImage.SizeX()), 1, aFormat, aType, aRowBuffer.ChangeData());
+      const Image_ColorRGBA* aRowDataRgba = (const Image_ColorRGBA* )aRowBuffer.Data();
+      if (theImage.Format() == Image_Format_BGR)
+      {
+        convertRowFromRgba ((Image_ColorBGR* )theImage.ChangeRow (aRow), aRowDataRgba, theImage.SizeX());
+      }
+      else
+      {
+        convertRowFromRgba ((Image_ColorRGB* )theImage.ChangeRow (aRow), aRowDataRgba, theImage.SizeX());
+      }
+    }
+  }
+  else if (!isBatchCopy)
   {
     // copy row by row
     for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
@@ -1278,6 +1077,7 @@ Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)&
   {
     glReadPixels (0, 0, GLsizei (theImage.SizeX()), GLsizei (theImage.SizeY()), aFormat, aType, theImage.ChangeData());
   }
+  const bool hasErrors = myGlContext->ResetErrors (true);
 
   glPixelStorei (GL_PACK_ALIGNMENT,  1);
 #if !defined(GL_ES_VERSION_2_0)
@@ -1294,19 +1094,26 @@ Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)&
     glReadBuffer (aReadBufferPrev);
   #endif
   }
-  return Standard_True;
+
+  if (toSwapRgbaBgra)
+  {
+    Image_PixMap::SwapRgbaBgra (theImage);
+  }
+
+  return !hasErrors;
 }
 
 // =======================================================================
-// function : CanRender
+// function : ShouldRender
 // purpose  :
 // =======================================================================
-Standard_Boolean OpenGl_RaytraceFilter::CanRender (const OpenGl_Element* theElement)
+Standard_Boolean OpenGl_RaytraceFilter::ShouldRender (const Handle(OpenGl_Workspace)& theWorkspace,
+                                                      const OpenGl_Element*           theElement)
 {
   Standard_Boolean aPrevFilterResult = Standard_True;
   if (!myPrevRenderFilter.IsNull())
   {
-    aPrevFilterResult = myPrevRenderFilter->CanRender (theElement);
+    aPrevFilterResult = myPrevRenderFilter->ShouldRender (theWorkspace, theElement);
   }
   return aPrevFilterResult &&
     !OpenGl_Raytrace::IsRaytracedElement (theElement);