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 
69 //== NAMESPACES ===============================================================
70 
71 namespace ACG {
72 namespace SceneGraph {
73 
74 //== CLASS DEFINITION =========================================================
75 
76 
77 
88 class ACGDLLEXPORT TextNode : public BaseNode
89 {
90 public:
91 
92  enum TextMode {
93  SCREEN_ALIGNED,
95  OBJECT_ALIGNED
96  };
97 
104  TextNode( BaseNode* _parent=0,
105  const std::string& _name="<TextNode>",
106  TextMode _textMode = SCREEN_ALIGNED,
107  bool _alwaysOnTop = false);
108 
110  ~TextNode();
111 
113  ACG_CLASSNAME(TextNode);
114 
116  DrawModes::DrawMode availableDrawModes() const override;
117 
119  void boundingBox(Vec3d& _bbMin, Vec3d& _bbMax) override;
120 
122  void draw(GLState& _state, const DrawModes::DrawMode& _drawMode) override;
123 
125  void enter(GLState& _state, const DrawModes::DrawMode& _drawmode) override;
126  void enterCompat(GLState& _state, const DrawModes::DrawMode& _drawmode);
127 
129  void leave(GLState& _state, const DrawModes::DrawMode& _drawmode) override;
130  void leaveCompat(GLState& _state, const DrawModes::DrawMode& _drawmode);
131 
133  void getRenderObjects(ACG::IRenderer* _renderer, ACG::GLState& _state , const ACG::SceneGraph::DrawModes::DrawMode& _drawMode , const ACG::SceneGraph::Material* _mat) override;
134 
137  void setRenderingMode(TextMode _textMode);
138 
140  void setAlwaysOnTop(bool _alwaysOnTop);
141 
143  bool alwaysOnTop();
144 
146  TextMode renderingMode();
147 
149  void setText(std::string _text);
150 
152  void setSize(const double _size);
153 
155  void setPixelSize(const unsigned int _size);
156 
158  void setFont(const QFont& _font);
159 
166  float lastScale() {return lastScale_;};
167 
168 protected:
173  static void updateFont();
174 
175 private:
177  static quint32 nearestPowerOfTwo(quint32 num);
178 
183  void updateVBO();
184 
186  void bindVBO();
187 
189  void unbindVBO();
190 
192  void applyScreenAligned(GLState &_state);
193 
198  static std::map< char, std::pair<unsigned int, unsigned int> > createMap();
199 
200 private:
201 
203  double size_;
204 
206  unsigned pixelSize_;
207 
209  std::string text_;
210 
213 
218  GLuint vbo_;
219 
221  std::vector<GLfloat> vertexBuffer_;
222  size_t oldVboSize_;
223 
226 
229 
232 
235 
238 
241 
244 
247 
249  GLint blendSrc_;
250 
252  GLint blendDest_;
253 
256 
258  float lastScale_;
259 
261  static std::map< char, std::pair<unsigned int, unsigned int> > charToIndex_;
262 
264  static QFont qfont_;
265 
267  static GLuint texture_;
268 
270  static int imageWidth_;
271 
273  static int imageHeight_;
274 
277  static QFontMetrics fontMetric_;
278 
280  static const int numberOfChars_ = 94;
281 
283  static const unsigned int rows_ = 10;
284 
286  static const unsigned int columns_ = 10;
287 
289  static bool initialised_;
290 
292  static QColor color_;
293 };
294 
295 //=============================================================================
296 } // namespace SceneGraph
297 } // namespace ACG
298 //=============================================================================
299 #endif // ACG_TEXTNODE_HH defined
300 //=============================================================================
bool depthEnabled_
stores if GL_DEPTH_TEST was enabled on entering TextNode
Definition: TextNode.hh:234
static QFontMetrics fontMetric_
Definition: TextNode.hh:277
static QFont qfont_
font that is used to generate the texture in updateFont()
Definition: TextNode.hh:264
unsigned pixelSize_
pixelSize of the text for the SCREEN_ALIGNED_STATIC_SIZE mode
Definition: TextNode.hh:206
static GLuint texture_
handle for the texture into which characters from qfont_ are painted in updateFont() ...
Definition: TextNode.hh:267
GLint blendDest_
stores the dfactor parameter of glBlendFunc on entering TextNode
Definition: TextNode.hh:252
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:261
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:289
Class to define the vertex input layout.
bool alphaTest_
stores if the alpha test was enabled
Definition: TextNode.hh:240
bool texture2dEnabled_
stores if GL_TEXTURE_2D was enabled on entering TextNode
Definition: TextNode.hh:228
Text will always stay parallel to screen.
Definition: TextNode.hh:94
double size_
scaling factor by which the quads in vbo_ are scaled
Definition: TextNode.hh:203
TextMode textMode_
current display mode of text_ (SCREEN_ALIGNED, SCREEN_ALIGNED_STATIC_SIZE or OBJECT_ALIGNED) ...
Definition: TextNode.hh:212
bool blendEnabled_
stores if GL_BLEND was enabled on entering TextNode
Definition: TextNode.hh:225
GLint blendSrc_
stores the sfactor parameter of glBlendFunc on entering TextNode
Definition: TextNode.hh:249
float lastScale_
stores the last scaling factor the text computed to SCREEN_ALIGNED_STATIC_SIZE
Definition: TextNode.hh:258
bool cullFaceEnabled_
stores if GL_CULL_FACE was enabled on entering TextNode
Definition: TextNode.hh:231
GLenum alphaTestFunc_
stores alpha test comparison function
Definition: TextNode.hh:246
float alphaTestValue_
stores the alpha value used for alpha test
Definition: TextNode.hh:243
float lastScale()
returns the scaling factor for screen aligned text the text. returns 0, if textmode is not SCREEN_ALI...
Definition: TextNode.hh:166
static int imageWidth_
width of the generated texture
Definition: TextNode.hh:270
bool alwaysOnTop_
stores if text should be drawn always on top
Definition: TextNode.hh:237
std::vector< GLfloat > vertexBuffer_
buffer of vertex coordinates and texture coordinates of the quads
Definition: TextNode.hh:221
ACG::VertexDeclaration vertexDecl_
stores the vertex declaration
Definition: TextNode.hh:255
static int imageHeight_
height of the generated texture
Definition: TextNode.hh:273
std::string text_
text to be displayed on quads in vbo_
Definition: TextNode.hh:209
static QColor color_
color that is used to draw the characters into the texture in updateFont()
Definition: TextNode.hh:292