0022819: Redesign of OpenGl driver
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_Layer.cxx
CommitLineData
2166f0fa
SK
1// File: OpenGl_GraphicDriver_Layer.cxx
2// Created: 20 October 2011
3// Author: Sergey ZERCHANINOV
4// Copyright: OPEN CASCADE 2011
7fd59977 5
2166f0fa 6#include <OpenGl_GraphicDriver.hxx>
7fd59977 7
2166f0fa 8#include <OSD_FontAspect.hxx>
7fd59977 9
2166f0fa 10#include <OpenGl_tgl_all.hxx>
7fd59977 11
2166f0fa
SK
12#include <OpenGl_Display.hxx>
13#include <OpenGl_AspectText.hxx>
14#include <OpenGl_TextParam.hxx>
7fd59977 15
2166f0fa 16/*----------------------------------------------------------------------*/
7fd59977 17
2166f0fa
SK
18struct OpenGl_LAYER_PROP
19{
20 int ListId;
7fd59977 21
2166f0fa
SK
22 TEL_COLOUR Color;
23 int NbPoints;
24 int LineType;
25 float LineWidth;
26 int FontCurrent;
7fd59977 27
2166f0fa 28 Standard_Boolean FontChanged;
7fd59977 29
2166f0fa
SK
30 OpenGl_AspectText AspectText;
31 OpenGl_TextParam TextParam;
32};
7fd59977 33
2166f0fa 34/*----------------------------------------------------------------------*/
7fd59977 35
2166f0fa
SK
36static const TEL_COLOUR myDefaultColor = {{ 1.F, 1.F, 1.F, 1.F }};
37static const CALL_DEF_CONTEXTTEXT myDefaultContextText =
38{
39 1, //IsDef
40 1, //IsSet
41 "Courier", //Font
42 0.3F, //Space
43 1.F, //Expan
44 { 1.F, 1.F, 1.F }, //Color
45 (int)Aspect_TOST_NORMAL, //Style
46 (int)Aspect_TODT_NORMAL, //DisplayType
47 { 1.F, 1.F, 1.F }, //ColorSubTitle
48 0, //TextZoomable
49 0.F, //TextAngle
50 (int)OSD_FA_Regular //TextFontAspect
51};
52
53static Standard_Boolean TheLayerIsOpen = Standard_False;
54static OpenGl_LAYER_PROP TheLayerProp;
55
56/*----------------------------------------------------------------------*/
57
58void InitLayerProp (const int AListId)
59{
60 TheLayerProp.ListId = AListId;
7fd59977 61
2166f0fa
SK
62 if (AListId)
63 {
64 TheLayerProp.Color = myDefaultColor;
65 TheLayerProp.NbPoints = 0;
66 TheLayerProp.LineType = -1;
67 TheLayerProp.LineWidth = -1.F;
68 TheLayerProp.FontCurrent = 0;
7fd59977 69
2166f0fa 70 TheLayerProp.FontChanged = Standard_False;
7fd59977 71
2166f0fa 72 TheLayerProp.AspectText.SetContext(myDefaultContextText);
7fd59977 73
2166f0fa
SK
74 TheLayerProp.TextParam.Height = -1;
75 TheLayerProp.TextParam.HAlign = Graphic3d_HTA_LEFT;
76 TheLayerProp.TextParam.VAlign = Graphic3d_VTA_BOTTOM;
7fd59977 77 }
7fd59977 78}
79
2166f0fa 80/*----------------------------------------------------------------------*/
7fd59977 81
2166f0fa
SK
82void OpenGl_GraphicDriver::Layer (Aspect_CLayer2d& ACLayer)
83{
84 ACLayer.ptrLayer = (call_def_ptrLayer) malloc (sizeof (CALL_DEF_PTRLAYER));
85 ACLayer.ptrLayer->listIndex = glGenLists(1);
7fd59977 86}
87
2166f0fa
SK
88/*----------------------------------------------------------------------*/
89
90void OpenGl_GraphicDriver::RemoveLayer (const Aspect_CLayer2d& ACLayer)
91{
92 if (!ACLayer.ptrLayer) return;
93 if (!ACLayer.ptrLayer->listIndex) return;
94 if (TheLayerProp.ListId == ACLayer.ptrLayer->listIndex)
95 EndLayer();
96 glDeleteLists (ACLayer.ptrLayer->listIndex, 1);
97 ACLayer.ptrLayer->listIndex = 0;
98 //szvgl: memory leak here?
99 //free ( ACLayer.ptrLayer );
100 //ACLayer.ptrLayer = NULL;
7fd59977 101}
102
2166f0fa
SK
103/*----------------------------------------------------------------------*/
104
105void OpenGl_GraphicDriver::BeginLayer (const Aspect_CLayer2d& ACLayer)
106{
107 call_def_ptrLayer ptrLayer = (call_def_ptrLayer) ACLayer.ptrLayer;
108 if (!ptrLayer) return;
109
110 InitLayerProp(ptrLayer->listIndex);
111 if (!TheLayerProp.ListId) return;
112
113 glEnable(GL_TEXTURE_2D);
114 //GLboolean stat = glIsEnabled( GL_TEXTURE_2D );
115
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
138 InitLayerProp(ACLayer.ptrLayer->listIndex);
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{
228 if (!TheLayerProp.ListId || openglDisplay.IsNull()) return;
229
230 if (TheLayerProp.LineType != Type)
231 {
232 TheLayerProp.LineType = Type;
233 openglDisplay->SetTypeOfLine((Aspect_TypeOfLine) Type);
234 }
235 if (TheLayerProp.LineWidth != Width)
236 {
237 TheLayerProp.LineWidth = Width;
238 glLineWidth ((GLfloat) Width);
7fd59977 239 }
7fd59977 240}
241
2166f0fa
SK
242void OpenGl_GraphicDriver::SetTextAttributes (const Standard_CString Font, const Standard_Integer AType, const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B)
243{
244 if (!TheLayerProp.ListId || openglDisplay.IsNull()) return;
245
246 if ( strcmp ( TheLayerProp.AspectText.Font(), Font ) != 0 ||
247 (int)TheLayerProp.AspectText.DisplayType() != AType )
248 {
249 CALL_DEF_CONTEXTTEXT aContextText = myDefaultContextText;
250
251 aContextText.Font = (char*)Font;
252 aContextText.DisplayType = AType;
253 aContextText.Color.r = R;
254 aContextText.Color.g = G;
255 aContextText.Color.b = B;
256 TheLayerProp.AspectText.SetContext(aContextText);
257
258 TheLayerProp.FontCurrent = openglDisplay->FindFont(TheLayerProp.AspectText.Font(), TheLayerProp.AspectText.FontAspect(), TheLayerProp.TextParam.Height);
259
260 TheLayerProp.FontChanged = Standard_True;
7fd59977 261 }
2166f0fa 262}
7fd59977 263
2166f0fa 264void OpenGl_GraphicDriver::Text (const Standard_CString AText, const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal AHeight)
13a22457 265{
2166f0fa
SK
266 if (!TheLayerProp.ListId || openglDisplay.IsNull()) return;
267
268 const Standard_ShortReal height = (AHeight < 0)? DefaultTextHeight() : AHeight;
269
270 if ( TheLayerProp.TextParam.Height != height || TheLayerProp.FontChanged || TheLayerProp.FontCurrent == 0 )
271 {
272 TheLayerProp.TextParam.Height = (int )height;
273 TheLayerProp.FontCurrent = openglDisplay->FindFont(TheLayerProp.AspectText.Font(), TheLayerProp.AspectText.FontAspect(), (int )height);
274 TheLayerProp.FontChanged = Standard_False;
7fd59977 275 }
2166f0fa 276
13a22457 277 TCollection_ExtendedString estr(AText);
2166f0fa
SK
278 const Techar *s = (const Techar *)estr.ToExtString();
279
280 //szv: conversion of Techar to wchar_t
281 wchar_t *s1 = (wchar_t*)s;
282 if (sizeof(Techar) != sizeof(wchar_t))
283 {
284 Tint i = 0; while (s[i++]);
285 s1 = new wchar_t[i];
286 i = 0; while (s1[i++] = (wchar_t)(*s++));
287 }
288
289 openglDisplay->RenderText(s1, 1, (float)X, (float)Y, 0.F, &TheLayerProp.AspectText, &TheLayerProp.TextParam);
290
291 //szv: delete temporary wide string
292 if (sizeof(Techar) != sizeof(wchar_t))
293 delete[] s1;
7fd59977 294}
295
13a22457
S
296void OpenGl_GraphicDriver::TextSize (const Standard_CString AText, const Standard_ShortReal AHeight, Standard_ShortReal& AWidth, Standard_ShortReal& AnAscent, Standard_ShortReal& ADescent) const
297{
2166f0fa
SK
298 if (!TheLayerProp.ListId || openglDisplay.IsNull()) return;
299
300 const Standard_ShortReal height = (AHeight < 0)? DefaultTextHeight() : AHeight;
301
302 if ( TheLayerProp.TextParam.Height != height || TheLayerProp.FontChanged || TheLayerProp.FontCurrent == 0 )
303 {
304 TheLayerProp.TextParam.Height = (int )height;
305 TheLayerProp.FontCurrent = openglDisplay->FindFont(TheLayerProp.AspectText.Font(), TheLayerProp.AspectText.FontAspect(), (int )height);
306 TheLayerProp.FontChanged = Standard_False;
307 }
308
13a22457 309 TCollection_ExtendedString estr(AText);
2166f0fa
SK
310 const Techar *s = (const Techar *)estr.ToExtString();
311
312 //szv: conversion of Techar to wchar_t
313 wchar_t *s1 = (wchar_t*)s;
314 if (sizeof(Techar) != sizeof(wchar_t))
315 {
316 Tint i = 0; while (s[i++]);
317 s1 = new wchar_t[i];
318 i = 0; while (s1[i++] = (wchar_t)(*s++));
7fd59977 319 }
2166f0fa
SK
320
321 int aWidth = 0, anAscent = 0, aDescent = 0;
322 openglDisplay->StringSize(s1, aWidth, anAscent, aDescent);
323
324 //szv: delete temporary wide string
325 if (sizeof(Techar) != sizeof(wchar_t))
326 delete[] s1;
327
328 AWidth = (Standard_ShortReal) aWidth;
329 AnAscent = (Standard_ShortReal) anAscent;
330 ADescent = (Standard_ShortReal) aDescent;
7fd59977 331}