0026122: Visualization, TKOpenGl - clipping and capping is broken when ffp is disable...
[occt.git] / src / OpenGl / OpenGl_Structure.cxx
index b0d8200..69d2e4d 100644 (file)
 #include <OpenGl_GraphicDriver.hxx>
 #include <OpenGl_ShaderManager.hxx>
 #include <OpenGl_ShaderProgram.hxx>
-#include <OpenGl_Structure.hxx>
+#include <OpenGl_StructureShadow.hxx>
 #include <OpenGl_telem_util.hxx>
 #include <OpenGl_Vec.hxx>
 #include <OpenGl_View.hxx>
 #include <OpenGl_Workspace.hxx>
 
-#include <Graphic3d_SequenceOfHClipPlane_Handle.hxx>
+#include <Graphic3d_SequenceOfHClipPlane.hxx>
 
-IMPLEMENT_STANDARD_HANDLE (OpenGl_Structure, Graphic3d_CStructure)
-IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Structure, Graphic3d_CStructure)
 
 //! Auxiliary class for bounding box presentation
 class OpenGl_BndBoxPrs : public OpenGl_Element
@@ -37,14 +35,15 @@ class OpenGl_BndBoxPrs : public OpenGl_Element
 public:
 
   //! Main constructor
-  OpenGl_BndBoxPrs (const CALL_DEF_BOUNDBOX& theBndBox)
-  {
-    const float Xm = theBndBox.Pmin.x;
-    const float Ym = theBndBox.Pmin.y;
-    const float Zm = theBndBox.Pmin.z;
-    const float XM = theBndBox.Pmax.x;
-    const float YM = theBndBox.Pmax.y;
-    const float ZM = theBndBox.Pmax.z;
+  OpenGl_BndBoxPrs (const Graphic3d_BndBox4f& theBndBox)
+  {
+    const float Xm = theBndBox.CornerMin().x();
+    const float Ym = theBndBox.CornerMin().y();
+    const float Zm = theBndBox.CornerMin().z();
+    const float XM = theBndBox.CornerMax().x();
+    const float YM = theBndBox.CornerMax().y();
+    const float ZM = theBndBox.CornerMax().z();
+
     myVerts[0]  = OpenGl_Vec3 (Xm, Ym, Zm);
     myVerts[1]  = OpenGl_Vec3 (Xm, Ym, ZM);
     myVerts[2]  = OpenGl_Vec3 (Xm, YM, ZM);
@@ -66,18 +65,15 @@ public:
   //! Render presentation
   virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const
   {
+  #if !defined(GL_ES_VERSION_2_0)
     // Apply line aspect
     const OpenGl_AspectLine*     anAspectLine = theWorkspace->AspectLine (Standard_True);
     const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture();
 
     glDisable (GL_LIGHTING);
-    if ((theWorkspace->NamedStatus & OPENGL_NS_IMMEDIATE) != 0)
-    {
-      glDepthMask (GL_FALSE);
-    }
 
     // Use highlight colors
-    glColor3fv ((theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) ? theWorkspace->HighlightColor->rgb : anAspectLine->Color().rgb);
+    theWorkspace->GetGlContext()->core11->glColor3fv ((theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) ? theWorkspace->HighlightColor->rgb : anAspectLine->Color().rgb);
 
     glEnableClientState (GL_VERTEX_ARRAY);
     glVertexPointer (3, GL_FLOAT, 0, (GLfloat* )&myVerts);
@@ -89,10 +85,11 @@ public:
     {
       theWorkspace->EnableTexture (aPrevTexture);
     }
+  #endif
   }
 
   //! Release graphical resources
-  virtual void Release (const Handle(OpenGl_Context)& )
+  virtual void Release (OpenGl_Context*)
   {
     //
   }
@@ -114,38 +111,25 @@ public:
 
 /*----------------------------------------------------------------------*/
 
-// =======================================================================
-// function : call_util_transpose_mat
-// purpose  :
-// =======================================================================
-static void call_util_transpose_mat (float tmat[16], float mat[4][4])
-{
-  int i, j;
-
-  for (i=0; i<4; i++)
-    for (j=0; j<4; j++)
-      tmat[j*4+i] = mat[i][j];
-}
-
 // =======================================================================
 // function : OpenGl_Structure
 // purpose  :
 // =======================================================================
 OpenGl_Structure::OpenGl_Structure (const Handle(Graphic3d_StructureManager)& theManager)
 : Graphic3d_CStructure (theManager),
-  myTransformation(NULL),
-  myTransPers(NULL),
-  myAspectLine(NULL),
-  myAspectFace(NULL),
-  myAspectMarker(NULL),
-  myAspectText(NULL),
-  myHighlightColor(NULL),
-  myNamedStatus(0),
-  myZLayer(0),
-  myIsRaytracable (Standard_False),
-  myModificationState (0)
+  myTransformation     (NULL),
+  myAspectLine         (NULL),
+  myAspectFace         (NULL),
+  myAspectMarker       (NULL),
+  myAspectText         (NULL),
+  myHighlightColor     (NULL),
+  myInstancedStructure (NULL),
+  myIsRaytracable      (Standard_False),
+  myModificationState  (0),
+  myIsCulled           (Standard_True),
+  myIsMirrored         (Standard_False)
 {
-  UpdateNamedStatus();
+  //
 }
 
 // =======================================================================
@@ -156,7 +140,6 @@ OpenGl_Structure::~OpenGl_Structure()
 {
   Release (Handle(OpenGl_Context)());
   delete myTransformation;  myTransformation  = NULL;
-  delete myTransPers;       myTransPers       = NULL;
 }
 
 // =======================================================================
@@ -165,8 +148,6 @@ OpenGl_Structure::~OpenGl_Structure()
 // =======================================================================
 void OpenGl_Structure::UpdateAspects()
 {
-  SetTransformPersistence (TransformPersistence);
-
   if (ContextLine.IsDef)
     SetAspectLine (ContextLine);
 
@@ -191,29 +172,24 @@ void OpenGl_Structure::UpdateTransformation()
     myTransformation = new OpenGl_Matrix();
   }
 
+  Standard_ShortReal (*aMat)[4] = Graphic3d_CStructure::Transformation;
+
+  Standard_ShortReal aDet =
+    aMat[0][0] * (aMat[1][1] * aMat[2][2] - aMat[2][1] * aMat[1][2]) -
+    aMat[0][1] * (aMat[1][0] * aMat[2][2] - aMat[2][0] * aMat[1][2]) +
+    aMat[0][2] * (aMat[1][0] * aMat[2][1] - aMat[2][0] * aMat[1][1]);
+
+  // Determinant of transform matrix less then 0 means that mirror transform applied.
+  myIsMirrored = aDet < 0.0f;
+
   matcpy (myTransformation->mat, &Graphic3d_CStructure::Transformation[0][0]);
 
-  if (myIsRaytracable)
+  if (IsRaytracable())
   {
-    UpdateStateWithAncestorStructures();
+    ++myModificationState;
   }
 }
 
-// =======================================================================
-// function : SetTransformPersistence
-// purpose  :
-// =======================================================================
-void OpenGl_Structure::SetTransformPersistence(const CALL_DEF_TRANSFORM_PERSISTENCE &ATransPers)
-{
-  if (!myTransPers)
-    myTransPers = new TEL_TRANSFORM_PERSISTENCE;
-
-  myTransPers->mode = ATransPers.Flag;
-  myTransPers->pointX = ATransPers.Point.x;
-  myTransPers->pointY = ATransPers.Point.y;
-  myTransPers->pointZ = ATransPers.Point.z;
-}
-
 // =======================================================================
 // function : SetAspectLine
 // purpose  :
@@ -239,9 +215,9 @@ void OpenGl_Structure::SetAspectFace (const CALL_DEF_CONTEXTFILLAREA& theAspect)
   }
   myAspectFace->SetAspect (theAspect);
 
