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