0025867: Visualization, TKOpenGl - fix misprint in transformation persistence math
[occt.git] / src / OpenGl / OpenGl_View.cxx
index c4d986f..30acf66 100644 (file)
-// File:      OpenGl_View.cxx
-// Created:   20 September 2011
-// Author:    Sergey ZERCHANINOV
-// Copyright: OPEN CASCADE 2011
-
+// Created on: 2011-09-20
+// Created by: Sergey ZERCHANINOV
+// Copyright (c) 2011-2014 OPEN CASCADE SAS
+//
+// This file is part of Open CASCADE Technology software library.
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
+//
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
+
+#include <NCollection_Mat4.hxx>
+
+#include <OpenGl_Context.hxx>
+#include <OpenGl_GlCore11.hxx>
+#include <OpenGl_GraduatedTrihedron.hxx>
+#include <OpenGl_GraphicDriver.hxx>
+#include <OpenGl_ShaderManager.hxx>
+#include <OpenGl_Texture.hxx>
+#include <OpenGl_Trihedron.hxx>
+#include <OpenGl_transform_persistence.hxx>
 #include <OpenGl_View.hxx>
-
+#include <OpenGl_Utils.hxx>
 #include <OpenGl_Workspace.hxx>
-#include <OpenGl_Display.hxx>
 
-#include <OpenGl_tgl_all.hxx>
-#include <GL/gl.h>
-
-#include <OpenGl_Display.hxx>
-#include <OpenGl_Trihedron.hxx>
-#include <OpenGl_GraduatedTrihedron.hxx>
-
-#include <OpenGl_transform_persistence.hxx>
+#include <Graphic3d_TextureEnv.hxx>
+#include <Graphic3d_Mat4d.hxx>
 
 IMPLEMENT_STANDARD_HANDLE(OpenGl_View,MMgt_TShared)
 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,MMgt_TShared)
 
 /*----------------------------------------------------------------------*/
 
-static const OPENGL_BG_TEXTURE myDefaultBgTexture = { 0, 0, 0, Aspect_FM_CENTERED };
-static const OPENGL_BG_GRADIENT myDefaultBgGradient = { {{ 0.F, 0.F, 0.F, 1.F }}, {{ 0.F, 0.F, 0.F, 1.F }}, Aspect_GFM_NONE };
 static const Tmatrix3 myDefaultMatrix = { { 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 } };
 static const OPENGL_ZCLIP myDefaultZClip = { { Standard_True, 0.F }, { Standard_True, 1.F } };
-static const OPENGL_EXTRA_REP myDefaultExtra =
-{
-  //vrp
-  { 0.F, 0.F, 0.F },
-  //vpn
-  { 0.F, 0.F, 1.F },
-  //vup
-  { 0.F, 1.F, 0.F },
-  //map
-  {
-    //window
-    { 0.F, 0.F, 1.F, 1.F },
-    //viewport
-    { 0.F, 0.F, 0.F, 1.F, 1.F, 1.F },
-    //proj
-    TelParallel,
-    //prp
-    { 0.F, 0.F, 0.F },
-    //vpd
-    0.F,
-    //fpd
-    0.F,
-    //bpd
-    -1.F
-  },
-  //scaleFactors
-  { 1.F, 1.F, 1.F }
-};
 
 static const OPENGL_FOG myDefaultFog = { Standard_False, 0.F, 1.F, { { 0.F, 0.F, 0.F, 1.F } } };
 static const TEL_TRANSFORM_PERSISTENCE myDefaultTransPers = { 0, 0.F, 0.F, 0.F };
+static const GLdouble THE_IDENTITY_MATRIX[4][4] =
+{
+  {1.0, 0.0, 0.0, 0.0},
+  {0.0, 1.0, 0.0, 0.0},
+  {0.0, 0.0, 1.0, 0.0},
+  {0.0, 0.0, 0.0, 1.0}
+};
 
 /*----------------------------------------------------------------------*/
 
-OpenGl_View::OpenGl_View (const CALL_DEF_VIEWCONTEXT &AContext)
-: myTextureEnv(0),
-  mySurfaceDetail(Visual3d_TOD_NONE),
+OpenGl_View::OpenGl_View (const CALL_DEF_VIEWCONTEXT &AContext,
+                          OpenGl_StateCounter*       theCounter)
+: mySurfaceDetail(Visual3d_TOD_ALL),
   myBackfacing(0),
-  myBgTexture(myDefaultBgTexture),
-  myBgGradient(myDefaultBgGradient),
-  //myOrientationMatrix(myDefaultMatrix),
-  //myMappingMatrix(myDefaultMatrix),
   //shield_indicator = TOn,
   //shield_colour = { { 0.F, 0.F, 0.F, 1.F } },
   //border_indicator = TOff,
   //border_colour = { { 0.F, 0.F, 0.F, 1.F } },
   //active_status = TOn,
   myZClip(myDefaultZClip),
-  myExtra(myDefaultExtra),
+  myCamera(AContext.Camera),
   myFog(myDefaultFog),
+  myTrihedron(NULL),
+  myGraduatedTrihedron(NULL),
   myVisualization(AContext.Visualization),
