0026361: Visualization - move OpenGl_TextFormatter to Font_TextFormatter
[occt.git] / src / OpenGl / OpenGl_Text.cxx
index 6280229..ddc1027 100644 (file)
@@ -2,47 +2,42 @@
 // Created by: Sergey ZERCHANINOV
 // Copyright (c) 2011-2013 OPEN CASCADE SAS
 //
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
 //
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// 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.
 //
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
-
-#include <OpenGl_GlCore11.hxx>
-#include <OpenGl_Text.hxx>
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
 #include <OpenGl_AspectText.hxx>
+#include <OpenGl_GlCore11.hxx>
 #include <OpenGl_GraphicDriver.hxx>
+#include <OpenGl_ShaderManager.hxx>
+#include <OpenGl_ShaderProgram.hxx>
+#include <OpenGl_ShaderStates.hxx>
+#include <OpenGl_Text.hxx>
+#include <OpenGl_Utils.hxx>
 #include <OpenGl_Workspace.hxx>
 
 #include <Font_FontMgr.hxx>
 #include <TCollection_HAsciiString.hxx>
 
-#ifdef HAVE_CONFIG_H
-  #include <config.h>
-#endif
-
 #ifdef HAVE_GL2PS
   #include <gl2ps.h>
 #endif
 
 namespace
 {
-  static const GLdouble THE_IDENTITY_MATRIX[4][4] =
+  static const GLdouble THE_IDENTITY_MATRIX[16] =
   {
-    {1.,0.,0.,0.},
-    {0.,1.,0.,0.},
-    {0.,0.,1.,0.},
-    {0.,0.,0.,1.}
+    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
   };
 
 #ifdef HAVE_GL2PS
@@ -118,6 +113,7 @@ namespace
     char aPsFont[64];
     getGL2PSFontName (theAspect.FontName().ToCString(), aPsFont);
 
+  #if !defined(GL_ES_VERSION_2_0)
     if (theIs2d)
     {
       glRasterPos2f (0.0f, 0.0f);
@@ -129,11 +125,12 @@ namespace
 
     GLubyte aZero = 0;
     glBitmap (1, 1, 0, 0, 0, 0, &aZero);
+  #endif
 
     // Standard GL2PS's alignment isn't used, because it doesn't work correctly
     // for all formats, therefore alignment is calculated manually relative
     // to the bottom-left corner, which corresponds to the GL2PS_TEXT_BL value
-    gl2psTextOpt (theText.ToCString(), aPsFont, theHeight, GL2PS_TEXT_BL, theAspect.Angle());
+    gl2psTextOpt (theText.ToCString(), aPsFont, (GLshort)theHeight, GL2PS_TEXT_BL, theAspect.Angle());
   }
 #endif
 
@@ -160,16 +157,16 @@ OpenGl_Text::OpenGl_Text()
 // function : OpenGl_Text
 // purpose  :
 // =======================================================================
-OpenGl_Text::OpenGl_Text (const TCollection_ExtendedString& theText,
-                          const OpenGl_Vec3&                thePoint,
-                          const OpenGl_TextParam&           theParams)
+OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText,
+                          const OpenGl_Vec3&       thePoint,
+                          const OpenGl_TextParam&  theParams)
 : myWinX (0.0f),
   myWinY (0.0f),
   myWinZ (0.0f),
   myScaleHeight  (1.0f),
   myExportHeight (1.0f),
   myParams (theParams),
