Developer Documentation
TextNode.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 
43 
44 //=============================================================================
45 //
46 // CLASS PointNode
47 //
48 //=============================================================================
49 
50 #ifndef ACG_TEXTNODE_HH
51 #define ACG_TEXTNODE_HH
52 
53 
54 //== INCLUDES =================================================================
55 
56 #include "BaseNode.hh"
57 #include "TransformNode.hh"
58 #include "DrawModes.hh"
59 #include <ACG/GL/IRenderer.hh>
60 #include <ACG/GL/VertexDeclaration.hh>
61 #include <vector>
62 #include <QFont>
63 #include <QFontMetrics>
64 #include <QImage>
65 #include <QPainter>
66 #include <QGLWidget>
67 
68 //== NAMESPACES ===============================================================
69 
70 namespace ACG {
71 namespace SceneGraph {
72 
73 //== CLASS DEFINITION =========================================================
74 
75 
76 
87 class ACGDLLEXPORT TextNode : public BaseNode
88 {
89 public:
90 
91  enum TextMode {
92  SCREEN_ALIGNED,
94  OBJECT_ALIGNED
95  };
96 
103  TextNode( BaseNode* _parent=0,
104  const std::string& _name="<TextNode>",
105  TextMode _textMode = SCREEN_ALIGNED,
106  bool _alwaysOnTop = false);
107 
109  ~TextNode();
110 
112  ACG_CLASSNAME(TextNode);
113 
115  DrawModes::DrawMode availableDrawModes() const override;
116 
118  void boundingBox(Vec3d& _bbMin, Vec3d& _bbMax) override;
119 
121  void draw(GLState& _state, const DrawModes::DrawMode& _drawMode) override;
122 
124  void enter(GLState& _state, const DrawModes::DrawMode& _drawmode) override;
125  void enterCompat(GLState& _state, const DrawModes::DrawMode& _drawmode);
126 
128  void leave(GLState& _state, const DrawModes::DrawMode& _drawmode) override;
129  void leaveCompat(GLState& _state, const DrawModes::DrawMode& _drawmode);
130 
132  void getRenderObjects(ACG::IRenderer* _renderer, ACG::GLState& _state , const ACG::SceneGraph::DrawModes::DrawMode& _drawMode , const ACG::SceneGraph::Material* _mat) override;
133 
136  void setRenderingMode(TextMode _textMode);
137 
139  void setAlwaysOnTop(bool _alwaysOnTop);
140 
142  bool alwaysOnTop();
143 
145  TextMode renderingMode();
146 
148  void setText(std::string _text);
149 
151  void setSize(const double _size);
152 
154  void setPixelSize(const unsigned int _size);
155 
157  void setFont(const QFont& _font);
158 
165  float lastScale() {return lastScale_;};
166 
167 protected:
172  static void updateFont();
173 
174 private:
176  static quint32 nearestPowerOfTwo(quint32 num);
177 
182  void updateVBO();
183 
185  void bindVBO();
186 
188  void unbindVBO();
189 
191  void applyScreenAligned(GLState &_state);
192 
197  static std::map< char, std::pair<unsigned int, unsigned int> > createMap();
198 
199 private:
200 
202  double size_;
203 
205  unsigned pixelSize_;
206 
208  std::string text_;
209 
212 
217  GLuint vbo_;
218 
220  std::vector<GLfloat> vertexBuffer_;
221  size_t oldVboSize_;
222 
225 
228 
231 
234 
237 
240 
243 
246 
248  GLint blendSrc_;
249 
251  GLint blendDest_;
252 
255 
257  float lastScale_;
258 
260  static std::map< char, std::pair<unsigned int, unsigned int> > charToIndex_;
261 
263  static QFont qfont_;
264 
266  static GLuint texture_;
267 
269  static int imageWidth_;
270 
272  static int imageHeight_;
273 
275  static qreal maxFontWidth_;
276 
278  static const int numberOfChars_ = 94;
279 
281  static const unsigned int rows_ = 10;
282 
284  static const unsigned int columns_ = 10;
285 
287  static bool initialised_;
288 
290  static QColor color_;
291 };
292 
293 //=============================================================================
294 } // namespace SceneGraph
295 } // namespace ACG
296 //=============================================================================
297 #endif // ACG_TEXTNODE_HH defined
298 //=============================================================================
bool depthEnabled_
stores if GL_DEPTH_TEST was enabled on entering TextNode
Definition: TextNode.hh:233
static QFont qfont_
font that is used to generate the texture in updateFont()
Definition: TextNode.hh:263
unsigned pixelSize_
pixelSize of the text for the SCREEN_ALIGNED_STATIC_SIZE mode
Definition: TextNode.hh:205
static GLuint texture_
handle for the texture into which characters from qfont_ are painted in updateFont() ...
Definition: TextNode.hh:266
GLint blendDest_
stores the dfactor parameter of glBlendFunc on entering TextNode
Definition: TextNode.hh:251
static std::map< char, std::pair< unsigned int, unsigned int > > charToIndex_
maps most readable characters to indices for texture coordinate calculation in updateVBO() ...
Definition: TextNode.hh:260
static qreal maxFontWidth_
max width of qfont
Definition: TextNode.hh:275
Namespace providing different geometric functions concerning angles.
static bool initialised_
this is used to ensure that the texture is only generated once when necessary
Definition: TextNode.hh:287
Class to define the vertex input layout.
bool alphaTest_
stores if the alpha test was enabled
Definition: TextNode.hh:239
bool texture2dEnabled_
stores if GL_TEXTURE_2D was enabled on entering TextNode
Definition: TextNode.hh:227
Text will always stay parallel to screen.
Definition: TextNode.hh:93
double size_
scaling factor by which the quads in vbo_ are scaled
Definition: TextNode.hh:202
TextMode textMode_
current display mode of text_ (SCREEN_ALIGNED, SCREEN_ALIGNED_STATIC_SIZE or OBJECT_ALIGNED) ...
Definition: TextNode.hh:211
bool blendEnabled_
stores if GL_BLEND was enabled on entering TextNode
Definition: TextNode.hh:224
GLint blendSrc_
stores the sfactor parameter of glBlendFunc on entering TextNode
Definition: TextNode.hh:248
float lastScale_
stores the last scaling factor the text computed to SCREEN_ALIGNED_STATIC_SIZE
Definition: TextNode.hh:257
bool cullFaceEnabled_
stores if GL_CULL_FACE was enabled on entering TextNode
Definition: TextNode.hh:230
GLenum alphaTestFunc_
stores alpha test comparison function
Definition: TextNode.hh:245
float alphaTestValue_
stores the alpha value used for alpha test
Definition: TextNode.hh:242
float lastScale()
returns the scaling factor for screen aligned text the text. returns 0, if textmode is not SCREEN_ALI...
Definition: TextNode.hh:165
static int imageWidth_
width of the generated texture
Definition: TextNode.hh:269
bool alwaysOnTop_
stores if text should be drawn always on top
Definition: TextNode.hh:236
std::vector< GLfloat > vertexBuffer_
buffer of vertex coordinates and texture coordinates of the quads
Definition: TextNode.hh:220
ACG::VertexDeclaration vertexDecl_
stores the vertex declaration
Definition: TextNode.hh:254
static int imageHeight_
height of the generated texture
Definition: TextNode.hh:272
std::string text_
text to be displayed on quads in vbo_
Definition: TextNode.hh:208
static QColor color_
color that is used to draw the characters into the texture in updateFont()
Definition: TextNode.hh:290