1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_GlCore11.hxx>
18 #include <OpenGl_GraphicDriver.hxx>
20 #include <Font_FontAspect.hxx>
22 #include <OpenGl_AspectText.hxx>
23 #include <OpenGl_Text.hxx>
24 #include <OpenGl_TextParam.hxx>
25 #include <OpenGl_LineAttributes.hxx>
27 /*----------------------------------------------------------------------*/
29 struct OpenGl_LAYER_PROP
37 OpenGl_AspectText AspectText;
38 OpenGl_TextParam TextParam;
41 /*----------------------------------------------------------------------*/
43 static const TEL_COLOUR myDefaultColor = {{ 1.F, 1.F, 1.F, 1.F }};
44 static const CALL_DEF_CONTEXTTEXT myDefaultContextText =
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
57 (int)Font_FA_Regular //TextFontAspect
60 static Standard_Boolean TheLayerIsOpen = Standard_False;
61 static OpenGl_LAYER_PROP TheLayerProp;
63 /*----------------------------------------------------------------------*/
65 void InitLayerProp (const int theListId)
67 TheLayerProp.ListId = theListId;
71 TheLayerProp.Color = myDefaultColor;
72 TheLayerProp.NbPoints = 0;
73 TheLayerProp.LineType = -1;
74 TheLayerProp.LineWidth = -1.F;
76 TheLayerProp.AspectText.SetAspect (myDefaultContextText);
78 TheLayerProp.TextParam.HAlign = Graphic3d_HTA_LEFT;
79 TheLayerProp.TextParam.VAlign = Graphic3d_VTA_BOTTOM;
83 /*----------------------------------------------------------------------*/
85 void OpenGl_GraphicDriver::Layer (Aspect_CLayer2d& ACLayer)
87 ACLayer.ptrLayer = new CALL_DEF_PTRLAYER();
88 ACLayer.ptrLayer->listIndex = glGenLists(1);
91 /*----------------------------------------------------------------------*/
93 void OpenGl_GraphicDriver::RemoveLayer (const Aspect_CLayer2d& ACLayer)
95 if (!ACLayer.ptrLayer) return;
96 if (!ACLayer.ptrLayer->listIndex) return;
97 if (TheLayerProp.ListId == ACLayer.ptrLayer->listIndex)
99 glDeleteLists (ACLayer.ptrLayer->listIndex, 1);
100 ACLayer.ptrLayer->listIndex = 0;
101 //szvgl: memory leak here?
102 //free ( ACLayer.ptrLayer );
103 //ACLayer.ptrLayer = NULL;
106 /*----------------------------------------------------------------------*/
108 void OpenGl_GraphicDriver::BeginLayer (const Aspect_CLayer2d& ACLayer)
110 call_def_ptrLayer ptrLayer = (call_def_ptrLayer) ACLayer.ptrLayer;
111 if (!ptrLayer) return;
113 InitLayerProp (ptrLayer->listIndex);
114 if (!TheLayerProp.ListId) return;
116 glNewList (TheLayerProp.ListId, GL_COMPILE);
117 TheLayerIsOpen = Standard_True;
120 void OpenGl_GraphicDriver::BeginPolygon2d ()
122 if (!TheLayerProp.ListId) return;
123 TheLayerProp.NbPoints = 0;
124 glBegin (GL_POLYGON);
127 void OpenGl_GraphicDriver::BeginPolyline2d ()
129 if (!TheLayerProp.ListId) return;
130 TheLayerProp.NbPoints = 0;
131 glBegin (GL_LINE_STRIP);
134 void OpenGl_GraphicDriver::ClearLayer (const Aspect_CLayer2d& ACLayer)
136 if (!ACLayer.ptrLayer) return;
138 InitLayerProp (ACLayer.ptrLayer->listIndex);
139 if (!TheLayerProp.ListId) return;
141 glNewList (TheLayerProp.ListId, GL_COMPILE);
145 void OpenGl_GraphicDriver::Draw (const Standard_ShortReal X, const Standard_ShortReal Y)
147 if (!TheLayerProp.ListId) return;
148 TheLayerProp.NbPoints++;
149 glVertex3f (X, Y, 0.F);
152 void OpenGl_GraphicDriver::Edge (const Standard_ShortReal X, const Standard_ShortReal Y)
154 if (!TheLayerProp.ListId) return;
155 TheLayerProp.NbPoints++;
156 glVertex3f (X, Y, 0.F);
159 void OpenGl_GraphicDriver::EndLayer ()
161 if (!TheLayerProp.ListId) return;
165 TheLayerIsOpen = Standard_False;
167 TheLayerProp.ListId = 0;
170 void OpenGl_GraphicDriver::EndPolygon2d ()
172 if (!TheLayerProp.ListId) return;
176 void OpenGl_GraphicDriver::EndPolyline2d ()
178 if (!TheLayerProp.ListId) return;
182 void OpenGl_GraphicDriver::Move (const Standard_ShortReal X, const Standard_ShortReal Y)
184 if (!TheLayerProp.ListId) return;
185 if (TheLayerProp.NbPoints)
188 TheLayerProp.NbPoints = 0;
189 glBegin (GL_LINE_STRIP);
191 TheLayerProp.NbPoints++;
192 glVertex3f (X, Y, 0.F);
195 void OpenGl_GraphicDriver::Rectangle (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Width, const Standard_ShortReal Height)
197 if (!TheLayerProp.ListId) return;
198 glRectf (X, Y, X + Width, Y + Height);
201 void OpenGl_GraphicDriver::SetColor (const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B)
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);
210 void OpenGl_GraphicDriver::SetTransparency (const Standard_ShortReal ATransparency)
212 if (!TheLayerProp.ListId) return;
213 TheLayerProp.Color.rgb[3] = ATransparency;
215 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
216 glColor4fv (TheLayerProp.Color.rgb);
219 void OpenGl_GraphicDriver::UnsetTransparency ()
221 if (!TheLayerProp.ListId) return;
222 TheLayerProp.Color.rgb[3] = 1.F;
223 glDisable (GL_BLEND);
226 void OpenGl_GraphicDriver::SetLineAttributes (const Standard_Integer theType,
227 const Standard_ShortReal theWidth)
229 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
230 if (!TheLayerProp.ListId
236 if (TheLayerProp.LineType != theType)
238 Handle(OpenGl_LineAttributes) aLineAttribs;
239 if (aCtx->GetResource ("OpenGl_LineAttributes", aLineAttribs))
241 TheLayerProp.LineType = theType;
242 aLineAttribs->SetTypeOfLine ((Aspect_TypeOfLine )theType);
245 if (TheLayerProp.LineWidth != theWidth)
247 TheLayerProp.LineWidth = theWidth;
248 glLineWidth ((GLfloat )theWidth);
252 void OpenGl_GraphicDriver::SetTextAttributes (const Standard_CString theFont,
253 const Standard_Integer theType,
254 const Standard_ShortReal theR,
255 const Standard_ShortReal theG,
256 const Standard_ShortReal theB)
258 if (!TheLayerProp.ListId)
263 TheLayerProp.AspectText.ChangeFontName() = theFont;
264 TheLayerProp.AspectText.SetDisplayType ((Aspect_TypeOfDisplayText )theType);
265 TEL_COLOUR& aSubColor = TheLayerProp.AspectText.ChangeSubtitleColor();
266 aSubColor.rgb[0] = (float )theR;
267 aSubColor.rgb[1] = (float )theG;
268 aSubColor.rgb[2] = (float )theB;
271 void OpenGl_GraphicDriver::Text (const Standard_CString theText,
272 const Standard_ShortReal theX,
273 const Standard_ShortReal theY,
274 const Standard_ShortReal theHeight)
276 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
277 if (!TheLayerProp.ListId || aCtx.IsNull())
282 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
283 TheLayerProp.TextParam.Height = (int )aHeight;
284 TheLayerProp.AspectText.ChangeColor() = TheLayerProp.Color;
286 myTempText->Init (aCtx, TCollection_ExtendedString (theText), OpenGl_Vec2 (theX, theY), TheLayerProp.TextParam);
287 myTempText->Render (myPrintContext, aCtx, TheLayerProp.AspectText);
288 //myTempText->Release (aCtx);
291 void OpenGl_GraphicDriver::TextSize (const Standard_CString theText,
292 const Standard_ShortReal theHeight,
293 Standard_ShortReal& theWidth,
294 Standard_ShortReal& theAscent,
295 Standard_ShortReal& theDescent) const
297 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
298 if (!TheLayerProp.ListId || aCtx.IsNull())
303 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
304 TheLayerProp.TextParam.Height = (int )aHeight;
306 TCollection_ExtendedString anExtText = theText;
307 NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString();
308 OpenGl_Text::StringSize (aCtx, aText, TheLayerProp.AspectText, TheLayerProp.TextParam, theWidth, theAscent, theDescent);