From 5f484abc2d0176015c4286459162bbf40759e1ac Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Fri, 28 Oct 2016 19:16:27 +0200 Subject: [PATCH] #279 Create QImage from offscreen frame buffer object --- Fwk/AppFwk/cafViewer/cafViewer.cpp | 32 +++++++++++++++++++++++++++--- Fwk/AppFwk/cafViewer/cafViewer.h | 2 ++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Fwk/AppFwk/cafViewer/cafViewer.cpp b/Fwk/AppFwk/cafViewer/cafViewer.cpp index 8af484e1c7..3a1907bb20 100644 --- a/Fwk/AppFwk/cafViewer/cafViewer.cpp +++ b/Fwk/AppFwk/cafViewer/cafViewer.cpp @@ -119,7 +119,9 @@ caf::Viewer::Viewer(const QGLFormat& format, QWidget* parent) m_releaseOGLResourcesEachFrame(false), m_paintCounter(0), m_navigationPolicyEnabled(true), - m_isOverlayPaintingEnabled(true) + m_isOverlayPaintingEnabled(true), + m_offscreenViewportWidth(0), + m_offscreenViewportHeight(0) { m_layoutWidget = parentWidget(); @@ -497,6 +499,9 @@ void caf::Viewer::resizeGL(int width, int height) m_offscreenFbo->resizeAttachedBuffers(width, height); m_quadRendering->camera()->viewport()->set(0, 0, width, height); + + m_offscreenViewportWidth = width; + m_offscreenViewportHeight = height; } updateCamera(width, height); @@ -810,9 +815,30 @@ bool caf::Viewer::isShadersSupported() QImage caf::Viewer::snapshotImage() { QImage image; - if (m_offscreenTexture.notNull() && m_offscreenTexture->image()) + if (m_offscreenFbo.notNull() && m_offscreenViewportWidth > 0 && m_offscreenViewportHeight > 0) { - image = cvfqt::Utils::toQImage(*(m_offscreenTexture->image())); + cvf::ref myOglContext = cvfOpenGLContext(); + + // TODO: Is this required??? + //m_offscreenFbo->bind(myOglContext.p()); + + GLint iOldPackAlignment = 0; + glGetIntegerv(GL_PACK_ALIGNMENT, &iOldPackAlignment); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + CVF_CHECK_OGL(myOglContext.p()); + + cvf::UByteArray arr(3 * m_offscreenViewportWidth * m_offscreenViewportHeight); + + glReadPixels(0, 0, static_cast(m_offscreenViewportWidth), static_cast(m_offscreenViewportHeight), GL_RGB, GL_UNSIGNED_BYTE, arr.ptr()); + CVF_CHECK_OGL(myOglContext.p()); + + glPixelStorei(GL_PACK_ALIGNMENT, iOldPackAlignment); + CVF_CHECK_OGL(myOglContext.p()); + + cvf::TextureImage texImage; + texImage.setFromRgb(arr.ptr(), m_offscreenViewportWidth, m_offscreenViewportHeight); + + image = cvfqt::Utils::toQImage(texImage); } else { diff --git a/Fwk/AppFwk/cafViewer/cafViewer.h b/Fwk/AppFwk/cafViewer/cafViewer.h index 38dad95e5b..c90fb63721 100644 --- a/Fwk/AppFwk/cafViewer/cafViewer.h +++ b/Fwk/AppFwk/cafViewer/cafViewer.h @@ -231,6 +231,8 @@ private: // Offscreen render objects cvf::ref m_offscreenFbo; cvf::ref m_offscreenTexture; + int m_offscreenViewportWidth; + int m_offscreenViewportHeight; cvf::ref m_quadRendering; };