Qt:QOpenGLWidget中的文本渲染

时间:2015-04-12 05:49:20

标签: c++ qt opengl

我想在QOpenGLWidget上绘制文字标签。我尝试使用QPainter执行此任务,但没有成功 - 文本看起来很丑陋而且没有抗锯齿。在Qt OpenGL / 2dpainting样本中看起来也很丑陋。然而,在使用OpenGL后端的QML控件中,文本呈现明显更好。这里http://blog.qt.io/blog/2011/07/15/text-rendering-in-the-qml-scene-graph/我发现了一种在QML中使用的技术。有没有办法使用该技术在QOpenGLWidget中绘制文本?

PS:可能是正确的方法是将我的所有场景嵌入到QQuickWidget的QSGSceneGraph中?

4 个答案:

答案 0 :(得分:2)

确保使用支持多重采样以进行抗锯齿的表面格式创建QGL / QopenGLWidget:

QSurfaceFormat format = QSurfaceFormat::defaultFormat();
format.setSamples(4);
QOpenGLWidget * glWidget = new QOpenGLWidget;
glWidget->setFormat(format);

然后,在该窗口小部件上使用QPainter时,请确保打开抗锯齿:

QPainter painter(paintDevice);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

然后画出你的文字。

答案 1 :(得分:1)

重新实现旧的QOGLWidget的renderText()以方便文本呈现。 这样文本将随场景移动。如果您想要静态版本,可以简化它。

使用以下方法从QOpenGLWidget(在此示例中为GLBox)中继承一个类:

renderText:

void GLBox::renderText(D3DVECTOR &textPosWorld, QString text)
{
    int width = this->width();
    int height = this->height();

    GLdouble model[4][4], proj[4][4];
    GLint view[4];
    glGetDoublev(GL_MODELVIEW_MATRIX, &model[0][0]);
    glGetDoublev(GL_PROJECTION_MATRIX, &proj[0][0]);
    glGetIntegerv(GL_VIEWPORT, &view[0]);
    GLdouble textPosX = 0, textPosY = 0, textPosZ = 0;

    project(textPosWorld.x, textPosWorld.y, textPosWorld.z, 
                &model[0][0], &proj[0][0], &view[0],
                &textPosX, &textPosY, &textPosZ);

    textPosY = height - textPosY; // y is inverted

    QPainter painter(this);
    painter.setPen(Qt::yellow);
    painter.setFont(QFont("Helvetica", 8));
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.drawText(textPosX, textPosY, text); // z = pointT4.z + distOverOp / 4
    painter.end();
}

项目:

inline GLint GLBox::project(GLdouble objx, GLdouble objy, GLdouble objz,
    const GLdouble model[16], const GLdouble proj[16],
    const GLint viewport[4],
    GLdouble * winx, GLdouble * winy, GLdouble * winz)
{
    GLdouble in[4], out[4];

    in[0] = objx;
    in[1] = objy;
    in[2] = objz;
    in[3] = 1.0;
    transformPoint(out, model, in);
    transformPoint(in, proj, out);

    if (in[3] == 0.0)
        return GL_FALSE;

    in[0] /= in[3];
    in[1] /= in[3];
    in[2] /= in[3];

    *winx = viewport[0] + (1 + in[0]) * viewport[2] / 2;
    *winy = viewport[1] + (1 + in[1]) * viewport[3] / 2;

    *winz = (1 + in[2]) / 2;
    return GL_TRUE;
}

最后是transformPoint:

inline void GLBox::transformPoint(GLdouble out[4], const GLdouble m[16], const GLdouble in[4])
{
#define M(row,col)  m[col*4+row]
    out[0] =
        M(0, 0) * in[0] + M(0, 1) * in[1] + M(0, 2) * in[2] + M(0, 3) * in[3];
    out[1] =
        M(1, 0) * in[0] + M(1, 1) * in[1] + M(1, 2) * in[2] + M(1, 3) * in[3];
    out[2] =
        M(2, 0) * in[0] + M(2, 1) * in[1] + M(2, 2) * in[2] + M(2, 3) * in[3];
    out[3] =
        M(3, 0) * in[0] + M(3, 1) * in[1] + M(3, 2) * in[2] + M(3, 3) * in[3];
#undef M
}

答案 2 :(得分:0)

对画家稍加旋转似乎可以增加文本的平滑度,但是质量有些中等:

clf.predict(np.array(30,4000,1))

答案 3 :(得分:0)

我在混合QPainterQOpenGLWidet时遇到了一些麻烦,特别是使用QOpenGLWidget作为绘画设备来渲染单个文本元素时。结果,我得到了清晰色彩的全屏显示。我以前的所有代码似乎都坏了。 因此,我想出了另一种使用QGraphicsScene作为绘画设备的解决方案。随后,我将绘画者渲染到QImage中,可以用来创建普通的2D-{QOpenGLTexture。可以照常通过QOpenGLBuffer缓冲区绘制此纹理。

void MyQOpenGLWidget::createTexture(int x, int y, int z, const QString& str, const QFont& font) {
    // create an image of the text with transparent backround
    QGraphicsScene scene;
    scene.addText(str, font);
    QImage img(scene.width(), scene.height(), QImage::Format_ARGB32_Premultiplied);
    img.fill(QColor(0, 0, 0, 0));
    QPainter painter(&img);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    scene.render(&painter);

    // store the tex as member somewhere in the QOpenGLWidget
    QOpenGLTexture tex(img);

    // store the bufferindex as member somewhere in the QOpenGLWidget
    int bufferIndex = 0
    /*this is the index corresponding to first element of coords writen into buffer
    -> the number of elements within your tex container times SPRITE_BUFFER_SIZE seems to be reasonable*/

    // calculate coordinates 
    GLfloat coords[SPRITE_BUFFER_SIZE];
    /* [SPRITE_BUFFER_SIZE=20=4*5]->4 corner points
    3-dimensions +2 binary (determine corner point)*/

    // write to buffer 
    buffer.bind()
    buffer.write(sizeof(GLfloat)*bufferIndex, coords, SPRITE_BUFFER_SIZE * sizeof(GLfloat))
}

然后在paintGL中绘制所创建的纹理

void MyQOpenGLWidget::paintGL() {
    // somewhere within this function
    tex.bind();
    glDrawArrays(GL_TRIANGLE_FAN, bufferIndex / 5, 4);
}

确保使用正确的混合功能。

void MyQOpenGLWidget::initializeGL()
{
    initializeOpenGLFunctions();

    // somewhere within initializeGL
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
}
相关问题