0024428: Implementation of LGPL license
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_Layer.cxx
CommitLineData
b311480e 1// Created on: 2011-10-20
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
973c2be1 7// This library is free software; you can redistribute it and / or modify it
8// under the terms of the GNU Lesser General Public version 2.1 as published
9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
5f8b738e 16#include <OpenGl_GlCore11.hxx>
17
2166f0fa 18#include <OpenGl_GraphicDriver.hxx>
7fd59977 19
eeaaaefb 20#include <Font_FontAspect.hxx>
7fd59977 21
2166f0fa
SK
22#include <OpenGl_Display.hxx>
23#include <OpenGl_AspectText.hxx>
a174a3c5 24#include <OpenGl_Text.hxx>
2166f0fa 25#include <OpenGl_TextParam.hxx>
7fd59977 26
2166f0fa 27/*----------------------------------------------------------------------*/
7fd59977 28
2166f0fa
SK
29struct OpenGl_LAYER_PROP
30{
31 int ListId;
7fd59977 32
2166f0fa
SK
33 TEL_COLOUR Color;
34 int NbPoints;
35 int LineType;
36 float LineWidth;
2166f0fa
SK
37 OpenGl_AspectText AspectText;
38 OpenGl_TextParam TextParam;
39};
7fd59977 40
2166f0fa 41/*----------------------------------------------------------------------*/
7fd59977 42
2166f0fa
SK
43static const TEL_COLOUR myDefaultColor = {{ 1.F, 1.F, 1.F, 1.F }};
44static const CALL_DEF_CONTEXTTEXT myDefaultContextText =
45{
46 1, //IsDef
47 1, //IsSet
48 "Courier", //Font
49 0.3F, //Space
50 1.F, //Expan
51 { 1.F, 1.F, 1.F }, //Color
52 (int)Aspect_TOST_NORMAL, //Style
53 (int)Aspect_TODT_NORMAL, //DisplayType
54 { 1.F, 1.F, 1.F }, //ColorSubTitle
55 0, //TextZoomable
56 0.F, //TextAngle
eeaaaefb 57 (int)Font_FA_Regular //TextFontAspect
2166f0fa
SK
58};
59
60static Standard_Boolean TheLayerIsOpen = Standard_False;
61static OpenGl_LAYER_PROP TheLayerProp;
62
63/*----------------------------------------------------------------------*/
64
30f0ad28 65void InitLayerProp (const int theListId)
2166f0fa 66{
30f0ad28 67 TheLayerProp.ListId = theListId;
7fd59977 68
30f0ad28 69 if (theListId)
2166f0fa
SK
70 {
71 TheLayerProp.Color = myDefaultColor;
72 TheLayerProp.NbPoints = 0;
73 TheLayerProp.LineType = -1;
74 TheLayerProp.LineWidth = -1.F;
7fd59977 75
fd4a6963 76 TheLayerProp.AspectText.SetAspect (myDefaultContextText);
7fd59977 77
2166f0fa
SK
78 TheLayerProp.TextParam.HAlign = Graphic3d_HTA_LEFT;
79 TheLayerProp.TextParam.VAlign = Graphic3d_VTA_BOTTOM;
7fd59977 80 }
7fd59977 81}
82
2166f0fa 83/*----------------------------------------------------------------------*/
7fd59977 84
2166f0fa
SK
85void OpenGl_GraphicDriver::Layer (Aspect_CLayer2d& ACLayer)
86{
9d35f668 87 ACLayer.ptrLayer = new CALL_DEF_PTRLAYER();
2166f0fa 88 ACLayer.ptrLayer->listIndex = glGenLists(1);
7fd59977 89}
90
2166f0fa
SK
91/*----------------------------------------------------------------------*/
92
93void OpenGl_GraphicDriver::RemoveLayer (const Aspect_CLayer2d& ACLayer)
94{
95 if (!ACLayer.ptrLayer) return;
96 if (!ACLayer.ptrLayer->listIndex) return;
97 if (TheLayerProp.ListId == ACLayer.ptrLayer->listIndex)
98 EndLayer();
99 glDeleteLists (ACLayer.ptrLayer->listIndex, 1);
100 ACLayer.ptrLayer->listIndex = 0;
101 //szvgl: memory leak here?
102 //free ( ACLayer.ptrLayer );
103 //ACLayer.ptrLayer = NULL;
7fd59977 104}
105
2166f0fa
SK
106/*----------------------------------------------------------------------*/
107
108void OpenGl_GraphicDriver::BeginLayer (const Aspect_CLayer2d& ACLayer)
109{
110 call_def_ptrLayer ptrLayer = (call_def_ptrLayer) ACLayer.ptrLayer;
111 if (!ptrLayer) return;
112
30f0ad28 113 InitLayerProp (ptrLayer->listIndex);
2166f0fa
SK
114 if (!TheLayerProp.ListId) return;
115
2166f0fa
SK
116 glNewList (TheLayerProp.ListId, GL_COMPILE);
117 TheLayerIsOpen = Standard_True;
7fd59977 118}
119
2166f0fa
SK
120void OpenGl_GraphicDriver::BeginPolygon2d ()
121{
122 if (!TheLayerProp.ListId) return;
123 TheLayerProp.NbPoints = 0;
124 glBegin (GL_POLYGON);
125}
7fd59977 126
2166f0fa
SK
127void OpenGl_GraphicDriver::BeginPolyline2d ()
128{
129 if (!TheLayerProp.ListId) return;
130 TheLayerProp.NbPoints = 0;
131 glBegin (GL_LINE_STRIP);
7fd59977 132}
133
2166f0fa
SK
134void OpenGl_GraphicDriver::ClearLayer (const Aspect_CLayer2d& ACLayer)
135{
136 if (!ACLayer.ptrLayer) return;
137
30f0ad28 138 InitLayerProp (ACLayer.ptrLayer->listIndex);
2166f0fa
SK
139 if (!TheLayerProp.ListId) return;
140
141 glNewList (TheLayerProp.ListId, GL_COMPILE);
142 glEndList ();
7fd59977 143}
144
2166f0fa
SK
145void OpenGl_GraphicDriver::Draw (const Standard_ShortReal X, const Standard_ShortReal Y)
146{
147 if (!TheLayerProp.ListId) return;
148 TheLayerProp.NbPoints++;
149 glVertex3f (X, Y, 0.F);
7fd59977 150}
151
2166f0fa
SK
152void OpenGl_GraphicDriver::Edge (const Standard_ShortReal X, const Standard_ShortReal Y)
153{
154 if (!TheLayerProp.ListId) return;
155 TheLayerProp.NbPoints++;
156 glVertex3f (X, Y, 0.F);
7fd59977 157}
158
2166f0fa
SK
159void OpenGl_GraphicDriver::EndLayer ()
160{
161 if (!TheLayerProp.ListId) return;
162 if (TheLayerIsOpen)
163 {
164 glEndList();
165 TheLayerIsOpen = Standard_False;
7fd59977 166 }
2166f0fa 167 TheLayerProp.ListId = 0;
7fd59977 168}
169
2166f0fa
SK
170void OpenGl_GraphicDriver::EndPolygon2d ()
171{
172 if (!TheLayerProp.ListId) return;
173 glEnd ();
7fd59977 174}
175
2166f0fa
SK
176void OpenGl_GraphicDriver::EndPolyline2d ()
177{
178 if (!TheLayerProp.ListId) return;
179 glEnd ();
7fd59977 180}
181
2166f0fa
SK
182void OpenGl_GraphicDriver::Move (const Standard_ShortReal X, const Standard_ShortReal Y)
183{
184 if (!TheLayerProp.ListId) return;
185 if (TheLayerProp.NbPoints)
186 {
187 glEnd ();
188 TheLayerProp.NbPoints = 0;
189 glBegin (GL_LINE_STRIP);
7fd59977 190 }
2166f0fa
SK
191 TheLayerProp.NbPoints++;
192 glVertex3f (X, Y, 0.F);
7fd59977 193}
194
2166f0fa
SK
195void OpenGl_GraphicDriver::Rectangle (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Width, const Standard_ShortReal Height)
196{
197 if (!TheLayerProp.ListId) return;
198 glRectf (X, Y, X + Width, Y + Height);
7fd59977 199}
200
2166f0fa
SK
201void OpenGl_GraphicDriver::SetColor (const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B)
202{
203 if (!TheLayerProp.ListId) return;
204 TheLayerProp.Color.rgb[0] = R;
205 TheLayerProp.Color.rgb[1] = G;
206 TheLayerProp.Color.rgb[2] = B;
207 glColor3fv (TheLayerProp.Color.rgb);
7fd59977 208}
209
2166f0fa
SK
210void OpenGl_GraphicDriver::SetTransparency (const Standard_ShortReal ATransparency)
211{
212 if (!TheLayerProp.ListId) return;
213 TheLayerProp.Color.rgb[3] = ATransparency;
214 glEnable (GL_BLEND);
215 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
216 glColor4fv (TheLayerProp.Color.rgb);
217}
218
219void OpenGl_GraphicDriver::UnsetTransparency ()
220{
221 if (!TheLayerProp.ListId) return;
222 TheLayerProp.Color.rgb[3] = 1.F;
223 glDisable (GL_BLEND);
7fd59977 224}
225
2166f0fa
SK
226void OpenGl_GraphicDriver::SetLineAttributes (const Standard_Integer Type, const Standard_ShortReal Width)
227{
fd4a6963 228 if (!TheLayerProp.ListId || myGlDisplay.IsNull()) return;
2166f0fa
SK
229
230 if (TheLayerProp.LineType != Type)
231 {
232 TheLayerProp.LineType = Type;
fd4a6963 233 myGlDisplay->SetTypeOfLine((Aspect_TypeOfLine) Type);
2166f0fa
SK
234 }
235 if (TheLayerProp.LineWidth != Width)
236 {
237 TheLayerProp.LineWidth = Width;
238 glLineWidth ((GLfloat) Width);
7fd59977 239 }
7fd59977 240}
241
a174a3c5 242void OpenGl_GraphicDriver::SetTextAttributes (const Standard_CString theFont,
243 const Standard_Integer theType,
244 const Standard_ShortReal theR,
245 const Standard_ShortReal theG,
246 const Standard_ShortReal theB)
2166f0fa 247{
a174a3c5 248 if (!TheLayerProp.ListId)
2166f0fa 249 {
a174a3c5 250 return;
7fd59977 251 }
a174a3c5 252
253 TheLayerProp.AspectText.ChangeFontName() = theFont;
254 TheLayerProp.AspectText.SetDisplayType ((Aspect_TypeOfDisplayText )theType);
255 TEL_COLOUR& aSubColor = TheLayerProp.AspectText.ChangeSubtitleColor();
256 aSubColor.rgb[0] = (float )theR;
257 aSubColor.rgb[1] = (float )theG;
258 aSubColor.rgb[2] = (float )theB;
2166f0fa 259}
7fd59977 260
a174a3c5 261void OpenGl_GraphicDriver::Text (const Standard_CString theText,
262 const Standard_ShortReal theX,
263 const Standard_ShortReal theY,
264 const Standard_ShortReal theHeight)
13a22457 265{
a174a3c5 266 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
267 if (!TheLayerProp.ListId || aCtx.IsNull())
2166f0fa 268 {
a174a3c5 269 return;
25289ec1 270 }
271
a174a3c5 272 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
273 TheLayerProp.TextParam.Height = (int )aHeight;
274 TheLayerProp.AspectText.ChangeColor() = TheLayerProp.Color;
25289ec1 275
a174a3c5 276 myTempText->Init (aCtx, TCollection_ExtendedString (theText), OpenGl_Vec2 (theX, theY), TheLayerProp.TextParam);
277 myTempText->Render (myPrintContext, aCtx, TheLayerProp.AspectText);
278 //myTempText->Release (aCtx);
7fd59977 279}
280
a174a3c5 281void OpenGl_GraphicDriver::TextSize (const Standard_CString theText,
282 const Standard_ShortReal theHeight,
283 Standard_ShortReal& theWidth,
284 Standard_ShortReal& theAscent,
285 Standard_ShortReal& theDescent) const
13a22457 286{
a174a3c5 287 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
288 if (!TheLayerProp.ListId || aCtx.IsNull())
2166f0fa 289 {
a174a3c5 290 return;
7fd59977 291 }
2166f0fa 292
a174a3c5 293 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
294 TheLayerProp.TextParam.Height = (int )aHeight;
2166f0fa 295
a174a3c5 296 TCollection_ExtendedString anExtText = theText;
297 NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString();
298 OpenGl_Text::StringSize (aCtx, aText, TheLayerProp.AspectText, TheLayerProp.TextParam, theWidth, theAscent, theDescent);
7fd59977 299}