-  myIntShadingMethod(TEL_SM_GOURAUD),
+  myShadingModel ((Visual3d_TypeOfModel )AContext.Model),
   myAntiAliasing(Standard_False),
-  myAnimationListIndex(0),
-  myAnimationListReady(Standard_False),
   myTransPers(&myDefaultTransPers),
   myIsTransPers(Standard_False),
-  myResetFLIST(Standard_False)
-{
-  // Initialize matrices
-  memcpy(myOrientationMatrix,myDefaultMatrix,sizeof(Tmatrix3));
-  memcpy(myMappingMatrix,myDefaultMatrix,sizeof(Tmatrix3));
-
-  // Shading method
-  switch (AContext.Model)
-  {
-    case 1 : /* VISUAL3D_TOM_INTERP_COLOR */
-    case 3 : /* VISUAL3D_TOM_VERTEX */
-      myIntShadingMethod = TEL_SM_GOURAUD;
-      break;
-    default :
-      myIntShadingMethod = TEL_SM_FLAT;
-      break;
-  }
-}
-
-/*----------------------------------------------------------------------*/
-
-OpenGl_View::~OpenGl_View ()
+  myProjectionState (0),
+  myModelViewState (0),
+  myStateCounter (theCounter),
+  myLastLightSourceState (0, 0),
+  myModificationState (1), // initial state
+  myTextureParams   (new OpenGl_AspectFace()),
+  myBgGradientArray (new OpenGl_BackgroundArray (Graphic3d_TOB_GRADIENT)),
+  myBgTextureArray  (new OpenGl_BackgroundArray (Graphic3d_TOB_TEXTURE))
 {
-  if (myAnimationListIndex)
-    glDeleteLists((GLuint)myAnimationListIndex,1);
-
-  if ( myBgTexture.TexId != 0 )
-    glDeleteTextures( 1, (GLuint*)&(myBgTexture.TexId) );
+  myCurrLightSourceState = myStateCounter->Increment();
 }
 
 /*----------------------------------------------------------------------*/
 
-void OpenGl_View::SetBackfacing (const Standard_Integer AMode)
+OpenGl_View::~OpenGl_View()
 {
-  myBackfacing = AMode;
-  myResetFLIST = Standard_True;
+  ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context
+  OpenGl_Element::Destroy (NULL, myTrihedron);
+  OpenGl_Element::Destroy (NULL, myGraduatedTrihedron);
+  OpenGl_Element::Destroy (NULL, myBgGradientArray);
+  OpenGl_Element::Destroy (NULL, myBgTextureArray);
+  OpenGl_Element::Destroy (NULL, myTextureParams);
 }
 
-/*----------------------------------------------------------------------*/
-
-//call_togl_setlight
-void OpenGl_View::SetLights (const CALL_DEF_VIEWCONTEXT &AContext)
+void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
 {
-  myLights.Clear();
-
-  const int nb_lights = AContext.NbActiveLight;
-
-  int i = 0;
-  const CALL_DEF_LIGHT *alight = &(AContext.ActiveLight[0]);
-  for ( ; i < nb_lights; i++, alight++ )
+  if (myTrihedron != NULL)
   {
-    OpenGl_Light rep;
-
-       switch( alight->LightType )
-    {
-      case 0 : /* TOLS_AMBIENT */
-        rep.type = TLightAmbient;
-        rep.col.rgb[0] = alight->Color.r;
-        rep.col.rgb[1] = alight->Color.g;
-        rep.col.rgb[2] = alight->Color.b;
-        break;
-
-      case 1 : /* TOLS_DIRECTIONAL */
-        rep.type = TLightDirectional;
-        rep.col.rgb[0] = alight->Color.r;
-        rep.col.rgb[1] = alight->Color.g;
-        rep.col.rgb[2] = alight->Color.b;
-        rep.dir[0] = alight->Direction.x;
-        rep.dir[1] = alight->Direction.y;
-        rep.dir[2] = alight->Direction.z;
-        break;
-
-      case 2 : /* TOLS_POSITIONAL */
-        rep.type = TLightPositional;
-        rep.col.rgb[0] = alight->Color.r;
-        rep.col.rgb[1] = alight->Color.g;
-        rep.col.rgb[2] = alight->Color.b;
-        rep.pos[0] = alight->Position.x;
-        rep.pos[1] = alight->Position.y;
-        rep.pos[2] = alight->Position.z;
-        rep.atten[0] = alight->Attenuation[0];
-        rep.atten[1] = alight->Attenuation[1];
-        break;
-
-      case 3 : /* TOLS_SPOT */
-        rep.type = TLightSpot;
-        rep.col.rgb[0] = alight->Color.r;
-        rep.col.rgb[1] = alight->Color.g;
-        rep.col.rgb[2] = alight->Color.b;
-        rep.pos[0] = alight->Position.x;
-        rep.pos[1] = alight->Position.y;
-        rep.pos[2] = alight->Position.z;
-        rep.dir[0] = alight->Direction.x;
-        rep.dir[1] = alight->Direction.y;
-        rep.dir[2] = alight->Direction.z;
-        rep.shine = alight->Concentration;
-        rep.atten[0] = alight->Attenuation[0];
-        rep.atten[1] = alight->Attenuation[1];
-        rep.angle = alight->Angle;
-        break;
-    }
-
-    rep.HeadLight = alight->Headlight;
-
-    myLights.Append(rep);
+    myTrihedron->Release (theCtx.operator->());
   }
-}
-
-/*----------------------------------------------------------------------*/
-
-//call_togl_setplane
-void OpenGl_View::SetClippingPlanes (const CALL_DEF_VIEWCONTEXT &AContext)
-{
-  // clear clipping planes information
-  myClippingPlanes.Clear();
-  // update information 
-  int i = 0;
-  for (; i < AContext.NbActivePlane; i++)
+  if (myGraduatedTrihedron != NULL)
   {
-    const CALL_DEF_PLANE &aCPlane = AContext.ActivePlane[i];
-    if ( aCPlane.Active && aCPlane.PlaneId > 0 )
-    {
-      OPENGL_CLIP_REP aPlane;
-      aPlane.equation[0] = aCPlane.CoefA;
-      aPlane.equation[1] = aCPlane.CoefB;
-      aPlane.equation[2] = aCPlane.CoefC;
-      aPlane.equation[3] = aCPlane.CoefD;
-      myClippingPlanes.Append( aPlane );
-    }
+    myGraduatedTrihedron->Release (theCtx.operator->());
   }
-}
 
