看本文章的人我相信都不需要我介绍Qt和OpenGL了。文字的绘制一直都是3D开发的一个难题,本文将介绍一种非常简单,效率也还可以的方法来渲染文字到OpenGL。
先讲原理,Qt的QPainter拥有将内容绘制到QImage设备的能力,也就是直接会知道文字,因此,可以通过QPainter的drawText方法将文本绘制到一张Image,再创建成纹理,渲染到OpenGL目标中。
先给一个效果图:
从文字创建纹理的方法如下:
QOpenGLTexture *GLWindow::genTexture(int width, int height, const QString &text, int textPixelSize, const QColor &textColor)
{
QOpenGLTexture *texture = new QOpenGLTexture(QOpenGLTexture::Target2D);
QImage img(width, height, QImage::Format_ARGB32_Premultiplied);//img是图片,模式为