0024001: Stereographic rendering support
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_Layer.cxx
old mode 100755 (executable)
new mode 100644 (file)
index b00046b..c74c81f
+// Created on: 2011-10-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 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 <OpenGl_GlCore11.hxx>
+
+#include <OpenGl_GraphicDriver.hxx>
+
+#include <Font_FontAspect.hxx>
+
+#include <OpenGl_Display.hxx>
+#include <OpenGl_AspectText.hxx>
+#include <OpenGl_Text.hxx>
+#include <OpenGl_TextParam.hxx>
+
+/*----------------------------------------------------------------------*/
+
+struct OpenGl_LAYER_PROP
+{
+  int        ListId;
 
-// File   OpenGl_GraphicDriver_Layer.cxx
-// Created  Mardi 3 novembre 1998
-// Author CAL
+  TEL_COLOUR Color;
+  int        NbPoints;
+  int        LineType;
+  float      LineWidth;
+  OpenGl_AspectText AspectText;
+  OpenGl_TextParam TextParam;
+};
 
-//-Copyright  MatraDatavision 1998
+/*----------------------------------------------------------------------*/
 
-//-Version  
+static const TEL_COLOUR myDefaultColor = {{ 1.F, 1.F, 1.F, 1.F }};
+static const CALL_DEF_CONTEXTTEXT myDefaultContextText =
+{
+  1, //IsDef
+  1, //IsSet
+  "Courier", //Font
+  0.3F, //Space
+  1.F, //Expan
+  { 1.F, 1.F, 1.F }, //Color
+  (int)Aspect_TOST_NORMAL, //Style
+  (int)Aspect_TODT_NORMAL, //DisplayType
+  { 1.F, 1.F, 1.F }, //ColorSubTitle
+  0, //TextZoomable
+  0.F, //TextAngle
+  (int)Font_FA_Regular //TextFontAspect
+};
+
+static Standard_Boolean TheLayerIsOpen = Standard_False;
+static OpenGl_LAYER_PROP TheLayerProp;
+
+/*----------------------------------------------------------------------*/
+
+void InitLayerProp (const int theListId)
+{
+  TheLayerProp.ListId = theListId;
 
-//-Design Declaration des variables specifiques aux Drivers
+  if (theListId)
+  {
+    TheLayerProp.Color = myDefaultColor;
+    TheLayerProp.NbPoints = 0;
+    TheLayerProp.LineType = -1;
+    TheLayerProp.LineWidth = -1.F;
 
-//-Warning  Un driver encapsule les Pex et OpenGl drivers
+    TheLayerProp.AspectText.SetAspect (myDefaultContextText);
 
-//-References 
+    TheLayerProp.TextParam.HAlign = Graphic3d_HTA_LEFT;
+    TheLayerProp.TextParam.VAlign = Graphic3d_VTA_BOTTOM;
+  }
+}
 
-//-Language C++ 2.0
+/*----------------------------------------------------------------------*/
 
-//-Declarations
+void OpenGl_GraphicDriver::Layer (Aspect_CLayer2d& ACLayer)
+{
+  ACLayer.ptrLayer = new CALL_DEF_PTRLAYER();
+  ACLayer.ptrLayer->listIndex = glGenLists(1);
+}
 
-// for the class
-#include <OpenGl_GraphicDriver.jxx>
+/*----------------------------------------------------------------------*/
 
-#include <Aspect_DriverDefinitionError.hxx>
+void OpenGl_GraphicDriver::RemoveLayer (const Aspect_CLayer2d& ACLayer)
+{
+  if (!ACLayer.ptrLayer) return;
+  if (!ACLayer.ptrLayer->listIndex) return;
+  if (TheLayerProp.ListId == ACLayer.ptrLayer->listIndex)
+    EndLayer();
+  glDeleteLists (ACLayer.ptrLayer->listIndex, 1);
+  ACLayer.ptrLayer->listIndex = 0;
+  //szvgl: memory leak here?
+  //free ( ACLayer.ptrLayer );
+  //ACLayer.ptrLayer = NULL;
+}
 
-#include <OpenGl_tgl_funcs.hxx>
+/*----------------------------------------------------------------------*/
 
