57 #include <ACG/GL/acg_glew.hh> 59 #include "TextNode.hh" 60 #include "../Utils/ImageConversion.hh" 76 QFont TextNode::qfont_ = QFont(
"Helvetica", 30);
92 const std::string& _name,
102 blendEnabled_(false),
103 texture2dEnabled_(false),
104 cullFaceEnabled_(false),
105 depthEnabled_(false),
106 alwaysOnTop_(_alwaysOnTop),
108 alphaTestValue_(0.5f),
109 alphaTestFunc_(GL_GREATER),
129 glDeleteBuffers(1, &
vbo_);
223 std::map< char, std::pair<unsigned int, unsigned int> >
226 std::map< char, std::pair<unsigned int, unsigned int> > m;
227 unsigned char c =
' ';
228 for (
unsigned int i = 0; i <
rows_; ++i) {
229 for (
unsigned int j = 0; j <
columns_; ++j, ++c) {
230 m[c] = std::make_pair(j, i);
244 if(_state.compatibilityProfile())
245 enterCompat(_state,_drawmode);
276 if(_state.compatibilityProfile())
277 leaveCompat(_state, _drawmode);
306 if (!
text_.empty()) {
316 glDrawArrays(GL_TRIANGLES, 0,
int(
text_.size() * 6) );
332 quint32 n = num > 0 ? num - 1 : 0;
368 QFontMetricsF metric(
qfont_);
369 for (
char c =
' '; c <
'~'; ++c) {
370 qreal width = metric.width(c) + std::abs(metric.leftBearing(c)) + std::abs(metric.rightBearing(c));
375 qreal height = metric.height();
384 finalImage.fill(Qt::transparent);
386 painter.begin(&finalImage);
387 painter.setRenderHints(QPainter::HighQualityAntialiasing
388 | QPainter::TextAntialiasing);
393 for (
char c =
' '; c <
'~'; ++c) {
394 std::pair<unsigned int, unsigned int> coords =
charToIndex_[c];
395 painter.drawText(coords.first*widthPow2,
imageHeight_ - (coords.second+1)*heightPow2, widthPow2, heightPow2, Qt::AlignLeft | Qt::AlignBottom, QString(c));
400 finalImage = ACG::Util::convertToGLFormat(finalImage);
407 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
408 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
409 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
410 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
411 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, finalImage.width(), finalImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, finalImage.bits());
412 glGenerateMipmap(GL_TEXTURE_2D);
425 if (
text_.size() == 0)
435 QFontMetricsF metric(
qfont_);
436 qreal avgWidth = metric.averageCharWidth();
438 float lastCharRight = 0.0f;
439 for (
unsigned int i = 0; i <
text_.size(); ++i) {
448 left = lastCharRight;
450 right = (left + width);
451 lastCharRight = right;
454 qreal leftBearing = std::abs(metric.leftBearing(
text_[i]));
455 qreal rightBearing = std::abs(metric.rightBearing(
text_[i]));
456 qreal metricWidth = metric.width(
text_[i]);
459 metricWidth += leftBearing + rightBearing;
462 const float widthTx = (float) metricWidth / (
float)
imageWidth_;
463 const float heightTx = (float) height/ (
float)
imageHeight_;
467 const float rightTx = leftTx + widthTx;
469 const float topTx = bottomTx + heightTx;
527 glGenBuffers(1, &
vbo_);
533 glBufferData( GL_ARRAY_BUFFER_ARB,
vertexBuffer_.size() *
sizeof(GLfloat), 0, GL_DYNAMIC_DRAW_ARB );
538 GLfloat *data =
reinterpret_cast<GLfloat*
>(glMapBuffer( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB ));
542 glUnmapBuffer(GL_ARRAY_BUFFER_ARB);
589 ro.debugName = std::string(
"TextNode: ")+
name();
609 ro.blendSrc = GL_SRC_ALPHA;
622 texture.type = GL_TEXTURE_2D;
623 texture.shadow =
false;
636 ro.setMaterial(&localMaterial);
638 ro.glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(
text_.size()) * 6);
653 scale = pTrans->
scale()(0,0);
656 pParent = pParent->
parent();
671 scale *= heightUnproj.
length();
BaseNode * parent()
Get the nodes parent node.
bool depthEnabled_
stores if GL_DEPTH_TEST was enabled on entering TextNode
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
void reset_modelview()
reset modelview matrix (load identity)
static QFont qfont_
font that is used to generate the texture in updateFont()
unsigned pixelSize_
pixelSize of the text for the SCREEN_ALIGNED_STATIC_SIZE mode
static GLuint texture_
handle for the texture into which characters from qfont_ are painted in updateFont() ...
void pop_modelview_matrix()
pop modelview matrix
GLint blendDest_
stores the dfactor parameter of glBlendFunc on entering TextNode
static std::map< char, std::pair< unsigned int, unsigned int > > charToIndex_
maps most readable characters to indices for texture coordinate calculation in updateVBO() ...
static std::map< char, std::pair< unsigned int, unsigned int > > createMap()
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode) override
draw Text
static qreal maxFontWidth_
max width of qfont
void addTexture(const Texture &_t)
adds a texture to stage RenderObjects::numTextures()
Namespace providing different geometric functions concerning angles.
static bool initialised_
this is used to ensure that the texture is only generated once when necessary
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode) override
restore texture and drawing states
void applyScreenAligned(GLState &_state)
modifies _state so that the modelviewmatrix will be screenaligned. remember to call "_state...
Vec3d project(const Vec3d &_point) const
project point in world coordinates to window coordinates
void setFont(const QFont &_font)
sets the font to be used for generating a texture with most characters of the chosen font ...
void baseColor(const Vec4f &_c)
set the base color (Sets the baseColor which is the same as the emission(const Vec4f& _c) ) ...
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
static void enableClientState(GLenum _cap)
replaces glEnableClientState, supports locking
ShaderGenDesc shaderDesc
Drawmode and other shader params.
void unbindVBO()
unbinds vbo_
static void bindBufferARB(GLenum _target, GLuint _buffer)
same function as bindBuffer
Text will always stay parallel to screen.
void scale(double _s)
scale by (_s, _s, _s)
void bindVBO()
binds vbo_ and sets the necessary OpenGL states
GLuint vertexBuffer
VBO, IBO ids, ignored if VAO is provided.
static void bindBuffer(GLenum _target, GLuint _buffer)
replaces glBindBuffer, supports locking
static void disableClientState(GLenum _cap)
replaces glDisableClientState, supports locking
void setSize(const double _size)
sets the size by which the quads displaying the text will be scaled
double size_
scaling factor by which the quads in vbo_ are scaled
Vec3d unproject(const Vec3d &_winPoint) const
unproject point in window coordinates _winPoint to world coordinates
void ambientColor(const Vec4f &_a)
set the ambient color.
void push_modelview_matrix()
push modelview matrix
TextMode textMode_
current display mode of text_ (SCREEN_ALIGNED, SCREEN_ALIGNED_STATIC_SIZE or OBJECT_ALIGNED) ...
void diffuseColor(const Vec4f &_d)
set the diffuse color.
static quint32 nearestPowerOfTwo(quint32 num)
returns the nearest greater power of 2 to num
void addElement(const VertexElement *_pElement)
bool blendEnabled_
stores if GL_BLEND was enabled on entering TextNode
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax) override
update bounding box
GLint blendSrc_
stores the sfactor parameter of glBlendFunc on entering TextNode
float lastScale_
stores the last scaling factor the text computed to SCREEN_ALIGNED_STATIC_SIZE
bool cullFaceEnabled_
stores if GL_CULL_FACE was enabled on entering TextNode
BaseNode * parent()
Get the nodes parent node.
const VertexDeclaration * vertexDecl
Defines the vertex buffer layout, ignored if VAO is provided.
const GLMatrixd & modelview() const
get modelview matrix
static const unsigned int columns_
number of columns of characters in the texture
static int imageWidth_
width of the generated texture
DrawMode POINTS
draw unlighted points using the default base color
bool alwaysOnTop_
stores if text should be drawn always on top
void setPixelSize(const unsigned int _size)
sets the pixelsize of the text (only available for the SCREEN_ALIGNED_STATIC_SIZE mode and only works...
static void texcoordPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glTexcoordPointer, supports locking
virtual void addRenderObject(RenderObject *_renderObject)
Callback for the scenegraph nodes, which send new render objects via this function.
TextNode(BaseNode *_parent=0, const std::string &_name="<TextNode>", TextMode _textMode=SCREEN_ALIGNED, bool _alwaysOnTop=false)
static void vertexPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glVertexPointer, supports locking
void setText(std::string _text)
sets the string that will be rendered
int priority
Priority to allow sorting of objects.
std::vector< GLfloat > vertexBuffer_
buffer of vertex coordinates and texture coordinates of the quads
static void activeTexture(GLenum _texunit)
replaces glActiveTexture, no locking support
auto length() const -> decltype(std::declval< VectorT< S, DIM >>().norm())
compute squared euclidean norm
static void blendFunc(GLenum _sfactor, GLenum _dfactor)
replaces glBlendFunc, supports locking
void getRenderObjects(ACG::IRenderer *_renderer, ACG::GLState &_state, const ACG::SceneGraph::DrawModes::DrawMode &_drawMode, const ACG::SceneGraph::Material *_mat) override
set RenderObject for Shader pipeline renderer
DrawMode POINTS_COLORED
draw colored, but not lighted points (requires point colors)
void push_modelview_matrix()
push modelview matrix
static const unsigned int rows_
number of rows of characters in the texture
void specularColor(const Vec4f &_s)
set the specular color
void setRenderingMode(TextMode _textMode)
static void enable(GLenum _cap, bool _warnRemoved=true)
replaces glEnable, but supports locking
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode) override
set texture and drawing states
TextMode renderingMode()
returns the rendering mode (SCREEN_ALIGNED or OBJECT_ALIGNED)
bool alwaysOnTop()
returns wheter always on top is setted or not
void setAlwaysOnTop(bool _alwaysOnTop)
draw the text always on top
std::string name() const
Returns: name of node (needs not be unique)
GLenum blendDest
glBlendFunc: GL_SRC_ALPHA, GL_ZERO, GL_ONE, GL_ONE_MINUS_SRC_ALPHA ...
DrawMode POINTS_SHADED
draw shaded points (requires point normals)
ACG::VertexDeclaration vertexDecl_
stores the vertex declaration
DrawModes::DrawMode availableDrawModes() const override
return available draw modes
static int imageHeight_
height of the generated texture
void scale(double _s)
scale by (_s, _s, _s)
void pop_modelview_matrix()
pop modelview matrix
Interface class between scenegraph and renderer.
static void bindTexture(GLenum _target, GLuint _buffer)
replaces glBindTexture, supports locking
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
std::string text_
text to be displayed on quads in vbo_
static QColor color_
color that is used to draw the characters into the texture in updateFont()
VectorT< double, 3 > Vec3d
GLMatrixd modelview
Modelview transform.