-  myString ((Standard_Utf16Char* )theText.ToExtString()),
+  myString (theText),
   myPoint  (thePoint),
   myIs2d   (false)
 {
@@ -194,7 +191,7 @@ void OpenGl_Text::SetFontSize (const Handle(OpenGl_Context)& theCtx,
 {
   if (myParams.Height != theFontSize)
   {
-    Release (theCtx);
+    Release (theCtx.operator->());
   }
   myParams.Height = theFontSize;
 }
@@ -207,7 +204,7 @@ void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
                         const Standard_Utf8Char*      theText,
                         const OpenGl_Vec3&            thePoint)
 {
-  releaseVbos (theCtx);
+  releaseVbos (theCtx.operator->());
   myIs2d   = false;
   myPoint  = thePoint;
   myString.FromUnicode (theText);
@@ -224,11 +221,11 @@ void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
 {
   if (myParams.Height != theParams.Height)
   {
-    Release (theCtx);
+    Release (theCtx.operator->());
   }
   else
   {
-    releaseVbos (theCtx);
+    releaseVbos (theCtx.operator->());
   }
   myIs2d   = false;
   myParams = theParams;
@@ -247,11 +244,11 @@ void OpenGl_Text::Init (const Handle(OpenGl_Context)&     theCtx,
 {
   if (myParams.Height != theParams.Height)
   {
-    Release (theCtx);
+    Release (theCtx.operator->());
   }
   else
   {
-    releaseVbos (theCtx);
+    releaseVbos (theCtx.operator->());
   }
   myIs2d       = true;
   myParams     = theParams;
@@ -273,14 +270,14 @@ OpenGl_Text::~OpenGl_Text()
 // function : releaseVbos
 // purpose  :
 // =======================================================================
-void OpenGl_Text::releaseVbos (const Handle(OpenGl_Context)& theCtx)
+void OpenGl_Text::releaseVbos (OpenGl_Context* theCtx)
 {
   for (Standard_Integer anIter = 0; anIter < myVertsVbo.Length(); ++anIter)
   {
     Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.ChangeValue (anIter);
     Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.ChangeValue (anIter);
 
-    if (!theCtx.IsNull())
+    if (theCtx)
     {
       theCtx->DelayedRelease (aVerts);
       theCtx->DelayedRelease (aTCrds);
@@ -291,15 +288,13 @@ void OpenGl_Text::releaseVbos (const Handle(OpenGl_Context)& theCtx)
   myTextures.Clear();
   myVertsVbo.Clear();
   myTCrdsVbo.Clear();
-  myVertsArray.Clear();
-  myTCrdsArray.Clear();
 }
 
 // =======================================================================
 // function : Release
 // purpose  :
 // =======================================================================
-void OpenGl_Text::Release (const Handle(OpenGl_Context)& theCtx)
+void OpenGl_Text::Release (OpenGl_Context* theCtx)
 {
   releaseVbos (theCtx);
   if (!myFont.IsNull())
@@ -307,7 +302,8 @@ void OpenGl_Text::Release (const Handle(OpenGl_Context)& theCtx)
     Handle(OpenGl_Context) aCtx = theCtx;
     const TCollection_AsciiString aKey = myFont->ResourceKey();
     myFont.Nullify();
-    aCtx->ReleaseResource (aKey, Standard_True);
+    if (! aCtx.IsNull())
+      aCtx->ReleaseResource (aKey, Standard_True);
   }
 }
 
@@ -382,14 +378,22 @@ void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
 // =======================================================================
 void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
 {
-  const OpenGl_AspectText* aTextAspect = theWorkspace->AspectText (Standard_True);
-  const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture();
+  const OpenGl_AspectText*      aTextAspect  = theWorkspace->AspectText (Standard_True);
+  const Handle(OpenGl_Texture)  aPrevTexture = theWorkspace->DisableTexture();
+  const Handle(OpenGl_Context)& aCtx         = theWorkspace->GetGlContext();
+
+  // Bind custom shader program or generate default version
+  if (aCtx->core20fwd != NULL)
+  {
+    aCtx->ShaderManager()->BindProgram (
+      aTextAspect, aTextAspect->ShaderProgramRes (aCtx));
+  }
 
   // use highlight color or colors from aspect
   if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
   {
     render (theWorkspace->PrinterContext(),
-            theWorkspace->GetGlContext(),
+            aCtx,
             *aTextAspect,
             *theWorkspace->HighlightColor,
             *theWorkspace->HighlightColor);
@@ -397,17 +401,25 @@ void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
   else
   {
     render (theWorkspace->PrinterContext(),
-            theWorkspace->GetGlContext(),
+            aCtx,
             *aTextAspect,
             aTextAspect->Color(),
             aTextAspect->SubtitleColor());
   }
 
+  aCtx->BindProgram (NULL);
+
   // restore aspects
   if (!aPrevTexture.IsNull())
   {
     theWorkspace->EnableTexture (aPrevTexture);
   }
+
+  // restore Z buffer settings
+  if (theWorkspace->UseZBuffer())
+  {
+    glEnable (GL_DEPTH_TEST);
+  }
 }
 
 // =======================================================================
@@ -426,31 +438,35 @@ void OpenGl_Text::Render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
 // purpose  :
 // =======================================================================
 void OpenGl_Text::setupMatrix (const Handle(OpenGl_PrinterContext)& thePrintCtx,
-                               const Handle(OpenGl_Context)&        /*theCtx*/,
+                               const Handle(OpenGl_Context)&        theCtx,
                                const OpenGl_AspectText&             theTextAspect,
                                const OpenGl_Vec3                    theDVec) const
 {
-  // setup matrix
+  OpenGl_Mat4d aModViewMat;
+
   if (myIs2d)
   {
-    glLoadIdentity();
-    glTranslatef (myPoint.x() + theDVec.x(), myPoint.y() + theDVec.y(), 0.0f);
-    glRotatef (180.0f, 1.0f, 0.0f, 0.0f);
+    OpenGl_Utils::Translate<GLdouble> (aModViewMat, myPoint.x() + theDVec.x(), myPoint.y() + theDVec.y(), 0.f);
+    OpenGl_Utils::Scale<GLdouble> (aModViewMat, 1.f, -1.f, 1.f);
+    OpenGl_Utils::Rotate<GLdouble> (aModViewMat, theTextAspect.Angle(), 0.f, 0.f, 1.f);
   }
   else
   {
     // align coordinates to the nearest integer
     // to avoid extra interpolation issues
     GLdouble anObjX, anObjY, anObjZ;
-    gluUnProject (std::floor (myWinX + (GLdouble )theDVec.x()),
-                  std::floor (myWinY + (GLdouble )theDVec.y()),
-                  myWinZ + (GLdouble )theDVec.z(),
-                  (GLdouble* )THE_IDENTITY_MATRIX, myProjMatrix, myViewport,
-                  &anObjX, &anObjY, &anObjZ);
-
-    glLoadIdentity();
-    glTranslated (anObjX, anObjY, anObjZ);
-    glRotated (theTextAspect.Angle(), 0.0, 0.0, 1.0);
+    OpenGl_Utils::UnProject<Standard_Real> (std::floor (myWinX + theDVec.x()),
+                                            std::floor (myWinY + theDVec.y()),
+                                            myWinZ + theDVec.z(),
+                                            OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
+                                            OpenGl_Mat4d::Map (myProjMatrix),
+                                            myViewport,
+                                            anObjX,
+                                            anObjY,
+                                            anObjZ);
+
+    OpenGl_Utils::Translate<GLdouble> (aModViewMat, anObjX, anObjY, anObjZ);
+    OpenGl_Utils::Rotate<GLdouble>    (aModViewMat, theTextAspect.Angle(), 0.0, 0.0, 1.0);
     if (!theTextAspect.IsZoomable())
     {
     #ifdef _WIN32
@@ -464,12 +480,21 @@ void OpenGl_Text::setupMatrix (const Handle(OpenGl_PrinterContext)& thePrintCtx,
         // text should be scaled in all directions with same
         // factor to save its proportions, so use height (y) scaling
         // as it is better for keeping text/3d graphics proportions
-        glScalef (aTextScaley, aTextScaley, aTextScaley);
+        OpenGl_Utils::Scale<GLdouble> (aModViewMat, aTextScaley, aTextScaley, aTextScaley);
       }
     #endif
-      glScaled (myScaleHeight, myScaleHeight, myScaleHeight);
+      OpenGl_Utils::Scale<GLdouble> (aModViewMat, myScaleHeight, myScaleHeight, myScaleHeight);
     }
   }
+
+  theCtx->WorldViewState.SetCurrent<Standard_Real> (aModViewMat);
+  theCtx->ApplyWorldViewMatrix();
+
+  if (!theCtx->ActiveProgram().IsNull())
+  {
+    // Upload updated state to shader program
+    theCtx->ShaderManager()->PushState (theCtx->ActiveProgram());
+  }
 }
 
 // =======================================================================
@@ -494,45 +519,28 @@ void OpenGl_Text::drawText (const Handle(OpenGl_PrinterContext)& ,
   }
 #endif
 
-  if (myVertsVbo.Length() == myTextures.Length())
+  if (myVertsVbo.Length() != myTextures.Length()
+   || myTextures.IsEmpty())
   {
-    for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
-    {
-      const GLuint aTexId = myTextures.Value (anIter);
-      const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter);
-      const Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.Value (anIter);
-      aVerts->BindFixed (theCtx, GL_VERTEX_ARRAY);
-      aTCrds->BindFixed (theCtx, GL_TEXTURE_COORD_ARRAY);
-      glBindTexture (GL_TEXTURE_2D, aTexId);
-
-      glDrawArrays (GL_TRIANGLES, 0, GLsizei(aVerts->GetElemsNb()));
-
-      glBindTexture (GL_TEXTURE_2D, 0);
-      aTCrds->UnbindFixed (theCtx, GL_TEXTURE_COORD_ARRAY);
-      aVerts->UnbindFixed (theCtx, GL_VERTEX_ARRAY);
-    }
+    return;
   }
-  else if (myVertsArray.Length() == myTextures.Length())
+
+  for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
   {
-    glEnableClientState (GL_VERTEX_ARRAY);
-    glEnableClientState (GL_TEXTURE_COORD_ARRAY);
-    for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
-    {
-      const GLuint aTexId = myTextures.Value (anIter);
-      const Handle(OpenGl_Vec2Array)& aVerts = myVertsArray.Value (anIter);
-      const Handle(OpenGl_Vec2Array)& aTCrds = myTCrdsArray.Value (anIter);
+    const GLuint aTexId = myTextures.Value (anIter);
+    glBindTexture (GL_TEXTURE_2D, aTexId);
 
-      glVertexPointer   (2, GL_FLOAT, 0, (GLfloat* )&aVerts->First());
-      glTexCoordPointer (2, GL_FLOAT, 0, (GLfloat* )&aTCrds->First());
-      glBindTexture (GL_TEXTURE_2D, aTexId);
+    const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter);
+    const Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.Value (anIter);
+    aVerts->BindAttribute (theCtx, Graphic3d_TOA_POS);
+    aTCrds->BindAttribute (theCtx, Graphic3d_TOA_UV);
 
-      glDrawArrays (GL_TRIANGLES, 0, aVerts->Length());
+    glDrawArrays (GL_TRIANGLES, 0, GLsizei(aVerts->GetElemsNb()));
 
-      glBindTexture (GL_TEXTURE_2D, 0);
-    }
-    glDisableClientState (GL_TEXTURE_COORD_ARRAY);
-    glDisableClientState (GL_VERTEX_ARRAY);
+    aTCrds->UnbindAttribute (theCtx, Graphic3d_TOA_UV);
+    aVerts->UnbindAttribute (theCtx, Graphic3d_TOA_POS);
   }
+  glBindTexture (GL_TEXTURE_2D, 0);
 }
 
 // =======================================================================
@@ -569,28 +577,49 @@ Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx,
     const Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (theAspect.FontName());
     const Font_FontAspect anAspect = (theAspect.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : Font_FA_Regular;
     Handle(Font_SystemFont) aRequestedFont = aFontMgr->FindFont (aFontName, anAspect, theHeight);
-    if (aRequestedFont.IsNull())
+    Handle(Font_FTFont) aFontFt;
+    if (!aRequestedFont.IsNull())
     {
-      return aFont;
-    }
+      aFontFt = new Font_FTFont (NULL);
 
-    Handle(Font_FTFont) aFontFt = new Font_FTFont (NULL);
-    if (!aFontFt->Init (aRequestedFont->FontPath()->ToCString(), theHeight))
-    {
-      return aFont;
+      if (aFontFt->Init (aRequestedFont->FontPath()->ToCString(), theHeight))
+      {
+        aFont = new OpenGl_Font (aFontFt, theKey);
+        if (!aFont->Init (theCtx))
+        {
+          TCollection_ExtendedString aMsg;
+          aMsg += "Font '";
+          aMsg += theAspect.FontName();
+          aMsg += "' - initialization of GL resources has failed!";
+          theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
+          aFontFt.Nullify();
+          aFont->Release (theCtx.operator->());
+          aFont = new OpenGl_Font (aFontFt, theKey);
+        }
+      }
+      else
+      {
+        TCollection_ExtendedString aMsg;
+        aMsg += "Font '";
+        aMsg += theAspect.FontName();
+        aMsg += "' is broken or has incompatible format! File path: ";
+        aMsg += aRequestedFont->FontPath()->ToCString();
+        theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
+        aFontFt.Nullify();
+        aFont = new OpenGl_Font (aFontFt, theKey);
+      }
     }
-
-    Handle(OpenGl_Context) aCtx = theCtx;
-    glPushAttrib (GL_TEXTURE_BIT);
-    aFont = new OpenGl_Font (aFontFt, theKey);
-    if (!aFont->Init (aCtx))
+    else
     {
-      //glPopAttrib();
-      //return aFont; // out of resources?
+      TCollection_ExtendedString aMsg;
+      aMsg += "Font '";
+      aMsg += theAspect.FontName();
+      aMsg += "' is not found in the system!";
+      theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
+      aFont = new OpenGl_Font (aFontFt, theKey);
     }
-    glPopAttrib(); // texture bit
 
-    aCtx->ShareResource (theKey, aFont);
+    theCtx->ShareResource (theKey, aFont);
   }
   return aFont;
 }
@@ -615,34 +644,35 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
    && !myFont->ResourceKey().IsEqual (aFontKey))
   {
     // font changed
-    const_cast<OpenGl_Text* > (this)->Release (theCtx);
+    const_cast<OpenGl_Text* > (this)->Release (theCtx.operator->());
   }
 
   if (myFont.IsNull())
   {
     myFont = FindFont (theCtx, theTextAspect, myParams.Height, aFontKey);
-    if (myFont.IsNull())
-    {
-      return;
-    }
+  }
+  if (!myFont->WasInitialized())
+  {
+    return;
   }
 
   if (myTextures.IsEmpty())
   {
-    OpenGl_TextFormatter aFormatter;
+    Font_TextFormatter aFormatter;
     aFormatter.SetupAlignment (myParams.HAlign, myParams.VAlign);
     aFormatter.Reset();
-    aFormatter.Append (theCtx, myString, *myFont.operator->());
+
+    aFormatter.Append (myString, *myFont->FTFont());
     aFormatter.Format();
 
-    if (!theCtx->caps->vboDisable && theCtx->core15 != NULL)
-    {
-      aFormatter.Result (theCtx, myTextures, myVertsVbo, myTCrdsVbo);
-    }
-    else
-    {
-      aFormatter.Result (theCtx, myTextures, myVertsArray, myTCrdsArray);
-    }
+    OpenGl_TextBuilder aBuilder;
+    aBuilder.Perform (aFormatter,
+                      theCtx,
+                      *myFont.operator->(),
+                      myTextures,
+                      myVertsVbo,
+                      myTCrdsVbo);
+
     aFormatter.BndBox (myBndBox);
   }
 
@@ -654,29 +684,48 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
   myExportHeight = 1.0f;
   myScaleHeight  = 1.0f;
 
-  glMatrixMode (GL_MODELVIEW);
-  glPushMatrix();
+  theCtx->WorldViewState.Push();
+
+  myModelMatrix.Convert (theCtx->WorldViewState.Current() * theCtx->ModelWorldState.Current());
+
   if (!myIs2d)
   {
-    // retrieve active matrices for project/unproject calls
-    glGetDoublev  (GL_MODELVIEW_MATRIX,  myModelMatrix);
-    glGetDoublev  (GL_PROJECTION_MATRIX, myProjMatrix);
     glGetIntegerv (GL_VIEWPORT,          myViewport);
-    gluProject (myPoint.x(), myPoint.y(), myPoint.z(),
-                myModelMatrix, myProjMatrix, myViewport,
-                &myWinX, &myWinY, &myWinZ);
+    myProjMatrix.Convert (theCtx->ProjectionState.Current());
+
+    OpenGl_Utils::Project<Standard_Real> (myPoint.x(),
+                                          myPoint.y(),
+                                          myPoint.z(),
+                                          myModelMatrix,
+                                          myProjMatrix,
+                                          myViewport,
+                                          myWinX,
+                                          myWinY,
+                                          myWinZ);
 
     // compute scale factor for constant text height
     GLdouble x1, y1, z1;
-    gluUnProject (myWinX, myWinY, myWinZ,
-                  (GLdouble* )THE_IDENTITY_MATRIX, myProjMatrix, myViewport,
-                  &x1, &y1, &z1);
+    OpenGl_Utils::UnProject<Standard_Real> (myWinX,
+                                            myWinY,
+                                            myWinZ,
+                                            OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
+                                            myProjMatrix,
+                                            myViewport,
+                                            x1,
+                                            y1,
+                                            z1);
 
     GLdouble x2, y2, z2;
     const GLdouble h = (GLdouble )myFont->FTFont()->PointSize();
-    gluUnProject (myWinX, myWinY + h - 1.0, myWinZ,
-                  (GLdouble* )THE_IDENTITY_MATRIX, myProjMatrix, myViewport,
-                  &x2, &y2, &z2);
+    OpenGl_Utils::UnProject<Standard_Real> (myWinX,
+                                            myWinY + h,
+                                            myWinZ,
+                                            OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
+                                            myProjMatrix,
+                                            myViewport,
+                                            x2,
+                                            y2,
+                                            z2);
 
     myScaleHeight = (y2 - y1) / h;
     if (theTextAspect.IsZoomable())
@@ -686,68 +735,80 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
   }
   myExportHeight = (float )myFont->FTFont()->PointSize() / myExportHeight;
 
-  // push enabled flags to the stack
-  glPushAttrib (GL_ENABLE_BIT);
-
-  // setup depth test
-  if (!myIs2d
-   && theTextAspect.StyleType() != Aspect_TOST_ANNOTATION)
+#if !defined(GL_ES_VERSION_2_0)
+  if (theCtx->core11 != NULL)
   {
-    glEnable (GL_DEPTH_TEST);
+    glDisable (GL_LIGHTING);
   }
-  else
+#endif
+
+  // setup depth test
+  if (myIs2d
+   || theTextAspect.StyleType() == Aspect_TOST_ANNOTATION)
   {
     glDisable (GL_DEPTH_TEST);
   }
 
-  // setup alpha test
+  if (theCtx->core15fwd != NULL)
+  {
+    theCtx->core15fwd->glActiveTexture (GL_TEXTURE0);
+  }
+#if !defined(GL_ES_VERSION_2_0)
+  // activate texture unit
   GLint aTexEnvParam = GL_REPLACE;
-  glGetTexEnviv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &aTexEnvParam);
-  if (aTexEnvParam != GL_REPLACE)
+  if (theCtx->core11 != NULL)
   {
-    glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+    // setup alpha test
+    glAlphaFunc (GL_GEQUAL, 0.285f);
+    glEnable (GL_ALPHA_TEST);
+
+    glDisable (GL_TEXTURE_1D);
+    glEnable  (GL_TEXTURE_2D);
+    glGetTexEnviv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &aTexEnvParam);
+    if (aTexEnvParam != GL_REPLACE)
+    {
+      glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+    }
   }
