0024001: Stereographic rendering support
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_Layer.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 9c884b1..c74c81f
@@ -1,22 +1,17 @@
 // Created on: 2011-10-20
 // Created by: Sergey ZERCHANINOV
-// Copyright (c) 2011-2012 OPEN CASCADE SAS
+// Copyright (c) 2011-2014 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 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.
-
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
 #include <OpenGl_GlCore11.hxx>
 
 
 #include <OpenGl_Display.hxx>
 #include <OpenGl_AspectText.hxx>
+#include <OpenGl_Text.hxx>
 #include <OpenGl_TextParam.hxx>
 
-#include <GL/glu.h>
-
 /*----------------------------------------------------------------------*/
 
 struct OpenGl_LAYER_PROP
@@ -40,10 +34,6 @@ struct OpenGl_LAYER_PROP
   int        NbPoints;
   int        LineType;
   float      LineWidth;
-  int        FontCurrent;
-
-  Standard_Boolean FontChanged;
-
   OpenGl_AspectText AspectText;
   OpenGl_TextParam TextParam;
 };
@@ -72,23 +62,19 @@ static OpenGl_LAYER_PROP TheLayerProp;
 
 /*----------------------------------------------------------------------*/
 
-void InitLayerProp (const int AListId)
+void InitLayerProp (const int theListId)
 {
-  TheLayerProp.ListId = AListId;
+  TheLayerProp.ListId = theListId;
 
-  if (AListId)
+  if (theListId)
   {
     TheLayerProp.Color = myDefaultColor;
     TheLayerProp.NbPoints = 0;
     TheLayerProp.LineType = -1;
     TheLayerProp.LineWidth = -1.F;
-    TheLayerProp.FontCurrent = 0;
 
-    TheLayerProp.FontChanged = Standard_False;
+    TheLayerProp.AspectText.SetAspect (myDefaultContextText);
 
-    TheLayerProp.AspectText.SetContext(myDefaultContextText);
-
-    TheLayerProp.TextParam.Height = -1;
     TheLayerProp.TextParam.HAlign = Graphic3d_HTA_LEFT;
     TheLayerProp.TextParam.VAlign = Graphic3d_VTA_BOTTOM;
   }
@@ -124,7 +110,7 @@ void OpenGl_GraphicDriver::BeginLayer (const Aspect_CLayer2d& ACLayer)
   call_def_ptrLayer ptrLayer = (call_def_ptrLayer) ACLayer.ptrLayer;
   if (!ptrLayer) return;
 
-  InitLayerProp(ptrLayer->listIndex);
+  InitLayerProp (ptrLayer->listIndex);
   if (!TheLayerProp.ListId) return;
 
   glNewList (TheLayerProp.ListId, GL_COMPILE);
@@ -149,7 +135,7 @@ void OpenGl_GraphicDriver::ClearLayer (const Aspect_CLayer2d& ACLayer)
 {
   if (!ACLayer.ptrLayer) return;
 
-  InitLayerProp(ACLayer.ptrLayer->listIndex);
+  InitLayerProp (ACLayer.ptrLayer->listIndex);
   if (!TheLayerProp.ListId) return;
 
   glNewList (TheLayerProp.ListId, GL_COMPILE);
@@ -239,12 +225,12 @@ void OpenGl_GraphicDriver::UnsetTransparency ()
 
 void OpenGl_GraphicDriver::SetLineAttributes (const Standard_Integer Type, const Standard_ShortReal Width)
 {
-  if (!TheLayerProp.ListId || openglDisplay.IsNull()) return;
+  if (!TheLayerProp.ListId || myGlDisplay.IsNull()) return;
 
   if (TheLayerProp.LineType != Type)
   {
     TheLayerProp.LineType = Type;
-    openglDisplay->SetTypeOfLine((Aspect_TypeOfLine) Type);
+    myGlDisplay->SetTypeOfLine((Aspect_TypeOfLine) Type);
   }
   if (TheLayerProp.LineWidth != Width)
   {
@@ -253,214 +239,61 @@ void OpenGl_GraphicDriver::SetLineAttributes (const Standard_Integer Type, const
   }
 }
 
-void OpenGl_GraphicDriver::SetTextAttributes (const Standard_CString Font, const Standard_Integer AType, const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B)
+void OpenGl_GraphicDriver::SetTextAttributes (const Standard_CString   theFont,
+                                              const Standard_Integer   theType,
+                                              const Standard_ShortReal theR,
+                                              const Standard_ShortReal theG,
+                                              const Standard_ShortReal theB)
 {
-  if (!TheLayerProp.ListId || openglDisplay.IsNull()) return;
-
-  // get current subtitle text color
-  const TEL_COLOUR &aSubColor = TheLayerProp.AspectText.SubtitleColor ();
-
-  // update if there are any modifications
-  if (strcmp (TheLayerProp.AspectText.Font(), Font) != 0   ||
-      (int)TheLayerProp.AspectText.DisplayType() != AType  ||
-      aSubColor.rgb[0] != (float)R || 
-      aSubColor.rgb[1] != (float)G ||
-      aSubColor.rgb[2] != (float)B)
+  if (!TheLayerProp.ListId)
   {
-    CALL_DEF_CONTEXTTEXT aContextText = myDefaultContextText;
-
-    aContextText.Font = Font;
-    aContextText.DisplayType = AType;
-    aContextText.ColorSubTitle.r = R;
-    aContextText.ColorSubTitle.g = G;
-    aContextText.ColorSubTitle.b = B;
-    TheLayerProp.AspectText.SetContext(aContextText);
-    TheLayerProp.FontChanged = Standard_True;
+    return;
   }
+
+  TheLayerProp.AspectText.ChangeFontName() = theFont;
+  TheLayerProp.AspectText.SetDisplayType ((Aspect_TypeOfDisplayText )theType);
+  TEL_COLOUR& aSubColor = TheLayerProp.AspectText.ChangeSubtitleColor();
+  aSubColor.rgb[0] = (float )theR;
+  aSubColor.rgb[1] = (float )theG;
+  aSubColor.rgb[2] = (float )theB;
 }
 
-void OpenGl_GraphicDriver::Text (const Standard_CString AText, const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal AHeight)
+void OpenGl_GraphicDriver::Text (const Standard_CString   theText,
+                                 const Standard_ShortReal theX,
+                                 const Standard_ShortReal theY,
+                                 const Standard_ShortReal theHeight)
 {
-  if (!TheLayerProp.ListId || openglDisplay.IsNull()) return;
-
-  const Standard_ShortReal height = (AHeight < 0)? DefaultTextHeight() : AHeight;
-
-  if ( TheLayerProp.TextParam.Height != height || TheLayerProp.FontChanged || TheLayerProp.FontCurrent == 0 )
-  {
-    TheLayerProp.TextParam.Height = (int )height;
-    TheLayerProp.FontCurrent = openglDisplay->FindFont(TheLayerProp.AspectText.Font(), TheLayerProp.AspectText.FontAspect(), (int )height);
-    TheLayerProp.FontChanged = Standard_False;
-  }
-
-  TCollection_ExtendedString aExtStr(AText);
-  const Techar *aTChStr = (const Techar *)aExtStr.ToExtString();
-
-  //szv: conversion of Techar to wchar_t
-  wchar_t *aWChStr = (wchar_t*)aTChStr;
-  if (sizeof(Techar) != sizeof(wchar_t))
+  const Handle(OpenGl_Context)& aCtx = GetSharedContext();
+  if (!TheLayerProp.ListId || aCtx.IsNull())
   {
-    Tint i = 0; while (aTChStr[i++]);
-    aWChStr = new wchar_t[i];
-    i = 0; while (aWChStr[i++] = (wchar_t)(*aTChStr++));
+    return;
   }
 
-  const Aspect_TypeOfDisplayText aDispType =
-    TheLayerProp.AspectText.DisplayType();
-
-  // display type of text
-  if (aDispType != Aspect_TODT_NORMAL)
-  {
-    switch (aDispType)
-    {
-      // blend type
-      case Aspect_TODT_BLEND:
-      {
-        glEnable(GL_COLOR_LOGIC_OP);
-        glLogicOp(GL_XOR);
-      }
-      break;
-      
-      // subtitle type
-      case Aspect_TODT_SUBTITLE:
-      {
-        GLint aViewport[4];
-        GLdouble aModelMatrix[16], aProjMatrix[16];
-        glGetIntegerv (GL_VIEWPORT, aViewport);
-        glGetDoublev (GL_MODELVIEW_MATRIX, aModelMatrix);
-        glGetDoublev (GL_PROJECTION_MATRIX, aProjMatrix);
-
-        int aWidth, anAscent, aDescent;
-        openglDisplay->StringSize(aWChStr, aWidth, anAscent, aDescent);
-
-        GLdouble aWinX, aWinY, aWinZ;
-        gluProject ((GLdouble)X, (GLdouble)Y, 0.0, aModelMatrix,
-          aProjMatrix, aViewport, &aWinX, &aWinY, &aWinZ);
-
-        // project coordinates
-        GLdouble aCoordX[4];
-        GLdouble aCoordY[4];
-        GLdouble aCoordZ[4];
-
-        // left bottom corner
-        gluUnProject (aWinX,  aWinY + aDescent, aWinZ, aModelMatrix,
-          aProjMatrix, aViewport, &aCoordX[0], &aCoordY[0], &aCoordZ[0]);
-
-        // right bottom corner        
-        gluUnProject (aWinX + aWidth, aWinY + aDescent, aWinZ, aModelMatrix,
-          aProjMatrix, aViewport, &aCoordX[1], &aCoordY[1], &aCoordZ[1]);
-
-        // right top corner
-        gluUnProject (aWinX + aWidth, aWinY + anAscent, aWinZ, aModelMatrix,
-          aProjMatrix, aViewport, &aCoordX[2], &aCoordY[2], &aCoordZ[2]);
-
-        // left top corner
-        gluUnProject (aWinX, aWinY + anAscent, aWinZ, aModelMatrix, 
-          aProjMatrix, aViewport, &aCoordX[3], &aCoordY[3], &aCoordZ[3]);
-
-        // draw colored plane and reset the color
-        glColor3fv (TheLayerProp.AspectText.SubtitleColor ().rgb);
-        glBegin(GL_POLYGON);
-        glVertex3d(aCoordX[0], aCoordY[0], aCoordZ[0]);
-        glVertex3d(aCoordX[1], aCoordY[1], aCoordZ[1]);
-        glVertex3d(aCoordX[2], aCoordY[2], aCoordZ[2]);
-        glVertex3d(aCoordX[3], aCoordY[3], aCoordZ[3]);
-        glEnd();
-        glColor3fv (TheLayerProp.Color.rgb);
-      }
-      break;
-
-      case Aspect_TODT_DEKALE:
-      {
-        GLint aViewport[4];
-        GLdouble aModelMatrix[16], aProjMatrix[16];
-        glGetIntegerv (GL_VIEWPORT, aViewport);
-        glGetDoublev (GL_MODELVIEW_MATRIX, aModelMatrix);
-        glGetDoublev (GL_PROJECTION_MATRIX, aProjMatrix);
-
-        GLdouble aWinX, aWinY, aWinZ;
-        gluProject ((GLdouble)X, (GLdouble)Y, 0.0, aModelMatrix,
-          aProjMatrix, aViewport, &aWinX, &aWinY, &aWinZ);
-
-        GLdouble aProjX, aProjY, aProjZ;
-
-        gluUnProject (aWinX + 1, aWinY + 1, aWinZ, aModelMatrix,
-          aProjMatrix, aViewport, &aProjX, &aProjY, &aProjZ);
-
-        // draw a decal
-        glColor3fv (TheLayerProp.AspectText.SubtitleColor ().rgb);
-        openglDisplay->RenderText (aWChStr, 1, (float)aProjX, (float)aProjY,
-          (float)aProjZ, &TheLayerProp.AspectText, &TheLayerProp.TextParam);
-
-        gluUnProject (aWinX, aWinY, aWinZ, aModelMatrix, aProjMatrix,
-          aViewport, &aProjX, &aProjY, &aProjZ);
-
-        gluUnProject (aWinX - 1, aWinY - 1, aWinZ, aModelMatrix, aProjMatrix, 
-          aViewport, &aProjX, &aProjY, &aProjZ);
-
-        openglDisplay->RenderText(aWChStr, 1, (float)aProjX, (float)aProjY,
-          (float)aProjZ, &TheLayerProp.AspectText, &TheLayerProp.TextParam);
-
-        gluUnProject (aWinX - 1, aWinY + 1, aWinZ, aModelMatrix, aProjMatrix, 
-          aViewport, &aProjX, &aProjY, &aProjZ);
-
-        openglDisplay->RenderText(aWChStr, 1, (float)aProjX, (float)aProjY,
-          (float)aProjZ, &TheLayerProp.AspectText, &TheLayerProp.TextParam);
-
-        gluUnProject (aWinX + 1, aWinY - 1, aWinZ, aModelMatrix, aProjMatrix,
-          aViewport, &aProjX, &aProjY, &aProjZ);
-
-        openglDisplay->RenderText(aWChStr, 1, (float)aProjX, (float)aProjY,
-          (float)aProjZ, &TheLayerProp.AspectText, &TheLayerProp.TextParam);
-        glColor3fv (TheLayerProp.Color.rgb);
-      }
-      break;
-    }
-  }
+  const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
+  TheLayerProp.TextParam.Height = (int )aHeight;
+  TheLayerProp.AspectText.ChangeColor() = TheLayerProp.Color;
 
-  openglDisplay->RenderText(aWChStr, 1, (float)X, (float)Y, 0.F,
-    &TheLayerProp.AspectText, &TheLayerProp.TextParam);
-
-  if (aDispType == Aspect_TODT_BLEND)
-    glDisable(GL_COLOR_LOGIC_OP);
-
-  //szv: delete temporary wide string
-  if (sizeof(Techar) != sizeof(wchar_t))
-    delete[] aWChStr;
+  myTempText->Init (aCtx, TCollection_ExtendedString (theText), OpenGl_Vec2 (theX, theY), TheLayerProp.TextParam);
+  myTempText->Render (myPrintContext, aCtx, TheLayerProp.AspectText);
+  //myTempText->Release (aCtx);
 }
 
-void OpenGl_GraphicDriver::TextSize (const Standard_CString AText, const Standard_ShortReal AHeight, Standard_ShortReal& AWidth, Standard_ShortReal& AnAscent, Standard_ShortReal& ADescent) const
+void OpenGl_GraphicDriver::TextSize (const Standard_CString   theText,
+                                     const Standard_ShortReal theHeight,
+                                     Standard_ShortReal&      theWidth,
+                                     Standard_ShortReal&      theAscent,
+                                     Standard_ShortReal&      theDescent) const
 {
-  if (!TheLayerProp.ListId || openglDisplay.IsNull()) return;
-
-  const Standard_ShortReal height = (AHeight < 0)? DefaultTextHeight() : AHeight;
-
-  if ( TheLayerProp.TextParam.Height != height || TheLayerProp.FontChanged || TheLayerProp.FontCurrent == 0 )
-  {
-    TheLayerProp.TextParam.Height = (int )height;
-    TheLayerProp.FontCurrent = openglDisplay->FindFont(TheLayerProp.AspectText.Font(), TheLayerProp.AspectText.FontAspect(), (int )height);
-    TheLayerProp.FontChanged = Standard_False;
-  }
-
-  TCollection_ExtendedString estr(AText);
-  const Techar *s = (const Techar *)estr.ToExtString();
-
-  //szv: conversion of Techar to wchar_t
-  wchar_t *s1 = (wchar_t*)s;
-  if (sizeof(Techar) != sizeof(wchar_t))
+  const Handle(OpenGl_Context)& aCtx = GetSharedContext();
+  if (!TheLayerProp.ListId || aCtx.IsNull())
   {
-    Tint i = 0; while (s[i++]);
-    s1 = new wchar_t[i];
-    i = 0; while (s1[i++] = (wchar_t)(*s++));
+    return;
   }
 
-  int aWidth = 0, anAscent = 0, aDescent = 0;
-  openglDisplay->StringSize(s1, aWidth, anAscent, aDescent);
-
-  //szv: delete temporary wide string
-  if (sizeof(Techar) != sizeof(wchar_t))
-    delete[] s1;
+  const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
+  TheLayerProp.TextParam.Height = (int )aHeight;
 
-  AWidth = (Standard_ShortReal) aWidth;
-  AnAscent = (Standard_ShortReal) anAscent;
-  ADescent = (Standard_ShortReal) aDescent;
+  TCollection_ExtendedString anExtText = theText;
+  NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString();
+  OpenGl_Text::StringSize (aCtx, aText, TheLayerProp.AspectText, TheLayerProp.TextParam, theWidth, theAscent, theDescent);
 }