-//-Aliases
-#define NO_DEBUG
+void OpenGl_GraphicDriver::BeginLayer (const Aspect_CLayer2d& ACLayer)
+{
+  call_def_ptrLayer ptrLayer = (call_def_ptrLayer) ACLayer.ptrLayer;
+  if (!ptrLayer) return;
 
-//-Global data definitions
+  InitLayerProp (ptrLayer->listIndex);
+  if (!TheLayerProp.ListId) return;
 
-//-Methods, in order
+  glNewList (TheLayerProp.ListId, GL_COMPILE);
+  TheLayerIsOpen = Standard_True;
+}
 
-void OpenGl_GraphicDriver::Layer (Aspect_CLayer2d& ACLayer) {
-  Aspect_CLayer2d MyCLayer = ACLayer;
+void OpenGl_GraphicDriver::BeginPolygon2d ()
+{
+  if (!TheLayerProp.ListId) return;
+  TheLayerProp.NbPoints = 0;
+  glBegin (GL_POLYGON);
+}
 
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_layer2d");
-    PrintInteger ("ALayerType", int (ACLayer.layerType));
-  }
-  call_togl_layer2d (&MyCLayer);
-  ACLayer = MyCLayer;
-#ifdef DEBUG
-  cout << "OpenGl_GraphicDriver::Layer" << endl;
-  call_def_ptrLayer ptrLayer;
-  ptrLayer = (call_def_ptrLayer) MyCLayer.ptrLayer;
-  if (ptrLayer == NULL)
-    cout << "\tptrLayer == NULL" << endl;
-  else
-    cout << "\tptrLayer->listIndex = " << ptrLayer->listIndex << endl;
-#endif
-}
-
-void OpenGl_GraphicDriver::RemoveLayer (const Aspect_CLayer2d& ACLayer) {
-  Aspect_CLayer2d MyCLayer = ACLayer;
-
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_removelayer2d");
-    PrintInteger ("ALayerType", int (ACLayer.layerType));
-  }
-  call_togl_removelayer2d (&MyCLayer);
+void OpenGl_GraphicDriver::BeginPolyline2d ()
+{
+  if (!TheLayerProp.ListId) return;
+  TheLayerProp.NbPoints = 0;
+  glBegin (GL_LINE_STRIP);
 }
 
-void OpenGl_GraphicDriver::BeginLayer (const Aspect_CLayer2d& ACLayer) {
-  Aspect_CLayer2d MyCLayer = ACLayer;
+void OpenGl_GraphicDriver::ClearLayer (const Aspect_CLayer2d& ACLayer)
+{
+  if (!ACLayer.ptrLayer) return;
 
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_begin_layer2d");
-    PrintInteger ("ALayerType", int (ACLayer.layerType));
-  }
-  call_togl_begin_layer2d (&MyCLayer);
-}
+  InitLayerProp (ACLayer.ptrLayer->listIndex);
+  if (!TheLayerProp.ListId) return;
 
-void OpenGl_GraphicDriver::BeginPolygon2d () {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_begin_polygon2d");
-  }
-  call_togl_begin_polygon2d ();
+  glNewList (TheLayerProp.ListId, GL_COMPILE);
+  glEndList ();
 }
 
-void OpenGl_GraphicDriver::BeginPolyline2d () {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_begin_polyline2d");
-  }
-  call_togl_begin_polyline2d ();
+void OpenGl_GraphicDriver::Draw (const Standard_ShortReal X, const Standard_ShortReal Y)
+{
+  if (!TheLayerProp.ListId) return;
+  TheLayerProp.NbPoints++;
+  glVertex3f (X, Y, 0.F);
 }
 
-void OpenGl_GraphicDriver::ClearLayer (const Aspect_CLayer2d& ACLayer) {
-  Aspect_CLayer2d MyCLayer = ACLayer;
-
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_clear_layer2d");
-    PrintInteger ("ALayerType", int (ACLayer.layerType));
-  }
-  call_togl_clear_layer2d (&MyCLayer);
+void OpenGl_GraphicDriver::Edge (const Standard_ShortReal X, const Standard_ShortReal Y)
+{
+  if (!TheLayerProp.ListId) return;
+  TheLayerProp.NbPoints++;
+  glVertex3f (X, Y, 0.F);
 }
 
