KGLLib
Classes | Public Member Functions
KGLLib::TextRenderer Class Reference

Enhanced text renderer. More...

#include <textrenderer.h>

List of all members.

Classes

class  Private

Public Member Functions

void begin (GLWidget *widget)
void end ()
int draw (const Eigen::Vector3d &pos, const QString &string, const QFont &font=QFont())
int draw (int x, int y, const QString &string, const QFont &font=QFont())
int draw (const QRect &rect, const QString &string, int flags, const QFont &font=QFont())
bool isActive ()

Detailed Description

Enhanced text renderer.

Author:
Benoit Jacob This class renders text inside a QGLWidget. It replaces and extends the functionality of QGLWidget::renderText().

Its advantages over the renderText() in Qt 4.2.3 are that it is much faster both at render-time and at startup, consumes less memory, and does outlined text. Its drawback is that its Unicode-safeness is not perfect as superpositions of unicode characters aren't handled.

Every QFont can be used, every character encodings supported by Qt can be used.

To draw plain 2D text on top of the scene, do:

 textRenderer.begin();
 textRenderer.draw( x1, y1, string1 );
 textRenderer.draw( x2, y2, string2 );
 textRenderer.draw( x3, y2, string3 );
 textRenderer.end();

To draw text as a transparent object inside the scene, do:

 textRenderer.begin();
 textRenderer.draw( pos1, string1 );
 textRenderer.draw( pos2, string2 );
 textRenderer.draw( pos3, string3 );
 textRenderer.end();

In order to set the text color, please call glColor3f or glColor4f before calling draw(). Of course you can also call qglColor or Color::apply(). You can achieve semitransparent text at no additional cost by choosing a semitransparent color.

Please make sure that no relevant OpenGL state change occurs between begin() and end(), except the state changes performed by the TextRenderer itself. In other words, please avoid calling glSomething() between begin() and end(), except if you are sure that this call won't result in a conflicting state change. Of course calling glColor*() is allowed.

If you experience rendering problems, you can try the following:


Member Function Documentation

void KGLLib::TextRenderer::begin ( GLWidget widget)

Call this before drawing any text. This method saves the GL state and changes it to prepare for text rendering.

References KGLLib::TextRenderer::Private::glwidget.

Referenced by KGLLib::GLWidget::paintGL().

int KGLLib::TextRenderer::draw ( int  x,
int  y,
const QString &  string,
const QFont &  font = QFont() 
)

Draw a single line of 2D text at the position (x,y) in window coordinates. Must be called between begin() and end(). (0,0) is the top-left corner.

Parameters:
xthe x-coordinate
ythe y-coordinate
stringthe QString to render
Returns:
the line spacing in pixels of the text just rendered (0 for an empty string).

References KGLLib::TextRenderer::Private::glwidget.

int KGLLib::TextRenderer::draw ( const QRect &  rect,
const QString &  string,
int  flags,
const QFont &  font = QFont() 
)

Draws 2D text within the given rectangle. The text will be positioned and layed out according to flags which is a bitwise OR of the following flags:

  • Qt::AlignLeft
  • Qt::AlignRight
  • Qt::AlignHCenter
  • Qt::AlignTop
  • Qt::AlignBottom
  • Qt::AlignVCenter
  • Qt::AlignCenter
  • Qt::TextSingleLine
  • Qt::TextWordWrap Any newlines contained in the string argument are preserved. Must be called between begin() and end().
    Returns:
    total height of the rendered text (i.e. font's line spacing times the number of drawn lines)

References draw().

int KGLLib::TextRenderer::draw ( const Eigen::Vector3d &  pos,
const QString &  string,
const QFont &  font = QFont() 
)

Draw text inside the 3D scene. Must be called between begin() and end(). The text is centered (both horizontally and vertically) around the specified position.

Parameters:
posthe position of the text in the scene's coordinate system
stringthe QString to render
Returns:
the line spacing in pixels of the text just rendered (0 for an empty string).

References KGLLib::TextRenderer::Private::glwidget.

Referenced by draw(), and KGLLib::GLWidget::paintGL().

void KGLLib::TextRenderer::end ( )

Call this after drawing text. This method restores the GL state to what it was when begin() was called.

References KGLLib::TextRenderer::Private::glwidget.

Referenced by KGLLib::GLWidget::paintGL().


The documentation for this class was generated from the following files: