4
votes

I'm using QGLWidget and this code to draw a text on the screen but the rendering is catastrophic if the string's length is too high : text rendering issue

Here's my code :

glPushMatrix();
glRotatef(90, 0, 0, 1);
QString qStr = QString("Here's a very long string which doesn't mean anything at all but had some rendering problems");
renderText(0.0, 0.0, 0.0, qStr);
glPopMatrix();
2
renderText is not a function that I am aware of from any library. If it's from a library, then tell us which one. If not, then provide the code for it. - Nicol Bolas
Just edit my post with source. - Thomas Ayoub
renderText() is part of the OpenGL API inside the Qt framework. You can deduce that based on the fact it takes a QString as input. - rbaleksandar

2 Answers

3
votes

I had the exact same problem when using Helvetica. Changing the font to Arial solved it.

I did a small wrapper around it to make things easier:

void _draw_text(double x, double y, double z, QString txt)
{
    glDisable(GL_LIGHTING);
    glDisable(GL_DEPTH_TEST);
    qglColor(Qt::white);
    renderText(x, y, z, txt, QFont("Arial", 12, QFont::Bold, false) );
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
}
0
votes

From the documentation:

This function can only be used inside a QPainter::beginNativePainting()/QPainter::endNativePainting() block if the default OpenGL paint engine is QPaintEngine::OpenGL. To make QPaintEngine::OpenGL the default GL engine, call QGL::setPreferredPaintEngine(QPaintEngine::OpenGL) before the QApplication constructor.

Hence, have you tried to use QPainter::beginNativePainting() just before the call, and QPainter::endNativePainting() just after?

Also, note that the text is rendered in window coordinate, not taking into account at all your current OpenGL matrix state (in short, your glRotatef(90, 0, 0, 1) call has no effect). You can see in the implementation here that they save your current OpenGL state by calling qt_save_gl_state(), then create their brand new matrices with:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width, height);
glOrtho(0, width, height, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Then draw the text, and finally restore your previous OpenGL state with qt_restore_gl_state()