首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用QOpenGLWidget呈现文本

如何使用QOpenGLWidget呈现文本
EN

Stack Overflow用户
提问于 2015-01-29 21:30:31
回答 5查看 14.8K关注 0票数 16

在旧版本的Qt中,有一个叫做renderText的很好的函数,叫做QGLWidget。现在我使用的是QOpenGLWidget类,缺少渲染文本的功能。

有没有一种使用QOpenGLWidget呈现文本的简单方法?我不喜欢用OpenGL从头开始构建整个文本渲染……

EN

回答 5

Stack Overflow用户

发布于 2015-11-12 22:43:38

您可以基于旧的Qt源代码自行实现此功能。

在从QOpenGLWidget继承的OpenGL小部件类(在本例中为GLBox)中,您必须实现以下方法:

renderText:

代码语言:javascript
复制
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();
}

项目:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
}

如果因为必须将Qt4应用程序移植到Qt5而需要renderText(),那么只需确保将这里提供的函数的签名更改为

代码语言:javascript
复制
void GLBox::renderText(double x, double y, double z, const QString & str, const QFont & font = QFont(), int listBase = 2000)

你再也不用担心这个了。

票数 5
EN

Stack Overflow用户

发布于 2015-12-11 15:22:35

我最终做了一个类似@jaba所写的解决方案。我还注意到一些图形损坏,除非我在方法结束时调用painter.end()。

代码语言:javascript
复制
void MapCanvas::renderText(double x, double y, double z, const QString &str, const QFont & font = QFont()) {
    // Identify x and y locations to render text within widget
    int height = this->height();
    GLdouble textPosX = 0, textPosY = 0, textPosZ = 0;
    project(x, y, 0f, &textPosX, &textPosY, &textPosZ);
    textPosY = height - textPosY; // y is inverted

    // Retrieve last OpenGL color to use as a font color
    GLdouble glColor[4];
    glGetDoublev(GL_CURRENT_COLOR, glColor);
    QColor fontColor = QColor(glColor[0], glColor[1], glColor[2], glColor[3]);

    // Render text
    QPainter painter(this);
    painter.setPen(fontColor);
    painter.setFont(font);
    painter.drawText(textPosX, textPosY, text);
    painter.end();
}
票数 5
EN

Stack Overflow用户

发布于 2015-07-10 22:21:48

如果您似乎想要绘制2D文本,请使用QPainter::drawText()。有关在QOpenGLWidget上使用QPainter的信息,请参阅here。有关在QOpenGLWidgets上使用抗锯齿进行文本呈现的信息,请参阅here

如果您想绘制2.5D文本(2D文本随3D场景移动),那么滚动您自己的类并不“太难”。使用QFont和QFontMetricsF为您的字体字形构建纹理,为每个字形构建一些四边形到VBO中,并为字符串中的字形绘制适当的四边形……

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28216001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档