-  if (myIsRaytracable)
+  if (IsRaytracable())
   {
-    UpdateStateWithAncestorStructures();
+    ++myModificationState;
   }
 }
 
@@ -291,11 +267,11 @@ void OpenGl_Structure::clearHighlightBox (const Handle(OpenGl_Context)& theGlCtx
 void OpenGl_Structure::HighlightWithColor (const Graphic3d_Vec3&  theColor,
                                            const Standard_Boolean theToCreate)
 {
-  const Handle(OpenGl_Context)& aCtx = GlDriver()->GetSharedContext();
+  const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
   if (theToCreate)
-    setHighlightColor   (aCtx, theColor);
+    setHighlightColor   (aContext, theColor);
   else
-    clearHighlightColor (aCtx);
+    clearHighlightColor (aContext);
 }
 
 // =======================================================================
@@ -305,16 +281,16 @@ void OpenGl_Structure::HighlightWithColor (const Graphic3d_Vec3&  theColor,
 void OpenGl_Structure::HighlightWithBndBox (const Handle(Graphic3d_Structure)& theStruct,
                                             const Standard_Boolean             theToCreate)
 {
-  const Handle(OpenGl_Context)& aCtx = GlDriver()->GetSharedContext();
+  const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
   if (!theToCreate)
   {
-    clearHighlightBox (aCtx);
+    clearHighlightBox (aContext);
     return;
   }
 
   if (!myHighlightBox.IsNull())
   {
-    myHighlightBox->Release (aCtx);
+    myHighlightBox->Release (aContext);
   }
   else
   {
@@ -323,12 +299,12 @@ void OpenGl_Structure::HighlightWithBndBox (const Handle(Graphic3d_Structure)& t
 
   CALL_DEF_CONTEXTLINE& aContextLine = myHighlightBox->ChangeContextLine();
   aContextLine.IsDef    = 1;
-  aContextLine.Color    = BoundBox.Color;
+  aContextLine.Color    = HighlightColor;
   aContextLine.LineType = Aspect_TOL_SOLID;
   aContextLine.Width    = 1.0f;
   myHighlightBox->UpdateAspectLine (Standard_True);
 
-  OpenGl_BndBoxPrs* aBndBoxPrs = new OpenGl_BndBoxPrs (BoundBox);
+  OpenGl_BndBoxPrs* aBndBoxPrs = new OpenGl_BndBoxPrs (myBndBox);
   myHighlightBox->AddElement (aBndBoxPrs);
 }
 
@@ -363,120 +339,46 @@ void OpenGl_Structure::clearHighlightColor (const Handle(OpenGl_Context)& theGlC
 }
 
 // =======================================================================
-// function : UpdateNamedStatus
-// purpose  :
-// =======================================================================
-void OpenGl_Structure::UpdateNamedStatus()
-{
-  myNamedStatus = 0;
-  if (highlight) myNamedStatus |= OPENGL_NS_HIGHLIGHT;
-  if (!visible)  myNamedStatus |= OPENGL_NS_HIDE;
-
-  if (myIsRaytracable)
-  {
-    UpdateStateWithAncestorStructures();
-  }
-}
-
-// =======================================================================
-// function : RegisterAncestorStructure
+// function : OnVisibilityChanged
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::RegisterAncestorStructure (const OpenGl_Structure* theStructure) const
+void OpenGl_Structure::OnVisibilityChanged()
 {
-  for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next())
+  if (IsRaytracable())
   {
-    if (anIt.Value() == theStructure)
-    {
-      return;
-    }
-  }
-
-  myAncestorStructures.Append (theStructure);
-}
-
-// =======================================================================
-// function : UnregisterAncestorStructure
-// purpose  :
-// =======================================================================
-void OpenGl_Structure::UnregisterAncestorStructure (const OpenGl_Structure* theStructure) const
-{
-  for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next())
-  {
-    if (anIt.Value() == theStructure)
-    {
-      myAncestorStructures.Remove (anIt);
-      return;
-    }
+    ++myModificationState;
   }
 }
 
 // =======================================================================
-// function : UnregisterFromAncestorStructure
+// function : IsRaytracable
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::UnregisterFromAncestorStructure() const
+Standard_Boolean OpenGl_Structure::IsRaytracable() const
 {
-  for (OpenGl_ListOfStructure::Iterator anIta (myAncestorStructures); anIta.More(); anIta.Next())
+  if (!myGroups.IsEmpty())
   {
-    OpenGl_Structure* anAncestor = const_cast<OpenGl_Structure*> (anIta.ChangeValue());
-
-    for (OpenGl_ListOfStructure::Iterator anIts (anAncestor->myConnected); anIts.More(); anIts.Next())
-    {
-      if (anIts.Value() == this)
-      {
-        anAncestor->myConnected.Remove (anIts);
-        return;
-      }
-    }
+    return myIsRaytracable; // geometry structure
   }
-}
-
-// =======================================================================
-// function : UpdateStateWithAncestorStructures
-// purpose  :
-// =======================================================================
-void OpenGl_Structure::UpdateStateWithAncestorStructures() const
-{
-  myModificationState++;
-
-  for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next())
+  else if (myInstancedStructure != NULL)
   {
-    anIt.Value()->UpdateStateWithAncestorStructures();
+    return myInstancedStructure->IsRaytracable(); // instance structure
   }
-}
-
-// =======================================================================
-// function : UpdateRaytracableWithAncestorStructures
-// purpose  :
-// =======================================================================
-void OpenGl_Structure::UpdateRaytracableWithAncestorStructures() const
-{
-  myIsRaytracable = OpenGl_Raytrace::IsRaytracedStructure (this);
 
-  if (!myIsRaytracable)
-  {
-    for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next())
-    {
-      anIt.Value()->UpdateRaytracableWithAncestorStructures();
-    }
-  }
+  return Standard_False; // has no any groups or structures
 }
 
 // =======================================================================
