TextNode.hh 9.52 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
37
38
 *   $Revision$                                                      *
 *   $Author$                                                          *
Jan Möbius's avatar
Jan Möbius committed
39
40
41
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58




//=============================================================================
//
//  CLASS PointNode
//
//=============================================================================

#ifndef ACG_TEXTNODE_HH
#define ACG_TEXTNODE_HH


//== INCLUDES =================================================================

#include "BaseNode.hh"
59
#include "TransformNode.hh"
Jan Möbius's avatar
 
Jan Möbius committed
60
61
#include "DrawModes.hh"
#include <vector>
62
63
64
65
66
#include <QFont>
#include <QFontMetrics>
#include <QImage>
#include <QPainter>
#include <QGLWidget>
Jan Möbius's avatar
 
Jan Möbius committed
67
68
69
70
71
72
73
74
75
76
77

//== NAMESPACES ===============================================================

namespace ACG {
namespace SceneGraph {

//== CLASS DEFINITION =========================================================



/** \class TextNode TextNode.hh <ACG/Scenegraph/TextNode.hh>
78
79
 *  TextNode can be used to display a string on quads in OpenGL. This string can be set with
 * 	setText(std::string _text). A TextNode can be attached to a parent node by using the function
80
81
82
83
 * 	BaseObjectData::addAdditionalNode. The quads can then be set to align to the parent (OBJECT_ALIGNED)
 *	via the setRenderingMode(TextMode _textMode) function. Alternatively the quads can be aligned
 *	to the screen (SCREEN_ALIGNED). The font that is used to display text on the screen
 *  can be set with the setFont(const QFont& _font) function. Finally the quads can be scaled
84
 * 	via the setSize(unsigned int _size) function.
Jan Möbius's avatar
 
Jan Möbius committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
**/

class ACGDLLEXPORT TextNode : public BaseNode
{
public:

  enum TextMode {
    SCREEN_ALIGNED, /// Text will always stay parallel to screen
    OBJECT_ALIGNED  /// Text will be transformed and projected by Modelview and projection matrix
  };

  /** default constructor
   * @param _parent Define the parent Node this node gets attached to
   * @param _name Name of this Node
   * @param _textMode Define the text rendering style ( see TextNode::TextMode )
100
   * @param _alwaysOnTop Draw text on top of everything else?
Jan Möbius's avatar
 
Jan Möbius committed
101
   */
102
  TextNode( BaseNode*     _parent=0,
103
            std::string  _name="<TextNode>",
Matthias Möller's avatar
Matthias Möller committed
104
105
            TextMode     _textMode = SCREEN_ALIGNED,
            bool         _alwaysOnTop = false);
Jan Möbius's avatar
 
Jan Möbius committed
106
107
108
109
110
111
112
113

  /// destructor
  ~TextNode();

  /// static name of this class
  ACG_CLASSNAME(TextNode);

  /// return available draw modes
Jan Möbius's avatar
Jan Möbius committed
114
  DrawModes::DrawMode  availableDrawModes() const;
Jan Möbius's avatar
 
Jan Möbius committed
115
116

  /// update bounding box
117
  void boundingBox(Vec3d& _bbMin, Vec3d& _bbMax);
Jan Möbius's avatar
 
Jan Möbius committed
118
119

  /// draw Text
120
  void draw(GLState& _state, const DrawModes::DrawMode& _drawMode);
Jan Möbius's avatar
 
Jan Möbius committed
121

122
123
124
125
126
127
  /// set texture and drawing states
  void enter(GLState& _state, const DrawModes::DrawMode& _drawmode);

  /// restore texture and drawing states
  void leave(GLState& _state, const DrawModes::DrawMode& _drawmode);

Jan Möbius's avatar
 
Jan Möbius committed
128
129
  /** Set the rendering mode ( see TextNode::TextMode )
   */
130
  void setRenderingMode(TextMode _textMode);
Jan Möbius's avatar
 
Jan Möbius committed
131

Matthias Möller's avatar
Matthias Möller committed
132
133
134
135
136
137
  /// draw the text always on top
  void setAlwaysOnTop(bool _alwaysOnTop);

