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 #ifndef OpenGl_Text_Header
17 #define OpenGl_Text_Header
19 #include <OpenGl_Element.hxx>
21 #include <OpenGl_Aspects.hxx>
22 #include <OpenGl_TextBuilder.hxx>
24 #include <TCollection_ExtendedString.hxx>
25 #include <Graphic3d_Vertex.hxx>
26 #include <Graphic3d_HorizontalTextAlignment.hxx>
27 #include <Graphic3d_RenderingParams.hxx>
28 #include <Graphic3d_Text.hxx>
29 #include <Graphic3d_VerticalTextAlignment.hxx>
34 class OpenGl_Text : public OpenGl_Element
39 //! Creates new text in 3D space.
40 Standard_EXPORT OpenGl_Text (const Handle(Graphic3d_Text)& theTextParams);
43 Standard_EXPORT virtual ~OpenGl_Text();
45 //! Release cached VBO resources and the previous font if height changed.
46 //! Cached structures will be refilled by the next render.
47 //! Call Reset after modifying text parameters.
48 Standard_EXPORT void Reset (const Handle(OpenGl_Context)& theCtx);
50 //! Returns text parameters
52 const Handle(Graphic3d_Text)& Text() const { return myText; }
54 //! Sets text parameters
56 void SetText (const Handle(Graphic3d_Text)& theText) { myText = theText; }
58 //! Return true if text is 2D
59 Standard_Boolean Is2D() const { return myIs2d; }
61 //! Set true if text is 2D
62 void Set2D (const Standard_Boolean theEnable) { myIs2d = theEnable; }
64 //! Setup new font size
65 Standard_EXPORT void SetFontSize (const Handle(OpenGl_Context)& theContext,
66 const Standard_Integer theFontSize);
68 Standard_EXPORT virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const Standard_OVERRIDE;
69 Standard_EXPORT virtual void Release (OpenGl_Context* theContext) Standard_OVERRIDE;
71 public: //! @name methods for compatibility with layers
74 Standard_EXPORT OpenGl_Text();
76 //! Create key for shared resource
77 Standard_EXPORT static TCollection_AsciiString FontKey (const OpenGl_Aspects& theAspect,
78 Standard_Integer theHeight,
79 unsigned int theResolution);
81 //! Find shared resource for specified font or initialize new one
82 Standard_EXPORT static Handle(OpenGl_Font) FindFont (const Handle(OpenGl_Context)& theCtx,
83 const OpenGl_Aspects& theAspect,
84 Standard_Integer theHeight,
85 unsigned int theResolution,
86 const TCollection_AsciiString& theKey);
88 //! Compute text width
89 Standard_EXPORT static void StringSize (const Handle(OpenGl_Context)& theCtx,
90 const NCollection_String& theText,
91 const OpenGl_Aspects& theTextAspect,
92 const Standard_ShortReal theHeight,
93 const unsigned int theResolution,
94 Standard_ShortReal& theWidth,
95 Standard_ShortReal& theAscent,
96 Standard_ShortReal& theDescent);
99 Standard_EXPORT void Render (const Handle(OpenGl_Context)& theCtx,
100 const OpenGl_Aspects& theTextAspect,
101 unsigned int theResolution = Graphic3d_RenderingParams::THE_DEFAULT_RESOLUTION) const;
103 //! Dumps the content of me into the stream
104 Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
106 //! @name obsolete methods
109 //! Setup new string and position
110 Standard_DEPRECATED("Deprecated method Init() with obsolete arguments, use Init() and Text() instead of it")
111 Standard_EXPORT void Init (const Handle(OpenGl_Context)& theCtx,
112 const Standard_Utf8Char* theText,
113 const OpenGl_Vec3& thePoint);
115 //! Setup new position
116 Standard_DEPRECATED("Deprecated method SetPosition(), use Graphic3d_Text for it")
117 Standard_EXPORT void SetPosition (const OpenGl_Vec3& thePoint);
121 friend class OpenGl_Trihedron;
122 friend class OpenGl_GraduatedTrihedron;
124 //! Release cached VBO resources
125 Standard_EXPORT void releaseVbos (OpenGl_Context* theCtx);
130 void setupMatrix (const Handle(OpenGl_Context)& theCtx,
131 const OpenGl_Aspects& theTextAspect,
132 const OpenGl_Vec3& theDVec) const;
134 //! Draw arrays of vertices.
135 void drawText (const Handle(OpenGl_Context)& theCtx,
136 const OpenGl_Aspects& theTextAspect) const;
138 //! Draw rectangle from bounding text box.
139 void drawRect (const Handle(OpenGl_Context)& theCtx,
140 const OpenGl_Aspects& theTextAspect,
141 const OpenGl_Vec4& theColorSubs) const;
143 //! Main rendering code
144 void render (const Handle(OpenGl_Context)& theCtx,
145 const OpenGl_Aspects& theTextAspect,
146 const OpenGl_Vec4& theColorText,
147 const OpenGl_Vec4& theColorSubs,
148 unsigned int theResolution) const;
152 Handle(Graphic3d_Text) myText; //!< text parameters
153 mutable Handle(OpenGl_Font) myFont;
154 mutable NCollection_Vector<GLuint> myTextures; //!< textures' IDs
155 mutable NCollection_Vector<Handle(OpenGl_VertexBuffer)> myVertsVbo; //!< VBOs of vertices
156 mutable NCollection_Vector<Handle(OpenGl_VertexBuffer)> myTCrdsVbo; //!< VBOs of texture coordinates
157 mutable Handle(OpenGl_VertexBuffer) myBndVertsVbo;//!< VBOs of vertices for bounding box
158 mutable Font_Rect myBndBox;
162 mutable OpenGl_Mat4d myProjMatrix;
163 mutable OpenGl_Mat4d myModelMatrix;
164 mutable OpenGl_Mat4d myOrientationMatrix;
165 mutable OpenGl_Vec3d myWinXYZ;
166 mutable GLdouble myScaleHeight;
170 Standard_Boolean myIs2d;
173 DEFINE_STANDARD_ALLOC
177 #endif //OpenGl_Text_Header