-// function : SetRaytracableWithAncestorStructures
+// function : UpdateRaytracableState
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::SetRaytracableWithAncestorStructures() const
+void OpenGl_Structure::UpdateStateIfRaytracable (const Standard_Boolean toCheck) const
 {
-  myIsRaytracable = Standard_True;
+  myIsRaytracable = !toCheck || OpenGl_Raytrace::IsRaytracedStructure (this);
 
-  for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next())
+  if (IsRaytracable())
   {
-    if (!anIt.Value()->IsRaytracable())
-    {
-      anIt.Value()->SetRaytracableWithAncestorStructures();
-    }
+    ++myModificationState;
   }
 }
 
@@ -486,17 +388,17 @@ void OpenGl_Structure::SetRaytracableWithAncestorStructures() const
 // =======================================================================
 void OpenGl_Structure::Connect (Graphic3d_CStructure& theStructure)
 {
-  OpenGl_Structure* aStruct = (OpenGl_Structure* )&theStructure;
-  Disconnect (theStructure);
-  myConnected.Append (aStruct);
+  OpenGl_Structure* aStruct = static_cast<OpenGl_Structure*> (&theStructure);
+
+  Standard_ASSERT_RAISE (myInstancedStructure == NULL || myInstancedStructure == aStruct,
+    "Error! Instanced structure is already defined");
+
+  myInstancedStructure = aStruct;
 
   if (aStruct->IsRaytracable())
   {
-    UpdateStateWithAncestorStructures();
-    SetRaytracableWithAncestorStructures();
+    UpdateStateIfRaytracable (Standard_False);
   }
-
-  aStruct->RegisterAncestorStructure (this);
 }
 
 // =======================================================================