  /// returns wheter always on top is setted or not
  bool alwaysOnTop();

138
139
  /// returns the rendering mode (SCREEN_ALIGNED or OBJECT_ALIGNED)
  TextMode renderingMode();
Jan Möbius's avatar
 
Jan Möbius committed
140

141
142
  /// sets the string that will be rendered
  void setText(std::string _text);
Jan Möbius's avatar
 
Jan Möbius committed
143

144
145
  /// sets the size by which the quads displaying the text will be scaled
  void setSize(unsigned int _size);
Jan Möbius's avatar
 
Jan Möbius committed
146

147
  /// sets the font to be used for generating a texture with most characters of the chosen font
148
  void setFont(const QFont& _font);
Jan Möbius's avatar
 
Jan Möbius committed
149
150

protected:
151
152
153
154
155
  /**
   * Generates a texture by drawing most characters into one texture.
   * This texture is then used to draw single characters onto quads
   */
  static void updateFont();
Jan Möbius's avatar
 
Jan Möbius committed
156
157

private:
158
  /// returns the nearest greater power of 2 to num
159
  static quint32 nearestPowerOfTwo(quint32 num);
160
161
162
163
164

  /**
   * generates a quad for each character in #text_ and also
   * calculates the texture coordinates for each quad's character
   */
165
  void updateVBO();
166
167

  /// binds #vbo_ and sets the necessary OpenGL states
168
  void bindVBO();
169
170

  /// unbinds #vbo_
171
  void unbindVBO();
172
173
174
175
176

  /**
   * Creates a map #charToIndex_ from most characters to an incrementing set of indices.
   * These indices are used to create the texture coordinates in updateVBO().
   */
177
  static std::map< char, std::pair<unsigned int, unsigned int> > createMap();
Jan Möbius's avatar
 
Jan Möbius committed
178

179
private:
180
181

  /// scaling factor by which the quads in #vbo_ are scaled
Jan Möbius's avatar
 
Jan Möbius committed
182
183
  unsigned int size_;

184
  /// text to be displayed on quads in #vbo_
Jan Möbius's avatar
 
Jan Möbius committed
185
186
  std::string text_;

187
  /// current display mode of #text_ (SCREEN_ALIGNED or OBJECT_ALIGNED)
Jan Möbius's avatar
 
Jan Möbius committed
188
189
  TextMode textMode_;

190
191
192
193
  /**
   * handle to the vertex buffer object, containing the quads on which the characters in
   * #text_ are rendered
   */
194
195
  GLuint vbo_;

196
  /// buffer of vertex coordinates and texture coordinates of the quads
197
198
  std::vector<GLfloat> vertexBuffer_;

199
  /// stores if GL_BLEND was enabled on entering TextNode
200
201
  bool blendEnabled_;

202
  /// stores if GL_TEXTURE_2D was enabled on entering TextNode
203
204
  bool texture2dEnabled_;

205
  /// stores if GL_CULL_FACE was enabled on entering TextNode
206
207
  bool cullFaceEnabled_;

Matthias Möller's avatar
Matthias Möller committed
208
209
210
211
212
213
  /// stores if GL_DEPTH_TEST was enabled on entering TextNode
  bool depthEnabled_;

  /// stores if text should be drawn always on top
  bool alwaysOnTop_;

214
  /// stores the sfactor parameter of glBlendFunc on entering TextNode
215
216
  GLint blendSrc_;

217
  /// stores the dfactor parameter of glBlendFunc on entering TextNode
218
219
  GLint blendDest_;

220
  /// maps most readable characters to indices for texture coordinate calculation in updateVBO()
221
  static std::map< char, std::pair<unsigned int, unsigned int> > charToIndex_;
222

223
  /// font that is used to generate the texture in updateFont()
224
225
  static QFont qfont_;

226
  /// handle for the texture into which characters from #qfont_ are painted in updateFont()
227
228
  static GLuint texture_;

229
  /// width of the generated texture
230
231
  static int imageWidth_;

232
233
234
  /// height of the generated texture
  static int imageHeight_;

235
  /// number of characters that are drawn into the texture
236
237
  static const int numberOfChars_ = 94;

238
239
240
241
242
243
  /// number of rows of characters in the texture
  static const unsigned int rows_ = 10;

  /// number of columns of characters in the texture
  static const unsigned int columns_ = 10;

244
  /// this is used to ensure that the texture is only generated once when necessary
245
246
  static bool initialised_;

247
  /// color that is used to draw the characters into the texture in updateFont()
248
249
  static QColor color_;
};
Jan Möbius's avatar
 
Jan Möbius committed
250
251
252
253
254
255
256

//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================
#endif // ACG_TEXTNODE_HH defined
//=============================================================================