0025442: Visualization, TKOpenGl - prevent inclusion of system header glxext.h
[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//
d5f74e42 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
973c2be1 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 22#include <OpenGl_AspectText.hxx>
a174a3c5 23#include <OpenGl_Text.hxx>
2166f0fa 24#include <OpenGl_TextParam.hxx>
73192b37 25#include <OpenGl_LineAttributes.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();
ca3c13d1 88#if !defined(GL_ES_VERSION_2_0)
2166f0fa 89 ACLayer.ptrLayer->listIndex = glGenLists(1);
ca3c13d1 90#endif
7fd59977 91}
92
2166f0fa
SK
93/*----------------------------------------------------------------------*/
94
95void 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();
ca3c13d1 101
102#if !defined(GL_ES_VERSION_2_0)
2166f0fa 103 glDeleteLists (ACLayer.ptrLayer->listIndex, 1);
ca3c13d1 104#endif
2166f0fa
SK
105 ACLayer.ptrLayer->listIndex = 0;
106 //szvgl: memory leak here?
107 //free ( ACLayer.ptrLayer );
108 //ACLayer.ptrLayer = NULL;
7fd59977 109}
110
2166f0fa
SK
111/*----------------------------------------------------------------------*/
112
113void OpenGl_GraphicDriver::BeginLayer (const Aspect_CLayer2d& ACLayer)
114{
115 call_def_ptrLayer ptrLayer = (call_def_ptrLayer) ACLayer.ptrLayer;
116 if (!ptrLayer) return;
117
30f0ad28 118 InitLayerProp (ptrLayer->listIndex);
2166f0fa
SK
119 if (!TheLayerProp.ListId) return;
120
ca3c13d1 121#if !defined(GL_ES_VERSION_2_0)
2166f0fa 122 glNewList (TheLayerProp.ListId, GL_COMPILE);
ca3c13d1 123#endif
2166f0fa 124 TheLayerIsOpen = Standard_True;
7fd59977 125}
126
2166f0fa
SK
127void OpenGl_GraphicDriver::BeginPolygon2d ()
128{
129 if (!TheLayerProp.ListId) return;
130 TheLayerProp.NbPoints = 0;
ca3c13d1 131#if !defined(GL_ES_VERSION_2_0)
2166f0fa 132 glBegin (GL_POLYGON);
ca3c13d1 133#endif
2166f0fa 134}
7fd59977 135
2166f0fa
SK
136void OpenGl_GraphicDriver::BeginPolyline2d ()
137{
138 if (!TheLayerProp.ListId) return;
139 TheLayerProp.NbPoints = 0;
ca3c13d1 140#if !defined(GL_ES_VERSION_2_0)
2166f0fa 141 glBegin (GL_LINE_STRIP);
ca3c13d1 142#endif
7fd59977 143}
144
2166f0fa
SK
145void OpenGl_GraphicDriver::ClearLayer (const Aspect_CLayer2d& ACLayer)
146{
147 if (!ACLayer.ptrLayer) return;
148
30f0ad28 149 InitLayerProp (ACLayer.ptrLayer->listIndex);
2166f0fa
SK
150 if (!TheLayerProp.ListId) return;
151
ca3c13d1 152#if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
153 glNewList (TheLayerProp.ListId, GL_COMPILE);
154 glEndList ();
ca3c13d1 155#endif
7fd59977 156}
157
2166f0fa
SK
158void OpenGl_GraphicDriver::Draw (const Standard_ShortReal X, const Standard_ShortReal Y)
159{
160 if (!TheLayerProp.ListId) return;
161 TheLayerProp.NbPoints++;
ca3c13d1 162#if !defined(GL_ES_VERSION_2_0)
2166f0fa 163 glVertex3f (X, Y, 0.F);
ca3c13d1 164#endif
7fd59977 165}
166
2166f0fa
SK
167void OpenGl_GraphicDriver::Edge (const Standard_ShortReal X, const Standard_ShortReal Y)
168{
169 if (!TheLayerProp.ListId) return;
170 TheLayerProp.NbPoints++;
ca3c13d1 171#if !defined(GL_ES_VERSION_2_0)
2166f0fa 172 glVertex3f (X, Y, 0.F);
ca3c13d1 173#endif
7fd59977 174}
175
2166f0fa
SK
176void OpenGl_GraphicDriver::EndLayer ()
177{
178 if (!TheLayerProp.ListId) return;
179 if (TheLayerIsOpen)
180 {
ca3c13d1 181 #if !defined(GL_ES_VERSION_2_0)
2166f0fa 182 glEndList();
ca3c13d1 183 #endif
2166f0fa 184 TheLayerIsOpen = Standard_False;
7fd59977 185 }
2166f0fa 186 TheLayerProp.ListId = 0;
7fd59977 187}
188
2166f0fa
SK
189void OpenGl_GraphicDriver::EndPolygon2d ()
190{
191 if (!TheLayerProp.ListId) return;
ca3c13d1 192#if !defined(GL_ES_VERSION_2_0)
2166f0fa 193 glEnd ();
ca3c13d1 194#endif
7fd59977 195}
196
2166f0fa
SK
197void OpenGl_GraphicDriver::EndPolyline2d ()
198{
199 if (!TheLayerProp.ListId) return;
ca3c13d1 200#if !defined(GL_ES_VERSION_2_0)
2166f0fa 201 glEnd ();
ca3c13d1 202#endif
7fd59977 203}
204
2166f0fa
SK
205void OpenGl_GraphicDriver::Move (const Standard_ShortReal X, const Standard_ShortReal Y)
206{
207 if (!TheLayerProp.ListId) return;
208 if (TheLayerProp.NbPoints)
209 {
ca3c13d1 210 #if !defined(GL_ES_VERSION_2_0)
2166f0fa 211 glEnd ();
2166f0fa 212 glBegin (GL_LINE_STRIP);
ca3c13d1 213 #endif
214 TheLayerProp.NbPoints = 0;
7fd59977 215 }
2166f0fa 216 TheLayerProp.NbPoints++;
ca3c13d1 217#if !defined(GL_ES_VERSION_2_0)
2166f0fa 218 glVertex3f (X, Y, 0.F);
ca3c13d1 219#endif
7fd59977 220}
221
2166f0fa
SK
222void 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;
ca3c13d1 225#if !defined(GL_ES_VERSION_2_0)
2166f0fa 226 glRectf (X, Y, X + Width, Y + Height);
ca3c13d1 227#endif
7fd59977 228}
229
2166f0fa
SK
230void 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;
ca3c13d1 236#if !defined(GL_ES_VERSION_2_0)
2166f0fa 237 glColor3fv (TheLayerProp.Color.rgb);
ca3c13d1 238#endif
7fd59977 239}
240
2166f0fa
SK
241void OpenGl_GraphicDriver::SetTransparency (const Standard_ShortReal ATransparency)
242{
243 if (!TheLayerProp.ListId) return;
244 TheLayerProp.Color.rgb[3] = ATransparency;
ca3c13d1 245#if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
246 glEnable (GL_BLEND);
247 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
248 glColor4fv (TheLayerProp.Color.rgb);
ca3c13d1 249#endif
2166f0fa
SK
250}
251
252void OpenGl_GraphicDriver::UnsetTransparency ()
253{
254 if (!TheLayerProp.ListId) return;
255 TheLayerProp.Color.rgb[3] = 1.F;
ca3c13d1 256#if !defined(GL_ES_VERSION_2_0)
2166f0fa 257 glDisable (GL_BLEND);
ca3c13d1 258#endif
7fd59977 259}
260
73192b37 261void OpenGl_GraphicDriver::SetLineAttributes (const Standard_Integer theType,
262 const Standard_ShortReal theWidth)
2166f0fa 263{
73192b37 264 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
265 if (!TheLayerProp.ListId
266 || aCtx.IsNull())
267 {
268 return;
269 }
2166f0fa 270
73192b37 271 if (TheLayerProp.LineType != theType)
2166f0fa 272 {
73192b37 273 Handle(OpenGl_LineAttributes) aLineAttribs;
274 if (aCtx->GetResource ("OpenGl_LineAttributes", aLineAttribs))
275 {
276 TheLayerProp.LineType = theType;
277 aLineAttribs->SetTypeOfLine ((Aspect_TypeOfLine )theType);
278 }
2166f0fa 279 }
73192b37 280 if (TheLayerProp.LineWidth != theWidth)
2166f0fa 281 {
73192b37 282 TheLayerProp.LineWidth = theWidth;
283 glLineWidth ((GLfloat )theWidth);
7fd59977 284 }
7fd59977 285}
286
a174a3c5 287void OpenGl_GraphicDriver::SetTextAttributes (const Standard_CString theFont,
288 const Standard_Integer theType,
289 const Standard_ShortReal theR,
290 const Standard_ShortReal theG,
291 const Standard_ShortReal theB)
2166f0fa 292{
a174a3c5 293 if (!TheLayerProp.ListId)
2166f0fa 294 {
a174a3c5 295 return;
7fd59977 296 }
a174a3c5 297
298 TheLayerProp.AspectText.ChangeFontName() = theFont;
299 TheLayerProp.AspectText.SetDisplayType ((Aspect_TypeOfDisplayText )theType);
300 TEL_COLOUR& aSubColor = TheLayerProp.AspectText.ChangeSubtitleColor();
301 aSubColor.rgb[0] = (float )theR;
302 aSubColor.rgb[1] = (float )theG;
303 aSubColor.rgb[2] = (float )theB;
2166f0fa 304}
7fd59977 305
a174a3c5 306void OpenGl_GraphicDriver::Text (const Standard_CString theText,
307 const Standard_ShortReal theX,
308 const Standard_ShortReal theY,
309 const Standard_ShortReal theHeight)
13a22457 310{
a174a3c5 311 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
312 if (!TheLayerProp.ListId || aCtx.IsNull())
2166f0fa 313 {
a174a3c5 314 return;
25289ec1 315 }
316
a174a3c5 317 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
318 TheLayerProp.TextParam.Height = (int )aHeight;
319 TheLayerProp.AspectText.ChangeColor() = TheLayerProp.Color;
25289ec1 320
a174a3c5 321 myTempText->Init (aCtx, TCollection_ExtendedString (theText), OpenGl_Vec2 (theX, theY), TheLayerProp.TextParam);
322 myTempText->Render (myPrintContext, aCtx, TheLayerProp.AspectText);
323 //myTempText->Release (aCtx);
7fd59977 324}
325
a174a3c5 326void OpenGl_GraphicDriver::TextSize (const Standard_CString theText,
327 const Standard_ShortReal theHeight,
328 Standard_ShortReal& theWidth,
329 Standard_ShortReal& theAscent,
330 Standard_ShortReal& theDescent) const
13a22457 331{
a174a3c5 332 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
333 if (!TheLayerProp.ListId || aCtx.IsNull())
2166f0fa 334 {
a174a3c5 335 return;
7fd59977 336 }
2166f0fa 337
a174a3c5 338 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
339 TheLayerProp.TextParam.Height = (int )aHeight;
2166f0fa 340
a174a3c5 341 TCollection_ExtendedString anExtText = theText;
342 NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString();
343 OpenGl_Text::StringSize (aCtx, aText, TheLayerProp.AspectText, TheLayerProp.TextParam, theWidth, theAscent, theDescent);
7fd59977 344}