namespace
{
- static const GLdouble THE_IDENTITY_MATRIX[16] =
- {
- 1.0, 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0, 0.0,
- 0.0, 0.0, 1.0, 0.0,
- 0.0, 0.0, 0.0, 1.0
- };
+ static const OpenGl_Mat4d THE_IDENTITY_MATRIX;
static const TCollection_AsciiString THE_DEFAULT_FONT (Font_NOF_ASCII_MONO);
//! Auxiliary tool for setting polygon offset temporarily.
struct BackPolygonOffsetSentry
{
- BackPolygonOffsetSentry (const Handle(OpenGl_Context)& theCtx)
- : myCtx (theCtx),
- myOffsetBack (!theCtx.IsNull() ? theCtx->PolygonOffset() : Graphic3d_PolygonOffset())
+ BackPolygonOffsetSentry (OpenGl_Context* theCtx)
+ : myCtx (theCtx)
{
- if (!theCtx.IsNull())
+ if (theCtx != NULL)
{
+ myOffsetBack = theCtx->PolygonOffset();
Graphic3d_PolygonOffset aPolyOffset = myOffsetBack;
aPolyOffset.Mode = Aspect_POM_Fill;
aPolyOffset.Units += 1.0f;
~BackPolygonOffsetSentry()
{
- if (!myCtx.IsNull())
+ if (myCtx != NULL)
{
myCtx->SetPolygonOffset (myOffsetBack);
}
BackPolygonOffsetSentry (const BackPolygonOffsetSentry& );
BackPolygonOffsetSentry& operator= (const BackPolygonOffsetSentry& );
private:
- const Handle(OpenGl_Context)& myCtx;
- const Graphic3d_PolygonOffset myOffsetBack;
+ OpenGl_Context* myCtx;
+ Graphic3d_PolygonOffset myOffsetBack;
};
} // anonymous namespace
// purpose :
// =======================================================================
OpenGl_Text::OpenGl_Text()
-: myWinX (0.0f),
- myWinY (0.0f),
- myWinZ (0.0f),
- myScaleHeight (1.0f),
- myPoint (0.0f, 0.0f, 0.0f),
- myIs2d (false),
- myHasPlane (false),
- myHasAnchorPoint (true)
+: myScaleHeight (1.0f),
+ myIs2d (Standard_False)
{
- myParams.Height = 10;
- myParams.HAlign = Graphic3d_HTA_LEFT;
- myParams.VAlign = Graphic3d_VTA_BOTTOM;
+ myText = new Graphic3d_Text (10.);
}
-// =======================================================================
-// function : OpenGl_Text
-// purpose :
-// =======================================================================
-OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText,
- const OpenGl_Vec3& thePoint,
- const OpenGl_TextParam& theParams)
-: myWinX (0.0f),
- myWinY (0.0f),
- myWinZ (0.0f),
- myScaleHeight (1.0f),
- myExportHeight (1.0f),
- myParams (theParams),
- myString (theText),
- myPoint (thePoint),
- myIs2d (false),
- myHasPlane (false),
- myHasAnchorPoint (true)
-{
- //
-}
// =======================================================================
// function : OpenGl_Text
// purpose :
// =======================================================================
-OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText,
- const gp_Ax2& theOrientation,
- const OpenGl_TextParam& theParams,
- const bool theHasOwnAnchor)
-: myWinX (0.0),
- myWinY (0.0),
- myWinZ (0.0),
- myScaleHeight (1.0),
- myExportHeight (1.0),
- myParams (theParams),
- myString (theText),
- myIs2d (false),
- myOrientation (theOrientation),
- myHasPlane (true),
- myHasAnchorPoint (theHasOwnAnchor)
+OpenGl_Text::OpenGl_Text (const Handle(Graphic3d_Text)& theTextParams)
+: myText (theTextParams),
+ myScaleHeight (1.0f),
+ myIs2d (Standard_False)
{
- const gp_Pnt& aPoint = theOrientation.Location();
- myPoint = OpenGl_Vec3 (static_cast<Standard_ShortReal> (aPoint.X()),
- static_cast<Standard_ShortReal> (aPoint.Y()),
- static_cast<Standard_ShortReal> (aPoint.Z()));
}
// =======================================================================
// =======================================================================
void OpenGl_Text::SetPosition (const OpenGl_Vec3& thePoint)
{
- myPoint = thePoint;
+ myText->SetPosition (gp_Pnt (thePoint.x(), thePoint.y(), thePoint.z()));
}
// =======================================================================
void OpenGl_Text::SetFontSize (const Handle(OpenGl_Context)& theCtx,
const Standard_Integer theFontSize)
{
- if (myParams.Height != theFontSize)
+ if (myText->Height() != theFontSize)
{
Release (theCtx.operator->());
}
- myParams.Height = theFontSize;
-}
-
-// =======================================================================
-// function : Init
-// purpose :
-// =======================================================================
-void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
- const Standard_Utf8Char* theText,
- const OpenGl_Vec3& thePoint)
-{
- releaseVbos (theCtx.operator->());
- myIs2d = false;
- myPoint = thePoint;
- myString.FromUnicode (theText);
+ myText->SetHeight ((Standard_ShortReal)theFontSize);
}
// =======================================================================
-// function : Init
+// function : Reset
// purpose :
// =======================================================================
-void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
- const Standard_Utf8Char* theText,
- const OpenGl_Vec3& thePoint,
- const OpenGl_TextParam& theParams)
+void OpenGl_Text::Reset (const Handle(OpenGl_Context)& theCtx)
{
- if (myParams.Height != theParams.Height)
+ if (!myFont.IsNull() && myFont->FTFont()->PointSize() != myText->Height())
{
Release (theCtx.operator->());
}
{
releaseVbos (theCtx.operator->());
}
- myIs2d = false;
- myParams = theParams;
- myPoint = thePoint;
- myString.FromUnicode (theText);
}
// =======================================================================
// function : Init
// purpose :
// =======================================================================
-void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
- const TCollection_ExtendedString& theText,
- const OpenGl_Vec2& thePoint,
- const OpenGl_TextParam& theParams)
+void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
+ const Standard_Utf8Char* theText,
+ const OpenGl_Vec3& thePoint)
{
- if (myParams.Height != theParams.Height)
- {
- Release (theCtx.operator->());
- }
- else
- {
- releaseVbos (theCtx.operator->());
- }
- myIs2d = true;
- myParams = theParams;
- myPoint.SetValues (thePoint, 0.0f);
- myString.FromUnicode (theText.ToExtString());
+ Reset (theCtx);
+ Set2D (Standard_False);
+
+ NCollection_String aText;
+ aText.FromUnicode (theText);
+ myText->SetText (aText);
+ myText->SetPosition (gp_Pnt (thePoint.x(), thePoint.y(), thePoint.z()));
}
// =======================================================================
}
}
+// =======================================================================
+// function : EstimatedDataSize
+// purpose :
+// =======================================================================
+Standard_Size OpenGl_Text::EstimatedDataSize() const
+{
+ Standard_Size aSize = 0;
+ for (Standard_Integer anIter = myVertsVbo.Lower(); anIter <= myVertsVbo.Upper(); ++anIter)
+ {
+ if (const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter))
+ {
+ aSize += aVerts->EstimatedDataSize();
+ }
+ if (const Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.Value (anIter))
+ {
+ aSize += aTCrds->EstimatedDataSize();
+ }
+ }
+ if (!myBndVertsVbo.IsNull())
+ {
+ aSize += myBndVertsVbo->EstimatedDataSize();
+ }
+ return aSize;
+}
+
+// =======================================================================
+// function : UpdateDrawStats
+// purpose :
+// =======================================================================
+void OpenGl_Text::UpdateDrawStats (Graphic3d_FrameStatsDataTmp& theStats,
+ bool theIsDetailed) const
+{
+ ++theStats[Graphic3d_FrameStatsCounter_NbElemsNotCulled];
+ ++theStats[Graphic3d_FrameStatsCounter_NbElemsTextNotCulled];
+ if (theIsDetailed)
+ {
+ for (Standard_Integer anIter = myVertsVbo.Lower(); anIter <= myVertsVbo.Upper(); ++anIter)
+ {
+ if (const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter))
+ {
+ theStats[Graphic3d_FrameStatsCounter_NbTrianglesNotCulled] += aVerts->GetElemsNb() / 3; // 2 non-indexed triangles per glyph
+ }
+ }
+ }
+}
+
// =======================================================================
// function : StringSize
// purpose :
void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
const NCollection_String& theText,
const OpenGl_Aspects& theTextAspect,
- const OpenGl_TextParam& theParams,
+ const Standard_ShortReal theHeight,
const unsigned int theResolution,
Standard_ShortReal& theWidth,
Standard_ShortReal& theAscent,
theWidth = 0.0f;
theAscent = 0.0f;
theDescent = 0.0f;
- const TCollection_AsciiString aFontKey = FontKey (theTextAspect, theParams.Height, theResolution);
- Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, theParams.Height, theResolution, aFontKey);
+ const TCollection_AsciiString aFontKey = FontKey (theTextAspect, (Standard_Integer)theHeight, theResolution);
+ Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, (Standard_Integer)theHeight, theResolution, aFontKey);
if (aFont.IsNull() || !aFont->IsValid())
{
return;
// =======================================================================
void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
{
- const OpenGl_Aspects* aTextAspect = theWorkspace->ApplyAspects();
+ const OpenGl_Aspects* aTextAspect = theWorkspace->ApplyAspects (false); // do not bind textures as they will be disabled anyway
const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
- const Handle(OpenGl_TextureSet) aPrevTexture = aCtx->BindTextures (Handle(OpenGl_TextureSet)());
// Bind custom shader program or generate default version
aCtx->ShaderManager()->BindFontProgram (aTextAspect->ShaderProgramRes (aCtx));
+ const Handle(OpenGl_TextureSet) aPrevTexture = aCtx->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
+
+ if (myText->HasPlane() && myText->HasOwnAnchorPoint())
+ {
+ myOrientationMatrix = theWorkspace->View()->Camera()->OrientationMatrix();
+ // reset translation part
+ myOrientationMatrix.ChangeValue (0, 3) = 0.0;
+ myOrientationMatrix.ChangeValue (1, 3) = 0.0;
+ myOrientationMatrix.ChangeValue (2, 3) = 0.0;
+ }
- myOrientationMatrix = theWorkspace->View()->Camera()->OrientationMatrix();
myProjMatrix.Convert (aCtx->ProjectionState.Current());
// use highlight color or colors from aspect
// restore aspects
if (!aPrevTexture.IsNull())
{
- aCtx->BindTextures (aPrevTexture);
+ aCtx->BindTextures (aPrevTexture, Handle(OpenGl_ShaderProgram)());
}
// restore Z buffer settings
const OpenGl_Aspects& theTextAspect,
const OpenGl_Vec3& theDVec) const
{
- OpenGl_Mat4d aModViewMat;
- OpenGl_Mat4d aProjectMat;
- if (myHasPlane && myHasAnchorPoint)
+ OpenGl_Mat4d aModViewMat, aProjectMat;
+ if (myText->HasPlane() && myText->HasOwnAnchorPoint())
{
aProjectMat = myProjMatrix * myOrientationMatrix;
}
if (myIs2d)
{
- Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, myPoint.x() + theDVec.x(), myPoint.y() + theDVec.y(), 0.f);
+ const gp_Pnt& aPoint = myText->Position();
+ Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, aPoint.X() + theDVec.x(), aPoint.Y() + theDVec.y(), 0.f);
Graphic3d_TransformUtils::Scale<GLdouble> (aModViewMat, 1.f, -1.f, 1.f);
Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->TextAngle(), 0.f, 0.f, 1.f);
}
else
{
- // align coordinates to the nearest integer
- // to avoid extra interpolation issues
- GLdouble anObjX, anObjY, anObjZ;
- Graphic3d_TransformUtils::UnProject<Standard_Real> (std::floor (myWinX + theDVec.x()),
- std::floor (myWinY + theDVec.y()),
- myWinZ + theDVec.z(),
- OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
- OpenGl_Mat4d::Map (aProjectMat),
- theCtx->Viewport(),
- anObjX,
- anObjY,
- anObjZ);
-
- if (myHasPlane)
+ OpenGl_Vec3d anObjXYZ;
+ OpenGl_Vec3d aWinXYZ = myWinXYZ + OpenGl_Vec3d (theDVec);
+ if (!myText->HasPlane() && !theTextAspect.Aspect()->IsTextZoomable())
{
- const gp_Dir& aVectorDir = myOrientation.XDirection();
- const gp_Dir& aVectorUp = myOrientation.Direction();
- const gp_Dir& aVectorRight = myOrientation.YDirection();
+ // Align coordinates to the nearest integer to avoid extra interpolation issues.
+ // Note that for better readability we could also try aligning freely rotated in 3D text (myHasPlane),
+ // when camera orientation co-aligned with horizontal text orientation,
+ // but this might look awkward while rotating camera.
+ aWinXYZ.x() = Floor (aWinXYZ.x());
+ aWinXYZ.y() = Floor (aWinXYZ.y());
+ }
+ Graphic3d_TransformUtils::UnProject<Standard_Real> (aWinXYZ.x(), aWinXYZ.y(), aWinXYZ.z(),
+ THE_IDENTITY_MATRIX, aProjectMat, theCtx->Viewport(),
+ anObjXYZ.x(), anObjXYZ.y(), anObjXYZ.z());
+
+ if (myText->HasPlane())
+ {
+ const gp_Ax2& anOrientation = myText->Orientation();
+ const gp_Dir& aVectorDir = anOrientation.XDirection();
+ const gp_Dir& aVectorUp = anOrientation.Direction();
+ const gp_Dir& aVectorRight = anOrientation.YDirection();
aModViewMat.SetColumn (2, OpenGl_Vec3d (aVectorUp.X(), aVectorUp.Y(), aVectorUp.Z()));
aModViewMat.SetColumn (1, OpenGl_Vec3d (aVectorRight.X(), aVectorRight.Y(), aVectorRight.Z()));
aModViewMat.SetColumn (0, OpenGl_Vec3d (aVectorDir.X(), aVectorDir.Y(), aVectorDir.Z()));
- if (!myHasAnchorPoint)
+ if (!myText->HasOwnAnchorPoint())
{
OpenGl_Mat4d aPosMat;
- aPosMat.SetColumn (3, OpenGl_Vec3d (myPoint.x(), myPoint.y(), myPoint.z()));
+ const gp_Pnt& aPoint = myText->Position();
+ aPosMat.SetColumn (3, OpenGl_Vec3d (aPoint.X(), aPoint.Y(), aPoint.Z()));
aPosMat *= aModViewMat;
aModViewMat.SetColumn (3, aPosMat.GetColumn (3));
}
else
{
- aModViewMat.SetColumn (3, OpenGl_Vec3d (anObjX, anObjY, anObjZ));
+ aModViewMat.SetColumn (3, anObjXYZ);
}
}
else
{
- Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, anObjX, anObjY, anObjZ);
+ Graphic3d_TransformUtils::Translate<GLdouble> (aModViewMat, anObjXYZ.x(), anObjXYZ.y(), anObjXYZ.z());
Graphic3d_TransformUtils::Rotate<GLdouble> (aModViewMat, theTextAspect.Aspect()->TextAngle(), 0.0, 0.0, 1.0);
}
}
}
- if (myHasPlane && !myHasAnchorPoint)
+ if (myText->HasPlane() && !myText->HasOwnAnchorPoint())
{
OpenGl_Mat4d aCurrentWorldViewMat;
aCurrentWorldViewMat.Convert (theCtx->WorldViewState.Current());
const OpenGl_Vec4& theColorSubs,
unsigned int theResolution) const
{
- if (myString.IsEmpty())
+ if (myText->Text().IsEmpty())
{
return;
}
// Note that using difference resolution in different Views in same Viewer
// will lead to performance regression (for example, text will be recreated every time).
- const TCollection_AsciiString aFontKey = FontKey (theTextAspect, myParams.Height, theResolution);
+ const TCollection_AsciiString aFontKey = FontKey (theTextAspect, (Standard_Integer)myText->Height(), theResolution);
if (!myFont.IsNull()
&& !myFont->ResourceKey().IsEqual (aFontKey))
{
if (myFont.IsNull())
{
- myFont = FindFont (theCtx, theTextAspect, myParams.Height, theResolution, aFontKey);
+ myFont = FindFont (theCtx, theTextAspect, (Standard_Integer)myText->Height(), theResolution, aFontKey);
}
if (!myFont->WasInitialized())
{
if (myTextures.IsEmpty())
{
Font_TextFormatter aFormatter;
- aFormatter.SetupAlignment (myParams.HAlign, myParams.VAlign);
+
+ aFormatter.SetupAlignment (myText->HorizontalAlignment(), myText->VerticalAlignment());
aFormatter.Reset();
- aFormatter.Append (myString, *myFont->FTFont().operator->());
+ aFormatter.Append (myText->Text(), *myFont->FTFont());
aFormatter.Format();
OpenGl_TextBuilder aBuilder;
return;
}
- myExportHeight = 1.0f;
myScaleHeight = 1.0f;
theCtx->WorldViewState.Push();
const GLdouble aPointSize = (GLdouble )myFont->FTFont()->PointSize();
if (!myIs2d)
{
- Graphic3d_TransformUtils::Project<Standard_Real> (myPoint.x(), myPoint.y(), myPoint.z(),
+ const gp_Pnt& aPoint = myText->Position();
+ Graphic3d_TransformUtils::Project<Standard_Real> (aPoint.X(), aPoint.Y(), aPoint.Z(),
myModelMatrix, myProjMatrix, theCtx->Viewport(),
- myWinX, myWinY, myWinZ);
+ myWinXYZ.x(), myWinXYZ.y(), myWinXYZ.z());
// compute scale factor for constant text height
- if (theTextAspect.Aspect()->IsTextZoomable())
- {
- myExportHeight = aPointSize;
- }
- else
+ if (!theTextAspect.Aspect()->IsTextZoomable())
{
Graphic3d_Vec3d aPnt1, aPnt2;
- Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinX, myWinY, myWinZ,
- OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX), myProjMatrix, theCtx->Viewport(),
+ Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinXYZ.x(), myWinXYZ.y(), myWinXYZ.z(),
+ THE_IDENTITY_MATRIX, myProjMatrix, theCtx->Viewport(),
aPnt1.x(), aPnt1.y(), aPnt1.z());
- Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinX, myWinY + aPointSize, myWinZ,
- OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX), myProjMatrix, theCtx->Viewport(),
+ Graphic3d_TransformUtils::UnProject<Standard_Real> (myWinXYZ.x(), myWinXYZ.y() + aPointSize, myWinXYZ.z(),
+ THE_IDENTITY_MATRIX, myProjMatrix, theCtx->Viewport(),
aPnt2.x(), aPnt2.y(), aPnt2.z());
myScaleHeight = (aPnt2.y() - aPnt1.y()) / aPointSize;
}
}
- myExportHeight = aPointSize / myExportHeight;
#if !defined(GL_ES_VERSION_2_0)
if (theCtx->core11 != NULL
}
case Aspect_TODT_SUBTITLE:
{
- BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx : Handle(OpenGl_Context)());
+ BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx.get() : NULL);
drawRect (theCtx, theTextAspect, theColorSubs);
break;
}
case Aspect_TODT_DEKALE:
{
- BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx : Handle(OpenGl_Context)());
+ BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx.get() : NULL);
theCtx->SetColor4fv (theColorSubs);
setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.0f));
drawText (theCtx, theTextAspect);
}
case Aspect_TODT_SHADOW:
{
- BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx : Handle(OpenGl_Context)());
+ BackPolygonOffsetSentry aPolygonOffsetTmp (hasDepthTest ? theCtx.get() : NULL);
theCtx->SetColor4fv (theColorSubs);
setupMatrix (theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.0f));
drawText (theCtx, theTextAspect);
theCtx->WorldViewState.Pop();
theCtx->ApplyModelViewMatrix();
}
+
+// =======================================================================
+// function : DumpJson
+// purpose :
+// =======================================================================
+void OpenGl_Text::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
+{
+ OCCT_DUMP_CLASS_BEGIN (theOStream, OpenGl_Text)
+ OCCT_DUMP_BASE_CLASS (theOStream, theDepth, OpenGl_Element)
+
+ OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTextures.Size())
+
+ for (NCollection_Vector<Handle(OpenGl_VertexBuffer)>::Iterator aCrdsIt (myTCrdsVbo); aCrdsIt.More(); aCrdsIt.Next())
+ {
+ const Handle(OpenGl_VertexBuffer)& aVertexBuffer = aCrdsIt.Value();
+ OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aVertexBuffer.get())
+ }
+
+ OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myBndBox)
+}