-  glAlphaFunc (GL_GEQUAL, 0.285f);
-  glEnable (GL_ALPHA_TEST);
+#endif
 
   // setup blending
   glEnable (GL_BLEND);
-  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
-
-  // activate texture unit
-  glDisable (GL_TEXTURE_1D);
-  glEnable  (GL_TEXTURE_2D);
-  if (theCtx->core13 != NULL)
-  {
-    theCtx->core13->glActiveTexture (GL_TEXTURE0);
-  }
+  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
   // extra drawings
   switch (theTextAspect.DisplayType())
   {
     case Aspect_TODT_BLEND:
     {
+    #if !defined(GL_ES_VERSION_2_0)
       glEnable  (GL_COLOR_LOGIC_OP);
       glLogicOp (GL_XOR);
+    #endif
       break;
     }
     case Aspect_TODT_SUBTITLE:
     {
-      glColor3fv  (theColorSubs.rgb);
-      setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
-
-      glBindTexture (GL_TEXTURE_2D, 0);
-      glBegin (GL_QUADS);
-      glVertex2f (myBndBox.Left,  myBndBox.Top);
-      glVertex2f (myBndBox.Right, myBndBox.Top);
-      glVertex2f (myBndBox.Right, myBndBox.Bottom);
-      glVertex2f (myBndBox.Left,  myBndBox.Bottom);
-      glEnd();
+    #if !defined(GL_ES_VERSION_2_0)
+      if (theCtx->core11 != NULL)
+      {
+        theCtx->core11->glColor3fv (theColorSubs.rgb);
+        setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
+
+        glBindTexture (GL_TEXTURE_2D, 0);
+        glBegin (GL_QUADS);
+        glVertex2f (myBndBox.Left,  myBndBox.Top);
+        glVertex2f (myBndBox.Right, myBndBox.Top);
+        glVertex2f (myBndBox.Right, myBndBox.Bottom);
+        glVertex2f (myBndBox.Left,  myBndBox.Bottom);
+        glEnd();
+      }
+    #endif
       break;
     }
     case Aspect_TODT_DEKALE:
     {
-      glColor3fv  (theColorSubs.rgb);
+      theCtx->SetColor4fv (*(const OpenGl_Vec4* )theColorSubs.rgb);
       setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.00001f));
       drawText    (thePrintCtx, theCtx, theTextAspect);
       setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, -1.0f, 0.00001f));
