diff --git a/lightpreview/glview.cpp b/lightpreview/glview.cpp index 48bc13e8..4aca21b7 100644 --- a/lightpreview/glview.cpp +++ b/lightpreview/glview.cpp @@ -29,6 +29,9 @@ See file, 'COPYING', for details. #include #include #include +#include +#include +#include #include #include @@ -308,6 +311,40 @@ void GLView::setDrawFlat(bool drawflat) update(); } +void GLView::takeScreenshot(int w, int h) +{ + // update aspect ratio + float backupDisplayAspect = m_displayAspect; + m_displayAspect = static_cast(w) / static_cast(h); + + makeCurrent(); + { + QOpenGLFramebufferObjectFormat format; + format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); + format.setSamples(4); + + QOpenGLFramebufferObject fbo(w, h, format); + assert(fbo.bind()); + + glViewport(0, 0, w, h); + paintGL(); + + QString dest = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + + QLatin1String("/Screenshots/lightpreview_") + + QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss") + QLatin1String(".png"); + + QImage image = fbo.toImage(); + image.save(dest); + + assert(fbo.release()); + } + doneCurrent(); + + // restore aspect ratio + m_displayAspect = backupDisplayAspect; + update(); +} + void GLView::renderBSP(const QString &file, const mbsp_t &bsp, const std::vector &entities) { // FIXME: move to a lightpreview_settings diff --git a/lightpreview/glview.h b/lightpreview/glview.h index 5b6cfd53..c46622e8 100644 --- a/lightpreview/glview.h +++ b/lightpreview/glview.h @@ -121,6 +121,8 @@ public: void setShowTris(bool showtris); void setDrawFlat(bool drawflat); + void takeScreenshot(int w, int h); + protected: void initializeGL() override; void paintGL() override; diff --git a/lightpreview/mainwindow.cpp b/lightpreview/mainwindow.cpp index ff8bbb6e..619bd629 100644 --- a/lightpreview/mainwindow.cpp +++ b/lightpreview/mainwindow.cpp @@ -129,6 +129,8 @@ void MainWindow::setupMenu() auto *openRecent = menu->addAction(tr("Open &Recent")); + auto *takeScreenshot = menu->addAction(tr("Take Screenshot"), this, [=]() { glView->takeScreenshot(3840, 2160); }); + auto *exit = menu->addAction(tr("E&xit"), this, &QWidget::close); exit->setShortcut(QKeySequence::Quit); }