0025305: Visualization, TKOpenGl - support stipple line aspects within built-in GLSL...
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_Layer.cxx
1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License 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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <OpenGl_GlCore11.hxx>
17
18 #include <OpenGl_GraphicDriver.hxx>
19
20 #include <Font_FontAspect.hxx>
21
22 #include <OpenGl_AspectText.hxx>
23 #include <OpenGl_Text.hxx>
24 #include <OpenGl_TextParam.hxx>
25 #include <OpenGl_LineAttributes.hxx>
26
27 /*----------------------------------------------------------------------*/
28
29 struct OpenGl_LAYER_PROP
30 {
31   int        ListId;
32
33   TEL_COLOUR Color;
34   int        NbPoints;
35   int        LineType;
36   float      LineWidth;
37   OpenGl_AspectText AspectText;
38   OpenGl_TextParam TextParam;
39 };
40
41 /*----------------------------------------------------------------------*/
42
43 static const TEL_COLOUR myDefaultColor = {{ 1.F, 1.F, 1.F, 1.F }};
44 static 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
57   (int)Font_FA_Regular //TextFontAspect
58 };
59
60 static Standard_Boolean TheLayerIsOpen = Standard_False;
61 static OpenGl_LAYER_PROP TheLayerProp;
62
63 /*----------------------------------------------------------------------*/
64
65 void InitLayerProp (const int theListId)
66 {
67   TheLayerProp.ListId = theListId;
68
69   if (theListId)
70   {
71     TheLayerProp.Color = myDefaultColor;
72     TheLayerProp.NbPoints = 0;
73     TheLayerProp.LineType = -1;
74     TheLayerProp.LineWidth = -1.F;
75
76     TheLayerProp.AspectText.SetAspect (myDefaultContextText);
77
78     TheLayerProp.TextParam.HAlign = Graphic3d_HTA_LEFT;
79     TheLayerProp.TextParam.VAlign = Graphic3d_VTA_BOTTOM;
80   }
81 }
82
83 /*----------------------------------------------------------------------*/
84
85 void OpenGl_GraphicDriver::Layer (Aspect_CLayer2d& ACLayer)
86 {
87   ACLayer.ptrLayer = new CALL_DEF_PTRLAYER();
88 #if !defined(GL_ES_VERSION_2_0)
89   ACLayer.ptrLayer->listIndex = glGenLists(1);
90 #endif
91 }
92
93 /*----------------------------------------------------------------------*/
94
95 void OpenGl_GraphicDriver::RemoveLayer (const Aspect_CLayer2d& ACLayer)
96 {
97   if (!ACLayer.ptrLayer) return;
98   if (!ACLayer.ptrLayer->listIndex) return;
99   if (TheLayerProp.ListId == ACLayer.ptrLayer->listIndex)
100     EndLayer();
101
102 #if !defined(GL_ES_VERSION_2_0)
103   glDeleteLists (ACLayer.ptrLayer->listIndex, 1);
104 #endif
105   ACLayer.ptrLayer->listIndex = 0;
106   //szvgl: memory leak here?
107   //free ( ACLayer.ptrLayer );
108   //ACLayer.ptrLayer = NULL;
109 }
110
111 /*----------------------------------------------------------------------*/
112
113 void OpenGl_GraphicDriver::BeginLayer (const Aspect_CLayer2d& ACLayer)
114 {
115   call_def_ptrLayer ptrLayer = (call_def_ptrLayer) ACLayer.ptrLayer;
116   if (!ptrLayer) return;
117
118   InitLayerProp (ptrLayer->listIndex);
119   if (!TheLayerProp.ListId) return;
120
121 #if !defined(GL_ES_VERSION_2_0)
122   glNewList (TheLayerProp.ListId, GL_COMPILE);
123 #endif
124   TheLayerIsOpen = Standard_True;
125 }
126
127 void OpenGl_GraphicDriver::BeginPolygon2d ()
128 {
129   if (!TheLayerProp.ListId) return;
130   TheLayerProp.NbPoints = 0;
131 #if !defined(GL_ES_VERSION_2_0)
132   glBegin (GL_POLYGON);
133 #endif
134 }
135
136 void OpenGl_GraphicDriver::BeginPolyline2d ()
137 {
138   if (!TheLayerProp.ListId) return;
139   TheLayerProp.NbPoints = 0;
140 #if !defined(GL_ES_VERSION_2_0)
141   glBegin (GL_LINE_STRIP);
142 #endif
143 }
144
145 void OpenGl_GraphicDriver::ClearLayer (const Aspect_CLayer2d& ACLayer)
146 {
147   if (!ACLayer.ptrLayer) return;
148
149   InitLayerProp (ACLayer.ptrLayer->listIndex);
150   if (!TheLayerProp.ListId) return;
151
152 #if !defined(GL_ES_VERSION_2_0)
153   glNewList (TheLayerProp.ListId, GL_COMPILE);
154   glEndList ();
155 #endif
156 }
157
158 void OpenGl_GraphicDriver::Draw (const Standard_ShortReal X, const Standard_ShortReal Y)
159 {
160   if (!TheLayerProp.ListId) return;
161   TheLayerProp.NbPoints++;
162 #if !defined(GL_ES_VERSION_2_0)
163   glVertex3f (X, Y, 0.F);
164 #endif
165 }
166
167 void OpenGl_GraphicDriver::Edge (const Standard_ShortReal X, const Standard_ShortReal Y)
168 {
169   if (!TheLayerProp.ListId) return;
170   TheLayerProp.NbPoints++;
171 #if !defined(GL_ES_VERSION_2_0)
172   glVertex3f (X, Y, 0.F);
173 #endif
174 }
175
176 void OpenGl_GraphicDriver::EndLayer ()
177 {
178   if (!TheLayerProp.ListId) return;
179   if (TheLayerIsOpen)
180   {
181   #if !defined(GL_ES_VERSION_2_0)
182     glEndList();
183   #endif
184     TheLayerIsOpen = Standard_False;
185   }
186   TheLayerProp.ListId = 0;
187 }
188
189 void OpenGl_GraphicDriver::EndPolygon2d ()
190 {
191   if (!TheLayerProp.ListId) return;
192 #if !defined(GL_ES_VERSION_2_0)
193   glEnd ();
194 #endif
195 }
196
197 void OpenGl_GraphicDriver::EndPolyline2d ()
198 {
199   if (!TheLayerProp.ListId) return;
200 #if !defined(GL_ES_VERSION_2_0)
201   glEnd ();
202 #endif
203 }
204
205 void OpenGl_GraphicDriver::Move (const Standard_ShortReal X, const Standard_ShortReal Y)
206 {
207   if (!TheLayerProp.ListId) return;
208   if (TheLayerProp.NbPoints)
209   {
210   #if !defined(GL_ES_VERSION_2_0)
211     glEnd ();
212     glBegin (GL_LINE_STRIP);
213   #endif
214     TheLayerProp.NbPoints = 0;
215   }
216   TheLayerProp.NbPoints++;
217 #if !defined(GL_ES_VERSION_2_0)
218   glVertex3f (X, Y, 0.F);
219 #endif
220 }
221
222 void OpenGl_GraphicDriver::Rectangle (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Width, const Standard_ShortReal Height)
223 {
224   if (!TheLayerProp.ListId) return;
225 #if !defined(GL_ES_VERSION_2_0)
226   glRectf (X, Y, X + Width, Y + Height);
227 #endif
228 }
229
230 void OpenGl_GraphicDriver::SetColor (const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B)
231 {
232   if (!TheLayerProp.ListId) return;
233   TheLayerProp.Color.rgb[0] = R;
234   TheLayerProp.Color.rgb[1] = G;
235   TheLayerProp.Color.rgb[2] = B;
236 #if !defined(GL_ES_VERSION_2_0)
237   glColor3fv (TheLayerProp.Color.rgb);
238 #endif
239 }
240
241 void OpenGl_GraphicDriver::SetTransparency (const Standard_ShortReal ATransparency)
242 {
243   if (!TheLayerProp.ListId) return;
244   TheLayerProp.Color.rgb[3] = ATransparency;
245 #if !defined(GL_ES_VERSION_2_0)
246   glEnable (GL_BLEND);
247   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
248   glColor4fv (TheLayerProp.Color.rgb);
249 #endif
250 }
251
252 void OpenGl_GraphicDriver::UnsetTransparency ()
253 {
254   if (!TheLayerProp.ListId) return;
255   TheLayerProp.Color.rgb[3] = 1.F;
256 #if !defined(GL_ES_VERSION_2_0)
257   glDisable (GL_BLEND);
258 #endif
259 }
260
261 void OpenGl_GraphicDriver::SetLineAttributes (const Standard_Integer   theType,
262                                               const Standard_ShortReal theWidth)
263 {
264   const Handle(OpenGl_Context)& aCtx = GetSharedContext();
265   if (!TheLayerProp.ListId
266    || aCtx.IsNull())
267   {
268     return;
269   }
270
271   if (TheLayerProp.LineType != theType)
272   {
273     TheLayerProp.LineType = theType;
274     aCtx->SetTypeOfLine ((Aspect_TypeOfLine) theType);
275   }
276   if (TheLayerProp.LineWidth != theWidth)
277   {
278     TheLayerProp.LineWidth = theWidth;
279     aCtx->SetLineWidth (theWidth);
280   }
281 }
282
283 void OpenGl_GraphicDriver::SetTextAttributes (const Standard_CString   theFont,
284                                               const Standard_Integer   theType,
285                                               const Standard_ShortReal theR,
286                                               const Standard_ShortReal theG,
287                                               const Standard_ShortReal theB)
288 {
289   if (!TheLayerProp.ListId)
290   {
291     return;
292   }
293
294   TheLayerProp.AspectText.ChangeFontName() = theFont;
295   TheLayerProp.AspectText.SetDisplayType ((Aspect_TypeOfDisplayText )theType);
296   TEL_COLOUR& aSubColor = TheLayerProp.AspectText.ChangeSubtitleColor();
297   aSubColor.rgb[0] = (float )theR;
298   aSubColor.rgb[1] = (float )theG;
299   aSubColor.rgb[2] = (float )theB;
300 }
301
302 void OpenGl_GraphicDriver::Text (const Standard_CString   theText,
303                                  const Standard_ShortReal theX,
304                                  const Standard_ShortReal theY,
305                                  const Standard_ShortReal theHeight)
306 {
307   const Handle(OpenGl_Context)& aCtx = GetSharedContext();
308   if (!TheLayerProp.ListId || aCtx.IsNull())
309   {
310     return;
311   }
312
313   const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
314   TheLayerProp.TextParam.Height = (int )aHeight;
315   TheLayerProp.AspectText.ChangeColor() = TheLayerProp.Color;
316
317   myTempText->Init (aCtx, TCollection_ExtendedString (theText), OpenGl_Vec2 (theX, theY), TheLayerProp.TextParam);
318   myTempText->Render (myPrintContext, aCtx, TheLayerProp.AspectText);
319   //myTempText->Release (aCtx);
320 }
321
322 void OpenGl_GraphicDriver::TextSize (const Standard_CString   theText,
323                                      const Standard_ShortReal theHeight,
324                                      Standard_ShortReal&      theWidth,
325                                      Standard_ShortReal&      theAscent,
326                                      Standard_ShortReal&      theDescent) const
327 {
328   const Handle(OpenGl_Context)& aCtx = GetSharedContext();
329   if (!TheLayerProp.ListId || aCtx.IsNull())
330   {
331     return;
332   }
333
334   const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
335   TheLayerProp.TextParam.Height = (int )aHeight;
336
337   TCollection_ExtendedString anExtText = theText;
338   NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString();
339   OpenGl_Text::StringSize (aCtx, aText, TheLayerProp.AspectText, TheLayerProp.TextParam, theWidth, theAscent, theDescent);
340 }