Warnings on vc14 were eliminated
[occt.git] / src / OpenGl / OpenGl_Text.hxx
1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef OpenGl_Text_Header
17 #define OpenGl_Text_Header
18
19 #include <OpenGl_Element.hxx>
20
21 #include <OpenGl_AspectText.hxx>
22 #include <OpenGl_TextParam.hxx>
23 #include <OpenGl_TextBuilder.hxx>
24
25 #include <TCollection_ExtendedString.hxx>
26 #include <Graphic3d_Vertex.hxx>
27 #include <Graphic3d_HorizontalTextAlignment.hxx>
28 #include <Graphic3d_RenderingParams.hxx>
29 #include <Graphic3d_VerticalTextAlignment.hxx>
30
31 #include <gp_Ax2.hxx>
32
33 //! Text rendering
34 class OpenGl_Text : public OpenGl_Element
35 {
36
37 public:
38
39   //! Main constructor
40   Standard_EXPORT OpenGl_Text (const Standard_Utf8Char* theText,
41                                const OpenGl_Vec3&       thePoint,
42                                const OpenGl_TextParam&  theParams);
43
44   //! Creates new text in 3D space.
45   Standard_EXPORT OpenGl_Text (const Standard_Utf8Char* theText,
46                                const gp_Ax2&            theOrientation,
47                                const OpenGl_TextParam&  theParams,
48                                const bool               theHasOwnAnchor = true);
49
50   //! Setup new string and position
51   Standard_EXPORT void Init (const Handle(OpenGl_Context)& theCtx,
52                              const Standard_Utf8Char*      theText,
53                              const OpenGl_Vec3&            thePoint);
54
55   //! Setup new string and parameters
56   Standard_EXPORT void Init (const Handle(OpenGl_Context)& theCtx,
57                              const Standard_Utf8Char*      theText,
58                              const OpenGl_Vec3&            thePoint,
59                              const OpenGl_TextParam&       theParams);
60
61   //! Setup new position
62   Standard_EXPORT void SetPosition (const OpenGl_Vec3& thePoint);
63
64   //! Setup new font size
65   Standard_EXPORT void SetFontSize (const Handle(OpenGl_Context)& theContext,
66                                     const Standard_Integer        theFontSize);
67
68   Standard_EXPORT virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const;
69   Standard_EXPORT virtual void Release (OpenGl_Context* theContext);
70
71 public: //! @name methods for compatibility with layers
72
73   //! Empty constructor
74   Standard_EXPORT OpenGl_Text();
75
76   //! Create key for shared resource
77   Standard_EXPORT static TCollection_AsciiString FontKey (const OpenGl_AspectText& theAspect,
78                                                           const Standard_Integer   theHeight,
79                                                           const unsigned int       theResolution);
80
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_AspectText&      theAspect,
84                                                        const Standard_Integer        theHeight,
85                                                        const unsigned int            theResolution,
86                                                        const TCollection_AsciiString theKey);
87
88   //! Compute text width
89   Standard_EXPORT static void StringSize (const Handle(OpenGl_Context)& theCtx,
90                                           const NCollection_String&     theText,
91                                           const OpenGl_AspectText&      theTextAspect,
92                                           const OpenGl_TextParam&       theParams,
93                                           const unsigned int            theResolution,
94                                           Standard_ShortReal&           theWidth,
95                                           Standard_ShortReal&           theAscent,
96                                           Standard_ShortReal&           theDescent);
97
98   //! Setup new string and parameters
99   Standard_EXPORT void Init (const Handle(OpenGl_Context)&     theCtx,
100                              const TCollection_ExtendedString& theText,
101                              const OpenGl_Vec2&                thePoint,
102                              const OpenGl_TextParam&           theParams);
103
104   //! Perform rendering
105   Standard_EXPORT void Render (const Handle(OpenGl_Context)& theCtx,
106                                const OpenGl_AspectText&      theTextAspect,
107                                const unsigned int            theResolution = Graphic3d_RenderingParams::THE_DEFAULT_RESOLUTION) const;
108
109 protected:
110
111   //! Destructor
112   Standard_EXPORT virtual ~OpenGl_Text();
113
114   friend class OpenGl_Trihedron;
115   friend class OpenGl_GraduatedTrihedron;
116
117 private:
118
119   //! Release cached VBO resources
120   void releaseVbos (OpenGl_Context* theCtx);
121
122   //! Setup matrix.
123   void setupMatrix (const Handle(OpenGl_Context)& theCtx,
124                     const OpenGl_AspectText&      theTextAspect,
125                     const OpenGl_Vec3             theDVec) const;
126
127   //! Draw arrays of vertices.
128   void drawText (const Handle(OpenGl_Context)& theCtx,
129                  const OpenGl_AspectText&      theTextAspect) const;
130
131   //! Draw rectangle from bounding text box.
132   void drawRect (const Handle(OpenGl_Context)& theCtx,
133                  const OpenGl_AspectText&      theTextAspect,
134                  const OpenGl_Vec4&            theColorSubs) const;
135
136   //! Main rendering code
137   void render (const Handle(OpenGl_Context)& theCtx,
138                const OpenGl_AspectText&      theTextAspect,
139                const OpenGl_Vec4&            theColorText,
140                const OpenGl_Vec4&            theColorSubs,
141                const unsigned int            theResolution) const;
142
143 protected:
144
145   mutable Handle(OpenGl_Font)                             myFont;
146   mutable NCollection_Vector<GLuint>                      myTextures;   //!< textures' IDs
147   mutable NCollection_Vector<Handle(OpenGl_VertexBuffer)> myVertsVbo;   //!< VBOs of vertices
148   mutable NCollection_Vector<Handle(OpenGl_VertexBuffer)> myTCrdsVbo;   //!< VBOs of texture coordinates
149   mutable Handle(OpenGl_VertexBuffer)                     myBndVertsVbo;//!< VBOs of vertices for bounding box
150   mutable Font_Rect                                       myBndBox;
151
152 protected:
153
154   mutable OpenGl_Mat4d myProjMatrix;
155   mutable OpenGl_Mat4d myModelMatrix;
156   mutable OpenGl_Mat4d myOrientationMatrix;
157   mutable GLdouble myWinX;
158   mutable GLdouble myWinY;
159   mutable GLdouble myWinZ;
160   mutable GLdouble myScaleHeight;
161   mutable GLdouble myExportHeight;
162
163 protected:
164
165   OpenGl_TextParam   myParams;
166   NCollection_String myString;
167   OpenGl_Vec3        myPoint;
168   bool               myIs2d;
169   gp_Ax2             myOrientation; //!< Text orientation in 3D space.
170   bool               myHasPlane;    //!< Check if text have orientation in 3D space.
171   bool               myHasAnchorPoint; //!< Shows if it has own attach point
172
173 public:
174
175   DEFINE_STANDARD_ALLOC
176
177 };
178
179 #endif //OpenGl_Text_Header