void renderText( QGLWidget* w, int x, int y, const QString& text, const QColor& col = Qt::white, const QFont& font = QFont() ){ glMatrixMode( GL_PROJECTION ); glPushMatrix(); glLoadIdentity(); glOrtho( 0, w->width(), w->height(), 0, 0, 1 ); glMatrixMode( GL_MODELVIEW ); glPushMatrix(); glLoadIdentity(); QFontMetrics fm(font); QRect rect = fm.boundingRect( text); QPixmap pixmap( rect.size() ); pixmap.fill( Qt::black ); QPainter painter(&pixmap); painter.setPen( Qt::white ); painter.setFont( font ); painter.drawText( -rect.left(), -rect.top(), text ); QImage img = pixmap.convertToImage(); img.setAlphaBuffer( true ); for ( int i = 0; i < img.height(); i++ ) { QRgb* rgb = (QRgb*) img.scanLine(i); for ( int j = 0; j < img.width(); j++ ) { rgb[j] = qRgba( col.red(), col.green(), col.blue(), qRed(rgb[j]) ); } } img = QGLWidget::convertToGLFormat(img); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glRasterPos2i( x, y ); glDrawPixels( rect.width(), rect.height(), GL_RGBA, GL_UNSIGNED_BYTE, img.bits() ); glDisable(GL_BLEND); glMatrixMode( GL_PROJECTION ); glPopMatrix(); glMatrixMode( GL_MODELVIEW ); glPopMatrix(); }