-/*----------------------------------------------------------------------*/
-
-//call_togl_setvisualisation
-void OpenGl_View::SetVisualisation (const CALL_DEF_VIEWCONTEXT &AContext)
-{
-  myVisualization = AContext.Visualization;
-  // Shading method
-  switch (AContext.Model)
+  if (!myTextureEnv.IsNull())
   {
-    case 1 : /* VISUAL3D_TOM_INTERP_COLOR */
-    case 3 : /* VISUAL3D_TOM_VERTEX */
-      myIntShadingMethod = TEL_SM_GOURAUD;
-      break;
-    default :
-      myIntShadingMethod = TEL_SM_FLAT;
-      break;
+    theCtx->DelayedRelease (myTextureEnv);
+    myTextureEnv.Nullify();
   }
-}
-
-/*----------------------------------------------------------------------*/
 
-//call_togl_cliplimit
-void OpenGl_View::SetClipLimit (const CALL_DEF_VIEW &ACView)
-{
-  myZClip.Back.Limit =
-    ( ACView.Context.ZClipBackPlane - ACView.Mapping.BackPlaneDistance ) /
-    ( ACView.Mapping.FrontPlaneDistance - ACView.Mapping.BackPlaneDistance );
-  myZClip.Front.Limit =
-    ( ACView.Context.ZClipFrontPlane - ACView.Mapping.BackPlaneDistance ) /
-    ( ACView.Mapping.FrontPlaneDistance - ACView.Mapping.BackPlaneDistance );
-  if ( myZClip.Back.Limit < 0.F )
-    myZClip.Back.Limit = 0.F;
-  if ( myZClip.Front.Limit > 1.F )
-    myZClip.Front.Limit = 1.F;
-  if ( myZClip.Back.Limit > myZClip.Front.Limit )
+  if (myTextureParams != NULL)
   {
-    myZClip.Back.Limit = 0.F;
-    myZClip.Front.Limit = 1.F;
+    myTextureParams->Release (theCtx.operator->());
+  }
+  if (myBgGradientArray != NULL)
+  {
+    myBgGradientArray->Release (theCtx.operator->());
+  }
+  if (myBgTextureArray != NULL)
+  {
+    myBgTextureArray->Release (theCtx.operator->());
   }
-
-  myZClip.Back.IsOn = (ACView.Context.BackZClipping != 0);
-  myZClip.Front.IsOn = (ACView.Context.FrontZClipping != 0);
 }
 
-/*----------------------------------------------------------------------*/
-
-//call_togl_viewmapping
-void OpenGl_View::SetMapping (const CALL_DEF_VIEW &ACView)
+void OpenGl_View::SetTextureEnv (const Handle(OpenGl_Context)&       theCtx,
+                                 const Handle(Graphic3d_TextureEnv)& theTexture)
 {
-  const float ratio = ACView.DefWindow.dy / ACView.DefWindow.dx;
-  const float r_ratio = ACView.DefWindow.dx / ACView.DefWindow.dy;
-
-  TEL_VIEW_MAPPING Map;
-
-  Map.window.xmin = ACView.Mapping.WindowLimit.um;
-  Map.window.ymin = ACView.Mapping.WindowLimit.vm;
-  Map.window.xmax = ACView.Mapping.WindowLimit.uM;
-  Map.window.ymax = ACView.Mapping.WindowLimit.vM;
-
-  Map.viewport.xmin = 0.F;
-  Map.viewport.xmax = ( 1.F < r_ratio ? 1.F : r_ratio );
-  Map.viewport.ymin = 0.F;
-  Map.viewport.ymax = ( 1.F < ratio ? 1.F : ratio );
-  Map.viewport.zmin = 0.F;
-  Map.viewport.zmax = 1.F;
+  if (!myTextureEnv.IsNull())
+  {
+    theCtx->DelayedRelease (myTextureEnv);
+    myTextureEnv.Nullify();
+  }
 
-  /* projection type */
-  switch( ACView.Mapping.Projection )
+  if (theTexture.IsNull())
   {
-    case 0 :
-      Map.proj = TelPerspective;
-      break;
-    case 1 :
-      Map.proj = TelParallel;
-      break;
+    return;
   }
 
-  /* projection reference point */
-  Map.prp[0] = ACView.Mapping.ProjectionReferencePoint.x;
-  Map.prp[1] = ACView.Mapping.ProjectionReferencePoint.y;
-  Map.prp[2] = ACView.Mapping.ProjectionReferencePoint.z;
-  if (!openglDisplay.IsNull() && !openglDisplay->Walkthrough())
-    Map.prp[2] += ACView.Mapping.FrontPlaneDistance;
+  myTextureEnv = new OpenGl_Texture (theTexture->GetParams());
+  Handle(Image_PixMap) anImage = theTexture->GetImage();
+  if (!anImage.IsNull())
+    myTextureEnv->Init (theCtx, *anImage.operator->(), theTexture->Type());
 
-  /* view plane distance */
-  Map.vpd = ACView.Mapping.ViewPlaneDistance;
+  myModificationState++;
+}
 
