我是 Opengl 中混合和纹理的新手。我的 render_text() 方法使用 QPainter 中的 drawText 方法,并将 QImage 作为其设备。QImage 变为 GL_TEXTURE_2D,然后附加到 GL_QUADS。文本在 2d 场景中正确显示为黑色。我想知道如何任意设置颜色。如果有人可以推荐一个关于源到目标混合如何工作的好教程,也会受益。
void GLView::render_text(char *txt, quint8 height)
{
QImage image;
qint32 font_height, width;
font_height = height * VIEW_SIZE / (ZOOM * OBJ_HEIGHT);
QFont font("Helvetica", font_height);
QFontMetrics fm(font);
// render text in QImage
QImage img(fm.width(txt), fm.height(), QImage::Format_ARGB32);
img.fill(0);
QPainter pixPaint(&img);
pixPaint.setFont(font);
pixPaint.drawText(0, font_height, txt);
// push to gl
image = QGLWidget::convertToGLFormat(img);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//glBlendColor(1, 0, 0, 0.5);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image.width(), image.height(), GL_RGBA, GL_UNSIGNED_BYTE, image.bits() );
width = image.width() * ZOOM * OBJ_WIDTH / VIEW_SIZE; //calc object width from image width
glBegin(GL_QUADS);
glTexCoord2i(0,0); glVertex2f(0, 0);
glTexCoord2i(0,1); glVertex2f(0, height);
glTexCoord2i(1,1); glVertex2f(width, height);
glTexCoord2i(1,0); glVertex2f(width, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDeleteTextures(1, &texture);
}