1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 #include <OpenGl_GlCore11.hxx>
22 #include <OpenGl_GraphicDriver.hxx>
24 #include <Font_FontAspect.hxx>
26 #include <OpenGl_Display.hxx>
27 #include <OpenGl_AspectText.hxx>
28 #include <OpenGl_Text.hxx>
29 #include <OpenGl_TextParam.hxx>
31 /*----------------------------------------------------------------------*/
33 struct OpenGl_LAYER_PROP
42 OpenGl_AspectText AspectText;
43 OpenGl_TextParam TextParam;
46 /*----------------------------------------------------------------------*/
48 static const TEL_COLOUR myDefaultColor = {{ 1.F, 1.F, 1.F, 1.F }};
49 static const CALL_DEF_CONTEXTTEXT myDefaultContextText =
56 { 1.F, 1.F, 1.F }, //Color
57 (int)Aspect_TOST_NORMAL, //Style
58 (int)Aspect_TODT_NORMAL, //DisplayType
59 { 1.F, 1.F, 1.F }, //ColorSubTitle
62 (int)Font_FA_Regular //TextFontAspect
65 static Standard_Boolean TheLayerIsOpen = Standard_False;
66 static OpenGl_LAYER_PROP TheLayerProp;
68 /*----------------------------------------------------------------------*/
70 void InitLayerProp (const int AListId)
72 TheLayerProp.ListId = AListId;
76 TheLayerProp.Color = myDefaultColor;
77 TheLayerProp.NbPoints = 0;
78 TheLayerProp.LineType = -1;
79 TheLayerProp.LineWidth = -1.F;
81 TheLayerProp.AspectText.SetContext(myDefaultContextText);
83 TheLayerProp.TextParam.HAlign = Graphic3d_HTA_LEFT;
84 TheLayerProp.TextParam.VAlign = Graphic3d_VTA_BOTTOM;
88 /*----------------------------------------------------------------------*/
90 void OpenGl_GraphicDriver::Layer (Aspect_CLayer2d& ACLayer)
92 ACLayer.ptrLayer = new CALL_DEF_PTRLAYER();
93 ACLayer.ptrLayer->listIndex = glGenLists(1);
96 /*----------------------------------------------------------------------*/
98 void OpenGl_GraphicDriver::RemoveLayer (const Aspect_CLayer2d& ACLayer)
100 if (!ACLayer.ptrLayer) return;
101 if (!ACLayer.ptrLayer->listIndex) return;
102 if (TheLayerProp.ListId == ACLayer.ptrLayer->listIndex)
104 glDeleteLists (ACLayer.ptrLayer->listIndex, 1);
105 ACLayer.ptrLayer->listIndex = 0;
106 //szvgl: memory leak here?
107 //free ( ACLayer.ptrLayer );
108 //ACLayer.ptrLayer = NULL;
111 /*----------------------------------------------------------------------*/
113 void OpenGl_GraphicDriver::BeginLayer (const Aspect_CLayer2d& ACLayer)
115 call_def_ptrLayer ptrLayer = (call_def_ptrLayer) ACLayer.ptrLayer;
116 if (!ptrLayer) return;
118 InitLayerProp(ptrLayer->listIndex);
119 if (!TheLayerProp.ListId) return;
121 glNewList (TheLayerProp.ListId, GL_COMPILE);
122 TheLayerIsOpen = Standard_True;
125 void OpenGl_GraphicDriver::BeginPolygon2d ()
127 if (!TheLayerProp.ListId) return;
128 TheLayerProp.NbPoints = 0;
129 glBegin (GL_POLYGON);
132 void OpenGl_GraphicDriver::BeginPolyline2d ()
134 if (!TheLayerProp.ListId) return;
135 TheLayerProp.NbPoints = 0;
136 glBegin (GL_LINE_STRIP);
139 void OpenGl_GraphicDriver::ClearLayer (const Aspect_CLayer2d& ACLayer)
141 if (!ACLayer.ptrLayer) return;
143 InitLayerProp(ACLayer.ptrLayer->listIndex);
144 if (!TheLayerProp.ListId) return;
146 glNewList (TheLayerProp.ListId, GL_COMPILE);
150 void OpenGl_GraphicDriver::Draw (const Standard_ShortReal X, const Standard_ShortReal Y)
152 if (!TheLayerProp.ListId) return;
153 TheLayerProp.NbPoints++;
154 glVertex3f (X, Y, 0.F);
157 void OpenGl_GraphicDriver::Edge (const Standard_ShortReal X, const Standard_ShortReal Y)
159 if (!TheLayerProp.ListId) return;
160 TheLayerProp.NbPoints++;
161 glVertex3f (X, Y, 0.F);
164 void OpenGl_GraphicDriver::EndLayer ()
166 if (!TheLayerProp.ListId) return;
170 TheLayerIsOpen = Standard_False;
172 TheLayerProp.ListId = 0;
175 void OpenGl_GraphicDriver::EndPolygon2d ()
177 if (!TheLayerProp.ListId) return;
181 void OpenGl_GraphicDriver::EndPolyline2d ()
183 if (!TheLayerProp.ListId) return;
187 void OpenGl_GraphicDriver::Move (const Standard_ShortReal X, const Standard_ShortReal Y)
189 if (!TheLayerProp.ListId) return;
190 if (TheLayerProp.NbPoints)
193 TheLayerProp.NbPoints = 0;
194 glBegin (GL_LINE_STRIP);
196 TheLayerProp.NbPoints++;
197 glVertex3f (X, Y, 0.F);
200 void OpenGl_GraphicDriver::Rectangle (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Width, const Standard_ShortReal Height)
202 if (!TheLayerProp.ListId) return;
203 glRectf (X, Y, X + Width, Y + Height);
206 void OpenGl_GraphicDriver::SetColor (const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B)
208 if (!TheLayerProp.ListId) return;
209 TheLayerProp.Color.rgb[0] = R;
210 TheLayerProp.Color.rgb[1] = G;
211 TheLayerProp.Color.rgb[2] = B;
212 glColor3fv (TheLayerProp.Color.rgb);
215 void OpenGl_GraphicDriver::SetTransparency (const Standard_ShortReal ATransparency)
217 if (!TheLayerProp.ListId) return;
218 TheLayerProp.Color.rgb[3] = ATransparency;
220 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
221 glColor4fv (TheLayerProp.Color.rgb);
224 void OpenGl_GraphicDriver::UnsetTransparency ()
226 if (!TheLayerProp.ListId) return;
227 TheLayerProp.Color.rgb[3] = 1.F;
228 glDisable (GL_BLEND);
231 void OpenGl_GraphicDriver::SetLineAttributes (const Standard_Integer Type, const Standard_ShortReal Width)
233 if (!TheLayerProp.ListId || openglDisplay.IsNull()) return;
235 if (TheLayerProp.LineType != Type)
237 TheLayerProp.LineType = Type;
238 openglDisplay->SetTypeOfLine((Aspect_TypeOfLine) Type);
240 if (TheLayerProp.LineWidth != Width)
242 TheLayerProp.LineWidth = Width;
243 glLineWidth ((GLfloat) Width);
247 void OpenGl_GraphicDriver::SetTextAttributes (const Standard_CString theFont,
248 const Standard_Integer theType,
249 const Standard_ShortReal theR,
250 const Standard_ShortReal theG,
251 const Standard_ShortReal theB)
253 if (!TheLayerProp.ListId)
258 TheLayerProp.AspectText.ChangeFontName() = theFont;
259 TheLayerProp.AspectText.SetDisplayType ((Aspect_TypeOfDisplayText )theType);
260 TEL_COLOUR& aSubColor = TheLayerProp.AspectText.ChangeSubtitleColor();
261 aSubColor.rgb[0] = (float )theR;
262 aSubColor.rgb[1] = (float )theG;
263 aSubColor.rgb[2] = (float )theB;
266 void OpenGl_GraphicDriver::Text (const Standard_CString theText,
267 const Standard_ShortReal theX,
268 const Standard_ShortReal theY,
269 const Standard_ShortReal theHeight)
271 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
272 if (!TheLayerProp.ListId || aCtx.IsNull())
277 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
278 TheLayerProp.TextParam.Height = (int )aHeight;
279 TheLayerProp.AspectText.ChangeColor() = TheLayerProp.Color;
281 myTempText->Init (aCtx, TCollection_ExtendedString (theText), OpenGl_Vec2 (theX, theY), TheLayerProp.TextParam);
282 myTempText->Render (myPrintContext, aCtx, TheLayerProp.AspectText);
283 //myTempText->Release (aCtx);
286 void OpenGl_GraphicDriver::TextSize (const Standard_CString theText,
287 const Standard_ShortReal theHeight,
288 Standard_ShortReal& theWidth,
289 Standard_ShortReal& theAscent,
290 Standard_ShortReal& theDescent) const
292 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
293 if (!TheLayerProp.ListId || aCtx.IsNull())
298 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
299 TheLayerProp.TextParam.Height = (int )aHeight;
301 TCollection_ExtendedString anExtText = theText;
302 NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString();
303 OpenGl_Text::StringSize (aCtx, aText, TheLayerProp.AspectText, TheLayerProp.TextParam, theWidth, theAscent, theDescent);