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 //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules.
72 Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE;
74 public: //! @name methods for compatibility with layers
77 Standard_EXPORT OpenGl_Text();
79 //! Create key for shared resource
80 Standard_EXPORT static TCollection_AsciiString FontKey (const OpenGl_Aspects& theAspect,
81 Standard_Integer theHeight,
82 unsigned int theResolution);
84 //! Find shared resource for specified font or initialize new one
85 Standard_EXPORT static Handle(OpenGl_Font) FindFont (const Handle(OpenGl_Context)& theCtx,
86 const OpenGl_Aspects& theAspect,
87 Standard_Integer theHeight,
88 unsigned int theResolution,
89 const TCollection_AsciiString& theKey);
91 //! Compute text width
92 Standard_EXPORT static void StringSize (const Handle(OpenGl_Context)& theCtx,
93 const NCollection_String& theText,
94 const OpenGl_Aspects& theTextAspect,
95 const Standard_ShortReal theHeight,
96 const unsigned int theResolution,
97 Standard_ShortReal& theWidth,
98 Standard_ShortReal& theAscent,
99 Standard_ShortReal& theDescent);
101 //! Perform rendering
102 Standard_EXPORT void Render (const Handle(OpenGl_Context)& theCtx,
103 const OpenGl_Aspects& theTextAspect,
104 unsigned int theResolution = Graphic3d_RenderingParams::THE_DEFAULT_RESOLUTION) const;
106 //! Dumps the content of me into the stream
107 Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
109 //! @name obsolete methods
112 //! Setup new string and position
113 Standard_DEPRECATED("Deprecated method Init() with obsolete arguments, use Init() and Text() instead of it")
114 Standard_EXPORT void Init (const Handle(OpenGl_Context)& theCtx,
115 const Standard_Utf8Char* theText,
116 const OpenGl_Vec3& thePoint);
118 //! Setup new position
119 Standard_DEPRECATED("Deprecated method SetPosition(), use Graphic3d_Text for it")
120 Standard_EXPORT void SetPosition (const OpenGl_Vec3& thePoint);
124 friend class OpenGl_Trihedron;
125 friend class OpenGl_GraduatedTrihedron;
127 //! Release cached VBO resources
128 Standard_EXPORT void releaseVbos (OpenGl_Context* theCtx);
133 void setupMatrix (const Handle(OpenGl_Context)& theCtx,
134 const OpenGl_Aspects& theTextAspect,
135 const OpenGl_Vec3& theDVec) const;
137 //! Draw arrays of vertices.
138 void drawText (const Handle(OpenGl_Context)& theCtx,
139 const OpenGl_Aspects& theTextAspect) const;
141 //! Draw rectangle from bounding text box.
142 void drawRect (const Handle(OpenGl_Context)& theCtx,
143 const OpenGl_Aspects& theTextAspect,
144 const OpenGl_Vec4& theColorSubs) const;
146 //! Main rendering code
147 void render (const Handle(OpenGl_Context)& theCtx,
148 const OpenGl_Aspects& theTextAspect,
149 const OpenGl_Vec4& theColorText,
150 const OpenGl_Vec4& theColorSubs,
151 unsigned int theResolution) const;
155 Handle(Graphic3d_Text) myText; //!< text parameters
156 mutable Handle(OpenGl_Font) myFont;
157 mutable NCollection_Vector<GLuint> myTextures; //!< textures' IDs
158 mutable NCollection_Vector<Handle(OpenGl_VertexBuffer)> myVertsVbo; //!< VBOs of vertices
159 mutable NCollection_Vector<Handle(OpenGl_VertexBuffer)> myTCrdsVbo; //!< VBOs of texture coordinates
160 mutable Handle(OpenGl_VertexBuffer) myBndVertsVbo;//!< VBOs of vertices for bounding box
161 mutable Font_Rect myBndBox;
165 mutable OpenGl_Mat4d myProjMatrix;
166 mutable OpenGl_Mat4d myModelMatrix;
167 mutable OpenGl_Mat4d myOrientationMatrix;
168 mutable OpenGl_Vec3d myWinXYZ;
169 mutable GLdouble myScaleHeight;
173 Standard_Boolean myIs2d;
176 DEFINE_STANDARD_ALLOC
180 #endif //OpenGl_Text_Header