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 (OpenGl_Context* theCtx)
46 myOffsetBack = theCtx->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 OpenGl_Context* myCtx;
67 Graphic3d_PolygonOffset myOffsetBack;
70 } // anonymous namespace
72 // =======================================================================
73 // function : OpenGl_Text
75 // =======================================================================
76 OpenGl_Text::OpenGl_Text()
77 : myScaleHeight (1.0f),
78 myIs2d (Standard_False)
80 myText = new Graphic3d_Text (10.);
84 // =======================================================================
85 // function : OpenGl_Text
87 // =======================================================================
88 OpenGl_Text::OpenGl_Text (const Handle(Graphic3d_Text)& theTextParams)
89 : myText (theTextParams),
91 myIs2d (Standard_False)
95 // =======================================================================
96 // function : SetPosition
98 // =======================================================================
99 void OpenGl_Text::SetPosition (const OpenGl_Vec3& thePoint)
101 myText->SetPosition (gp_Pnt (thePoint.x(), thePoint.y(), thePoint.z()));
104 // =======================================================================
105 // function : SetFontSize
107 // =======================================================================
108 void OpenGl_Text::SetFontSize (const Handle(OpenGl_Context)& theCtx,
109 const Standard_Integer theFontSize)
111 if (myText->Height() != theFontSize)
113 Release (theCtx.operator->());
115 myText->SetHeight ((Standard_ShortReal)theFontSize);
118 // =======================================================================
121 // =======================================================================
122 void OpenGl_Text::Reset (const Handle(OpenGl_Context)& theCtx)
124 if (!myFont.IsNull() && myFont->FTFont()->PointSize() != myText->Height())
126 Release (theCtx.operator->());
130 releaseVbos (theCtx.operator->());
134 // =======================================================================
137 // =======================================================================
138 void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
139 const Standard_Utf8Char* theText,
140 const OpenGl_Vec3& thePoint)
143 Set2D (Standard_False);
145 NCollection_String aText;
146 aText.FromUnicode (theText);
147 myText->SetText (aText);
148 myText->SetPosition (gp_Pnt (thePoint.x(), thePoint.y(), thePoint.z()));
151 // =======================================================================
152 // function : ~OpenGl_Text
154 // =======================================================================
155 OpenGl_Text::~OpenGl_Text()
160 // =======================================================================
161 // function : releaseVbos
163 // =======================================================================
164 void OpenGl_Text::releaseVbos (OpenGl_Context* theCtx)
166 for (Standard_Integer anIter = 0; anIter < myVertsVbo.Length(); ++anIter)
168 Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.ChangeValue (anIter);
169 Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.ChangeValue (anIter);
173 theCtx->DelayedRelease (aVerts);
174 theCtx->DelayedRelease (aTCrds);
180 && !myBndVertsVbo.IsNull())
182 theCtx->DelayedRelease (myBndVertsVbo);
188 myBndVertsVbo.Nullify();
191 // =======================================================================
192 // function : Release
194 // =======================================================================
195 void OpenGl_Text::Release (OpenGl_Context* theCtx)
197 releaseVbos (theCtx);
198 if (!myFont.IsNull())
200 const TCollection_AsciiString aKey = myFont->ResourceKey();
204 theCtx->ReleaseResource (aKey, Standard_True);
209 // =======================================================================
210 // function : EstimatedDataSize
212 // =======================================================================
213 Standard_Size OpenGl_Text::EstimatedDataSize() const
215 Standard_Size aSize = 0;
216 for (Standard_Integer anIter = myVertsVbo.Lower(); anIter <= myVertsVbo.Upper(); ++anIter)
218 if (const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter))
220 aSize += aVerts->EstimatedDataSize();
222 if (const Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.Value (anIter))
224 aSize += aTCrds->EstimatedDataSize();
227 if (!myBndVertsVbo.IsNull())
229 aSize += myBndVertsVbo->EstimatedDataSize();
234 // =======================================================================
235 // function : UpdateDrawStats
237 // =======================================================================
238 void OpenGl_Text::UpdateDrawStats (Graphic3d_FrameStatsDataTmp& theStats,
239 bool theIsDetailed) const
241 ++theStats[Graphic3d_FrameStatsCounter_NbElemsNotCulled];
242 ++theStats[Graphic3d_FrameStatsCounter_NbElemsTextNotCulled];
245 for (Standard_Integer anIter = myVertsVbo.Lower(); anIter <= myVertsVbo.Upper(); ++anIter)
247 if (const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter))
249 theStats[Graphic3d_FrameStatsCounter_NbTrianglesNotCulled] += aVerts->GetElemsNb() / 3; // 2 non-indexed triangles per glyph
255 // =======================================================================
256 // function : StringSize
258 // =======================================================================
259 void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
260 const NCollection_String& theText,
261 const OpenGl_Aspects& theTextAspect,
262 const Standard_ShortReal theHeight,
263 const unsigned int theResolution,
264 Standard_ShortReal& theWidth,
265 Standard_ShortReal& theAscent,
266 Standard_ShortReal& theDescent)
271 const TCollection_AsciiString aFontKey = FontKey (theTextAspect, (Standard_Integer)theHeight, theResolution);
272 Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, (Standard_Integer)theHeight, theResolution, aFontKey);
273 if (aFont.IsNull() || !aFont->IsValid())
278 theAscent = aFont->Ascender();
279 theDescent = aFont->Descender();
281 GLfloat aWidth = 0.0f;
282 for (NCollection_Utf8Iter anIter = theText.Iterator(); *anIter != 0;)
284 const Standard_Utf32Char aCharThis = *anIter;
285 const Standard_Utf32Char aCharNext = *++anIter;
287 if (aCharThis == '\x0D' // CR (carriage return)
288 || aCharThis == '\a' // BEL (alarm)
289 || aCharThis == '\f' // FF (form feed) NP (new page)
290 || aCharThis == '\b' // BS (backspace)
291 || aCharThis == '\v') // VT (vertical tab)
293 continue; // skip unsupported carriage control codes
295 else if (aCharThis == '\x0A') // LF (line feed, new line)
297 theWidth = Max (theWidth, aWidth);
301 else if (aCharThis == ' ')
303 aWidth += aFont->FTFont()->AdvanceX (aCharThis, aCharNext);
306 else if (aCharThis == '\t')
308 aWidth += aFont->FTFont()->AdvanceX (' ', aCharNext) * 8.0f;
312 aWidth += aFont->FTFont()->AdvanceX (aCharThis, aCharNext);
314 theWidth = Max (theWidth, aWidth);
316 Handle(OpenGl_Context) aCtx = theCtx;
318 aCtx->ReleaseResource (aFontKey, Standard_True);
321 // =======================================================================
324 // =======================================================================
325 void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
327 const OpenGl_Aspects* aTextAspect = theWorkspace->ApplyAspects (false); // do not bind textures as they will be disabled anyway
328 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
330 // Bind custom shader program or generate default version
331 aCtx->ShaderManager()->BindFontProgram (aTextAspect->ShaderProgramRes (aCtx));
332 const Handle(OpenGl_TextureSet) aPrevTexture = aCtx->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
334 if (myText->HasPlane() && myText->HasOwnAnchorPoint())
336 myOrientationMatrix = theWorkspace->View()->Camera()->OrientationMatrix();
337 // reset translation part
338 myOrientationMatrix.ChangeValue (0, 3) = 0.0;
339 myOrientationMatrix.ChangeValue (1, 3) = 0.0;
340 myOrientationMatrix.ChangeValue (2, 3) = 0.0;
343 myProjMatrix.Convert (aCtx->ProjectionState.Current());
345 // use highlight color or colors from aspect
348 theWorkspace->TextColor(),
349 theWorkspace->TextSubtitleColor(),
353 if (!aPrevTexture.IsNull())
355 aCtx->BindTextures (aPrevTexture, Handle(OpenGl_ShaderProgram)());
358 // restore Z buffer settings
359 if (theWorkspace->UseZBuffer())
361 glEnable (GL_DEPTH_TEST);
365 // =======================================================================
368 // =======================================================================
369 void OpenGl_Text::Render (const Handle(OpenGl_Context)& theCtx,
370 const OpenGl_Aspects& theTextAspect,
371 unsigned int theResolution) const
373 #if !defined(GL_ES_VERSION_2_0)
374 const Standard_Integer aPrevPolygonMode = theCtx->SetPolygonMode (GL_FILL);
375 const bool aPrevHatchingMode = theCtx->SetPolygonHatchEnabled (false);
378 render (theCtx, theTextAspect,
379 theTextAspect.Aspect()->ColorRGBA(),
380 theTextAspect.Aspect()->ColorSubTitleRGBA(),
383 #if !defined(GL_ES_VERSION_2_0)
384 theCtx->SetPolygonMode (aPrevPolygonMode);
385 theCtx->SetPolygonHatchEnabled (aPrevHatchingMode);
389 // =======================================================================
390 // function : setupMatrix
392 // =======================================================================
393 void OpenGl_Text::setupMatrix (const Handle(OpenGl_Context)& theCtx,
394 const OpenGl_Aspects& theTextAspect,
395 const OpenGl_Vec3& theDVec) const
397 OpenGl_Mat4d aModViewMat, aProjectMat;
398 if (myText->HasPlane() && myText->HasOwnAnchorPoint())
400 aProjectMat = myProjMatrix * myOrientationMatrix;
404 aProjectMat = myProjMatrix;
409 const gp_Pnt& aPoint = myText->Position();
410 Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, aPoint.X() + theDVec.x(), aPoint.Y() + theDVec.y(), 0.f);
411 Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, 1.f, -1.f, 1.f);
412 Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->TextAngle(), 0.f, 0.f, 1.f);
416 OpenGl_Vec3d anObjXYZ;
417 OpenGl_Vec3d aWinXYZ = myWinXYZ + OpenGl_Vec3d (theDVec);
418 if (!myText->HasPlane() && !theTextAspect.Aspect()->IsTextZoomable())
420 // Align coordinates to the nearest integer to avoid extra interpolation issues.
421 // Note that for better readability we could also try aligning freely rotated in 3D text (myHasPlane),
422 // when camera orientation co-aligned with horizontal text orientation,
423 // but this might look awkward while rotating camera.
424 aWinXYZ.x() = Floor (aWinXYZ.x());
425 aWinXYZ.y() = Floor (aWinXYZ.y());
427 Graphic3d_TransformUtils::UnProject<Standard_Real> (aWinXYZ.x(), aWinXYZ.y(), aWinXYZ.z(),
428 THE_IDENTITY_MATRIX, aProjectMat, theCtx->Viewport(),
429 anObjXYZ.x(), anObjXYZ.y(), anObjXYZ.z());
431 if (myText->HasPlane())
433 const gp_Ax2& anOrientation = myText->Orientation();
434 const gp_Dir& aVectorDir = anOrientation.XDirection();
435 const gp_Dir& aVectorUp = anOrientation.Direction();
436 const gp_Dir& aVectorRight = anOrientation.YDirection();
438 aModViewMat.SetColumn (2, OpenGl_Vec3d (aVectorUp.X(), aVectorUp.Y(), aVectorUp.Z()));
439 aModViewMat.SetColumn (1, OpenGl_Vec3d (aVectorRight.X(), aVectorRight.Y(), aVectorRight.Z()));
440 aModViewMat.SetColumn (0, OpenGl_Vec3d (aVectorDir.X(), aVectorDir.Y(), aVectorDir.Z()));
442 if (!myText->HasOwnAnchorPoint())
444 OpenGl_Mat4d aPosMat;
445 const gp_Pnt& aPoint = myText->Position();
446 aPosMat.SetColumn (3, OpenGl_Vec3d (aPoint.X(), aPoint.Y(), aPoint.Z()));
447 aPosMat *= aModViewMat;
448 aModViewMat.SetColumn (3, aPosMat.GetColumn (3));
452 aModViewMat.SetColumn (3, anObjXYZ);
457 Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, anObjXYZ.x(), anObjXYZ.y(), anObjXYZ.z());
458 Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->TextAngle(), 0.0, 0.0, 1.0);
461 if (!theTextAspect.Aspect()->IsTextZoomable())
463 Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, myScaleHeight, myScaleHeight, myScaleHeight);
465 else if (theCtx->HasRenderScale())
467 Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, theCtx->RenderScaleInv(), theCtx->RenderScaleInv(), theCtx->RenderScaleInv());
471 if (myText->HasPlane() && !myText->HasOwnAnchorPoint())
473 OpenGl_Mat4d aCurrentWorldViewMat;
474 aCurrentWorldViewMat.Convert (theCtx->WorldViewState.Current());
475 theCtx->WorldViewState.SetCurrent<Standard_Real> (aCurrentWorldViewMat * aModViewMat);
479 theCtx->WorldViewState.SetCurrent<Standard_Real> (aModViewMat);
481 theCtx->ApplyWorldViewMatrix();
485 theCtx->ProjectionState.SetCurrent<Standard_Real> (aProjectMat);
486 theCtx->ApplyProjectionMatrix();
489 // Upload updated state to shader program
490 theCtx->ShaderManager()->PushState (theCtx->ActiveProgram());
493 // =======================================================================
494 // function : drawText
496 // =======================================================================
497 void OpenGl_Text::drawText (const Handle(OpenGl_Context)& theCtx,
498 const OpenGl_Aspects& theTextAspect) const
500 (void )theTextAspect;
501 if (myVertsVbo.Length() != myTextures.Length()
502 || myTextures.IsEmpty())
507 for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
509 const GLuint aTexId = myTextures.Value (anIter);
510 glBindTexture (GL_TEXTURE_2D, aTexId);
512 const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter);
513 const Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.Value (anIter);
514 aVerts->BindAttribute (theCtx, Graphic3d_TOA_POS);
515 aTCrds->BindAttribute (theCtx, Graphic3d_TOA_UV);
517 glDrawArrays (GL_TRIANGLES, 0, GLsizei(aVerts->GetElemsNb()));
519 aTCrds->UnbindAttribute (theCtx, Graphic3d_TOA_UV);
520 aVerts->UnbindAttribute (theCtx, Graphic3d_TOA_POS);
522 glBindTexture (GL_TEXTURE_2D, 0);
525 // =======================================================================
526 // function : FontKey
528 // =======================================================================
529 TCollection_AsciiString OpenGl_Text::FontKey (const OpenGl_Aspects& theAspect,
530 Standard_Integer theHeight,
531 unsigned int theResolution)
533 const Font_FontAspect anAspect = theAspect.Aspect()->TextFontAspect() != Font_FA_Undefined
534 ? theAspect.Aspect()->TextFontAspect()
536 const TCollection_AsciiString& aFont = !theAspect.Aspect()->TextFont().IsNull() ? theAspect.Aspect()->TextFont()->String() : THE_DEFAULT_FONT;
538 + TCollection_AsciiString(":") + Standard_Integer(anAspect)
539 + TCollection_AsciiString(":") + Standard_Integer(theResolution)
540 + TCollection_AsciiString(":") + theHeight;
543 // =======================================================================
544 // function : FindFont
546 // =======================================================================
547 Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx,
548 const OpenGl_Aspects& theAspect,
549 Standard_Integer theHeight,
550 unsigned int theResolution,
551 const TCollection_AsciiString& theKey)
553 Handle(OpenGl_Font) aFont;
556 return aFont; // invalid parameters
559 if (!theCtx->GetResource (theKey, aFont))
561 Handle(Font_FontMgr) aFontMgr = Font_FontMgr::GetInstance();
562 const TCollection_AsciiString& aFontName = !theAspect.Aspect()->TextFont().IsNull()
563 ? theAspect.Aspect()->TextFont()->String()
565 Font_FontAspect anAspect = theAspect.Aspect()->TextFontAspect() != Font_FA_Undefined
566 ? theAspect.Aspect()->TextFontAspect()
568 Font_FTFontParams aParams;
569 aParams.PointSize = theHeight;
570 aParams.Resolution = theResolution;
571 if (Handle(Font_FTFont) aFontFt = Font_FTFont::FindAndCreate (aFontName, anAspect, aParams, Font_StrictLevel_Any))
573 aFont = new OpenGl_Font (aFontFt, theKey);
574 if (!aFont->Init (theCtx))
576 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
577 TCollection_AsciiString ("Font '") + aFontName + "' - initialization of GL resources has failed!");
579 aFont->Release (theCtx.get());
580 aFont = new OpenGl_Font (aFontFt, theKey);
585 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
586 TCollection_AsciiString ("Font '") + aFontName + "' is not found in the system!");
587 aFont = new OpenGl_Font (aFontFt, theKey);
590 theCtx->ShareResource (theKey, aFont);
595 // =======================================================================
596 // function : drawRect
598 // =======================================================================
599 void OpenGl_Text::drawRect (const Handle(OpenGl_Context)& theCtx,
600 const OpenGl_Aspects& theTextAspect,
601 const OpenGl_Vec4& theColorSubs) const
603 Handle(OpenGl_ShaderProgram) aPrevProgram = theCtx->ActiveProgram();
604 if (myBndVertsVbo.IsNull())
606 OpenGl_Vec2 aQuad[4] =
608 OpenGl_Vec2(myBndBox.Right, myBndBox.Bottom),
609 OpenGl_Vec2(myBndBox.Right, myBndBox.Top),
610 OpenGl_Vec2(myBndBox.Left, myBndBox.Bottom),
611 OpenGl_Vec2(myBndBox.Left, myBndBox.Top)
613 if (theCtx->ToUseVbo())
615 myBndVertsVbo = new OpenGl_VertexBuffer();
619 myBndVertsVbo = new OpenGl_VertexBufferCompat();
621 myBndVertsVbo->Init (theCtx, 2, 4, aQuad[0].GetData());
624 // bind unlit program
625 theCtx->ShaderManager()->BindFaceProgram (Handle(OpenGl_TextureSet)(), Graphic3d_TOSM_UNLIT,
626 Graphic3d_AlphaMode_Opaque, Standard_False, Standard_False,
627 Handle(OpenGl_ShaderProgram)());
629 #if !defined(GL_ES_VERSION_2_0)
630 if (theCtx->core11 != NULL
631 && theCtx->ActiveProgram().IsNull())
633 glBindTexture (GL_TEXTURE_2D, 0);
636 theCtx->SetColor4fv (theColorSubs);
637 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f));
638 myBndVertsVbo->BindAttribute (theCtx, Graphic3d_TOA_POS);
640 theCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
642 myBndVertsVbo->UnbindAttribute (theCtx, Graphic3d_TOA_POS);
643 theCtx->BindProgram (aPrevProgram);
646 // =======================================================================
649 // =======================================================================
650 void OpenGl_Text::render (const Handle(OpenGl_Context)& theCtx,
651 const OpenGl_Aspects& theTextAspect,
652 const OpenGl_Vec4& theColorText,
653 const OpenGl_Vec4& theColorSubs,
654 unsigned int theResolution) const
656 if (myText->Text().IsEmpty())
661 // Note that using difference resolution in different Views in same Viewer
662 // will lead to performance regression (for example, text will be recreated every time).
663 const TCollection_AsciiString aFontKey = FontKey (theTextAspect, (Standard_Integer)myText->Height(), theResolution);
665 && !myFont->ResourceKey().IsEqual (aFontKey))
668 const_cast<OpenGl_Text* > (this)->Release (theCtx.operator->());
673 myFont = FindFont (theCtx, theTextAspect, (Standard_Integer)myText->Height(), theResolution, aFontKey);
675 if (!myFont->WasInitialized())
680 if (myTextures.IsEmpty())
682 Font_TextFormatter aFormatter;
684 aFormatter.SetupAlignment (myText->HorizontalAlignment(), myText->VerticalAlignment());
687 aFormatter.Append (myText->Text(), *myFont->FTFont());
690 OpenGl_TextBuilder aBuilder;
691 aBuilder.Perform (aFormatter,
693 *myFont.operator->(),
698 aFormatter.BndBox (myBndBox);
699 if (!myBndVertsVbo.IsNull())
701 myBndVertsVbo->Release (theCtx.get());
702 myBndVertsVbo.Nullify();
706 if (myTextures.IsEmpty())
711 myScaleHeight = 1.0f;
713 theCtx->WorldViewState.Push();
714 myModelMatrix.Convert (theCtx->WorldViewState.Current() * theCtx->ModelWorldState.Current());
716 const GLdouble aPointSize = (GLdouble )myFont->FTFont()->PointSize();
719 const gp_Pnt& aPoint = myText->Position();
720 Graphic3d_TransformUtils::Project<Standard_Real> (aPoint.X(), aPoint.Y(), aPoint.Z(),
721 myModelMatrix, myProjMatrix, theCtx->Viewport(),
722 myWinXYZ.x(), myWinXYZ.y(), myWinXYZ.z());
724 // compute scale factor for constant text height
725 if (!theTextAspect.Aspect()->IsTextZoomable())
727 Graphic3d_Vec3d aPnt1, aPnt2;
728 Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinXYZ.x(), myWinXYZ.y(), myWinXYZ.z(),
729 THE_IDENTITY_MATRIX, myProjMatrix, theCtx->Viewport(),
730 aPnt1.x(), aPnt1.y(), aPnt1.z());
731 Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinXYZ.x(), myWinXYZ.y() + aPointSize, myWinXYZ.z(),
732 THE_IDENTITY_MATRIX, myProjMatrix, theCtx->Viewport(),
733 aPnt2.x(), aPnt2.y(), aPnt2.z());
734 myScaleHeight = (aPnt2.y() - aPnt1.y()) / aPointSize;
738 #if !defined(GL_ES_VERSION_2_0)
739 if (theCtx->core11 != NULL
740 && theCtx->caps->ffpEnable)
742 glDisable (GL_LIGHTING);
747 const bool hasDepthTest = !myIs2d
748 && theTextAspect.Aspect()->TextStyle() != Aspect_TOST_ANNOTATION;
751 glDisable (GL_DEPTH_TEST);
754 if (theCtx->core15fwd != NULL)
756 theCtx->core15fwd->glActiveTexture (GL_TEXTURE0);
758 #if !defined(GL_ES_VERSION_2_0)
759 // activate texture unit
760 GLint aTexEnvParam = GL_REPLACE;
761 if (theCtx->core11 != NULL)
763 glDisable (GL_TEXTURE_1D);
764 glEnable (GL_TEXTURE_2D);
765 glGetTexEnviv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &aTexEnvParam);
766 if (aTexEnvParam != GL_REPLACE)
768 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
775 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
777 // alpha to coverage makes text too thin
778 theCtx->SetSampleAlphaToCoverage (false);
781 switch (theTextAspect.Aspect()->TextDisplayType())
783 case Aspect_TODT_BLEND:
785 #if !defined(GL_ES_VERSION_2_0)
786 glEnable (GL_COLOR_LOGIC_OP);
791 case Aspect_TODT_SUBTITLE:
793 BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx.get() : NULL);
794 drawRect (theCtx, theTextAspect, theColorSubs);
797 case Aspect_TODT_DEKALE:
799 BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx.get() : NULL);
800 theCtx->SetColor4fv (theColorSubs);
801 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.0f));
802 drawText (theCtx, theTextAspect);
803 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, -1.0f, 0.0f));
804 drawText (theCtx, theTextAspect);
805 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, +1.0f, 0.0f));
806 drawText (theCtx, theTextAspect);
807 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.0f));
808 drawText (theCtx, theTextAspect);
811 case Aspect_TODT_SHADOW:
813 BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx.get() : NULL);
814 theCtx->SetColor4fv (theColorSubs);
815 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.0f));
816 drawText (theCtx, theTextAspect);
819 case Aspect_TODT_DIMENSION:
820 case Aspect_TODT_NORMAL:
827 theCtx->SetColor4fv (theColorText);
828 setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f));
829 drawText (theCtx, theTextAspect);
833 theCtx->ProjectionState.SetCurrent<Standard_Real> (myProjMatrix);
834 theCtx->ApplyProjectionMatrix();
837 #if !defined(GL_ES_VERSION_2_0)
838 if (theCtx->core11 != NULL)
840 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aTexEnvParam);
844 if (theTextAspect.Aspect()->TextDisplayType() == Aspect_TODT_DIMENSION)
846 glDisable (GL_BLEND);
849 glDisable (GL_DEPTH_TEST);
851 #if !defined(GL_ES_VERSION_2_0)
852 if (theCtx->core11 != NULL)
854 glDisable (GL_TEXTURE_2D);
857 const bool aColorMaskBack = theCtx->SetColorMask (false);
859 glClear (GL_STENCIL_BUFFER_BIT);
860 glEnable (GL_STENCIL_TEST);
861 glStencilFunc (GL_ALWAYS, 1, 0xFF);
862 glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
864 drawRect (theCtx, theTextAspect, OpenGl_Vec4 (1.0f, 1.0f, 1.0f, 1.0f));
866 glStencilFunc (GL_ALWAYS, 0, 0xFF);
868 theCtx->SetColorMask (aColorMaskBack);
871 // reset OpenGL state
872 glDisable (GL_BLEND);
873 glDisable (GL_STENCIL_TEST);
874 #if !defined(GL_ES_VERSION_2_0)
875 glDisable (GL_COLOR_LOGIC_OP);
878 // model view matrix was modified
879 theCtx->WorldViewState.Pop();
880 theCtx->ApplyModelViewMatrix();
883 // =======================================================================
884 // function : DumpJson
886 // =======================================================================
887 void OpenGl_Text::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
889 OCCT_DUMP_CLASS_BEGIN (theOStream, OpenGl_Text)
890 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, OpenGl_Element)
892 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTextures.Size())
894 for (NCollection_Vector<Handle(OpenGl_VertexBuffer)>::Iterator aCrdsIt (myTCrdsVbo); aCrdsIt.More(); aCrdsIt.Next())
896 const Handle(OpenGl_VertexBuffer)& aVertexBuffer = aCrdsIt.Value();
897 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aVertexBuffer.get())
900 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myBndBox)