Skip to content

Instantly share code, notes, and snippets.

@giraphics
Created June 25, 2018 03:53
Show Gist options
  • Save giraphics/e14d40d243eb9bbff8fbc32f2ba97dfb to your computer and use it in GitHub Desktop.
Save giraphics/e14d40d243eb9bbff8fbc32f2ba97dfb to your computer and use it in GitHub Desktop.
#include <QtCore>
#include <QtGui>
#include <QtWidgets>
QImage createImageWithFBO()
{
QSurfaceFormat format;
format.setMajorVersion(3);
format.setMinorVersion(3);
QWindow window;
window.setSurfaceType(QWindow::OpenGLSurface);
window.setFormat(format);
window.create();
QOpenGLContext context;
context.setFormat(format);
if (!context.create())
qFatal("Cannot create the requested OpenGL context!");
context.makeCurrent(&window);
const QRect drawRect(0, 0, 400, 400);
const QSize drawRectSize = drawRect.size();
QOpenGLFramebufferObjectFormat fboFormat;
fboFormat.setSamples(16);
fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
QOpenGLFramebufferObject fbo(drawRectSize, fboFormat);
fbo.bind();
QOpenGLPaintDevice device(drawRectSize);
QPainter painter;
painter.begin(&device);
painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing);
painter.fillRect(drawRect, Qt::blue);
painter.drawTiledPixmap(drawRect, QPixmap(":/qt-project.org/qmessagebox/images/qtlogo-64.png"));
painter.setPen(QPen(Qt::green, 5));
painter.setBrush(Qt::red);
painter.drawEllipse(0, 100, 400, 200);
painter.drawEllipse(100, 0, 200, 400);
painter.setPen(QPen(Qt::white, 0));
QFont font;
font.setPointSize(24);
painter.setFont(font);
painter.drawText(drawRect, "Hello FBO", QTextOption(Qt::AlignCenter));
painter.end();
fbo.release();
return fbo.toImage();
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QImage targetImage = createImageWithFBO();
QLabel label;
label.setPixmap(QPixmap::fromImage(targetImage));
label.show();
return app.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment