0025442: Visualization, TKOpenGl - prevent inclusion of system header glxext.h
[occt.git] / src / OpenGl / OpenGl_Text.cxx
CommitLineData
b311480e 1// Created on: 2011-07-13
2// Created by: Sergey ZERCHANINOV
a174a3c5 3// Copyright (c) 2011-2013 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
2166f0fa 16#include <OpenGl_AspectText.hxx>
30f0ad28 17#include <OpenGl_GlCore11.hxx>
a174a3c5 18#include <OpenGl_GraphicDriver.hxx>
30f0ad28 19#include <OpenGl_ShaderManager.hxx>
20#include <OpenGl_ShaderProgram.hxx>
21#include <OpenGl_ShaderStates.hxx>
25ef750e 22#include <OpenGl_Sampler.hxx>
30f0ad28 23#include <OpenGl_Text.hxx>
bf75be98 24#include <OpenGl_Workspace.hxx>
2166f0fa 25
a174a3c5 26#include <Font_FontMgr.hxx>
27#include <TCollection_HAsciiString.hxx>
28
a174a3c5 29#ifdef HAVE_GL2PS
30 #include <gl2ps.h>
31#endif
32
33namespace
2166f0fa 34{
a174a3c5 35 static const GLdouble THE_IDENTITY_MATRIX[4][4] =
36 {
37 {1.,0.,0.,0.},
38 {0.,1.,0.,0.},
39 {0.,0.,1.,0.},
40 {0.,0.,0.,1.}
41 };
42
43#ifdef HAVE_GL2PS
44 static char const* TheFamily[] = {"Helvetica", "Courier", "Times"};
45 static char const* TheItalic[] = {"Oblique", "Oblique", "Italic"};
46 static char const* TheBase[] = {"", "", "-Roman"};
47
48 //! Convert font name used for rendering to some "good" font names
49 //! that produce good vector text.
50 static void getGL2PSFontName (const char* theSrcFont,
51 char* thePsFont)
52 {
53 if (strstr (theSrcFont, "Symbol"))
54 {
55 sprintf (thePsFont, "%s", "Symbol");
56 return;
57 }
58 else if (strstr (theSrcFont, "ZapfDingbats"))
59 {
60 sprintf (thePsFont, "%s", "WingDings");
61 return;
62 }
2166f0fa 63
a174a3c5 64 int aFontId = 0;
65 bool isBold = false;
66 bool isItalic = false;
67 if (strstr (theSrcFont, "Courier"))
68 {
69 aFontId = 1;
70 }
71 else if (strstr (theSrcFont, "Times"))
72 {
73 aFontId = 2;
74 }
2166f0fa 75
a174a3c5 76 if (strstr (theSrcFont, "Bold"))
77 {
78 isBold = true;
79 }
80 if (strstr (theSrcFont, "Italic")
81 || strstr (theSrcFont, "Oblique"))
82 {
83 isItalic = true;
84 }
2166f0fa 85
a174a3c5 86 if (isBold)
87 {
a174a3c5 88 if (isItalic)
89 {
8b224a09 90 sprintf (thePsFont, "%s-Bold%s", TheFamily[aFontId], TheItalic[aFontId]);
91 }
92 else
93 {
94 sprintf (thePsFont, "%s-Bold", TheFamily[aFontId]);
a174a3c5 95 }
96 }
97 else if (isItalic)
98 {
99 sprintf (thePsFont, "%s-%s", TheFamily[aFontId], TheItalic[aFontId]);
100 }
101 else
102 {
103 sprintf (thePsFont, "%s%s", TheFamily[aFontId], TheBase[aFontId]);
104 }
105 }
2166f0fa 106
a174a3c5 107 static void exportText (const NCollection_String& theText,
108 const Standard_Boolean theIs2d,
109 const OpenGl_AspectText& theAspect,
110 const Standard_Integer theHeight)
111 {
2166f0fa 112
a174a3c5 113 char aPsFont[64];
114 getGL2PSFontName (theAspect.FontName().ToCString(), aPsFont);
2166f0fa 115
ca3c13d1 116 #if !defined(GL_ES_VERSION_2_0)
a174a3c5 117 if (theIs2d)
118 {
119 glRasterPos2f (0.0f, 0.0f);
120 }
121 else
122 {
123 glRasterPos3f (0.0f, 0.0f, 0.0f);
124 }
125
126 GLubyte aZero = 0;
127 glBitmap (1, 1, 0, 0, 0, 0, &aZero);
ca3c13d1 128 #endif
a174a3c5 129
130 // Standard GL2PS's alignment isn't used, because it doesn't work correctly
131 // for all formats, therefore alignment is calculated manually relative
132 // to the bottom-left corner, which corresponds to the GL2PS_TEXT_BL value
105aae76 133 gl2psTextOpt (theText.ToCString(), aPsFont, (GLshort)theHeight, GL2PS_TEXT_BL, theAspect.Angle());
a174a3c5 134 }
135#endif
136
137};
138
139// =======================================================================
140// function : OpenGl_Text
141// purpose :
142// =======================================================================
143OpenGl_Text::OpenGl_Text()
144: myWinX (0.0f),
145 myWinY (0.0f),
146 myWinZ (0.0f),
147 myScaleHeight (1.0f),
148 myPoint (0.0f, 0.0f, 0.0f),
149 myIs2d (false)
150{
151 myParams.Height = 10;
152 myParams.HAlign = Graphic3d_HTA_LEFT;
153 myParams.VAlign = Graphic3d_VTA_BOTTOM;
2166f0fa
SK
154}
155
a174a3c5 156// =======================================================================
157// function : OpenGl_Text
158// purpose :
159// =======================================================================
b64d84be 160OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText,
161 const OpenGl_Vec3& thePoint,
162 const OpenGl_TextParam& theParams)
a174a3c5 163: myWinX (0.0f),
164 myWinY (0.0f),
165 myWinZ (0.0f),
166 myScaleHeight (1.0f),
167 myExportHeight (1.0f),
168 myParams (theParams),
b64d84be 169 myString (theText),
a174a3c5 170 myPoint (thePoint),
171 myIs2d (false)
172{
173 //
174}
2166f0fa 175
a174a3c5 176// =======================================================================
177// function : SetPosition
178// purpose :
179// =======================================================================
180void OpenGl_Text::SetPosition (const OpenGl_Vec3& thePoint)
2166f0fa 181{
a174a3c5 182 myPoint = thePoint;
2166f0fa
SK
183}
184
a174a3c5 185// =======================================================================
186// function : SetFontSize
187// purpose :
188// =======================================================================
189void OpenGl_Text::SetFontSize (const Handle(OpenGl_Context)& theCtx,
190 const Standard_Integer theFontSize)
191{
192 if (myParams.Height != theFontSize)
193 {
10b9c7df 194 Release (theCtx.operator->());
a174a3c5 195 }
196 myParams.Height = theFontSize;
197}
198
199// =======================================================================
200// function : Init
201// purpose :
202// =======================================================================
203void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
204 const Standard_Utf8Char* theText,
205 const OpenGl_Vec3& thePoint)
206{
10b9c7df 207 releaseVbos (theCtx.operator->());
a174a3c5 208 myIs2d = false;
209 myPoint = thePoint;
210 myString.FromUnicode (theText);
211}
212
213// =======================================================================
214// function : Init
215// purpose :
216// =======================================================================
217void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
218 const Standard_Utf8Char* theText,
219 const OpenGl_Vec3& thePoint,
220 const OpenGl_TextParam& theParams)
221{
222 if (myParams.Height != theParams.Height)
223 {
10b9c7df 224 Release (theCtx.operator->());
a174a3c5 225 }
226 else
227 {
10b9c7df 228 releaseVbos (theCtx.operator->());
a174a3c5 229 }
230 myIs2d = false;
231 myParams = theParams;
232 myPoint = thePoint;
233 myString.FromUnicode (theText);
234}
2166f0fa 235
a174a3c5 236// =======================================================================
237// function : Init
238// purpose :
239// =======================================================================
240void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
241 const TCollection_ExtendedString& theText,
242 const OpenGl_Vec2& thePoint,
243 const OpenGl_TextParam& theParams)
2166f0fa 244{
a174a3c5 245 if (myParams.Height != theParams.Height)
246 {
10b9c7df 247 Release (theCtx.operator->());
a174a3c5 248 }
249 else
250 {
10b9c7df 251 releaseVbos (theCtx.operator->());
a174a3c5 252 }
253 myIs2d = true;
254 myParams = theParams;
255 myPoint.xy() = thePoint;
256 myPoint.z() = 0.0f;
257 myString.FromUnicode ((Standard_Utf16Char* )theText.ToExtString());
258}
259
260// =======================================================================
261// function : ~OpenGl_Text
262// purpose :
263// =======================================================================
264OpenGl_Text::~OpenGl_Text()
265{
266 //
267}
268
269// =======================================================================
270// function : releaseVbos
271// purpose :
272// =======================================================================
10b9c7df 273void OpenGl_Text::releaseVbos (OpenGl_Context* theCtx)
a174a3c5 274{
275 for (Standard_Integer anIter = 0; anIter < myVertsVbo.Length(); ++anIter)
276 {
277 Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.ChangeValue (anIter);
278 Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.ChangeValue (anIter);
279
10b9c7df 280 if (theCtx)
a174a3c5 281 {
282 theCtx->DelayedRelease (aVerts);
283 theCtx->DelayedRelease (aTCrds);
284 }
285 aVerts.Nullify();
286 aTCrds.Nullify();
287 }
288 myTextures.Clear();
289 myVertsVbo.Clear();
290 myTCrdsVbo.Clear();
a174a3c5 291}
292
293// =======================================================================
294// function : Release
295// purpose :
296// =======================================================================
10b9c7df 297void OpenGl_Text::Release (OpenGl_Context* theCtx)
a174a3c5 298{
299 releaseVbos (theCtx);
300 if (!myFont.IsNull())
301 {
302 Handle(OpenGl_Context) aCtx = theCtx;
303 const TCollection_AsciiString aKey = myFont->ResourceKey();
304 myFont.Nullify();
10b9c7df 305 if (aCtx)
306 aCtx->ReleaseResource (aKey, Standard_True);
a174a3c5 307 }
308}
309
310// =======================================================================
311// function : StringSize
312// purpose :
313// =======================================================================
314void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
315 const NCollection_String& theText,
316 const OpenGl_AspectText& theTextAspect,
317 const OpenGl_TextParam& theParams,
318 Standard_ShortReal& theWidth,
319 Standard_ShortReal& theAscent,
320 Standard_ShortReal& theDescent)
321{
322 theWidth = 0.0f;
323 theAscent = 0.0f;
324 theDescent = 0.0f;
325 const TCollection_AsciiString aFontKey = FontKey (theTextAspect, theParams.Height);
326 Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, theParams.Height, aFontKey);
327 if (aFont.IsNull() || !aFont->IsValid())
328 {
2166f0fa 329 return;
a174a3c5 330 }
331
332 theAscent = aFont->Ascender();
333 theDescent = aFont->Descender();
334
335 GLfloat aWidth = 0.0f;
336 for (NCollection_Utf8Iter anIter = theText.Iterator(); *anIter != 0;)
337 {
338 const Standard_Utf32Char aCharThis = *anIter;
339 const Standard_Utf32Char aCharNext = *++anIter;
340
341 if (aCharThis == '\x0D' // CR (carriage return)
342 || aCharThis == '\a' // BEL (alarm)
343 || aCharThis == '\f' // FF (form feed) NP (new page)
344 || aCharThis == '\b' // BS (backspace)
345 || aCharThis == '\v') // VT (vertical tab)
346 {
347 continue; // skip unsupported carriage control codes
348 }
349 else if (aCharThis == '\x0A') // LF (line feed, new line)
350 {
351 theWidth = Max (theWidth, aWidth);
352 aWidth = 0.0f;
353 continue;
354 }
355 else if (aCharThis == ' ')
356 {
357 aWidth += aFont->AdvanceX (aCharThis, aCharNext);
358 continue;
359 }
360 else if (aCharThis == '\t')
361 {
362 aWidth += aFont->AdvanceX (' ', aCharNext) * 8.0f;
363 continue;
364 }
365
366 aWidth += aFont->AdvanceX (aCharThis, aCharNext);
367 }
368 theWidth = Max (theWidth, aWidth);
369
370 Handle(OpenGl_Context) aCtx = theCtx;
371 aFont.Nullify();
372 aCtx->ReleaseResource (aFontKey, Standard_True);
373}
374
375// =======================================================================
376// function : Render
377// purpose :
378// =======================================================================
379void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
380{
25ef750e 381 const OpenGl_AspectText* aTextAspect = theWorkspace->AspectText (Standard_True);
382 const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture();
383 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
384 const Handle(OpenGl_Sampler)& aSampler = aCtx->TextureSampler();
385 if (!aSampler.IsNull())
386 {
387 aSampler->Unbind (*aCtx);
388 }
30f0ad28 389
390 if (aCtx->IsGlGreaterEqual (2, 0))
391 {
8625ef7e 392 const Handle(OpenGl_ShaderProgram)& aProgram = aTextAspect->ShaderProgramRes (aCtx);
7d3e64ef 393 aCtx->BindProgram (aProgram);
30f0ad28 394 if (!aProgram.IsNull())
395 {
7d3e64ef 396 aProgram->ApplyVariables (aCtx);
30f0ad28 397
398 const OpenGl_MaterialState* aMaterialState = aCtx->ShaderManager()->MaterialState (aProgram);
5322131b 399
30f0ad28 400 if (aMaterialState == NULL || aMaterialState->Aspect() != aTextAspect)
401 aCtx->ShaderManager()->UpdateMaterialStateTo (aProgram, aTextAspect);
5322131b 402
30f0ad28 403 aCtx->ShaderManager()->PushState (aProgram);
404 }
30f0ad28 405 }
406
a174a3c5 407 // use highlight color or colors from aspect
408 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
409 {
410 render (theWorkspace->PrinterContext(),
30f0ad28 411 aCtx,
a174a3c5 412 *aTextAspect,
413 *theWorkspace->HighlightColor,
414 *theWorkspace->HighlightColor);
415 }
416 else
417 {
418 render (theWorkspace->PrinterContext(),
30f0ad28 419 aCtx,
a174a3c5 420 *aTextAspect,
421 aTextAspect->Color(),
422 aTextAspect->SubtitleColor());
423 }
2166f0fa 424
a174a3c5 425 // restore aspects
25ef750e 426 if (!aSampler.IsNull())
427 {
428 aSampler->Bind (*aCtx);
429 }
a174a3c5 430 if (!aPrevTexture.IsNull())
431 {
432 theWorkspace->EnableTexture (aPrevTexture);
433 }
434}
2166f0fa 435
a174a3c5 436// =======================================================================
437// function : Render
438// purpose :
439// =======================================================================
440void OpenGl_Text::Render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
441 const Handle(OpenGl_Context)& theCtx,
442 const OpenGl_AspectText& theTextAspect) const
443{
444 render (thePrintCtx, theCtx, theTextAspect, theTextAspect.Color(), theTextAspect.SubtitleColor());
445}
2166f0fa 446
a174a3c5 447// =======================================================================
448// function : setupMatrix
449// purpose :
450// =======================================================================
451void OpenGl_Text::setupMatrix (const Handle(OpenGl_PrinterContext)& thePrintCtx,
ca3c13d1 452 const Handle(OpenGl_Context)& theCtx,
a174a3c5 453 const OpenGl_AspectText& theTextAspect,
454 const OpenGl_Vec3 theDVec) const
455{
456 // setup matrix
ca3c13d1 457#if !defined(GL_ES_VERSION_2_0)
a174a3c5 458 if (myIs2d)
bf75be98 459 {
a174a3c5 460 glLoadIdentity();
461 glTranslatef (myPoint.x() + theDVec.x(), myPoint.y() + theDVec.y(), 0.0f);
03e04ead 462 glScalef (1.0f, -1.0f, 1.0f);
463 glRotatef (theTextAspect.Angle(), 0.0, 0.0, 1.0);
2166f0fa
SK
464 }
465 else
466 {
a174a3c5 467 // align coordinates to the nearest integer
468 // to avoid extra interpolation issues
469 GLdouble anObjX, anObjY, anObjZ;
470 gluUnProject (std::floor (myWinX + (GLdouble )theDVec.x()),
471 std::floor (myWinY + (GLdouble )theDVec.y()),
472 myWinZ + (GLdouble )theDVec.z(),
473 (GLdouble* )THE_IDENTITY_MATRIX, myProjMatrix, myViewport,
474 &anObjX, &anObjY, &anObjZ);
475
476 glLoadIdentity();
ca3c13d1 477 theCtx->core11->glTranslated (anObjX, anObjY, anObjZ);
478 theCtx->core11->glRotated (theTextAspect.Angle(), 0.0, 0.0, 1.0);
a174a3c5 479 if (!theTextAspect.IsZoomable())
480 {
481 #ifdef _WIN32
482 // if the context has assigned printer context, use it's parameters
483 if (!thePrintCtx.IsNull())
484 {
485 // get printing scaling in x and y dimensions
486 GLfloat aTextScalex = 1.0f, aTextScaley = 1.0f;
487 thePrintCtx->GetScale (aTextScalex, aTextScaley);
488
489 // text should be scaled in all directions with same
490 // factor to save its proportions, so use height (y) scaling
491 // as it is better for keeping text/3d graphics proportions
ca3c13d1 492 theCtx->core11->glScaled ((GLfloat )aTextScaley, (GLfloat )aTextScaley, (GLfloat )aTextScaley);
a174a3c5 493 }
494 #endif
ca3c13d1 495 theCtx->core11->glScaled (myScaleHeight, myScaleHeight, myScaleHeight);
a174a3c5 496 }
497 }
ca3c13d1 498#endif
a174a3c5 499}
500
501// =======================================================================
502// function : drawText
503// purpose :
504// =======================================================================
35e08fe8 505
506void OpenGl_Text::drawText (const Handle(OpenGl_PrinterContext)& ,
a174a3c5 507 const Handle(OpenGl_Context)& theCtx,
35e08fe8 508 #ifdef HAVE_GL2PS
a174a3c5 509 const OpenGl_AspectText& theTextAspect) const
35e08fe8 510 #else
511 const OpenGl_AspectText& ) const
512 #endif
a174a3c5 513{
514#ifdef HAVE_GL2PS
515 if (theCtx->IsFeedback())
516 {
517 // position of the text and alignment is calculated by transformation matrix
518 exportText (myString, myIs2d, theTextAspect, (Standard_Integer )myExportHeight);
519 return;
2166f0fa 520 }
a174a3c5 521#endif
2166f0fa 522
7d3e64ef 523 if (myVertsVbo.Length() != myTextures.Length()
524 || myTextures.IsEmpty())
2166f0fa 525 {
7d3e64ef 526 return;
2166f0fa 527 }
7d3e64ef 528
529 for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
a174a3c5 530 {
7d3e64ef 531 const GLuint aTexId = myTextures.Value (anIter);
532 glBindTexture (GL_TEXTURE_2D, aTexId);
a174a3c5 533
7d3e64ef 534 const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter);
535 const Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.Value (anIter);
536 aVerts->BindAttribute (theCtx, Graphic3d_TOA_POS);
537 aTCrds->BindAttribute (theCtx, Graphic3d_TOA_UV);
2166f0fa 538
7d3e64ef 539 glDrawArrays (GL_TRIANGLES, 0, GLsizei(aVerts->GetElemsNb()));
2166f0fa 540
7d3e64ef 541 aVerts->UnbindAttribute (theCtx, Graphic3d_TOA_UV);
542 aVerts->UnbindAttribute (theCtx, Graphic3d_TOA_POS);
a174a3c5 543 }
7d3e64ef 544 glBindTexture (GL_TEXTURE_2D, 0);
a174a3c5 545}
2166f0fa 546
a174a3c5 547// =======================================================================
548// function : FontKey
549// purpose :
550// =======================================================================
551TCollection_AsciiString OpenGl_Text::FontKey (const OpenGl_AspectText& theAspect,
552 const Standard_Integer theHeight)
553{
554 const Font_FontAspect anAspect = (theAspect.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : Font_FA_Regular;
555 return theAspect.FontName()
556 + TCollection_AsciiString(":") + Standard_Integer(anAspect)
557 + TCollection_AsciiString(":") + theHeight;
558}
559
560// =======================================================================
561// function : FindFont
562// purpose :
563// =======================================================================
564Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx,
565 const OpenGl_AspectText& theAspect,
566 const Standard_Integer theHeight,
567 const TCollection_AsciiString theKey)
568{
569 Handle(OpenGl_Font) aFont;
570 if (theHeight < 2)
2166f0fa 571 {
a174a3c5 572 return aFont; // invalid parameters
573 }
2166f0fa 574
a174a3c5 575 if (!theCtx->GetResource (theKey, aFont))
576 {
577 Handle(Font_FontMgr) aFontMgr = Font_FontMgr::GetInstance();
578 const Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (theAspect.FontName());
579 const Font_FontAspect anAspect = (theAspect.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : Font_FA_Regular;
580 Handle(Font_SystemFont) aRequestedFont = aFontMgr->FindFont (aFontName, anAspect, theHeight);
581 if (aRequestedFont.IsNull())
2166f0fa 582 {
a174a3c5 583 return aFont;
584 }
585
586 Handle(Font_FTFont) aFontFt = new Font_FTFont (NULL);
587 if (!aFontFt->Init (aRequestedFont->FontPath()->ToCString(), theHeight))
588 {
589 return aFont;
590 }
591
592 Handle(OpenGl_Context) aCtx = theCtx;
ca3c13d1 593 #if !defined(GL_ES_VERSION_2_0)
a174a3c5 594 glPushAttrib (GL_TEXTURE_BIT);
ca3c13d1 595 #endif
a174a3c5 596 aFont = new OpenGl_Font (aFontFt, theKey);
597 if (!aFont->Init (aCtx))
598 {
599 //glPopAttrib();
600 //return aFont; // out of resources?
601 }
ca3c13d1 602 #if !defined(GL_ES_VERSION_2_0)
a174a3c5 603 glPopAttrib(); // texture bit
ca3c13d1 604 #endif
a174a3c5 605
606 aCtx->ShareResource (theKey, aFont);
607 }
608 return aFont;
609}
610
611// =======================================================================
612// function : render
613// purpose :
614// =======================================================================
615void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
616 const Handle(OpenGl_Context)& theCtx,
617 const OpenGl_AspectText& theTextAspect,
618 const TEL_COLOUR& theColorText,
619 const TEL_COLOUR& theColorSubs) const
620{
621 if (myString.IsEmpty())
622 {
623 return;
624 }
625
626 const TCollection_AsciiString aFontKey = FontKey (theTextAspect, myParams.Height);
627 if (!myFont.IsNull()
628 && !myFont->ResourceKey().IsEqual (aFontKey))
629 {
630 // font changed
10b9c7df 631 const_cast<OpenGl_Text* > (this)->Release (theCtx.operator->());
a174a3c5 632 }
633
634 if (myFont.IsNull())
635 {
636 myFont = FindFont (theCtx, theTextAspect, myParams.Height, aFontKey);
637 if (myFont.IsNull())
638 {
639 return;
640 }
641 }
642
643 if (myTextures.IsEmpty())
644 {
645 OpenGl_TextFormatter aFormatter;
646 aFormatter.SetupAlignment (myParams.HAlign, myParams.VAlign);
647 aFormatter.Reset();
648 aFormatter.Append (theCtx, myString, *myFont.operator->());
649 aFormatter.Format();
650
7d3e64ef 651 aFormatter.Result (theCtx, myTextures, myVertsVbo, myTCrdsVbo);
a174a3c5 652 aFormatter.BndBox (myBndBox);
653 }
654
655 if (myTextures.IsEmpty())
656 {
657 return;
658 }
659
660 myExportHeight = 1.0f;
661 myScaleHeight = 1.0f;
662
ca3c13d1 663#if !defined(GL_ES_VERSION_2_0)
a174a3c5 664 glMatrixMode (GL_MODELVIEW);
665 glPushMatrix();
666 if (!myIs2d)
667 {
668 // retrieve active matrices for project/unproject calls
669 glGetDoublev (GL_MODELVIEW_MATRIX, myModelMatrix);
670 glGetDoublev (GL_PROJECTION_MATRIX, myProjMatrix);
671 glGetIntegerv (GL_VIEWPORT, myViewport);
672 gluProject (myPoint.x(), myPoint.y(), myPoint.z(),
673 myModelMatrix, myProjMatrix, myViewport,
674 &myWinX, &myWinY, &myWinZ);
675
676 // compute scale factor for constant text height
677 GLdouble x1, y1, z1;
678 gluUnProject (myWinX, myWinY, myWinZ,
679 (GLdouble* )THE_IDENTITY_MATRIX, myProjMatrix, myViewport,
680 &x1, &y1, &z1);
681
682 GLdouble x2, y2, z2;
683 const GLdouble h = (GLdouble )myFont->FTFont()->PointSize();
353474f0 684 gluUnProject (myWinX, myWinY + h, myWinZ,
a174a3c5 685 (GLdouble* )THE_IDENTITY_MATRIX, myProjMatrix, myViewport,
686 &x2, &y2, &z2);
687
688 myScaleHeight = (y2 - y1) / h;
689 if (theTextAspect.IsZoomable())
690 {
691 myExportHeight = (float )h;
692 }
693 }
694 myExportHeight = (float )myFont->FTFont()->PointSize() / myExportHeight;
695
696 // push enabled flags to the stack
697 glPushAttrib (GL_ENABLE_BIT);
5322131b 698 glDisable (GL_LIGHTING);
a174a3c5 699
700 // setup depth test
701 if (!myIs2d
702 && theTextAspect.StyleType() != Aspect_TOST_ANNOTATION)
703 {
704 glEnable (GL_DEPTH_TEST);
705 }
706 else
707 {
708 glDisable (GL_DEPTH_TEST);
709 }
710
ca3c13d1 711
a174a3c5 712 // setup alpha test
713 GLint aTexEnvParam = GL_REPLACE;
714 glGetTexEnviv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &aTexEnvParam);
715 if (aTexEnvParam != GL_REPLACE)
716 {
717 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
718 }
719 glAlphaFunc (GL_GEQUAL, 0.285f);
720 glEnable (GL_ALPHA_TEST);
721
722 // setup blending
723 glEnable (GL_BLEND);
724 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
725
726 // activate texture unit
727 glDisable (GL_TEXTURE_1D);
728 glEnable (GL_TEXTURE_2D);
01ca42b2 729 if (theCtx->core15fwd != NULL)
a174a3c5 730 {
01ca42b2 731 theCtx->core15fwd->glActiveTexture (GL_TEXTURE0);
a174a3c5 732 }
733
25ef750e 734 // unbind current OpenGL sampler
735 const Handle(OpenGl_Sampler)& aSampler = theCtx->TextureSampler();
736 if (!aSampler.IsNull() && aSampler->IsValid())
737 {
738 aSampler->Unbind (*theCtx);
739 }
740
a174a3c5 741 // extra drawings
742 switch (theTextAspect.DisplayType())
743 {
2166f0fa 744 case Aspect_TODT_BLEND:
a174a3c5 745 {
746 glEnable (GL_COLOR_LOGIC_OP);
747 glLogicOp (GL_XOR);
2166f0fa 748 break;
a174a3c5 749 }
2166f0fa
SK
750 case Aspect_TODT_SUBTITLE:
751 {
ca3c13d1 752 theCtx->core11->glColor3fv (theColorSubs.rgb);
a174a3c5 753 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
754
755 glBindTexture (GL_TEXTURE_2D, 0);
756 glBegin (GL_QUADS);
757 glVertex2f (myBndBox.Left, myBndBox.Top);
758 glVertex2f (myBndBox.Right, myBndBox.Top);
759 glVertex2f (myBndBox.Right, myBndBox.Bottom);
760 glVertex2f (myBndBox.Left, myBndBox.Bottom);
2166f0fa
SK
761 glEnd();
762 break;
763 }
2166f0fa 764 case Aspect_TODT_DEKALE:
a174a3c5 765 {
ca3c13d1 766 theCtx->core11->glColor3fv (theColorSubs.rgb);
a174a3c5 767 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.00001f));
768 drawText (thePrintCtx, theCtx, theTextAspect);
769 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, -1.0f, 0.00001f));
770 drawText (thePrintCtx, theCtx, theTextAspect);
771 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, +1.0f, 0.00001f));
772 drawText (thePrintCtx, theCtx, theTextAspect);
773 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.00001f));
774 drawText (thePrintCtx, theCtx, theTextAspect);
775 break;
776 }
a6eb515f 777 case Aspect_TODT_DIMENSION:
a174a3c5 778 case Aspect_TODT_NORMAL:
779 {
2166f0fa
SK
780 break;
781 }
782 }
783
a174a3c5 784 // main draw call
ca3c13d1 785 theCtx->core11->glColor3fv (theColorText.rgb);
a174a3c5 786 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f));
787 drawText (thePrintCtx, theCtx, theTextAspect);
2166f0fa 788
a174a3c5 789 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aTexEnvParam);
543f0db0 790
791 if (theTextAspect.DisplayType() == Aspect_TODT_DIMENSION)
792 {
793 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
794
795 glDisable (GL_BLEND);
796 glDisable (GL_TEXTURE_2D);
797 glDisable (GL_ALPHA_TEST);
798 if (!myIs2d)
799 {
800 glDisable (GL_DEPTH_TEST);
801 }
802 glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
803
804 glClear (GL_STENCIL_BUFFER_BIT);
805 glEnable (GL_STENCIL_TEST);
806 glStencilFunc (GL_ALWAYS, 1, 0xFF);
807 glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
808
809 glBegin (GL_QUADS);
810 glVertex2f (myBndBox.Left, myBndBox.Top);
811 glVertex2f (myBndBox.Right, myBndBox.Top);
812 glVertex2f (myBndBox.Right, myBndBox.Bottom);
813 glVertex2f (myBndBox.Left, myBndBox.Bottom);
814 glEnd();
815
816 glStencilFunc (GL_ALWAYS, 0, 0xFF);
817 // glPopAttrib() will reset state for us
818 //glDisable (GL_STENCIL_TEST);
819 //if (!myIs2d) glEnable (GL_DEPTH_TEST);
820
821 glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
822 }
823
824 // revert OpenGL state
a174a3c5 825 glPopAttrib(); // enable bit
826 glPopMatrix(); // model view matrix was modified
25ef750e 827
828 // revert custom OpenGL sampler
829 if (!aSampler.IsNull() && aSampler->IsValid())
830 {
831 aSampler->Bind (*theCtx);
832 }
ca3c13d1 833#endif
5e27df78 834}