0024192: Adding support for shaders to OCCT visualization toolkit
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_Layer.cxx
CommitLineData
b311480e 1// Created on: 2011-10-20
2// Created by: Sergey ZERCHANINOV
3// Copyright (c) 2011-2012 OPEN CASCADE SAS
4//
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.
9//
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.
12//
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.
19
5f8b738e 20#include <OpenGl_GlCore11.hxx>
21
2166f0fa 22#include <OpenGl_GraphicDriver.hxx>
7fd59977 23
eeaaaefb 24#include <Font_FontAspect.hxx>
7fd59977 25
2166f0fa
SK
26#include <OpenGl_Display.hxx>
27#include <OpenGl_AspectText.hxx>
a174a3c5 28#include <OpenGl_Text.hxx>
2166f0fa 29#include <OpenGl_TextParam.hxx>
7fd59977 30
2166f0fa 31/*----------------------------------------------------------------------*/
7fd59977 32
2166f0fa
SK
33struct OpenGl_LAYER_PROP
34{
35 int ListId;
7fd59977 36
2166f0fa
SK
37 TEL_COLOUR Color;
38 int NbPoints;
39 int LineType;
40 float LineWidth;
2166f0fa
SK
41 OpenGl_AspectText AspectText;
42 OpenGl_TextParam TextParam;
43};
7fd59977 44
2166f0fa 45/*----------------------------------------------------------------------*/
7fd59977 46
2166f0fa
SK
47static const TEL_COLOUR myDefaultColor = {{ 1.F, 1.F, 1.F, 1.F }};
48static const CALL_DEF_CONTEXTTEXT myDefaultContextText =
49{
50 1, //IsDef
51 1, //IsSet
52 "Courier", //Font
53 0.3F, //Space
54 1.F, //Expan
55 { 1.F, 1.F, 1.F }, //Color
56 (int)Aspect_TOST_NORMAL, //Style
57 (int)Aspect_TODT_NORMAL, //DisplayType
58 { 1.F, 1.F, 1.F }, //ColorSubTitle
59 0, //TextZoomable
60 0.F, //TextAngle
eeaaaefb 61 (int)Font_FA_Regular //TextFontAspect
2166f0fa
SK
62};
63
64static Standard_Boolean TheLayerIsOpen = Standard_False;
65static OpenGl_LAYER_PROP TheLayerProp;
66
67/*----------------------------------------------------------------------*/
68
30f0ad28 69void InitLayerProp (const int theListId)
2166f0fa 70{
30f0ad28 71 TheLayerProp.ListId = theListId;
7fd59977 72
30f0ad28 73 if (theListId)
2166f0fa
SK
74 {
75 TheLayerProp.Color = myDefaultColor;
76 TheLayerProp.NbPoints = 0;
77 TheLayerProp.LineType = -1;
78 TheLayerProp.LineWidth = -1.F;
7fd59977 79
fd4a6963 80 TheLayerProp.AspectText.SetAspect (myDefaultContextText);
7fd59977 81
2166f0fa
SK
82 TheLayerProp.TextParam.HAlign = Graphic3d_HTA_LEFT;
83 TheLayerProp.TextParam.VAlign = Graphic3d_VTA_BOTTOM;
7fd59977 84 }
7fd59977 85}
86
2166f0fa 87/*----------------------------------------------------------------------*/
7fd59977 88
2166f0fa
SK
89void OpenGl_GraphicDriver::Layer (Aspect_CLayer2d& ACLayer)
90{
9d35f668 91 ACLayer.ptrLayer = new CALL_DEF_PTRLAYER();
2166f0fa 92 ACLayer.ptrLayer->listIndex = glGenLists(1);
7fd59977 93}
94
2166f0fa
SK
95/*----------------------------------------------------------------------*/
96
97void OpenGl_GraphicDriver::RemoveLayer (const Aspect_CLayer2d& ACLayer)
98{
99 if (!ACLayer.ptrLayer) return;
100 if (!ACLayer.ptrLayer->listIndex) return;
101 if (TheLayerProp.ListId == ACLayer.ptrLayer->listIndex)
102 EndLayer();
103 glDeleteLists (ACLayer.ptrLayer->listIndex, 1);
104 ACLayer.ptrLayer->listIndex = 0;
105 //szvgl: memory leak here?
106 //free ( ACLayer.ptrLayer );
107 //ACLayer.ptrLayer = NULL;
7fd59977 108}
109
2166f0fa
SK
110/*----------------------------------------------------------------------*/
111
112void OpenGl_GraphicDriver::BeginLayer (const Aspect_CLayer2d& ACLayer)
113{
114 call_def_ptrLayer ptrLayer = (call_def_ptrLayer) ACLayer.ptrLayer;
115 if (!ptrLayer) return;
116
30f0ad28 117 InitLayerProp (ptrLayer->listIndex);
2166f0fa
SK
118 if (!TheLayerProp.ListId) return;
119
2166f0fa
SK
120 glNewList (TheLayerProp.ListId, GL_COMPILE);
121 TheLayerIsOpen = Standard_True;
7fd59977 122}
123
2166f0fa
SK
124void OpenGl_GraphicDriver::BeginPolygon2d ()
125{
126 if (!TheLayerProp.ListId) return;
127 TheLayerProp.NbPoints = 0;
128 glBegin (GL_POLYGON);
129}
7fd59977 130
2166f0fa
SK
131void OpenGl_GraphicDriver::BeginPolyline2d ()
132{
133 if (!TheLayerProp.ListId) return;
134 TheLayerProp.NbPoints = 0;
135 glBegin (GL_LINE_STRIP);
7fd59977 136}
137
2166f0fa
SK
138void OpenGl_GraphicDriver::ClearLayer (const Aspect_CLayer2d& ACLayer)
139{
140 if (!ACLayer.ptrLayer) return;
141
30f0ad28 142 InitLayerProp (ACLayer.ptrLayer->listIndex);
2166f0fa
SK
143 if (!TheLayerProp.ListId) return;
144
145 glNewList (TheLayerProp.ListId, GL_COMPILE);
146 glEndList ();
7fd59977 147}
148
2166f0fa
SK
149void OpenGl_GraphicDriver::Draw (const Standard_ShortReal X, const Standard_ShortReal Y)
150{
151 if (!TheLayerProp.ListId) return;
152 TheLayerProp.NbPoints++;
153 glVertex3f (X, Y, 0.F);
7fd59977 154}
155
2166f0fa
SK
156void OpenGl_GraphicDriver::Edge (const Standard_ShortReal X, const Standard_ShortReal Y)
157{
158 if (!TheLayerProp.ListId) return;
159 TheLayerProp.NbPoints++;
160 glVertex3f (X, Y, 0.F);
7fd59977 161}
162
2166f0fa
SK
163void OpenGl_GraphicDriver::EndLayer ()
164{
165 if (!TheLayerProp.ListId) return;
166 if (TheLayerIsOpen)
167 {
168 glEndList();
169 TheLayerIsOpen = Standard_False;
7fd59977 170 }
2166f0fa 171 TheLayerProp.ListId = 0;
7fd59977 172}
173
2166f0fa
SK
174void OpenGl_GraphicDriver::EndPolygon2d ()
175{
176 if (!TheLayerProp.ListId) return;
177 glEnd ();
7fd59977 178}
179
2166f0fa
SK
180void OpenGl_GraphicDriver::EndPolyline2d ()
181{
182 if (!TheLayerProp.ListId) return;
183 glEnd ();
7fd59977 184}
185
2166f0fa
SK
186void OpenGl_GraphicDriver::Move (const Standard_ShortReal X, const Standard_ShortReal Y)
187{
188 if (!TheLayerProp.ListId) return;
189 if (TheLayerProp.NbPoints)
190 {
191 glEnd ();
192 TheLayerProp.NbPoints = 0;
193 glBegin (GL_LINE_STRIP);
7fd59977 194 }
2166f0fa
SK
195 TheLayerProp.NbPoints++;
196 glVertex3f (X, Y, 0.F);
7fd59977 197}
198
2166f0fa
SK
199void OpenGl_GraphicDriver::Rectangle (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Width, const Standard_ShortReal Height)
200{
201 if (!TheLayerProp.ListId) return;
202 glRectf (X, Y, X + Width, Y + Height);
7fd59977 203}
204
2166f0fa
SK
205void OpenGl_GraphicDriver::SetColor (const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B)
206{
207 if (!TheLayerProp.ListId) return;
208 TheLayerProp.Color.rgb[0] = R;
209 TheLayerProp.Color.rgb[1] = G;
210 TheLayerProp.Color.rgb[2] = B;
211 glColor3fv (TheLayerProp.Color.rgb);
7fd59977 212}
213
2166f0fa
SK
214void OpenGl_GraphicDriver::SetTransparency (const Standard_ShortReal ATransparency)
215{
216 if (!TheLayerProp.ListId) return;
217 TheLayerProp.Color.rgb[3] = ATransparency;
218 glEnable (GL_BLEND);
219 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
220 glColor4fv (TheLayerProp.Color.rgb);
221}
222
223void OpenGl_GraphicDriver::UnsetTransparency ()
224{
225 if (!TheLayerProp.ListId) return;
226 TheLayerProp.Color.rgb[3] = 1.F;
227 glDisable (GL_BLEND);
7fd59977 228}
229
2166f0fa
SK
230void OpenGl_GraphicDriver::SetLineAttributes (const Standard_Integer Type, const Standard_ShortReal Width)
231{
fd4a6963 232 if (!TheLayerProp.ListId || myGlDisplay.IsNull()) return;
2166f0fa
SK
233
234 if (TheLayerProp.LineType != Type)
235 {
236 TheLayerProp.LineType = Type;
fd4a6963 237 myGlDisplay->SetTypeOfLine((Aspect_TypeOfLine) Type);
2166f0fa
SK
238 }
239 if (TheLayerProp.LineWidth != Width)
240 {
241 TheLayerProp.LineWidth = Width;
242 glLineWidth ((GLfloat) Width);
7fd59977 243 }
7fd59977 244}
245
a174a3c5 246void OpenGl_GraphicDriver::SetTextAttributes (const Standard_CString theFont,
247 const Standard_Integer theType,
248 const Standard_ShortReal theR,
249 const Standard_ShortReal theG,
250 const Standard_ShortReal theB)
2166f0fa 251{
a174a3c5 252 if (!TheLayerProp.ListId)
2166f0fa 253 {
a174a3c5 254 return;
7fd59977 255 }
a174a3c5 256
257 TheLayerProp.AspectText.ChangeFontName() = theFont;
258 TheLayerProp.AspectText.SetDisplayType ((Aspect_TypeOfDisplayText )theType);
259 TEL_COLOUR& aSubColor = TheLayerProp.AspectText.ChangeSubtitleColor();
260 aSubColor.rgb[0] = (float )theR;
261 aSubColor.rgb[1] = (float )theG;
262 aSubColor.rgb[2] = (float )theB;
2166f0fa 263}
7fd59977 264
a174a3c5 265void OpenGl_GraphicDriver::Text (const Standard_CString theText,
266 const Standard_ShortReal theX,
267 const Standard_ShortReal theY,
268 const Standard_ShortReal theHeight)
13a22457 269{
a174a3c5 270 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
271 if (!TheLayerProp.ListId || aCtx.IsNull())
2166f0fa 272 {
a174a3c5 273 return;
25289ec1 274 }
275
a174a3c5 276 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
277 TheLayerProp.TextParam.Height = (int )aHeight;
278 TheLayerProp.AspectText.ChangeColor() = TheLayerProp.Color;
25289ec1 279
a174a3c5 280 myTempText->Init (aCtx, TCollection_ExtendedString (theText), OpenGl_Vec2 (theX, theY), TheLayerProp.TextParam);
281 myTempText->Render (myPrintContext, aCtx, TheLayerProp.AspectText);
282 //myTempText->Release (aCtx);
7fd59977 283}
284
a174a3c5 285void OpenGl_GraphicDriver::TextSize (const Standard_CString theText,
286 const Standard_ShortReal theHeight,
287 Standard_ShortReal& theWidth,
288 Standard_ShortReal& theAscent,
289 Standard_ShortReal& theDescent) const
13a22457 290{
a174a3c5 291 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
292 if (!TheLayerProp.ListId || aCtx.IsNull())
2166f0fa 293 {
a174a3c5 294 return;
7fd59977 295 }
2166f0fa 296
a174a3c5 297 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
298 TheLayerProp.TextParam.Height = (int )aHeight;
2166f0fa 299
a174a3c5 300 TCollection_ExtendedString anExtText = theText;
301 NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString();
302 OpenGl_Text::StringSize (aCtx, aText, TheLayerProp.AspectText, TheLayerProp.TextParam, theWidth, theAscent, theDescent);
7fd59977 303}