63 #include <ACG/GL/acg_glew.hh>
65 #include "TextNode.hh"
71 namespace SceneGraph {
81 QFont TextNode::qfont_ = QFont(
"Helvetica", 30);
103 textMode_(_textMode),
107 blendEnabled_(false),
108 texture2dEnabled_(false),
109 cullFaceEnabled_(false),
110 depthEnabled_(false),
111 alwaysOnTop_(_alwaysOnTop),
113 alphaTestValue_(0.5f),
114 alphaTestFunc_(GL_GREATER),
134 glDeleteBuffers(1, &
vbo_);
228 std::map< char, std::pair<unsigned int, unsigned int> >
231 std::map< char, std::pair<unsigned int, unsigned int> > m;
232 unsigned char c =
' ';
233 for (
unsigned int i = 0; i <
rows_; ++i) {
234 for (
unsigned int j = 0; j <
columns_; ++j, ++c) {
235 m[c] = std::make_pair(j, i);
314 if (!
text_.empty()) {
324 glDrawArrays(GL_QUADS, 0,
int(
text_.size() * 4) );
340 quint32 n = num > 0 ? num - 1 : 0;
376 QFontMetricsF metric(
qfont_);
377 for (
char c =
' '; c <
'~'; ++c) {
378 qreal width = metric.width(c) + std::abs(metric.leftBearing(c)) + std::abs(metric.rightBearing(c));
383 qreal height = metric.height();
392 finalImage.fill(Qt::transparent);
394 painter.begin(&finalImage);
395 painter.setRenderHints(QPainter::HighQualityAntialiasing
396 | QPainter::TextAntialiasing);
401 for (
char c =
' '; c <
'~'; ++c) {
402 std::pair<unsigned int, unsigned int> coords =
charToIndex_[c];
403 painter.drawText(coords.first*widthPow2,
imageHeight_ - (coords.second+1)*heightPow2, widthPow2, heightPow2, Qt::AlignLeft | Qt::AlignBottom, QString(c));
408 finalImage = QGLWidget::convertToGLFormat(finalImage);
415 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
416 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
417 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
418 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
419 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, finalImage.width(), finalImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, finalImage.bits());
420 glGenerateMipmap(GL_TEXTURE_2D);
433 if (
text_.size() == 0)
443 QFontMetricsF metric(
qfont_);
444 qreal avgWidth = metric.averageCharWidth();
446 float lastCharRight = 0.0f;
447 for (
unsigned int i = 0; i <
text_.size(); ++i) {
456 left = lastCharRight;
458 right = (left + width);
459 lastCharRight = right;
462 qreal leftBearing = std::abs(metric.leftBearing(
text_[i]));
463 qreal rightBearing = std::abs(metric.rightBearing(
text_[i]));
464 qreal metricWidth = metric.width(
text_[i]);
467 metricWidth += leftBearing + rightBearing;
468 #elif QT_VERSION < 0x050000
477 metricWidth += leftBearing + rightBearing;
481 float widthTx = (float) metricWidth / (
float)
imageWidth_;
486 float rightTx = leftTx + widthTx;
488 float topTx = bottomTx + heightTx;
528 glGenBuffers(1, &
vbo_);
534 glBufferDataARB( GL_ARRAY_BUFFER_ARB,
vertexBuffer_.size() *
sizeof(GLfloat), 0, GL_DYNAMIC_DRAW_ARB );
539 GLfloat *data =
reinterpret_cast<GLfloat*
>(glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB ));
543 glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
590 ro.debugName = (std::string(
"TextNode: ")+
name()).c_str();
610 ro.blendSrc = GL_SRC_ALPHA;
623 texture.type = GL_TEXTURE_2D;
624 texture.shadow =
false;
637 ro.setMaterial(&localMaterial);
639 ro.glDrawArrays(GL_QUADS, 0, static_cast<GLsizei>(
text_.size()) * 4);
654 scale = pTrans->
scale()(0,0);
657 pParent = pParent->
parent();
672 scale *= heightUnproj.length();
void scale(double _s)
scale by (_s, _s, _s)
static QColor color_
color that is used to draw the characters into the texture in updateFont()
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax)
update bounding box
ShaderGenDesc shaderDesc
Drawmode and other shader params.
Namespace providing different geometric functions concerning angles.
static void activeTexture(GLenum _texunit)
replaces glActiveTexture, no locking support
static void enable(GLenum _cap)
replaces glEnable, but supports locking
static void disableClientState(GLenum _cap)
replaces glDisableClientState, supports locking
std::string name() const
Returns: name of node (needs not be unique)
void scale(double _s)
scale by (_s, _s, _s)
static void disable(GLenum _cap)
replaces glDisable, but supports locking
static int imageWidth_
width of the generated texture
DrawModes::DrawMode availableDrawModes() const
return available draw modes
static const unsigned int rows_
number of rows of characters in the texture
void unbindVBO()
unbinds vbo_
bool texture2dEnabled_
stores if GL_TEXTURE_2D was enabled on entering TextNode
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
Vec3d unproject(const Vec3d &_winPoint) const
unproject point in window coordinates _winPoint to world coordinates
void setFont(const QFont &_font)
sets the font to be used for generating a texture with most characters of the chosen font ...
const GLMatrixd & modelview() const
get modelview matrix
GLuint vertexBuffer
VBO, IBO ids, ignored if VAO is provided.
bool alwaysOnTop_
stores if text should be drawn always on top
void setSize(const double _size)
sets the size by which the quads displaying the text will be scaled
static void bindTexture(GLenum _target, GLuint _buffer)
replaces glBindTexture, supports locking
int priority
Priority to allow sorting of objects.
TextMode textMode_
current display mode of text_ (SCREEN_ALIGNED, SCREEN_ALIGNED_STATIC_SIZE or OBJECT_ALIGNED) ...
void bindVBO()
binds vbo_ and sets the necessary OpenGL states
TextMode renderingMode()
returns the rendering mode (SCREEN_ALIGNED or OBJECT_ALIGNED)
static int imageHeight_
height of the generated texture
bool alphaTest_
stores if the alpha test was enabled
void applyScreenAligned(GLState &_state)
modifies _state so that the modelviewmatrix will be screenaligned. remember to call "_state...
VectorT< double, 3 > Vec3d
static void alphaFunc(GLenum _func, GLclampf _ref)
replaces glAlphaFunc, supports locking
void addElement(const VertexElement *_pElement)
bool depthEnabled_
stores if GL_DEPTH_TEST was enabled on entering TextNode
bool alwaysOnTop()
returns wheter always on top is setted or not
void setText(std::string _text)
sets the string that will be rendered
unsigned pixelSize_
pixelSize of the text for the SCREEN_ALIGNED_STATIC_SIZE mode
TextNode(BaseNode *_parent=0, std::string _name="<TextNode>", TextMode _textMode=SCREEN_ALIGNED, bool _alwaysOnTop=false)
bool blendEnabled_
stores if GL_BLEND was enabled on entering TextNode
void push_modelview_matrix()
push modelview matrix
void baseColor(const Vec4f &_c)
set the base color
static GLuint texture_
handle for the texture into which characters from qfont_ are painted in updateFont() ...
void push_modelview_matrix()
push modelview matrix
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
void reset_modelview()
reset modelview matrix (load identity)
static void bindBuffer(GLenum _target, GLuint _buffer)
replaces glBindBuffer, supports locking
DrawMode POINTS_COLORED
draw colored, but not lighted points (requires point colors)
float lastScale_
stores the last scaling factor the text computed to SCREEN_ALIGNED_STATIC_SIZE
static bool initialised_
this is used to ensure that the texture is only generated once when necessary
static QFont qfont_
font that is used to generate the texture in updateFont()
Interface class between scenegraph and renderer.
DrawMode POINTS
draw unlighted points using the default base color
static std::map< char, std::pair< unsigned int, unsigned int > > charToIndex_
maps most readable characters to indices for texture coordinate calculation in updateVBO() ...
void pop_modelview_matrix()
pop modelview matrix
GLMatrixd modelview
Modelview transform.
float alphaTestValue_
stores the alpha value used for alpha test
GLint blendSrc_
stores the sfactor parameter of glBlendFunc on entering TextNode
DrawMode POINTS_SHADED
draw shaded points (requires point normals)
void diffuseColor(const Vec4f &_d)
set the diffuse color.
bool cullFaceEnabled_
stores if GL_CULL_FACE was enabled on entering TextNode
ACG::VertexDeclaration vertexDecl_
stores the vertex declaration
GLenum alphaTestFunc_
stores alpha test comparison function
static void vertexPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glVertexPointer, supports locking
void setPixelSize(const unsigned int _size)
sets the pixelsize of the text (only available for the SCREEN_ALIGNED_STATIC_SIZE mode and only works...
GLint blendDest_
stores the dfactor parameter of glBlendFunc on entering TextNode
void scale(double _s)
Add scaling to the current Transformation.
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode)
draw Text
void addTexture(const Texture &_t)
adds a texture to stage RenderObjects::numTextures()
GLenum blendDest
glBlendFunc: GL_SRC_ALPHA, GL_ZERO, GL_ONE, GL_ONE_MINUS_SRC_ALPHA ...
static std::map< char, std::pair< unsigned int, unsigned int > > createMap()
static void bindBufferARB(GLenum _target, GLuint _buffer)
same function as bindBuffer
static void texcoordPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glTexcoordPointer, supports locking
BaseNode * parent()
Get the nodes parent node.
static void enableClientState(GLenum _cap)
replaces glEnableClientState, supports locking
double size_
scaling factor by which the quads in vbo_ are scaled
void pop_modelview_matrix()
pop modelview matrix
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode)
restore texture and drawing states
std::string text_
text to be displayed on quads in vbo_
void specularColor(const Vec4f &_s)
set the specular color
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode)
set texture and drawing states
virtual void addRenderObject(RenderObject *_renderObject)
Callback for the scenegraph nodes, which send new render objects via this function.
Text will always stay parallel to screen.
static void getAlphaFunc(GLenum *_func, GLclampf *_ref)
get alpha function, null-ptr safe
const VertexDeclaration * vertexDecl
Defines the vertex buffer layout, ignored if VAO is provided.
Vec3d project(const Vec3d &_point) const
project point in world coordinates to window coordinates
static quint32 nearestPowerOfTwo(quint32 num)
returns the nearest greater power of 2 to num
static void blendFunc(GLenum _sfactor, GLenum _dfactor)
replaces glBlendFunc, supports locking
static qreal maxFontWidth_
max width of qfont
void getRenderObjects(ACG::IRenderer *_renderer, ACG::GLState &_state, const ACG::SceneGraph::DrawModes::DrawMode &_drawMode, const ACG::SceneGraph::Material *_mat)
set RenderObject for Shader pipeline renderer
void setAlwaysOnTop(bool _alwaysOnTop)
draw the text always on top
std::vector< GLfloat > vertexBuffer_
buffer of vertex coordinates and texture coordinates of the quads
void setRenderingMode(TextMode _textMode)
void ambientColor(const Vec4f &_a)
set the ambient color.
static const unsigned int columns_
number of columns of characters in the texture
BaseNode * parent()
Get the nodes parent node.