-  /* back plane distance */
-  Map.bpd = ACView.Mapping.BackPlaneDistance;
+void OpenGl_View::SetSurfaceDetail (const Visual3d_TypeOfSurfaceDetail theMode)
+{
+  mySurfaceDetail = theMode;
 
-  /* front plane distance */
-  Map.fpd = ACView.Mapping.FrontPlaneDistance;
+  myModificationState++;
+}
 
-  Tint err_ind = 0;
+// =======================================================================
+// function : SetBackfacing
+// purpose  :
+// =======================================================================
+void OpenGl_View::SetBackfacing (const Standard_Integer theMode)
+{
+  myBackfacing = theMode;
+}
 
-  /* use user-defined matrix */
-  if ( ACView.Mapping.IsCustomMatrix )
+// =======================================================================
+// function : SetLights
+// purpose  :
+// =======================================================================
+void OpenGl_View::SetLights (const CALL_DEF_VIEWCONTEXT& theViewCtx)
+{
+  myLights.Clear();
+  for (Standard_Integer aLightIt = 0; aLightIt < theViewCtx.NbActiveLight; ++aLightIt)
   {
-    int i, j;
-    for( i = 0; i < 4; i++ )
-      for( j = 0; j < 4; j++ )
-        myMappingMatrix[i][j] = ACView.Mapping.ProjectionMatrix[i][j];
+    myLights.Append (theViewCtx.ActiveLight[aLightIt]);
   }
-  else 
-    TelEvalViewMappingMatrix( &Map, &err_ind, myMappingMatrix );
-
-  if ( !err_ind )
-    myExtra.map = Map;
+  myCurrLightSourceState = myStateCounter->Increment();
 }
 
 /*----------------------------------------------------------------------*/
 
-//call_togl_vieworientation
-void OpenGl_View::SetOrientation (const CALL_DEF_VIEW &ACView)
+//call_togl_setvisualisation
+void OpenGl_View::SetVisualisation (const CALL_DEF_VIEWCONTEXT &AContext)
 {
-  Tfloat Vrp[3];
-  Tfloat Vpn[3];
-  Tfloat Vup[3];
-  Tfloat ScaleFactors[3];
-
-  Vrp[0] = ACView.Orientation.ViewReferencePoint.x;
-  Vrp[1] = ACView.Orientation.ViewReferencePoint.y;
-  Vrp[2] = ACView.Orientation.ViewReferencePoint.z;
-
-  Vpn[0] = ACView.Orientation.ViewReferencePlane.x;
-  Vpn[1] = ACView.Orientation.ViewReferencePlane.y;
-  Vpn[2] = ACView.Orientation.ViewReferencePlane.z;
-
-  Vup[0] = ACView.Orientation.ViewReferenceUp.x;
-  Vup[1] = ACView.Orientation.ViewReferenceUp.y;
-  Vup[2] = ACView.Orientation.ViewReferenceUp.z;
-
-  ScaleFactors[0] = ACView.Orientation.ViewScaleX;
-  ScaleFactors[1] = ACView.Orientation.ViewScaleY;
-  ScaleFactors[2] = ACView.Orientation.ViewScaleZ;
-
-  Tint err_ind = 0;
-
-  // use user-defined matrix
-  if ( ACView.Orientation.IsCustomMatrix )
-  {
-    int i, j;
-    for( i = 0; i < 4; i++ )
-      for( j = 0; j < 4; j++ )
-        myOrientationMatrix[i][j] = ACView.Orientation.ModelViewMatrix[i][j];
-  }
-  else
-    TelEvalViewOrientationMatrix( Vrp, Vpn, Vup, ScaleFactors, &err_ind, myOrientationMatrix );
-
-  if ( !err_ind )
-  {
-    myExtra.vrp[0] = Vrp[0];
-    myExtra.vrp[1] = Vrp[1];
-    myExtra.vrp[2] = Vrp[2];
+  myVisualization = AContext.Visualization;
+  myShadingModel  = (Visual3d_TypeOfModel )AContext.Model;
+}
 
-    myExtra.vpn[0] = Vpn[0];
-    myExtra.vpn[1] = Vpn[1];
-    myExtra.vpn[2] = Vpn[2];
+/*----------------------------------------------------------------------*/
 
-    myExtra.vup[0] = Vup[0];
-    myExtra.vup[1] = Vup[1];
-    myExtra.vup[2] = Vup[2];
+//call_togl_cliplimit
+void OpenGl_View::SetClipLimit (const Graphic3d_CView& theCView)
+{
+  myZClip.Back.Limit = theCView.Context.ZClipBackPlane;
+  myZClip.Front.Limit = theCView.Context.ZClipFrontPlane;
 
-    myExtra.scaleFactors[0] = ScaleFactors[0],
-    myExtra.scaleFactors[1] = ScaleFactors[1],
-    myExtra.scaleFactors[2] = ScaleFactors[2];
-  }
+  myZClip.Back.IsOn  = (theCView.Context.BackZClipping  != 0);
+  myZClip.Front.IsOn = (theCView.Context.FrontZClipping != 0);
 }
 
 /*----------------------------------------------------------------------*/
 
-void OpenGl_View::SetFog (const CALL_DEF_VIEW &ACView, const Standard_Boolean AFlag)
+void OpenGl_View::SetFog (const Graphic3d_CView& theCView,
+                          const Standard_Boolean theFlag)
 {
-  if( !AFlag )
+  if (!theFlag)
   {
     myFog.IsOn = Standard_False;
   }
@@ -396,250 +210,246 @@ void OpenGl_View::SetFog (const CALL_DEF_VIEW &ACView, const Standard_Boolean AF
   {
     myFog.IsOn = Standard_True;
 
-    myFog.Front =
-      (ACView.Context.DepthFrontPlane - ACView.Mapping.BackPlaneDistance) /
-      (ACView.Mapping.FrontPlaneDistance - ACView.Mapping.BackPlaneDistance);
-
-    myFog.Back =
-      (ACView.Context.DepthBackPlane - ACView.Mapping.BackPlaneDistance) /
-      (ACView.Mapping.FrontPlaneDistance - ACView.Mapping.BackPlaneDistance);
-
-    if (myFog.Front < 0.F)
-      myFog.Front = 0.F;
-    else if (myFog.Front > 1.F)
-      myFog.Front = 1.F;
+    myFog.Front = theCView.Context.DepthFrontPlane;
+    myFog.Back = theCView.Context.DepthBackPlane;
 
-    if (myFog.Back < 0.F)
-      myFog.Back = 0.F;
-    else if (myFog.Back > 1.F)
-      myFog.Back = 1.F;
-
-    if (myFog.Back > myFog.Front)
-    {
-      myFog.Front = 1.F;
-      myFog.Back = 0.F;
-    }
-
-    myFog.Color.rgb[0] = ACView.DefWindow.Background.r;
-    myFog.Color.rgb[1] = ACView.DefWindow.Background.g;
-    myFog.Color.rgb[2] = ACView.DefWindow.Background.b;
-    myFog.Color.rgb[3] = 1.F;
+    myFog.Color.rgb[0] = theCView.DefWindow.Background.r;
+    myFog.Color.rgb[1] = theCView.DefWindow.Background.g;
+    myFog.Color.rgb[2] = theCView.DefWindow.Background.b;
+    myFog.Color.rgb[3] = 1.0f;
   }
 }
 
 /*----------------------------------------------------------------------*/
 
-void OpenGl_View::TriedronDisplay (const Aspect_TypeOfTriedronPosition APosition, const Quantity_NameOfColor AColor,
-                                  const Standard_Real AScale, const Standard_Boolean AsWireframe)
+void OpenGl_View::TriedronDisplay (const Handle(OpenGl_Context)&       theCtx,
+                                   const Aspect_TypeOfTriedronPosition thePosition,
+                                   const Quantity_NameOfColor          theColor,
+                                   const Standard_Real                 theScale,
+                                   const Standard_Boolean              theAsWireframe)
 {
-  myTrihedron = new OpenGl_Trihedron (APosition, AColor, AScale, AsWireframe);
+  OpenGl_Element::Destroy (theCtx.operator->(), myTrihedron);
+  myTrihedron = new OpenGl_Trihedron (thePosition, theColor, theScale, theAsWireframe);
 }
 
 /*----------------------------------------------------------------------*/
 
-void OpenGl_View::TriedronErase ()
+void OpenGl_View::TriedronErase (const Handle(OpenGl_Context)& theCtx)
 {
-  myTrihedron.Nullify();
+  OpenGl_Element::Destroy (theCtx.operator->(), myTrihedron);
 }
 
 /*----------------------------------------------------------------------*/
 
-void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_CGraduatedTrihedron &data)
+void OpenGl_View::GraduatedTrihedronDisplay (const Handle(OpenGl_Context)&        theCtx,
+                                             const Graphic3d_CGraduatedTrihedron& theData)
 {
-  myGraduatedTrihedron = new OpenGl_GraduatedTrihedron(data);
+  OpenGl_Element::Destroy (theCtx.operator->(), myGraduatedTrihedron);
+  myGraduatedTrihedron = new OpenGl_GraduatedTrihedron (theData);
 }
 
 /*----------------------------------------------------------------------*/
 
