OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Text.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #ifndef OSGTEXT_TEXT
15 #define OSGTEXT_TEXT 1
16 
17 
18 #include <osg/Drawable>
19 #include <osg/Quat>
20 
21 #include <osgText/TextBase>
22 #include <osgText/Font>
23 
24 namespace osgText {
25 
26 
28 {
29 public:
30 
31  Text();
32  Text(const Text& text,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
33 
34  virtual osg::Object* cloneType() const { return new Text(); }
35  virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new Text(*this,copyop); }
36  virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Text*>(obj)!=NULL; }
37  virtual const char* className() const { return "Text"; }
38  virtual const char* libraryName() const { return "osgText"; }
39 
40  virtual void setFont(Font* font=0) { setFont(osg::ref_ptr<Font>(font)); };
41 
43  virtual void setFont(osg::ref_ptr<Font> font);
44 
48  virtual void setFont(const std::string& fontfile) { TextBase::setFont(fontfile); }
49 
56  void setEnableDepthWrites(bool enable) { _enableDepthWrites = enable; }
57  bool getEnableDepthWrites() const { return _enableDepthWrites; }
58 
59 
61  {
62  DROP_SHADOW_BOTTOM_RIGHT = 0, // usually the type of shadow you see
71  NONE
72  };
73 
75  {
76  /* POLYGON_OFFSET:
77  * This uses glPolygonOffset to draw the text multiple times to
78  * create the drop-shadow and outline effects. glPolygonOffset
79  * is used to prevent z-fighting of the overlapping text.
80  * This probably should have been the best option, but all the ATI
81  * cards we have encountered so far have serious problems with this.
82  * We see little white holes/artifacts in the rendered glyph textures
83  * which move around depending on the viewing angle. For moving text,
84  * the moving holes give an extremely unpleasant flickering effect.
85  * Pumping up the "units" parameter in glPolygonOffset can minimize
86  * this problem, but two other bad side-effects occur if you do this.
87  * First, high values will cause problems with clipping, particularly
88  * when there are objects behind the text. The drop-shadows or outline
89  * may be culled because their computed offset is behind the object or
90  * z-far plane. Second, there is an additional problem associated with
91  * the Z-slope. High values can make large chunks of the backdrop
92  * suddenly disappear. This can be reduced by the "factor" parameter.
93  * Making the "factor" value small, can help, but experimentally, we've
94  * found that it creates a new, different kind of z-fighting problem.
95  * So there is no perfect solution. With units, you trade off the 'holes'
96  * for the large-section clipping.
97  * Experimentally, we have found units values from 150-512 to be tolerable
98  * to acceptable with respect to the 'holes'. A factor of .1 seems to
99  * bring down the large clipping problem without creating a new z-fighting
100  * problem.
101  * (You can experiment with these numbers by playing with the
102  * osg:PolygonOffset multipliers which this backend tries to respect.)
103  *
104  * If ATI ever fixes their cards/drivers, then this might become the
105  * best option.*/
106  POLYGON_OFFSET = 0,
107 
108  /* NO_DEPTH_BUFFER
109  * Instead of using glPolygonOffset to prevent z-fighting, this mode
110  * just disables the depth buffer when rendering the text. This allows
111  * the text to be rendered without any z-fighting. The downside to this
112  * mode is that render order begins to matter and the text will not
113  * necessarily correctly appear above or behind other objects in the
114  * scene based on depth values.
115  * This mode is best for text that only needs to be ontop and
116  * not obscured by any objects.*/
118 
119  /* DEPTH_RANGE
120  * This mode is inspired by Paul Martz's OpenGL FAQ, item 13.050.
121  * This uses glDepthRange as a substitute for glPolygonOffset.
122  * Strangely, experiments on ATI cards seem to produce cleaner results
123  * than when using glPolygonOffset. The trade-off for this is that the
124  * backdrop still may be placed too far back and might be culled by objects
125  * directly behind the object or by the far z-plane. If ATI ever fixes
126  * the glPolygonOffset problem, polygon offset is probably a slightly
127  * better solution because you can use smaller offsets. But with the
128  * current ATI problem, this option may be preferable.*/
130  /* STENCIL_BUFFER
131  * (Assuming the backend is written correctly,) the Stencil Buffer is
132  * the most "correct" and reliable way of producing backdrop text.
133  * The stencil buffer is a multipass system that allows writing to the
134  * same z-values without needing to resort to offsets. This implementation
135  * should not have any of the problems associated with the 3 previous
136  * implementations. But the trade-off for this mode is that without
137  * hardware acceleration for the stencil buffer, rendering will be
138  * extremely slow. (There is also potentially more overhead for this
139  * algorithm so it could be slower than the other implementations.
140  * Benchmarking would be required to determine if the speed differences
141  * are significant on your particular hardware.) This mode is best for
142  * when quality is important and stencil buffer hardware acceleration
143  * is available.*/
145 
146  /* DELAYED_DEPTH_WRITES
147  * This mode renders all text with depth writes turned off, then
148  * again with depth writes on, but with the color buffer disabled.
149  * This should render text accurately for all graphics cards. The
150  * only downside is the additional pass to render to the depth
151  * buffer. But if you don't need the depth buffer updated for
152  * your, this extra pass can be disabled by calling
153  * enableDepthWrites(false).*/
154  DELAYED_DEPTH_WRITES
155  };
156 
172  void setBackdropType(BackdropType type);
173 
174  BackdropType getBackdropType() const { return _backdropType; }
175 
192  void setBackdropOffset(float offset = 0.07f);
197  void setBackdropOffset(float horizontal, float vertical);
198 
199  float getBackdropHorizontalOffset() const { return _backdropHorizontalOffset; }
200 
201  float getBackdropVerticalOffset() const { return _backdropVerticalOffset; }
202 
207  void setBackdropColor(const osg::Vec4& color);
208 
209  const osg::Vec4& getBackdropColor() const { return _backdropColor; }
210 
217  void setBackdropImplementation(BackdropImplementation implementation);
218 
219  BackdropImplementation getBackdropImplementation() const { return _backdropImplementation; }
220 
221 
222 
224  {
225  SOLID = 0, // a.k.a. ColorGradients off
227  OVERALL
228  };
229 
247  void setColorGradientMode(ColorGradientMode mode);
248 
249  ColorGradientMode getColorGradientMode() const { return _colorGradientMode; }
250 
256  void setColorGradientCorners(const osg::Vec4& topLeft, const osg::Vec4& bottomLeft, const osg::Vec4& bottomRight, const osg::Vec4& topRight);
257 
258  const osg::Vec4& getColorGradientTopLeft() const { return _colorGradientTopLeft; }
259  const osg::Vec4& getColorGradientBottomLeft() const { return _colorGradientBottomLeft; }
260  const osg::Vec4& getColorGradientBottomRight() const { return _colorGradientBottomRight; }
261  const osg::Vec4& getColorGradientTopRight() const { return _colorGradientTopRight; }
262 
263 
264 
266  virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
267 
269  virtual bool supports(const osg::Drawable::AttributeFunctor&) const { return false; }
270 
272  virtual bool supports(const osg::Drawable::ConstAttributeFunctor&) const { return true; }
273 
275  virtual void accept(osg::Drawable::ConstAttributeFunctor& af) const;
276 
278  virtual bool supports(const osg::PrimitiveFunctor&) const { return true; }
279 
281  virtual void accept(osg::PrimitiveFunctor& pf) const;
282 
283 
285  virtual void setThreadSafeRefUnref(bool threadSafe);
286 
288  virtual void resizeGLObjectBuffers(unsigned int maxSize);
289 
293  virtual void releaseGLObjects(osg::State* state=0) const;
294 
295 public:
296 
297  // internal structures, variable and methods used for rendering of characters.
299  {
300  typedef std::vector<Glyph*> Glyphs;
301  typedef std::vector<unsigned int> LineNumbers;
306 
307  Glyphs _glyphs;
308  Coords2 _coords;
310  TexCoords _texcoords;
311  LineNumbers _lineNumbers;
312 
313  osg::buffered_object<Coords3> _transformedBackdropCoords[8];
314  ColorCoords _colorCoords;
315 
317  void updateQuadIndices();
318 
319  GlyphQuads();
320  GlyphQuads(const GlyphQuads& gq);
321 
322  void initGlyphQuads();
323  void initGPUBufferObjects();
324 
325  Glyphs& getGlyphs() { return _glyphs; }
326  const Glyphs& getGlyphs() const { return _glyphs; }
327 
328  Coords2& getCoords() { return _coords; }
329  const Coords2& getCoords() const { return _coords; }
330 
331  Coords3& getTransformedCoords(unsigned int contexID) { return _transformedCoords[contexID]; }
332  const Coords3& getTransformedCoords(unsigned int contexID) const { return _transformedCoords[contexID]; }
333 
334  TexCoords& getTexCoords() { return _texcoords; }
335  const TexCoords& getTexCoords() const { return _texcoords; }
336 
337  LineNumbers& getLineNumbers() { return _lineNumbers; }
338  const LineNumbers& getLineNumbers() const { return _lineNumbers; }
339 
341  void resizeGLObjectBuffers(unsigned int maxSize);
342 
346  void releaseGLObjects(osg::State* state=0) const;
347 
348  private:
349 
350  GlyphQuads& operator = (const GlyphQuads&) { return *this; }
351  };
352 
353  typedef std::map<osg::ref_ptr<GlyphTexture>,GlyphQuads> TextureGlyphQuadMap;
354 
356  const GlyphQuads* getGlyphQuads(GlyphTexture* texture) const
357  {
358  TextureGlyphQuadMap::iterator itGlyphQuad = _textureGlyphQuadMap.find(texture);
359  if (itGlyphQuad == _textureGlyphQuadMap.end()) return NULL;
360 
361  return &itGlyphQuad->second;
362  }
363 
364  const TextureGlyphQuadMap& getTextureGlyphQuadMap() const
365  {
366  return _textureGlyphQuadMap;
367  }
368 
369 
370 protected:
371 
372  virtual ~Text();
373 
374  Font* getActiveFont();
375  const Font* getActiveFont() const;
376 
377  String::iterator computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last);
378 
379  // members which have public access.
380 
381  // iternal map used for rendering. Set up by the computeGlyphRepresentation() method.
382  mutable TextureGlyphQuadMap _textureGlyphQuadMap;
383 
384  void computeGlyphRepresentation();
385 
386  // internal caches of the positioning of the text.
387 
388  bool computeAverageGlyphWidthAndHeight(float& avg_width, float& avg_height) const;
389 
390  virtual void computePositions(unsigned int contextID) const;
391 
392  void computeBackdropPositions(unsigned int contextID) const;
393  void computeBackdropBoundingBox() const;
394  void computeBoundingBoxMargin() const;
395 
396  void computeColorGradients() const;
397  void computeColorGradientsOverall() const;
398  void computeColorGradientsPerCharacter() const;
399 
400  void drawImplementation(osg::State& state, const osg::Vec4& colorMultiplier) const;
401  void drawForegroundText(osg::State& state, const GlyphQuads& glyphquad, const osg::Vec4& colorMultiplier) const;
402  void drawTextWithBackdrop(osg::State& state, const osg::Vec4& colorMultiplier) const;
403  void renderOnlyForegroundText(osg::State& state, const osg::Vec4& colorMultiplier) const;
404  void renderWithPolygonOffset(osg::State& state, const osg::Vec4& colorMultiplier) const;
405  void renderWithNoDepthBuffer(osg::State& state, const osg::Vec4& colorMultiplier) const;
406  void renderWithDepthRange(osg::State& state, const osg::Vec4& colorMultiplier) const;
407  void renderWithStencilBuffer(osg::State& state, const osg::Vec4& colorMultiplier) const;
408  void renderWithDelayedDepthWrites(osg::State& state, const osg::Vec4& colorMultiplier) const;
409 
411 
414 
418 
424 
425  // Helper function for color interpolation
426  float bilinearInterpolate(float x1, float x2, float y1, float y2, float x, float y, float q11, float q12, float q21, float q22) const;
427 };
428 
429 }
430 
431 
432 #endif
osg::Vec4 _backdropColor
Definition: Text.h:417
bool _enableDepthWrites
Definition: Text.h:410
virtual bool supports(const osg::Drawable::ConstAttributeFunctor &) const
Definition: Text.h:272
std::vector< Glyph * > Glyphs
Definition: Text.h:300
virtual void setFont(Font *font=0)
Definition: TextBase.h:44
Coords2 & getCoords()
Definition: Text.h:328
BackdropType getBackdropType() const
Definition: Text.h:174
#define NULL
Definition: Export.h:59
const LineNumbers & getLineNumbers() const
Definition: Text.h:338
TextureGlyphQuadMap _textureGlyphQuadMap
Definition: Text.h:382
bool getEnableDepthWrites() const
Definition: Text.h:57
const osg::Vec4 & getColorGradientBottomLeft() const
Definition: Text.h:259
LineNumbers & getLineNumbers()
Definition: Text.h:337
virtual bool supports(const osg::Drawable::AttributeFunctor &) const
Definition: Text.h:269
float _backdropHorizontalOffset
Definition: Text.h:415
const TextureGlyphQuadMap & getTextureGlyphQuadMap() const
Definition: Text.h:364
osg::ref_ptr< osg::DrawElementsUInt > _quadIndices
Definition: Text.h:316
LineNumbers _lineNumbers
Definition: Text.h:311
BackdropType _backdropType
Definition: Text.h:412
osg::ref_ptr< osg::Vec2Array > TexCoords
Definition: Text.h:304
BackdropType
Definition: Text.h:60
virtual const char * className() const
Definition: Text.h:37
ColorGradientMode _colorGradientMode
Definition: Text.h:419
const Glyphs & getGlyphs() const
Definition: Text.h:326
osg::ref_ptr< osg::Vec2Array > Coords2
Definition: Text.h:302
std::vector< unsigned int > LineNumbers
Definition: Text.h:301
osg::Vec4 _colorGradientTopRight
Definition: Text.h:423
const GlyphQuads * getGlyphQuads(GlyphTexture *texture) const
Definition: Text.h:356
virtual bool isSameKindAs(const osg::Object *obj) const
Definition: Text.h:36
virtual bool supports(const osg::PrimitiveFunctor &) const
Definition: Text.h:278
osg::ref_ptr< osg::Vec3Array > Coords3
Definition: Text.h:303
const TexCoords & getTexCoords() const
Definition: Text.h:335
std::map< osg::ref_ptr< GlyphTexture >, GlyphQuads > TextureGlyphQuadMap
Definition: Text.h:353
BackdropImplementation getBackdropImplementation() const
Definition: Text.h:219
float getBackdropVerticalOffset() const
Definition: Text.h:201
const Coords2 & getCoords() const
Definition: Text.h:329
TexCoords _texcoords
Definition: Text.h:310
virtual osg::Object * clone(const osg::CopyOp &copyop) const
Definition: Text.h:35
osg::Vec4 _colorGradientBottomRight
Definition: Text.h:422
Coords3 & getTransformedCoords(unsigned int contexID)
Definition: Text.h:331
Glyphs & getGlyphs()
Definition: Text.h:325
virtual void setFont(Font *font=0)
Definition: Text.h:40
osg::Vec4 _colorGradientTopLeft
Definition: Text.h:420
BackdropImplementation
Definition: Text.h:74
void setEnableDepthWrites(bool enable)
Definition: Text.h:56
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: GLU.h:71
osg::Vec4 _colorGradientBottomLeft
Definition: Text.h:421
TexCoords & getTexCoords()
Definition: Text.h:334
float _backdropVerticalOffset
Definition: Text.h:416
osg::ref_ptr< osg::Vec4Array > ColorCoords
Definition: Text.h:305
ColorCoords _colorCoords
Definition: Text.h:314
const osg::Vec4 & getColorGradientTopLeft() const
Definition: Text.h:258
BackdropImplementation _backdropImplementation
Definition: Text.h:413
const osg::Vec4 & getColorGradientBottomRight() const
Definition: Text.h:260
ColorGradientMode
Definition: Text.h:223
const Coords3 & getTransformedCoords(unsigned int contexID) const
Definition: Text.h:332
virtual osg::Object * cloneType() const
Definition: Text.h:34
float getBackdropHorizontalOffset() const
Definition: Text.h:199
osg::buffered_object< Coords3 > _transformedCoords
Definition: Text.h:309
const osg::Vec4 & getColorGradientTopRight() const
Definition: Text.h:261
ColorGradientMode getColorGradientMode() const
Definition: Text.h:249
#define OSGTEXT_EXPORT
Definition: Export.h:39
virtual void setFont(const std::string &fontfile)
Definition: Text.h:48
virtual const char * libraryName() const
Definition: Text.h:38
const osg::Vec4 & getBackdropColor() const
Definition: Text.h:209