63 #include <ACG/GL/acg_glew.hh> 65 #include "TextNode.hh" 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();
static void bindTexture(GLenum _target, GLuint _buffer)
replaces glBindTexture, supports locking
static QColor color_
color that is used to draw the characters into the texture in updateFont()
TextMode renderingMode()
returns the rendering mode (SCREEN_ALIGNED or OBJECT_ALIGNED)
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode)
restore texture and drawing states
DrawMode POINTS_COLORED
draw colored, but not lighted points (requires point colors)
auto length() const -> decltype(std::declval< VectorT< S, DIM >>().norm())
compute squared euclidean norm
static void enableClientState(GLenum _cap)
replaces glEnableClientState, supports locking
float alphaTestValue_
stores the alpha value used for alpha test
std::string text_
text to be displayed on quads in vbo_
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode)
draw Text
Text will always stay parallel to screen.
GLuint vertexBuffer
VBO, IBO ids, ignored if VAO is provided.
static qreal maxFontWidth_
max width of qfont
Interface class between scenegraph and renderer.
void scale(double _s)
Add scaling to the current Transformation.
GLint blendSrc_
stores the sfactor parameter of glBlendFunc on entering TextNode
static void activeTexture(GLenum _texunit)
replaces glActiveTexture, no locking support
static void blendFunc(GLenum _sfactor, GLenum _dfactor)
replaces glBlendFunc, 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...
bool blendEnabled_
stores if GL_BLEND was enabled on entering TextNode
BaseNode * parent()
Get the nodes parent node.
static void getAlphaFunc(GLenum *_func, GLclampf *_ref)
get alpha function, null-ptr safe
void push_modelview_matrix()
push modelview matrix
void baseColor(const Vec4f &_c)
set the base color
static void vertexPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glVertexPointer, supports locking
void bindVBO()
binds vbo_ and sets the necessary OpenGL states
ACG::VertexDeclaration vertexDecl_
stores the vertex declaration
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax)
update bounding box
ShaderGenDesc shaderDesc
Drawmode and other shader params.
void setAlwaysOnTop(bool _alwaysOnTop)
draw the text always on top
TextNode(BaseNode *_parent=0, std::string _name="<TextNode>", TextMode _textMode=SCREEN_ALIGNED, bool _alwaysOnTop=false)
static void alphaFunc(GLenum _func, GLclampf _ref)
replaces glAlphaFunc, supports locking
bool alwaysOnTop_
stores if text should be drawn always on top
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode)
set texture and drawing states
DrawMode POINTS
draw unlighted points using the default base color
Vec3d project(const Vec3d &_point) const
project point in world coordinates to window coordinates
TextMode textMode_
current display mode of text_ (SCREEN_ALIGNED, SCREEN_ALIGNED_STATIC_SIZE or OBJECT_ALIGNED) ...
std::vector< GLfloat > vertexBuffer_
buffer of vertex coordinates and texture coordinates of the quads
float lastScale_
stores the last scaling factor the text computed to SCREEN_ALIGNED_STATIC_SIZE
static void bindBuffer(GLenum _target, GLuint _buffer)
replaces glBindBuffer, supports locking
void addElement(const VertexElement *_pElement)
static void enable(GLenum _cap)
replaces glEnable, but supports locking
DrawModes::DrawMode availableDrawModes() const
return available draw modes
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
const GLMatrixd & modelview() const
get modelview matrix
void setText(std::string _text)
sets the string that will be rendered
void setRenderingMode(TextMode _textMode)
void setSize(const double _size)
sets the size by which the quads displaying the text will be scaled
Vec3d unproject(const Vec3d &_winPoint) const
unproject point in window coordinates _winPoint to world coordinates
static std::map< char, std::pair< unsigned int, unsigned int > > charToIndex_
maps most readable characters to indices for texture coordinate calculation in updateVBO() ...
void unbindVBO()
unbinds vbo_
bool alphaTest_
stores if the alpha test was enabled
void pop_modelview_matrix()
pop modelview matrix
static void bindBufferARB(GLenum _target, GLuint _buffer)
same function as bindBuffer
static void disable(GLenum _cap)
replaces glDisable, but supports locking
DrawMode POINTS_SHADED
draw shaded points (requires point normals)
void push_modelview_matrix()
push modelview matrix
std::string name() const
Returns: name of node (needs not be unique)
Namespace providing different geometric functions concerning angles.
bool cullFaceEnabled_
stores if GL_CULL_FACE was enabled on entering TextNode
bool alwaysOnTop()
returns wheter always on top is setted or not
void scale(double _s)
scale by (_s, _s, _s)
static GLuint texture_
handle for the texture into which characters from qfont_ are painted in updateFont() ...
static void disableClientState(GLenum _cap)
replaces glDisableClientState, supports locking
void pop_modelview_matrix()
pop modelview matrix
static quint32 nearestPowerOfTwo(quint32 num)
returns the nearest greater power of 2 to num
void setFont(const QFont &_font)
sets the font to be used for generating a texture with most characters of the chosen font ...
void diffuseColor(const Vec4f &_d)
set the diffuse color.
GLMatrixd modelview
Modelview transform.
void addTexture(const Texture &_t)
adds a texture to stage RenderObjects::numTextures()
int priority
Priority to allow sorting of objects.
static bool initialised_
this is used to ensure that the texture is only generated once when necessary
unsigned pixelSize_
pixelSize of the text for the SCREEN_ALIGNED_STATIC_SIZE mode
static void texcoordPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glTexcoordPointer, supports locking
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
virtual void addRenderObject(RenderObject *_renderObject)
Callback for the scenegraph nodes, which send new render objects via this function.
void applyScreenAligned(GLState &_state)
modifies _state so that the modelviewmatrix will be screenaligned. remember to call "_state...
bool texture2dEnabled_
stores if GL_TEXTURE_2D was enabled on entering TextNode
static std::map< char, std::pair< unsigned int, unsigned int > > createMap()
double size_
scaling factor by which the quads in vbo_ are scaled
GLenum blendDest
glBlendFunc: GL_SRC_ALPHA, GL_ZERO, GL_ONE, GL_ONE_MINUS_SRC_ALPHA ...
void scale(double _s)
scale by (_s, _s, _s)
static int imageHeight_
height of the generated texture
BaseNode * parent()
Get the nodes parent node.
GLenum alphaTestFunc_
stores alpha test comparison function
bool depthEnabled_
stores if GL_DEPTH_TEST was enabled on entering TextNode
VectorT< double, 3 > Vec3d
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 ambientColor(const Vec4f &_a)
set the ambient color.
static const unsigned int rows_
number of rows of characters in the texture
static const unsigned int columns_
number of columns of characters in the texture
void specularColor(const Vec4f &_s)
set the specular color
void reset_modelview()
reset modelview matrix (load identity)
const VertexDeclaration * vertexDecl
Defines the vertex buffer layout, ignored if VAO is provided.
static int imageWidth_
width of the generated texture
GLint blendDest_
stores the dfactor parameter of glBlendFunc on entering TextNode
static QFont qfont_
font that is used to generate the texture in updateFont()