-void OpenGl_GraphicDriver::Draw (const Standard_ShortReal X, const Standard_ShortReal Y) {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_draw2d");
-    PrintShortReal ("X", X);
-    PrintShortReal ("Y", Y);
+void OpenGl_GraphicDriver::EndLayer ()
+{
+  if (!TheLayerProp.ListId) return;
+  if (TheLayerIsOpen)
+  {
+    glEndList();
+    TheLayerIsOpen = Standard_False;
   }
-  call_togl_draw2d (X, Y);
+  TheLayerProp.ListId = 0;
 }
 
-void OpenGl_GraphicDriver::Edge (const Standard_ShortReal X, const Standard_ShortReal Y) {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_edge2d");
-    PrintShortReal ("X", X);
-    PrintShortReal ("Y", Y);
-  }
-  call_togl_edge2d (X, Y);
+void OpenGl_GraphicDriver::EndPolygon2d ()
+{
+  if (!TheLayerProp.ListId) return;
+  glEnd ();
 }
 
-void OpenGl_GraphicDriver::EndLayer () {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_end_layer2d");
-  }
-  call_togl_end_layer2d ();
+void OpenGl_GraphicDriver::EndPolyline2d ()
+{
+  if (!TheLayerProp.ListId) return;
+  glEnd ();
 }
 
-void OpenGl_GraphicDriver::EndPolygon2d () {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_end_polygon2d");
+void OpenGl_GraphicDriver::Move (const Standard_ShortReal X, const Standard_ShortReal Y)
+{
+  if (!TheLayerProp.ListId) return;
+  if (TheLayerProp.NbPoints)
+  {
+    glEnd ();
+    TheLayerProp.NbPoints = 0;
+    glBegin (GL_LINE_STRIP);
   }
-  call_togl_end_polygon2d ();
+  TheLayerProp.NbPoints++;
+  glVertex3f (X, Y, 0.F);
 }
 
-void OpenGl_GraphicDriver::EndPolyline2d () {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_end_polyline2d");
-  }
-  call_togl_end_polyline2d ();
+void OpenGl_GraphicDriver::Rectangle (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Width, const Standard_ShortReal Height)
+{
+  if (!TheLayerProp.ListId) return;
+  glRectf (X, Y, X + Width, Y + Height);
 }
 
-void OpenGl_GraphicDriver::Move (const Standard_ShortReal X, const Standard_ShortReal Y) {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_move2d");
-    PrintShortReal ("X", X);
-    PrintShortReal ("Y", Y);
-  }
-  call_togl_move2d (X, Y);
+void OpenGl_GraphicDriver::SetColor (const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B)
+{
+  if (!TheLayerProp.ListId) return;
+  TheLayerProp.Color.rgb[0] = R;
+  TheLayerProp.Color.rgb[1] = G;
+  TheLayerProp.Color.rgb[2] = B;
+  glColor3fv (TheLayerProp.Color.rgb);
 }
 
-void OpenGl_GraphicDriver::Rectangle (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Width, const Standard_ShortReal Height) {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_rectangle2d");
-    PrintShortReal ("X", X);
-    PrintShortReal ("Y", Y);
-    PrintShortReal ("Width", Width);
-    PrintShortReal ("Height", Height);
-  }
-  call_togl_rectangle2d (X, Y, Width, Height);
+void OpenGl_GraphicDriver::SetTransparency (const Standard_ShortReal ATransparency)
+{
+  if (!TheLayerProp.ListId) return;
+  TheLayerProp.Color.rgb[3] = ATransparency;
+  glEnable (GL_BLEND);
+  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+  glColor4fv (TheLayerProp.Color.rgb);
 }
 
-void OpenGl_GraphicDriver::SetColor (const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B) {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_set_color");
-    PrintShortReal ("R", R);
-    PrintShortReal ("G", G);
-    PrintShortReal ("B", B);
-  }
-  call_togl_set_color (R, G, B);
+void OpenGl_GraphicDriver::UnsetTransparency ()
+{
+  if (!TheLayerProp.ListId) return;
+  TheLayerProp.Color.rgb[3] = 1.F;
+  glDisable (GL_BLEND);
 }
 
-void OpenGl_GraphicDriver::SetTransparency (const Standard_ShortReal ATransparency) {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_set_transparency");
-    PrintShortReal ("Transparency", ATransparency);
-  }
-  call_togl_set_transparency (ATransparency);
-}
+void OpenGl_GraphicDriver::SetLineAttributes (const Standard_Integer Type, const Standard_ShortReal Width)
+{
+  if (!TheLayerProp.ListId || myGlDisplay.IsNull()) return;
 
-void OpenGl_GraphicDriver::UnsetTransparency () {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_unset_transparency");
+  if (TheLayerProp.LineType != Type)
+  {
+    TheLayerProp.LineType = Type;
+    myGlDisplay->SetTypeOfLine((Aspect_TypeOfLine) Type);
+  }
+  if (TheLayerProp.LineWidth != Width)
+  {
+    TheLayerProp.LineWidth = Width;
+    glLineWidth ((GLfloat) Width);
   }
-  call_togl_unset_transparency ();
 }
 
-void OpenGl_GraphicDriver::SetLineAttributes (const Standard_Integer Type, const Standard_ShortReal Width) {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_set_line_attributes");
-    PrintInteger ("Type", Type);
-    PrintShortReal ("Width", Width);
+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)
+  {
+    return;
   }
-  call_togl_set_line_attributes (Type, Width);
+
+  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::SetTextAttributes (const Standard_CString Font, const Standard_Integer AType, const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B) {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_set_text_attributes");
-    PrintString ("Font", Font);
-    PrintInteger ("Type", AType);
-    PrintShortReal ("R", R);
-    PrintShortReal ("G", G);
-    PrintShortReal ("B", B);
+void OpenGl_GraphicDriver::Text (const Standard_CString   theText,
+                                 const Standard_ShortReal theX,
+                                 const Standard_ShortReal theY,
+                                 const Standard_ShortReal theHeight)
+{
+  const Handle(OpenGl_Context)& aCtx = GetSharedContext();
+  if (!TheLayerProp.ListId || aCtx.IsNull())
+  {
+    return;
   }
-  call_togl_set_text_attributes ((Tchar*)Font, AType, R, G, B);
-}   
 
-void OpenGl_GraphicDriver::Text (const Standard_CString AText, const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Height)
+  const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
+  TheLayerProp.TextParam.Height = (int )aHeight;
+  TheLayerProp.AspectText.ChangeColor() = TheLayerProp.Color;
+
+  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   theText,
+                                     const Standard_ShortReal theHeight,
+                                     Standard_ShortReal&      theWidth,
+                                     Standard_ShortReal&      theAscent,
+                                     Standard_ShortReal&      theDescent) const
 {
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_text2d");
-    PrintString ("Text", AText);
-    PrintShortReal ("X", X);
-    PrintShortReal ("Y", Y);
-    PrintShortReal ("Height", Height);
-  }
-  const Standard_ShortReal h = (Height < 0)? DefaultTextHeight() : Height;
-  TCollection_ExtendedString estr(AText);
-  call_togl_text2d ((Techar *)estr.ToExtString(), X, Y, h);
-}
-
-void OpenGl_GraphicDriver::TextSize (const Standard_CString AText, const Standard_ShortReal AHeight, Standard_ShortReal& AWidth, Standard_ShortReal& AnAscent, Standard_ShortReal& ADescent) const
-{
-  TCollection_ExtendedString estr(AText);
-  call_togl_textsize2d ((Techar *)estr.ToExtString(), AHeight, &AWidth, &AnAscent, &ADescent);
-  if (MyTraceLevel) {
-    PrintFunction ("call_togl_textsize2d");
-    PrintString ("Text", AText);
-    PrintShortReal ("AHeight", AHeight);
-    PrintShortReal ("AWidth", AWidth);
-    PrintShortReal ("AnAscent", AnAscent);
-    PrintShortReal ("ADescent", ADescent);
+  const Handle(OpenGl_Context)& aCtx = GetSharedContext();
+  if (!TheLayerProp.ListId || aCtx.IsNull())
+  {
+    return;
   }
+
+  const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
+  TheLayerProp.TextParam.Height = (int )aHeight;
+
+  TCollection_ExtendedString anExtText = theText;
+  NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString();
+  OpenGl_Text::StringSize (aCtx, aText, TheLayerProp.AspectText, TheLayerProp.TextParam, theWidth, theAscent, theDescent);
 }