-void OpenGl_View::GraduatedTrihedronErase ()
+void OpenGl_View::GraduatedTrihedronErase (const Handle(OpenGl_Context)& theCtx)
 {
-  myGraduatedTrihedron.Nullify();
+  OpenGl_Element::Destroy (theCtx.operator->(), myGraduatedTrihedron);
 }
 
 /*----------------------------------------------------------------------*/
 
 //transform_persistence_end
-void OpenGl_View::EndTransformPersistence ()
+void OpenGl_View::EndTransformPersistence(const Handle(OpenGl_Context)& theCtx)
 {
-  if ( myIsTransPers )
+  if (myIsTransPers)
   {
-    /* restore matrix */
-    glMatrixMode (GL_PROJECTION);
-    glPopMatrix ();
-    glMatrixMode (GL_MODELVIEW);
-    glPopMatrix ();
+    theCtx->WorldViewState.Pop();
+    theCtx->ProjectionState.Pop();
+
+    theCtx->ApplyProjectionMatrix();
+
     myIsTransPers = Standard_False;
   }
-} 
+}
 
 /*----------------------------------------------------------------------*/
 
 //transform_persistence_begin
-const TEL_TRANSFORM_PERSISTENCE * OpenGl_View::BeginTransformPersistence (const TEL_TRANSFORM_PERSISTENCE *ATransPers)
+const TEL_TRANSFORM_PERSISTENCE* OpenGl_View::BeginTransformPersistence (const Handle(OpenGl_Context)& theCtx,
+                                                                         const TEL_TRANSFORM_PERSISTENCE* theTransPers)
 {
-  const TEL_TRANSFORM_PERSISTENCE *TransPers_old = myTransPers;
-
-  if ( ATransPers->mode == 0 )
+  const TEL_TRANSFORM_PERSISTENCE* aTransPersPrev = myTransPers;
+  myTransPers = theTransPers;
+  if (theTransPers->mode == 0)
   {
-    EndTransformPersistence();
-    return TransPers_old;
+    EndTransformPersistence (theCtx);
+    return aTransPersPrev;
   }
 
-  myTransPers = ATransPers;
+  GLint aViewport[4];
+  OpenGl_Mat4d aModelMatrix, aProjMatrix;
+  theCtx->core11fwd->glGetIntegerv (GL_VIEWPORT, aViewport);
+  aModelMatrix.Convert (theCtx->ModelWorldState.Current() * theCtx->WorldViewState.Current());
+  aProjMatrix .Convert (theCtx->ProjectionState.Current());
 
-  GLint viewport[4];
-  glGetIntegerv (GL_VIEWPORT, viewport);
-  GLdouble modelMatrix[4][4];
-  glGetDoublev( GL_MODELVIEW_MATRIX,  (GLdouble *) modelMatrix );
-  GLdouble projMatrix[4][4];
-  glGetDoublev( GL_PROJECTION_MATRIX, (GLdouble *) projMatrix );
-
-  double W = viewport[2];
-  double H = viewport[3];
-
-  if ( myIsTransPers )
+  const GLdouble aViewportW = (GLdouble )aViewport[2];
+  const GLdouble aViewportH = (GLdouble )aViewport[3];
+  if (myIsTransPers)
   {
-    /* restore matrix */
-    glMatrixMode (GL_PROJECTION);
-    glPopMatrix ();
-    glMatrixMode (GL_MODELVIEW);
-    glPopMatrix ();
+    // pop matrix stack - it will be overridden later
+    theCtx->WorldViewState.Pop();
+    theCtx->ProjectionState.Pop();
   }
   else
+  {
     myIsTransPers = Standard_True;
+  }
 
-  glMatrixMode( GL_MODELVIEW );
-  glPushMatrix();
-  glLoadIdentity();
-
-  glMatrixMode( GL_PROJECTION );
-  glPushMatrix();
-  glLoadIdentity();
+  // push matrices into stack and reset them
+  theCtx->WorldViewState.Push();
+  theCtx->ProjectionState.Push();
 
-  /*pre loading matrix*/
-  if( ATransPers->mode & TPF_PAN )
-    /* Annulate translate matrix */
+  // get the window's (fixed) coordinates for theTransPers->point before matrixes modifications
+  GLdouble aWinX = 0.0, aWinY = 0.0, aWinZ = 0.0;
+  if ((theTransPers->mode & TPF_PAN) != TPF_PAN)
   {
-    modelMatrix[3][0] = 0.;
-    modelMatrix[3][1] = 0.;
-    modelMatrix[3][2] = 0.;
-    projMatrix[3][0] = 0.;
-    projMatrix[3][1] = 0.;
-    projMatrix[3][2] = 0.;
+    OpenGl_Utils::Project<Standard_Real> (theTransPers->pointX,
+                                          theTransPers->pointY,
+                                          theTransPers->pointZ,
+                                          aModelMatrix,
+                                          aProjMatrix,
+                                          aViewport,
+                                          aWinX,
+                                          aWinY,
+                                          aWinZ);
   }
 
-  if( ATransPers->mode & TPF_ZOOM )
-    /* Annulate zoom matrix */
+  // prevent zooming
+  if ((theTransPers->mode & TPF_ZOOM)
+   || (theTransPers->mode == TPF_TRIEDRON))
   {
-    const double scaleX = myExtra.scaleFactors[0];
-    const double scaleY = myExtra.scaleFactors[1];
-    const double scaleZ = myExtra.scaleFactors[2];
-
-    for (int i = 0; i < 3; ++i)
-    {
-      modelMatrix[0][i] /= scaleX;
-      modelMatrix[1][i] /= scaleY;
-      modelMatrix[2][i] /= scaleZ;
-    }
-
-    const double det2 = 0.002 / ( W > H ? projMatrix[1][1] : projMatrix[0][0]);
-    projMatrix[0][0] *= det2;
-    projMatrix[1][1] *= det2;
+    // compute fixed-zoom multiplier
+    // actually function works ugly with TelPerspective!
+    const GLdouble aDet2 = 0.002 / (aViewportW > aViewportH ? aProjMatrix.GetValue (1, 1) : aProjMatrix.GetValue (0, 0));
+    aProjMatrix.ChangeValue (0, 0) *= aDet2;
+    aProjMatrix.ChangeValue (1, 1) *= aDet2;
+    aProjMatrix.ChangeValue (2, 2) *= aDet2;
   }
 
-  if( ATransPers->mode & TPF_ROTATE )
-    /* Annulate rotate matrix */
+  // prevent translation - annulate translate matrix
+  if ((theTransPers->mode & TPF_PAN)
+   || (theTransPers->mode == TPF_TRIEDRON))
   {
-    modelMatrix[0][0] = 1.;
-    modelMatrix[1][1] = 1.;
-    modelMatrix[2][2] = 1.;
-
-    modelMatrix[1][0] = 0.;
-    modelMatrix[2][0] = 0.;
-    modelMatrix[0][1] = 0.;
-    modelMatrix[2][1] = 0.;
-    modelMatrix[0][2] = 0.;
-    modelMatrix[1][2] = 0.;
+    aModelMatrix.SetValue (0, 3, 0.0);
+    aModelMatrix.SetValue (1, 3, 0.0);
+    aModelMatrix.SetValue (2, 3, 0.0);
+    aProjMatrix .SetValue (0, 3, 0.0);
+    aProjMatrix .SetValue (1, 3, 0.0);
+    aProjMatrix .SetValue (2, 3, 0.0);
   }
-  else if( ATransPers->mode & TPF_RELATIVE_ROTATE )
-    /* Initialize relative rotate matrix*/
-  {
-    modelMatrix[3][0] = 0.;
-    modelMatrix[3][1] = 0.;
-    modelMatrix[3][2] = 0.;
 
-    glMatrixMode( GL_MODELVIEW );
-    glLoadIdentity();
-    glTranslated( ATransPers->pointX, ATransPers->pointY, ATransPers->pointZ );        
+  // prevent scaling-on-axis
+  if (theTransPers->mode & TPF_ZOOM)
+  {
+    const gp_Pnt anAxialScale = myCamera->AxialScale();
+    const double aScaleX = anAxialScale.X();
+    const double aScaleY = anAxialScale.Y();
+    const double aScaleZ = anAxialScale.Z();
+    for (int i = 0; i < 3; ++i)
+    {
+      aModelMatrix.ChangeValue (0, i) /= aScaleX;
+      aModelMatrix.ChangeValue (1, i) /= aScaleY;
+      aModelMatrix.ChangeValue (2, i) /= aScaleZ;
+    }
   }
 
-  if( ATransPers->mode == TPF_TRIEDRON )
+  // prevent rotating - annulate rotate matrix
+  if (theTransPers->mode & TPF_ROTATE)
   {
-    /* Annulate translation matrix */
-    modelMatrix[3][0] = 0.;
-    modelMatrix[3][1] = 0.;
-    modelMatrix[3][2] = 0.;
-
-    projMatrix[3][0] = 0.;
-    projMatrix[3][1] = 0.;
-    projMatrix[3][2] = 0.;
-
-    const double det2 = 0.002 / ( W > H ? projMatrix[1][1] : projMatrix[0][0]);
-    projMatrix[0][0] *= det2;
-    projMatrix[1][1] *= det2;
+    aModelMatrix.SetValue (0, 0, 1.0);
+    aModelMatrix.SetValue (1, 1, 1.0);
+    aModelMatrix.SetValue (2, 2, 1.0);
+
+    aModelMatrix.SetValue (1, 0, 0.0);
+    aModelMatrix.SetValue (2, 0, 0.0);
+    aModelMatrix.SetValue (0, 1, 0.0);
+    aModelMatrix.SetValue (2, 1, 0.0);
+    aModelMatrix.SetValue (0, 2, 0.0);
+    aModelMatrix.SetValue (1, 2, 0.0);
   }
 
-  /* load matrix */
-  glMatrixMode (GL_MODELVIEW);
-  glMultMatrixd ((GLdouble *) modelMatrix);
+  // load computed matrices
+  theCtx->ModelWorldState.SetIdentity();
+  theCtx->WorldViewState.SetCurrent<Standard_Real> (aModelMatrix);
+  theCtx->ProjectionState.SetCurrent<Standard_Real> (aProjMatrix);
 
-  glMatrixMode (GL_PROJECTION);
-  glMultMatrixd ((GLdouble *) projMatrix);
-
-  /*post loading matrix*/
-  if( ATransPers->mode == TPF_TRIEDRON )
+  if (theTransPers->mode == TPF_TRIEDRON)
   {
-    glMatrixMode( GL_PROJECTION );
-
-       double winx, winy, winz;
-    const GLdouble idenMatrix[4][4] = { {1.,0.,0.,0.}, {0.,1.,0.,0.}, {0.,0.,1.,0.}, {0.,0.,0.,1.} };
-
-    gluUnProject( W/2., H/2., 0., (GLdouble*)idenMatrix, (GLdouble*)projMatrix, (GLint*)viewport, &winx, &winy, &winz);
-    double W1, H1;
-    W1 = winx;
-    H1 = winy;
-    gluUnProject( -W/2., -H/2., 0., (GLdouble*)idenMatrix, (GLdouble*)projMatrix, (GLint*)viewport, &winx, &winy, &winz);
-    double W2, H2;
-    W2 = winx;
-    H2 = winy;
-
-    if( ATransPers->pointX == 0. && ATransPers->pointY == 0. )
-    {
-      /*center*/
-    }
-    else if( ATransPers->pointX > 0. && ATransPers->pointY > 0. )
-    {
-      /*right upper*/
-      glTranslated( 0.5*(W1 - W2 - ATransPers->pointZ), 0.5*(H1 - H2 - ATransPers->pointZ), 0. );
-    }
-    else if( ATransPers->pointX > 0. && ATransPers->pointY < 0. )
-    {
-      /*right lower*/
-      glTranslated( 0.5*(W1 - W2 - ATransPers->pointZ), 0.5*(H2 - H1 + ATransPers->pointZ), 0. );
-    }
-    else if( ATransPers->pointX < 0. && ATransPers->pointY > 0. )
+    // move to the window corner
+    if (theTransPers->pointX != 0.0
+     && theTransPers->pointY != 0.0)
     {
-      /*left upper*/
-      glTranslated( 0.5*(W2 - W1 + ATransPers->pointZ), 0.5*(H1 - H2 - ATransPers->pointZ), 0. );
-    }
-    else if( ATransPers->pointX < 0 && ATransPers->pointY < 0 )
-    {
-      /*left lower*/
-      glTranslated( -(W1 - W2)/2. + ATransPers->pointZ/2., -(H1-H2)/2. + ATransPers->pointZ/2., 0. );
+      GLdouble aW1, aH1, aW2, aH2, aDummy;
+
+      OpenGl_Mat4d anIdentity;
+
+      OpenGl_Utils::UnProject<Standard_Real> (0.5 * aViewportW,
+                                              0.5 * aViewportH,
+                                              0.0,
+                                              anIdentity,
+                                              aProjMatrix,
+                                              aViewport,
+                                              aW1,
+                                              aH1,
+                                              aDummy);
+
+      OpenGl_Utils::UnProject<Standard_Real> (-0.5 * aViewportW,
+                                              -0.5 * aViewportH,
+                                              0.0,
+                                              anIdentity,
+                                              aProjMatrix,
+                                              aViewport,
+                                              aW2,
+                                              aH2,
+                                              aDummy);
+
+      GLdouble aMoveX = 0.5 * (aW1 - aW2 - theTransPers->pointZ);
+      GLdouble aMoveY = 0.5 * (aH1 - aH2 - theTransPers->pointZ);
+      aMoveX = (theTransPers->pointX > 0.0) ? aMoveX : -aMoveX;
+      aMoveY = (theTransPers->pointY > 0.0) ? aMoveY : -aMoveY;
+
+      OpenGl_Utils::Translate<Standard_Real> (aProjMatrix, aMoveX, aMoveY, 0.0);
+      theCtx->ProjectionState.SetCurrent<Standard_Real> (aProjMatrix);
     }
   }
+  else if ((theTransPers->mode & TPF_PAN) != TPF_PAN)
+  {
+    // move to thePoint using saved win-coordinates ('marker-behaviour')
+    GLdouble aMoveX, aMoveY, aMoveZ;
+
+    OpenGl_Utils::UnProject<Standard_Real> (aWinX,
+                                            aWinY,
+                                            aWinZ,
+                                            aModelMatrix,
+                                            aProjMatrix,
+                                            aViewport,
+                                            aMoveX,
+                                            aMoveY,
+                                            aMoveZ);
+
+    OpenGl_Utils::Translate<Standard_Real> (aModelMatrix, aMoveX, aMoveY, aMoveZ);
+    theCtx->WorldViewState.SetCurrent<Standard_Real> (aModelMatrix);
+  }
 
-  return TransPers_old;
+  theCtx->ApplyProjectionMatrix();
+  return aTransPersPrev;
 }
 
 /*----------------------------------------------------------------------*/
+
+void OpenGl_View::GetMatrices (OpenGl_Mat4& theOrientation,
+                               OpenGl_Mat4& theViewMapping) const
+{
+  theViewMapping = myCamera->ProjectionMatrixF();
+  theOrientation = myCamera->OrientationMatrixF();
+}
+/*----------------------------------------------------------------------*/