@@ -505,22 +407,15 @@ void OpenGl_Structure::Connect (Graphic3d_CStructure& theStructure)
 // =======================================================================
 void OpenGl_Structure::Disconnect (Graphic3d_CStructure& theStructure)
 {
-  OpenGl_Structure* aStruct = (OpenGl_Structure* )&theStructure;
-  for (OpenGl_ListOfStructure::Iterator anIter (myConnected); anIter.More(); anIter.Next())
-  {
-    // Check for the given structure
-    if (anIter.Value() == aStruct)
-    {
-      myConnected.Remove (anIter);
+  OpenGl_Structure* aStruct = static_cast<OpenGl_Structure*> (&theStructure);
 
-      if (aStruct->IsRaytracable())
-      {
-        UpdateStateWithAncestorStructures();
-        UpdateRaytracableWithAncestorStructures();
-      }
+  if (myInstancedStructure == aStruct)
+  {
+    myInstancedStructure = NULL;
 
-      aStruct->UnregisterAncestorStructure (this);
-      return;
+    if (aStruct->IsRaytracable())
+    {
+      UpdateStateIfRaytracable();
     }
   }
 }
@@ -552,12 +447,14 @@ void OpenGl_Structure::RemoveGroup (const Handle(Graphic3d_Group)& theGroup)
     // Check for the given group
     if (aGroupIter.Value() == theGroup)
     {
+      const Standard_Boolean wasRaytracable =
+        static_cast<const OpenGl_Group&> (*theGroup).IsRaytracable();
+
       theGroup->Clear (Standard_False);
 
-      if (((OpenGl_Group* )theGroup.operator->())->IsRaytracable())
+      if (wasRaytracable)
       {
-        UpdateStateWithAncestorStructures();
-        UpdateRaytracableWithAncestorStructures();
+        UpdateStateIfRaytracable();
       }
 
       myGroups.Remove (aGroupIter);
@@ -595,8 +492,24 @@ void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx)
 
   if (aRaytracableGroupDeleted)
   {
-    UpdateStateWithAncestorStructures();
-    UpdateRaytracableWithAncestorStructures();
+    myIsRaytracable = Standard_False;
+  }
+
+  Is2dText       = Standard_False;
+  IsForHighlight = Standard_False;
+}
+
+// =======================================================================
+// function : RenderGeometry
+// purpose  :
+// =======================================================================
+void OpenGl_Structure::RenderGeometry (const Handle(OpenGl_Workspace) &theWorkspace) const
+{
+  // Render groups
+  const Graphic3d_SequenceOfGroup& aGroups = DrawGroups();
+  for (OpenGl_Structure::GroupIterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
+  {
+    aGroupIter.Value()->Render (theWorkspace);
   }
 }
 
@@ -604,112 +517,109 @@ void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx)
 // function : Render
 // purpose  :
 // =======================================================================
-void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
+void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
 {
   // Process the structure only if visible
-  if ( myNamedStatus & OPENGL_NS_HIDE )
+  if (!visible)
+  {
     return;
+  }
 
-  // Render named status
-  const Standard_Integer named_status = AWorkspace->NamedStatus;
-  AWorkspace->NamedStatus |= myNamedStatus;
+  const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
 
-  // Is rendering in ADD or IMMEDIATE mode?
-  const Standard_Boolean isImmediate = (AWorkspace->NamedStatus & OPENGL_NS_IMMEDIATE) != 0;
+  // Render named status
+  const Standard_Integer aNamedStatus = theWorkspace->NamedStatus;
+  if (highlight)
+  {
+    theWorkspace->NamedStatus |= OPENGL_NS_HIGHLIGHT;
+  }
 
-  const Handle(OpenGl_Context)& aCtx = AWorkspace->GetGlContext();
+  // Do we need to restore GL_NORMALIZE?
+  const Standard_Boolean anOldGlNormalize = aCtx->IsGlNormalizeEnabled();
 
   // Apply local transformation
-  GLint matrix_mode = 0;
-  const OpenGl_Matrix *local_trsf = NULL;
   if (myTransformation)
   {
-    if (isImmediate)
-    {
-      Tmatrix3 aModelWorld;
-      call_util_transpose_mat (*aModelWorld, myTransformation->mat);
-      glGetIntegerv (GL_MATRIX_MODE, &matrix_mode);
-
-      if (!aCtx->ShaderManager()->IsEmpty())
-      {
-        Tmatrix3 aWorldView;
-        glGetFloatv (GL_MODELVIEW_MATRIX, *aWorldView);
-
-        Tmatrix3 aProjection;
-        glGetFloatv (GL_PROJECTION_MATRIX, *aProjection);
+    OpenGl_Matrix aModelWorld;
+    OpenGl_Transposemat3 (&aModelWorld, myTransformation);
+    aCtx->ModelWorldState.Push();
+    aCtx->ModelWorldState.SetCurrent (OpenGl_Mat4::Map ((Standard_ShortReal* )aModelWorld.mat));
 
-        aCtx->ShaderManager()->UpdateModelWorldStateTo (&aModelWorld);
-        aCtx->ShaderManager()->UpdateWorldViewStateTo (&aWorldView);
-        aCtx->ShaderManager()->UpdateProjectionStateTo (&aProjection);
-      }
-
-      glMatrixMode (GL_MODELVIEW);
-      glPushMatrix ();
-      glScalef (1.F, 1.F, 1.F);
-      glMultMatrixf (*aModelWorld);
-    }
-    else
+    Standard_ShortReal aScaleX = OpenGl_Vec3 (myTransformation->mat[0][0],
+                                              myTransformation->mat[0][1],
+                                              myTransformation->mat[0][2]).SquareModulus();
+    // Scale transform detected.
+    if (Abs (aScaleX - 1.f) > Precision::Confusion())
     {
-      glMatrixMode (GL_MODELVIEW);
-      glPushMatrix();
-
-      local_trsf = AWorkspace->SetStructureMatrix (myTransformation);
+      aCtx->SetGlNormalizeEnabled (Standard_True);
     }
   }
-
-  // Apply transform persistence
-  const TEL_TRANSFORM_PERSISTENCE *trans_pers = NULL;
-  if ( myTransPers && myTransPers->mode != 0 )
+  if (TransformPersistence.Flags)
   {
-    trans_pers = AWorkspace->ActiveView()->BeginTransformPersistence (aCtx, myTransPers);
+    OpenGl_Mat4 aProjection = aCtx->ProjectionState.Current();
+    OpenGl_Mat4 aWorldView  = aCtx->WorldViewState.Current();
+    TransformPersistence.Apply (aProjection, aWorldView, theWorkspace->Width(), theWorkspace->Height());
+
+    aCtx->ProjectionState.Push();
+    aCtx->WorldViewState.Push();
+    aCtx->ProjectionState.SetCurrent (aProjection);
+    aCtx->WorldViewState.SetCurrent (aWorldView);
+    aCtx->ApplyProjectionMatrix();
   }
 
+  // Take into account transform persistence
+  aCtx->ApplyModelViewMatrix();
+
   // Apply aspects
-  const OpenGl_AspectLine *aspect_line = AWorkspace->AspectLine(Standard_False);
-  const OpenGl_AspectFace *aspect_face = AWorkspace->AspectFace(Standard_False);
-  const OpenGl_AspectMarker *aspect_marker = AWorkspace->AspectMarker(Standard_False);
-  const OpenGl_AspectText *aspect_text = AWorkspace->AspectText(Standard_False);
+  const OpenGl_AspectLine *anAspectLine = theWorkspace->AspectLine (Standard_False);
+  const OpenGl_AspectFace *anAspectFace = theWorkspace->AspectFace (Standard_False);
+  const OpenGl_AspectMarker *anAspectMarker = theWorkspace->AspectMarker (Standard_False);
+  const OpenGl_AspectText *anAspectText = theWorkspace->AspectText (Standard_False);
   if (myAspectLine)
-    AWorkspace->SetAspectLine(myAspectLine);
+  {
+    theWorkspace->SetAspectLine (myAspectLine);
+  }
   if (myAspectFace)
-    AWorkspace->SetAspectFace(myAspectFace);
+  {
+    theWorkspace->SetAspectFace (myAspectFace);
+  }
   if (myAspectMarker)
-    AWorkspace->SetAspectMarker(myAspectMarker);
+  {
+    theWorkspace->SetAspectMarker (myAspectMarker);
+  }
   if (myAspectText)
-    AWorkspace->SetAspectText(myAspectText);
+  {
+    theWorkspace->SetAspectText (myAspectText);
+  }
 
-  // Apply highlight box
-  if (!myHighlightBox.IsNull())
+  // Apply correction for mirror transform
+  if (myIsMirrored)
   {
-    myHighlightBox->Render (AWorkspace);
+    aCtx->core11fwd->glFrontFace (GL_CW);
   }
 
   // Apply highlight color
-  const TEL_COLOUR *highlight_color = AWorkspace->HighlightColor;
+  const TEL_COLOUR *aHighlightColor = theWorkspace->HighlightColor;
   if (myHighlightColor)
-    AWorkspace->HighlightColor = myHighlightColor;
+    theWorkspace->HighlightColor = myHighlightColor;
 
-  // Render connected structures
-  OpenGl_ListOfStructure::Iterator its(myConnected);
-  while (its.More())
+  // Render instanced structure (if exists)
+  if (myInstancedStructure != NULL)
   {
-    its.Value()->Render(AWorkspace);
-    its.Next();
+    myInstancedStructure->RenderGeometry (theWorkspace);
   }
 
   // Set up plane equations for non-structure transformed global model-view matrix
-  const Handle(OpenGl_Context)& aContext = AWorkspace->GetGlContext();
-
   // List of planes to be applied to context state
-  Handle(Graphic3d_SequenceOfHClipPlane) aUserPlanes;
+  NCollection_Handle<Graphic3d_SequenceOfHClipPlane> aUserPlanes;
 
   // Collect clipping planes of structure scope
   if (!myClipPlanes.IsEmpty())
   {
-    Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes);
-    for (; aClippingIt.More(); aClippingIt.Next())
+    Graphic3d_SequenceOfHClipPlane::Iterator aClippingIter (myClipPlanes);
+    for (; aClippingIter.More(); aClippingIter.Next())
     {
-      const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value();
+      const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIter.Value();
       if (!aClipPlane->IsOn())
       {
         continue;
@@ -727,12 +637,12 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
   if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty())
   {
     // add planes at loaded view matrix state
-    aContext->ChangeClipping().AddWorld (*aUserPlanes, AWorkspace);
+    aCtx->ChangeClipping().AddWorld (aCtx, *aUserPlanes);
 
     // Set OCCT state uniform variables
-    if (!aContext->ShaderManager()->IsEmpty())
+    if (!aCtx->ShaderManager()->IsEmpty())
     {
-      aContext->ShaderManager()->UpdateClippingState();
+      aCtx->ShaderManager()->UpdateClippingState();
     }
   }
 
@@ -740,68 +650,63 @@ void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
   const Graphic3d_SequenceOfGroup& aGroups = DrawGroups();
   for (OpenGl_Structure::GroupIterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
   {
-    aGroupIter.Value()->Render (AWorkspace);
+    aGroupIter.Value()->Render (theWorkspace);
+  }
+
+  // Reset correction for mirror transform
+  if (myIsMirrored)
+  {
+    aCtx->core11fwd->glFrontFace (GL_CCW);
   }
 
   // Render capping for structure groups
-  if (!aContext->Clipping().Planes().IsEmpty())
+  if (!aCtx->Clipping().Planes().IsEmpty())
   {
-    OpenGl_CappingAlgo::RenderCapping (AWorkspace, aGroups);
+    OpenGl_CappingAlgo::RenderCapping (theWorkspace, aGroups);
   }
 
   // Revert structure clippings
   if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty())
   {
-    aContext->ChangeClipping().Remove (*aUserPlanes);
+    aCtx->ChangeClipping().Remove (aCtx, *aUserPlanes);
 
     // Set OCCT state uniform variables
-    if (!aContext->ShaderManager()->IsEmpty())
+    if (!aCtx->ShaderManager()->IsEmpty())
     {
-      aContext->ShaderManager()->RevertClippingState();
+      aCtx->ShaderManager()->RevertClippingState();
     }
   }
 
-  // Restore highlight color
-  AWorkspace->HighlightColor = highlight_color;
-
-  // Restore aspects
-  AWorkspace->SetAspectLine(aspect_line);
-  AWorkspace->SetAspectFace(aspect_face);
-  AWorkspace->SetAspectMarker(aspect_marker);
-  AWorkspace->SetAspectText(aspect_text);
-
-  // Restore transform persistence
-  if ( myTransPers && myTransPers->mode != 0 )
-  {
-    AWorkspace->ActiveView()->BeginTransformPersistence (aContext, trans_pers);
-  }
-
   // Restore local transformation
   if (myTransformation)
   {
-    if (isImmediate)
-    {
-      glPopMatrix ();
-      glMatrixMode (matrix_mode);
+    aCtx->ModelWorldState.Pop();
+    aCtx->SetGlNormalizeEnabled (anOldGlNormalize);
+  }
+  if (TransformPersistence.Flags)
+  {
+    aCtx->ProjectionState.Pop();
+    aCtx->WorldViewState.Pop();
+    aCtx->ApplyProjectionMatrix();
+  }
 
-      Tmatrix3 aModelWorldState = { { 1.f, 0.f, 0.f, 0.f },
-                                    { 0.f, 1.f, 0.f, 0.f },
-                                    { 0.f, 0.f, 1.f, 0.f },
-                                    { 0.f, 0.f, 0.f, 1.f } };
+  // Restore highlight color
+  theWorkspace->HighlightColor = aHighlightColor;
 
-      aContext->ShaderManager()->RevertModelWorldStateTo (&aModelWorldState);
-    }
-    else
-    {
-      AWorkspace->SetStructureMatrix (local_trsf, true);
+  // Restore aspects
+  theWorkspace->SetAspectLine (anAspectLine);
+  theWorkspace->SetAspectFace (anAspectFace);
+  theWorkspace->SetAspectMarker (anAspectMarker);
+  theWorkspace->SetAspectText (anAspectText);
 
-      glMatrixMode (GL_MODELVIEW);
-      glPopMatrix();
-    }
+  // Apply highlight box
+  if (!myHighlightBox.IsNull())
+  {
+    myHighlightBox->Render (theWorkspace);
   }
 
   // Restore named status
-  AWorkspace->NamedStatus = named_status;
+  theWorkspace->NamedStatus = aNamedStatus;
 }
 
 // =======================================================================
@@ -812,14 +717,11 @@ void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx)
 {
   // Release groups
   Clear (theGlCtx);
-  OpenGl_Element::Destroy (theGlCtx, myAspectLine);
-  OpenGl_Element::Destroy (theGlCtx, myAspectFace);
-  OpenGl_Element::Destroy (theGlCtx, myAspectMarker);
-  OpenGl_Element::Destroy (theGlCtx, myAspectText);
+  OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectLine);
+  OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectFace);
+  OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectMarker);
+  OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectText);
   clearHighlightColor (theGlCtx);