@@ -758,6 +819,7 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
       drawText    (thePrintCtx, theCtx, theTextAspect);
       break;
     }
+    case Aspect_TODT_DIMENSION:
     case Aspect_TODT_NORMAL:
     {
       break;
@@ -765,12 +827,69 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
   }
 
   // main draw call
-  glColor3fv  (theColorText.rgb);
+  theCtx->SetColor4fv (*(const OpenGl_Vec4* )theColorText.rgb);
   setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f));
   drawText    (thePrintCtx, theCtx, theTextAspect);
 
-  // revert OpenGL state
-  glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aTexEnvParam);
-  glPopAttrib(); // enable bit
-  glPopMatrix(); // model view matrix was modified
+#if !defined(GL_ES_VERSION_2_0)
+  if (theCtx->core11 != NULL)
+  {
+    glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aTexEnvParam);
+  }
+#endif
+
+  if (theTextAspect.DisplayType() == Aspect_TODT_DIMENSION)
+  {
+    setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
+
+    glDisable (GL_BLEND);
+    if (!myIs2d)
+    {
+      glDisable (GL_DEPTH_TEST);
+    }
+  #if !defined(GL_ES_VERSION_2_0)
+    if (theCtx->core11 != NULL)
+    {
+      glDisable (GL_TEXTURE_2D);
+      glDisable (GL_ALPHA_TEST);
+    }
+  #endif
+    glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+
+    glClear (GL_STENCIL_BUFFER_BIT);
+    glEnable (GL_STENCIL_TEST);
+    glStencilFunc (GL_ALWAYS, 1, 0xFF);
+    glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
+
+  #if !defined(GL_ES_VERSION_2_0)
+    if (theCtx->core11 != NULL)
+    {
+      glBegin (GL_QUADS);
+      glVertex2f (myBndBox.Left,  myBndBox.Top);
+      glVertex2f (myBndBox.Right, myBndBox.Top);
+      glVertex2f (myBndBox.Right, myBndBox.Bottom);
+      glVertex2f (myBndBox.Left,  myBndBox.Bottom);
+      glEnd();
+    }
+  #endif
+
+    glStencilFunc (GL_ALWAYS, 0, 0xFF);
+
+    glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+  }
+
+  // reset OpenGL state
+  glDisable (GL_BLEND);
+  glDisable (GL_STENCIL_TEST);
+#if !defined(GL_ES_VERSION_2_0)
+  if (theCtx->core11 != NULL)
+  {
+    glDisable (GL_ALPHA_TEST);
+  }
+  glDisable (GL_COLOR_LOGIC_OP);
+#endif
+
+  // model view matrix was modified
+  theCtx->WorldViewState.Pop();
+  theCtx->ApplyModelViewMatrix();
 }