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_HeaderFile 17 #define OpenGl_Text_HeaderFile 18 19 #include <OpenGl_Element.hxx> 20 21 #include <OpenGl_Aspects.hxx> 22 #include <OpenGl_TextBuilder.hxx> 23 24 #include <TCollection_ExtendedString.hxx> 25 #include <Graphic3d_HorizontalTextAlignment.hxx> 26 #include <Graphic3d_RenderingParams.hxx> 27 #include <Graphic3d_Text.hxx> 28 #include <Graphic3d_VerticalTextAlignment.hxx> 29 30 #include <gp_Ax2.hxx> 31 32 //! Text rendering 33 class OpenGl_Text : public OpenGl_Element 34 { 35 36 public: 37 38 //! Creates new text in 3D space. 39 Standard_EXPORT OpenGl_Text (const Handle(Graphic3d_Text)& theTextParams); 40 41 //! Destructor 42 Standard_EXPORT virtual ~OpenGl_Text(); 43 44 //! Release cached VBO resources and the previous font if height changed. 45 //! Cached structures will be refilled by the next render. 46 //! Call Reset after modifying text parameters. 47 Standard_EXPORT void Reset (const Handle(OpenGl_Context)& theCtx); 48 49 //! Returns text parameters 50 //! @sa Reset() Handle(Graphic3d_Text)51 const Handle(Graphic3d_Text)& Text() const { return myText; } 52 53 //! Sets text parameters 54 //! @sa Reset() SetText(const Handle (Graphic3d_Text)& theText)55 void SetText (const Handle(Graphic3d_Text)& theText) { myText = theText; } 56 57 //! Return true if text is 2D Is2D() const58 Standard_Boolean Is2D() const { return myIs2d; } 59 60 //! Set true if text is 2D Set2D(const Standard_Boolean theEnable)61 void Set2D (const Standard_Boolean theEnable) { myIs2d = theEnable; } 62 63 //! Setup new font size 64 Standard_EXPORT void SetFontSize (const Handle(OpenGl_Context)& theContext, 65 const Standard_Integer theFontSize); 66 67 Standard_EXPORT virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const Standard_OVERRIDE; 68 Standard_EXPORT virtual void Release (OpenGl_Context* theContext) Standard_OVERRIDE; 69 70 //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules. 71 Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE; 72 73 //! Increment draw calls statistics. 74 Standard_EXPORT virtual void UpdateDrawStats (Graphic3d_FrameStatsDataTmp& theStats, 75 bool theIsDetailed) const Standard_OVERRIDE; 76 77 public: //! @name methods for compatibility with layers 78 79 //! Empty constructor 80 Standard_EXPORT OpenGl_Text(); 81 82 //! Create key for shared resource 83 Standard_EXPORT static TCollection_AsciiString FontKey (const OpenGl_Aspects& theAspect, 84 Standard_Integer theHeight, 85 unsigned int theResolution, 86 Font_Hinting theFontHinting); 87 88 //! Find shared resource for specified font or initialize new one 89 Standard_EXPORT static Handle(OpenGl_Font) FindFont (const Handle(OpenGl_Context)& theCtx, 90 const OpenGl_Aspects& theAspect, 91 Standard_Integer theHeight, 92 unsigned int theResolution, 93 Font_Hinting theFontHinting, 94 const TCollection_AsciiString& theKey); 95 96 //! Compute text width 97 Standard_EXPORT static void StringSize (const Handle(OpenGl_Context)& theCtx, 98 const NCollection_String& theText, 99 const OpenGl_Aspects& theTextAspect, 100 const Standard_ShortReal theHeight, 101 const unsigned int theResolution, 102 const Font_Hinting theFontHinting, 103 Standard_ShortReal& theWidth, 104 Standard_ShortReal& theAscent, 105 Standard_ShortReal& theDescent); 106 107 //! Perform rendering 108 Standard_EXPORT void Render (const Handle(OpenGl_Context)& theCtx, 109 const OpenGl_Aspects& theTextAspect, 110 unsigned int theResolution = Graphic3d_RenderingParams::THE_DEFAULT_RESOLUTION, 111 Font_Hinting theFontHinting = Font_Hinting_Off) const; 112 113 //! Dumps the content of me into the stream 114 Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE; 115 116 //! @name obsolete methods 117 public: 118 119 //! Setup new string and position 120 Standard_DEPRECATED("Deprecated method Init() with obsolete arguments, use Init() and Text() instead of it") 121 Standard_EXPORT void Init (const Handle(OpenGl_Context)& theCtx, 122 const Standard_Utf8Char* theText, 123 const OpenGl_Vec3& thePoint); 124 125 //! Setup new position 126 Standard_DEPRECATED("Deprecated method SetPosition(), use Graphic3d_Text for it") 127 Standard_EXPORT void SetPosition (const OpenGl_Vec3& thePoint); 128 129 protected: 130 131 friend class OpenGl_Trihedron; 132 friend class OpenGl_GraduatedTrihedron; 133 134 //! Release cached VBO resources 135 Standard_EXPORT void releaseVbos (OpenGl_Context* theCtx); 136 137 private: 138 139 //! Setup matrix. 140 void setupMatrix (const Handle(OpenGl_Context)& theCtx, 141 const OpenGl_Aspects& theTextAspect, 142 const OpenGl_Vec3& theDVec) const; 143 144 //! Draw arrays of vertices. 145 void drawText (const Handle(OpenGl_Context)& theCtx, 146 const OpenGl_Aspects& theTextAspect) const; 147 148 //! Draw rectangle from bounding text box. 149 void drawRect (const Handle(OpenGl_Context)& theCtx, 150 const OpenGl_Aspects& theTextAspect, 151 const OpenGl_Vec4& theColorSubs) const; 152 153 //! Main rendering code 154 void render (const Handle(OpenGl_Context)& theCtx, 155 const OpenGl_Aspects& theTextAspect, 156 const OpenGl_Vec4& theColorText, 157 const OpenGl_Vec4& theColorSubs, 158 unsigned int theResolution, 159 Font_Hinting theFontHinting) const; 160 161 protected: 162 163 Handle(Graphic3d_Text) myText; //!< text parameters 164 mutable Handle(OpenGl_Font) myFont; 165 mutable NCollection_Vector<GLuint> myTextures; //!< textures' IDs 166 mutable NCollection_Vector<Handle(OpenGl_VertexBuffer)> myVertsVbo; //!< VBOs of vertices 167 mutable NCollection_Vector<Handle(OpenGl_VertexBuffer)> myTCrdsVbo; //!< VBOs of texture coordinates 168 mutable Handle(OpenGl_VertexBuffer) myBndVertsVbo;//!< VBOs of vertices for bounding box 169 mutable Font_Rect myBndBox; 170 171 protected: 172 173 mutable OpenGl_Mat4d myProjMatrix; 174 mutable OpenGl_Mat4d myModelMatrix; 175 mutable OpenGl_Mat4d myOrientationMatrix; 176 mutable OpenGl_Vec3d myWinXYZ; 177 mutable GLdouble myScaleHeight; 178 179 protected: 180 181 Standard_Boolean myIs2d; 182 public: 183 184 DEFINE_STANDARD_ALLOC 185 186 }; 187 188 #endif //OpenGl_Text_Header 189