-
-  // Remove from connected list of ancestor
-  UnregisterFromAncestorStructure();
 }
 
 // =======================================================================
@@ -834,94 +736,26 @@ void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCt
   }
   if (myAspectLine != NULL)
   {
-    myAspectLine->Release (theGlCtx);
+    myAspectLine->Release (theGlCtx.operator->());
   }
   if (myAspectFace != NULL)
   {
-    myAspectFace->Release (theGlCtx);
+    myAspectFace->Release (theGlCtx.operator->());
   }
   if (myAspectMarker != NULL)
   {
-    myAspectMarker->Release (theGlCtx);
+    myAspectMarker->Release (theGlCtx.operator->());
   }
   if (myAspectText != NULL)
   {
-    myAspectText->Release (theGlCtx);
+    myAspectText->Release (theGlCtx.operator->());
   }
   if (!myHighlightBox.IsNull())
   {
-    myHighlightBox->Release (theGlCtx);
+    myHighlightBox->Release (theGlCtx.operator->());
   }
 }
 
-//=======================================================================
-//function : SetZLayer
-//purpose  :
-//=======================================================================
-void OpenGl_Structure::SetZLayer (const Standard_Integer theLayerIndex)
-{
-  myZLayer = theLayerIndex;
-}
-
-//=======================================================================
-//function : GetZLayer
-//purpose  :
-//=======================================================================
-Standard_Integer OpenGl_Structure::GetZLayer () const
-{
-  return myZLayer;
-}
-
-class OpenGl_StructureShadow : public OpenGl_Structure
-{
-
-public:
-
-  //! Create empty structure
-  OpenGl_StructureShadow (const Handle(Graphic3d_StructureManager)& theManager,
-                          const Handle(OpenGl_Structure)&           theStructure);
-
-  virtual const Graphic3d_SequenceOfGroup& DrawGroups() const { return myParent->DrawGroups(); }
-
-private:
-
-  Handle(OpenGl_Structure) myParent;
-
-public:
-
-  DEFINE_STANDARD_RTTI(OpenGl_Structure) // Type definition
-
-};
-
-DEFINE_STANDARD_HANDLE(OpenGl_StructureShadow, OpenGl_Structure)
-
-IMPLEMENT_STANDARD_HANDLE (OpenGl_StructureShadow, OpenGl_Structure)
-IMPLEMENT_STANDARD_RTTIEXT(OpenGl_StructureShadow, OpenGl_Structure)
-
-OpenGl_StructureShadow::OpenGl_StructureShadow (const Handle(Graphic3d_StructureManager)& theManager,
-                                                const Handle(OpenGl_Structure)&           theStructure)
-: OpenGl_Structure (theManager)
-{
-  Handle(OpenGl_StructureShadow) aShadow = Handle(OpenGl_StructureShadow)::DownCast (theStructure);
-  myParent = aShadow.IsNull() ? theStructure : aShadow->myParent;
-
-
-  Composition   = myParent->Composition;
-  ContainsFacet = myParent->ContainsFacet;
-  IsInfinite    = myParent->IsInfinite;
-  for (Standard_Integer i = 0; i <= 3; ++i)
-  {
-    for (Standard_Integer j = 0; j <= 3; ++j)
-    {
-      Graphic3d_CStructure::Transformation[i][j] = myParent->Graphic3d_CStructure::Transformation[i][j];
-    }
-  }
-
-  TransformPersistence.IsSet = myParent->TransformPersistence.IsSet;
-  TransformPersistence.Flag  = myParent->TransformPersistence.Flag;
-  TransformPersistence.Point = myParent->TransformPersistence.Point;
-}
-
 //=======================================================================
 //function : ShadowLink
 //purpose  :