1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 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_Aspects.hxx>
17 #include <OpenGl_GlCore11.hxx>
18 #include <OpenGl_GraphicDriver.hxx>
19 #include <OpenGl_ShaderManager.hxx>
20 #include <OpenGl_ShaderProgram.hxx>
21 #include <OpenGl_ShaderStates.hxx>
22 #include <OpenGl_Text.hxx>
23 #include <OpenGl_Workspace.hxx>
24 #include <OpenGl_View.hxx>
25 #include <OpenGl_VertexBufferCompat.hxx>
27 #include <Font_FontMgr.hxx>
28 #include <Font_FTFont.hxx>
29 #include <Graphic3d_TransformUtils.hxx>
30 #include <TCollection_HAsciiString.hxx>
34 static const OpenGl_Mat4d THE_IDENTITY_MATRIX;
36 static const TCollection_AsciiString THE_DEFAULT_FONT (Font_NOF_ASCII_MONO);
38 //! Auxiliary tool for setting polygon offset temporarily.
39 struct BackPolygonOffsetSentry
41 BackPolygonOffsetSentry (const Handle(OpenGl_Context)& theCtx)
43 myOffsetBack (!theCtx.IsNull() ? theCtx->PolygonOffset() : Graphic3d_PolygonOffset())
47 Graphic3d_PolygonOffset aPolyOffset = myOffsetBack;
48 aPolyOffset.Mode = Aspect_POM_Fill;
49 aPolyOffset.Units += 1.0f;
50 theCtx->SetPolygonOffset (aPolyOffset);
54 ~BackPolygonOffsetSentry()
58 myCtx->SetPolygonOffset (myOffsetBack);
63 BackPolygonOffsetSentry (const BackPolygonOffsetSentry& );
64 BackPolygonOffsetSentry& operator= (const BackPolygonOffsetSentry& );
66 const Handle(OpenGl_Context)& myCtx;
67 const Graphic3d_PolygonOffset myOffsetBack;
70 } // anonymous namespace
72 // =======================================================================
73 // function : OpenGl_Text
75 // =======================================================================
76 OpenGl_Text::OpenGl_Text()
77 : myScaleHeight (1.0f),
78 myPoint (0.0f, 0.0f, 0.0f),
81 myHasAnchorPoint (true)
84 myParams.HAlign = Graphic3d_HTA_LEFT;
85 myParams.VAlign = Graphic3d_VTA_BOTTOM;
88 // =======================================================================
89 // function : OpenGl_Text
91 // =======================================================================
92 OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText,
93 const OpenGl_Vec3& thePoint,
94 const OpenGl_TextParam& theParams)
95 : myScaleHeight (1.0f),
96 myExportHeight (1.0f),
102 myHasAnchorPoint (true)
107 // =======================================================================
108 // function : OpenGl_Text
110 // =======================================================================
111 OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText,
112 const gp_Ax2& theOrientation,
113 const OpenGl_TextParam& theParams,
114 const bool theHasOwnAnchor)
115 : myScaleHeight (1.0),
116 myExportHeight (1.0),
117 myParams (theParams),
120 myOrientation (theOrientation),
122 myHasAnchorPoint (theHasOwnAnchor)
124 const gp_Pnt& aPoint = theOrientation.Location();
125 myPoint = OpenGl_Vec3 (static_cast<Standard_ShortReal> (aPoint.X()),
126 static_cast<Standard_ShortReal> (aPoint.Y()),
127 static_cast<Standard_ShortReal> (aPoint.Z()));
130 // =======================================================================
131 // function : SetPosition
133 // =======================================================================
134 void OpenGl_Text::SetPosition (const OpenGl_Vec3& thePoint)
139 // =======================================================================
140 // function : SetFontSize
142 // =======================================================================
143 void OpenGl_Text::SetFontSize (const Handle(OpenGl_Context)& theCtx,
144 const Standard_Integer theFontSize)
146 if (myParams.Height != theFontSize)
148 Release (theCtx.operator->());
150 myParams.Height = theFontSize;
153 // =======================================================================
156 // =======================================================================
157 void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
158 const Standard_Utf8Char* theText,
159 const OpenGl_Vec3& thePoint)
161 releaseVbos (theCtx.operator->());
164 myString.FromUnicode (theText);
167 // =======================================================================
170 // =======================================================================
171 void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
172 const Standard_Utf8Char* theText,
173 const OpenGl_Vec3& thePoint,
174 const OpenGl_TextParam& theParams)
176 if (myParams.Height != theParams.Height)
178 Release (theCtx.operator->());
182 releaseVbos (theCtx.operator->());
185 myParams = theParams;
187 myString.FromUnicode (theText);
190 // =======================================================================
193 // =======================================================================
194 void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
195 const TCollection_ExtendedString& theText,
196 const OpenGl_Vec2& thePoint,
197 const OpenGl_TextParam& theParams)
199 if (myParams.Height != theParams.Height)
201 Release (theCtx.operator->());
205 releaseVbos (theCtx.operator->());
208 myParams = theParams;
209 myPoint.SetValues (thePoint, 0.0f);
210 myString.FromUnicode (theText.ToExtString());
213 // =======================================================================
214 // function : ~OpenGl_Text
216 // =======================================================================
217 OpenGl_Text::~OpenGl_Text()
222 // =======================================================================
223 // function : releaseVbos
225 // =======================================================================
226 void OpenGl_Text::releaseVbos (OpenGl_Context* theCtx)
228 for (Standard_Integer anIter = 0; anIter < myVertsVbo.Length(); ++anIter)
230 Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.ChangeValue (anIter);
231 Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.ChangeValue (anIter);
235 theCtx->DelayedRelease (aVerts);
236 theCtx->DelayedRelease (aTCrds);
242 && !myBndVertsVbo.IsNull())
244 theCtx->DelayedRelease (myBndVertsVbo);
250 myBndVertsVbo.Nullify();
253 // =======================================================================
254 // function : Release
256 // =======================================================================
257 void OpenGl_Text::Release (OpenGl_Context* theCtx)
259 releaseVbos (theCtx);
260 if (!myFont.IsNull())
262 const TCollection_AsciiString aKey = myFont->ResourceKey();
266 theCtx->ReleaseResource (aKey, Standard_True);
271 // =======================================================================
272 // function : StringSize
274 // =======================================================================
275 void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
276 const NCollection_String& theText,
277 const OpenGl_Aspects& theTextAspect,
278 const OpenGl_TextParam& theParams,
279 const unsigned int theResolution,
280 Standard_ShortReal& theWidth,
281 Standard_ShortReal& theAscent,
282 Standard_ShortReal& theDescent)
287 const TCollection_AsciiString aFontKey = FontKey (theTextAspect, theParams.Height, theResolution);
288 Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, theParams.Height, theResolution, aFontKey);
289 if (aFont.IsNull() || !aFont->IsValid())
294 theAscent = aFont->Ascender();
295 theDescent = aFont->Descender();
297 GLfloat aWidth = 0.0f;
298 for (NCollection_Utf8Iter anIter = theText.Iterator(); *anIter != 0;)
300 const Standard_Utf32Char aCharThis = *anIter;
301 const Standard_Utf32Char aCharNext = *++anIter;
303 if (aCharThis == '\x0D' // CR (carriage return)
304 || aCharThis == '\a' // BEL (alarm)
305 || aCharThis == '\f' // FF (form feed) NP (new page)
306 || aCharThis == '\b' // BS (backspace)
307 || aCharThis == '\v') // VT (vertical tab)
309 continue; // skip unsupported carriage control codes
311 else if (aCharThis == '\x0A') // LF (line feed, new line)
313 theWidth = Max (theWidth, aWidth);
317 else if (aCharThis == ' ')
319 aWidth += aFont->FTFont()->AdvanceX (aCharThis, aCharNext);
322 else if (aCharThis == '\t')
324 aWidth += aFont->FTFont()->AdvanceX (' ', aCharNext) * 8.0f;
328 aWidth += aFont->FTFont()->AdvanceX (aCharThis, aCharNext);
330 theWidth = Max (theWidth, aWidth);
332 Handle(OpenGl_Context) aCtx = theCtx;
334 aCtx->ReleaseResource (aFontKey, Standard_True);
337 // =======================================================================
340 // =======================================================================
341 void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
343 const OpenGl_Aspects* aTextAspect = theWorkspace->ApplyAspects();
344 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
345 const Handle(OpenGl_TextureSet) aPrevTexture = aCtx->BindTextures (Handle(OpenGl_TextureSet)());
347 // Bind custom shader program or generate default version
348 aCtx->ShaderManager()->BindFontProgram (aTextAspect->ShaderProgramRes (aCtx));
350 if (myHasPlane && myHasAnchorPoint)
352 myOrientationMatrix = theWorkspace->View()->Camera()->OrientationMatrix();
353 // reset translation part
354 myOrientationMatrix.ChangeValue (0, 3) = 0.0;
355 myOrientationMatrix.ChangeValue (1, 3) = 0.0;
356 myOrientationMatrix.ChangeValue (2, 3) = 0.0;
359 myProjMatrix.Convert (aCtx->ProjectionState.Current());
361 // use highlight color or colors from aspect
364 theWorkspace->TextColor(),
365 theWorkspace->TextSubtitleColor(),
369 if (!aPrevTexture.IsNull())
371 aCtx->BindTextures (aPrevTexture);
374 // restore Z buffer settings
375 if (theWorkspace->UseZBuffer())
377 glEnable (GL_DEPTH_TEST);
381 // =======================================================================
384 // =======================================================================
385 void OpenGl_Text::Render (const Handle(OpenGl_Context)& theCtx,
386 const OpenGl_Aspects& theTextAspect,
387 unsigned int theResolution) const
389 #if !defined(GL_ES_VERSION_2_0)
390 const Standard_Integer aPrevPolygonMode = theCtx->SetPolygonMode (GL_FILL);
391 const bool aPrevHatchingMode = theCtx->SetPolygonHatchEnabled (false);
394 render (theCtx, theTextAspect,
395 theTextAspect.Aspect()->ColorRGBA(),
396 theTextAspect.Aspect()->ColorSubTitleRGBA(),
399 #if !defined(GL_ES_VERSION_2_0)
400 theCtx->SetPolygonMode (aPrevPolygonMode);
401 theCtx->SetPolygonHatchEnabled (aPrevHatchingMode);
405 // =======================================================================
406 // function : setupMatrix
408 // =======================================================================
409 void OpenGl_Text::setupMatrix (const Handle(OpenGl_Context)& theCtx,
410 const OpenGl_Aspects& theTextAspect,
411 const OpenGl_Vec3& theDVec) const
413 OpenGl_Mat4d aModViewMat, aProjectMat;
414 if (myHasPlane && myHasAnchorPoint)
416 aProjectMat = myProjMatrix * myOrientationMatrix;
420 aProjectMat = myProjMatrix;
425 Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, myPoint.x() + theDVec.x(), myPoint.y() + theDVec.y(), 0.f);
426 Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, 1.f, -1.f, 1.f);
427 Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->TextAngle(), 0.f, 0.f, 1.f);
431 OpenGl_Vec3d anObjXYZ;
432 OpenGl_Vec3d aWinXYZ = myWinXYZ + OpenGl_Vec3d (theDVec);
433 if (!myHasPlane && !theTextAspect.Aspect()->IsTextZoomable())
435 // Align coordinates to the nearest integer to avoid extra interpolation issues.
436 // Note that for better readability we could also try aligning freely rotated in 3D text (myHasPlane),
437 // when camera orientation co-aligned with horizontal text orientation,
438 // but this might look awkward while rotating camera.
439 aWinXYZ.x() = Floor (aWinXYZ.x());
440 aWinXYZ.y() = Floor (aWinXYZ.y());
442 Graphic3d_TransformUtils::UnProject<Standard_Real> (aWinXYZ.x(), aWinXYZ.y(), aWinXYZ.z(),
443 THE_IDENTITY_MATRIX, aProjectMat, theCtx->Viewport(),
444 anObjXYZ.x(), anObjXYZ.y(), anObjXYZ.z());
448 const gp_Dir& aVectorDir = myOrientation.XDirection();
449 const gp_Dir& aVectorUp = myOrientation.Direction();
450 const gp_Dir& aVectorRight = myOrientation.YDirection();
452 aModViewMat.SetColumn (2, OpenGl_Vec3d (aVectorUp.X(), aVectorUp.Y(), aVectorUp.Z()));
453 aModViewMat.SetColumn (1, OpenGl_Vec3d (aVectorRight.X(), aVectorRight.Y(), aVectorRight.Z()));
454 aModViewMat.SetColumn (0, OpenGl_Vec3d (aVectorDir.X(), aVectorDir.Y(), aVectorDir.Z()));
456 if (!myHasAnchorPoint)
458 OpenGl_Mat4d aPosMat;
459 aPosMat.SetColumn (3, OpenGl_Vec3d (myPoint.x(), myPoint.y(), myPoint.z()));
460 aPosMat *= aModViewMat;
461 aModViewMat.SetColumn (3, aPosMat.GetColumn (3));
465 aModViewMat.SetColumn (3, anObjXYZ);
470 Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, anObjXYZ.x(), anObjXYZ.y(), anObjXYZ.z());
471 Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->TextAngle(), 0.0, 0.0, 1.0);
474 if (!theTextAspect.Aspect()->IsTextZoomable())
476 Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, myScaleHeight, myScaleHeight, myScaleHeight);
478 else if (theCtx->HasRenderScale())
480 Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, theCtx->RenderScaleInv(), theCtx->RenderScaleInv(), theCtx->RenderScaleInv());
484 if (myHasPlane && !myHasAnchorPoint)
486 OpenGl_Mat4d aCurrentWorldViewMat;
487 aCurrentWorldViewMat.Convert (theCtx->WorldViewState.Current());
488 theCtx->WorldViewState.SetCurrent<Standard_Real> (aCurrentWorldViewMat * aModViewMat);
492 theCtx->WorldViewState.SetCurrent<Standard_Real> (aModViewMat);
494 theCtx->ApplyWorldViewMatrix();
498 theCtx->ProjectionState.SetCurrent<Standard_Real> (aProjectMat);
499 theCtx->ApplyProjectionMatrix();
502 // Upload updated state to shader program
503 theCtx->ShaderManager()->PushState (theCtx->ActiveProgram());
506 // =======================================================================
507 // function : drawText
509 // =======================================================================
510 void OpenGl_Text::drawText (const Handle(OpenGl_Context)& theCtx,
511 const OpenGl_Aspects& theTextAspect) const
513 (void )theTextAspect;
514 if (myVertsVbo.Length() != myTextures.Length()
515 || myTextures.IsEmpty())
520 for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
522 const GLuint aTexId = myTextures.Value (anIter);
523 glBindTexture (GL_TEXTURE_2D, aTexId);
525 const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter);
526 const Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.Value (anIter);
527 aVerts->BindAttribute (theCtx, Graphic3d_TOA_POS);
528 aTCrds->BindAttribute (theCtx, Graphic3d_TOA_UV);
530 glDrawArrays (GL_TRIANGLES, 0, GLsizei(aVerts->GetElemsNb()));
532 aTCrds->UnbindAttribute (theCtx, Graphic3d_TOA_UV);
533 aVerts->UnbindAttribute (theCtx, Graphic3d_TOA_POS);
535 glBindTexture (GL_TEXTURE_2D, 0);
538 // =======================================================================
539 // function : FontKey
541 // =======================================================================
542 TCollection_AsciiString OpenGl_Text::FontKey (const OpenGl_Aspects& theAspect,
543 Standard_Integer theHeight,
544 unsigned int theResolution)
546 const Font_FontAspect anAspect = theAspect.Aspect()->TextFontAspect() != Font_FA_Undefined
547 ? theAspect.Aspect()->TextFontAspect()
549 const TCollection_AsciiString& aFont = !theAspect.Aspect()->TextFont().IsNull() ? theAspect.Aspect()->TextFont()->String() : THE_DEFAULT_FONT;
551 + TCollection_AsciiString(":") + Standard_Integer(anAspect)
552 + TCollection_AsciiString(":") + Standard_Integer(theResolution)
553 + TCollection_AsciiString(":") + theHeight;
556 // =======================================================================
557 // function : FindFont
559 // =======================================================================
560 Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx,
561 const OpenGl_Aspects& theAspect,
562 Standard_Integer theHeight,
563 unsigned int theResolution,
564 const TCollection_AsciiString& theKey)
566 Handle(OpenGl_Font) aFont;
569 return aFont; // invalid parameters
572 if (!theCtx->GetResource (theKey, aFont))
574 Handle(Font_FontMgr) aFontMgr = Font_FontMgr::GetInstance();
575 const TCollection_AsciiString& aFontName = !theAspect.Aspect()->TextFont().IsNull()
576 ? theAspect.Aspect()->TextFont()->String()
578 Font_FontAspect anAspect = theAspect.Aspect()->TextFontAspect() != Font_FA_Undefined
579 ? theAspect.Aspect()->TextFontAspect()
581 Font_FTFontParams aParams;
582 aParams.PointSize = theHeight;
583 aParams.Resolution = theResolution;
584 if (Handle(Font_FTFont) aFontFt = Font_FTFont::FindAndCreate (aFontName, anAspect, aParams, Font_StrictLevel_Any))
586 aFont = new OpenGl_Font (aFontFt, theKey);
587 if (!aFont->Init (theCtx))
589 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
590 TCollection_AsciiString ("Font '") + aFontName + "' - initialization of GL resources has failed!");
592 aFont->Release (theCtx.get());
593 aFont = new OpenGl_Font (aFontFt, theKey);
598 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
599 TCollection_AsciiString ("Font '") + aFontName + "' is not found in the system!");
600 aFont = new OpenGl_Font (aFontFt, theKey);
603 theCtx->ShareResource (theKey, aFont);
608 // =======================================================================
609 // function : drawRect
611 // =======================================================================
612 void OpenGl_Text::drawRect (const Handle(OpenGl_Context)& theCtx,
613 const OpenGl_Aspects& theTextAspect,
614 const OpenGl_Vec4& theColorSubs) const
616 Handle(OpenGl_ShaderProgram) aPrevProgram = theCtx->ActiveProgram();
617 if (myBndVertsVbo.IsNull())
619 OpenGl_Vec2 aQuad[4] =
621 OpenGl_Vec2(myBndBox.Right, myBndBox.Bottom),
622 OpenGl_Vec2(myBndBox.Right, myBndBox.Top),
623 OpenGl_Vec2(myBndBox.Left, myBndBox.Bottom),
624 OpenGl_Vec2(myBndBox.Left, myBndBox.Top)
626 if (theCtx->ToUseVbo())
628 myBndVertsVbo = new OpenGl_VertexBuffer();
632 myBndVertsVbo = new OpenGl_VertexBufferCompat();
634 myBndVertsVbo->Init (theCtx, 2, 4, aQuad[0].GetData());
637 // bind unlit program
638 theCtx->ShaderManager()->BindFaceProgram (Handle(OpenGl_TextureSet)(), Graphic3d_TOSM_UNLIT,
639 Graphic3d_AlphaMode_Opaque, Standard_False, Standard_False,
640 Handle(OpenGl_ShaderProgram)());
642 #if !defined(GL_ES_VERSION_2_0)
643 if (theCtx->core11 != NULL
644 && theCtx->ActiveProgram().IsNull())
646 glBindTexture (GL_TEXTURE_2D, 0);
649 theCtx->SetColor4fv (theColorSubs);
650 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f));
651 myBndVertsVbo->BindAttribute (theCtx, Graphic3d_TOA_POS);
653 theCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
655 myBndVertsVbo->UnbindAttribute (theCtx, Graphic3d_TOA_POS);
656 theCtx->BindProgram (aPrevProgram);
659 // =======================================================================
662 // =======================================================================
663 void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
664 const OpenGl_Aspects& theTextAspect,
665 const OpenGl_Vec4& theColorText,
666 const OpenGl_Vec4& theColorSubs,
667 unsigned int theResolution) const
669 if (myString.IsEmpty())
674 // Note that using difference resolution in different Views in same Viewer
675 // will lead to performance regression (for example, text will be recreated every time).
676 const TCollection_AsciiString aFontKey = FontKey (theTextAspect, myParams.Height, theResolution);
678 && !myFont->ResourceKey().IsEqual (aFontKey))
681 const_cast<OpenGl_Text* > (this)->Release (theCtx.operator->());
686 myFont = FindFont (theCtx, theTextAspect, myParams.Height, theResolution, aFontKey);
688 if (!myFont->WasInitialized())
693 if (myTextures.IsEmpty())
695 Font_TextFormatter aFormatter;
696 aFormatter.SetupAlignment (myParams.HAlign, myParams.VAlign);
699 aFormatter.Append (myString, *myFont->FTFont().operator->());
702 OpenGl_TextBuilder aBuilder;
703 aBuilder.Perform (aFormatter,
705 *myFont.operator->(),
710 aFormatter.BndBox (myBndBox);
711 if (!myBndVertsVbo.IsNull())
713 myBndVertsVbo->Release (theCtx.get());
714 myBndVertsVbo.Nullify();
718 if (myTextures.IsEmpty())
723 myExportHeight = 1.0f;
724 myScaleHeight = 1.0f;
726 theCtx->WorldViewState.Push();
727 myModelMatrix.Convert (theCtx->WorldViewState.Current() * theCtx->ModelWorldState.Current());
729 const GLdouble aPointSize = (GLdouble )myFont->FTFont()->PointSize();
732 Graphic3d_TransformUtils::Project<Standard_Real> (myPoint.x(), myPoint.y(), myPoint.z(),
733 myModelMatrix, myProjMatrix, theCtx->Viewport(),
734 myWinXYZ.x(), myWinXYZ.y(), myWinXYZ.z());
736 // compute scale factor for constant text height
737 if (theTextAspect.Aspect()->IsTextZoomable())
739 myExportHeight = aPointSize;
743 Graphic3d_Vec3d aPnt1, aPnt2;
744 Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinXYZ.x(), myWinXYZ.y(), myWinXYZ.z(),
745 THE_IDENTITY_MATRIX, myProjMatrix, theCtx->Viewport(),
746 aPnt1.x(), aPnt1.y(), aPnt1.z());
747 Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinXYZ.x(), myWinXYZ.y() + aPointSize, myWinXYZ.z(),
748 THE_IDENTITY_MATRIX, myProjMatrix, theCtx->Viewport(),
749 aPnt2.x(), aPnt2.y(), aPnt2.z());
750 myScaleHeight = (aPnt2.y() - aPnt1.y()) / aPointSize;
753 myExportHeight = aPointSize / myExportHeight;
755 #if !defined(GL_ES_VERSION_2_0)
756 if (theCtx->core11 != NULL
757 && theCtx->caps->ffpEnable)
759 glDisable (GL_LIGHTING);
764 const bool hasDepthTest = !myIs2d
765 && theTextAspect.Aspect()->TextStyle() != Aspect_TOST_ANNOTATION;
768 glDisable (GL_DEPTH_TEST);
771 if (theCtx->core15fwd != NULL)
773 theCtx->core15fwd->glActiveTexture (GL_TEXTURE0);
775 #if !defined(GL_ES_VERSION_2_0)
776 // activate texture unit
777 GLint aTexEnvParam = GL_REPLACE;
778 if (theCtx->core11 != NULL)
780 glDisable (GL_TEXTURE_1D);
781 glEnable (GL_TEXTURE_2D);
782 glGetTexEnviv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &aTexEnvParam);
783 if (aTexEnvParam != GL_REPLACE)
785 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
792 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
794 // alpha to coverage makes text too thin
795 theCtx->SetSampleAlphaToCoverage (false);
798 switch (theTextAspect.Aspect()->TextDisplayType())
800 case Aspect_TODT_BLEND:
802 #if !defined(GL_ES_VERSION_2_0)
803 glEnable (GL_COLOR_LOGIC_OP);
808 case Aspect_TODT_SUBTITLE:
810 BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx : Handle(OpenGl_Context)());
811 drawRect (theCtx, theTextAspect, theColorSubs);
814 case Aspect_TODT_DEKALE:
816 BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx : Handle(OpenGl_Context)());
817 theCtx->SetColor4fv (theColorSubs);
818 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.0f));
819 drawText (theCtx, theTextAspect);
820 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, -1.0f, 0.0f));
821 drawText (theCtx, theTextAspect);
822 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, +1.0f, 0.0f));
823 drawText (theCtx, theTextAspect);
824 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.0f));
825 drawText (theCtx, theTextAspect);
828 case Aspect_TODT_SHADOW:
830 BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx : Handle(OpenGl_Context)());
831 theCtx->SetColor4fv (theColorSubs);
832 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.0f));
833 drawText (theCtx, theTextAspect);
836 case Aspect_TODT_DIMENSION:
837 case Aspect_TODT_NORMAL:
844 theCtx->SetColor4fv (theColorText);
845 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f));
846 drawText (theCtx, theTextAspect);
850 theCtx->ProjectionState.SetCurrent<Standard_Real> (myProjMatrix);
851 theCtx->ApplyProjectionMatrix();
854 #if !defined(GL_ES_VERSION_2_0)
855 if (theCtx->core11 != NULL)
857 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aTexEnvParam);
861 if (theTextAspect.Aspect()->TextDisplayType() == Aspect_TODT_DIMENSION)
863 glDisable (GL_BLEND);
866 glDisable (GL_DEPTH_TEST);
868 #if !defined(GL_ES_VERSION_2_0)
869 if (theCtx->core11 != NULL)
871 glDisable (GL_TEXTURE_2D);
874 const bool aColorMaskBack = theCtx->SetColorMask (false);
876 glClear (GL_STENCIL_BUFFER_BIT);
877 glEnable (GL_STENCIL_TEST);
878 glStencilFunc (GL_ALWAYS, 1, 0xFF);
879 glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
881 drawRect (theCtx, theTextAspect, OpenGl_Vec4 (1.0f, 1.0f, 1.0f, 1.0f));
883 glStencilFunc (GL_ALWAYS, 0, 0xFF);
885 theCtx->SetColorMask (aColorMaskBack);
888 // reset OpenGL state
889 glDisable (GL_BLEND);
890 glDisable (GL_STENCIL_TEST);
891 #if !defined(GL_ES_VERSION_2_0)
892 glDisable (GL_COLOR_LOGIC_OP);
895 // model view matrix was modified
896 theCtx->WorldViewState.Pop();
897 theCtx->ApplyModelViewMatrix();