Qt5OpenGL: make off-screen renderer working

This commit is contained in:
wmayer
2017-03-12 18:22:36 +01:00
parent 67f05c98fa
commit 34f277e7bb
6 changed files with 59 additions and 14 deletions

View File

@@ -46,6 +46,11 @@
#include "SoFCOffscreenRenderer.h"
#include "BitmapFactory.h"
#if defined(HAVE_QT5_OPENGL)
# include <QOffscreenSurface>
# include <QOpenGLContext>
#endif
using namespace Gui;
using namespace std;
@@ -474,7 +479,7 @@ SoQtOffscreenRenderer::getViewportRegion(void) const
before rendering.
*/
void
SoQtOffscreenRenderer::setBackgroundColor(const SbColor & color)
SoQtOffscreenRenderer::setBackgroundColor(const SbColor4f & color)
{
PRIVATE(this)->backgroundcolor = color;
}
@@ -482,7 +487,7 @@ SoQtOffscreenRenderer::setBackgroundColor(const SbColor & color)
/*!
Returns the background color.
*/
const SbColor &
const SbColor4f &
SoQtOffscreenRenderer::getBackgroundColor(void) const
{
return PRIVATE(this)->backgroundcolor;
@@ -582,6 +587,9 @@ SoQtOffscreenRenderer::makeFrameBuffer(int width, int height, int samples)
QtGLFramebufferObjectFormat fmt;
fmt.setSamples(samples);
fmt.setAttachment(QtGLFramebufferObject::Depth);
#if defined(HAVE_QT5_OPENGL)
fmt.setInternalTextureFormat(GL_RGB32F_ARB);
#endif
#else
QtGLFramebufferObject::Attachment fmt;
fmt = QtGLFramebufferObject::Depth;
@@ -596,6 +604,19 @@ SoQtOffscreenRenderer::renderFromBase(SoBase * base)
{
const SbVec2s fullsize = this->viewport.getViewportSizePixels();
#if defined(HAVE_QT5_OPENGL)
QSurfaceFormat format;
format.setSamples(PRIVATE(this)->numSamples);
QOpenGLContext context;
context.setFormat(format);
if (!context.create())
return false;
QOffscreenSurface offscreen;
offscreen.setFormat(format);
offscreen.create();
context.makeCurrent(&offscreen);
#endif
#if !defined(HAVE_QT5_OPENGL)
if (PRIVATE(this)->pbuffer) {
if (!pixelbuffer) {
@@ -631,7 +652,7 @@ SoQtOffscreenRenderer::renderFromBase(SoBase * base)
glClearColor(this->backgroundcolor[0],
this->backgroundcolor[1],
this->backgroundcolor[2],
0.0f);
this->backgroundcolor[3]);
// needed to clear viewport after glViewport() is called from
// SoGLRenderAction
@@ -660,6 +681,11 @@ SoQtOffscreenRenderer::renderFromBase(SoBase * base)
this->renderaction->setCacheContext(oldcontext); // restore old
#if defined(HAVE_QT5_OPENGL)
glImage = framebuffer->toImage();
context.doneCurrent();
#endif
return true;
}
@@ -730,12 +756,13 @@ SoQtOffscreenRenderer::writeToImage (QImage& img) const
if (pixelbuffer)
img = pixelbuffer->toImage();
}
else
#endif
{
else {
if (framebuffer)
img = framebuffer->toImage();
}
#else
img = this->glImage;
#endif
}
/*!

View File

@@ -26,11 +26,11 @@
#include <Inventor/SoOffscreenRenderer.h>
#include <Inventor/SbMatrix.h>
#include <Inventor/SbColor4f.h>
#include <QImage>
#include <QStringList>
#include <QtOpenGL.h>
class QImage;
namespace Gui {
/**
@@ -108,8 +108,8 @@ public:
void setViewportRegion(const SbViewportRegion & region);
const SbViewportRegion & getViewportRegion(void) const;
void setBackgroundColor(const SbColor & color);
const SbColor & getBackgroundColor(void) const;
void setBackgroundColor(const SbColor4f & color);
const SbColor4f & getBackgroundColor(void) const;
void setGLRenderAction(SoGLRenderAction * action);
SoGLRenderAction * getGLRenderAction(void) const;
@@ -142,11 +142,14 @@ private:
uint32_t cache_context; // our unique context id
SbViewportRegion viewport;
SbColor backgroundcolor;
SbColor4f backgroundcolor;
SoGLRenderAction * renderaction;
SbBool didallocation;
SbBool pbuffer;
int numSamples;
#if defined(HAVE_QT5_OPENGL)
QImage glImage;
#endif
};
} // namespace Gui

View File

@@ -136,7 +136,14 @@ void Thumbnail::createThumbnailFromFramebuffer(QImage& img) const
// Alternative way of off-screen rendering
if (this->viewer->isActiveWindow()) {
static_cast<QtGLWidget*>(this->viewer->getGLWidget())->makeCurrent();
QtGLFramebufferObject fbo(this->size, this->size,QtGLFramebufferObject::Depth);
#if defined(HAVE_QT5_OPENGL)
QtGLFramebufferObjectFormat format;
format.setAttachment(QtGLFramebufferObject::Depth);
format.setInternalTextureFormat(GL_RGB32F_ARB);
QtGLFramebufferObject fbo(this->size, this->size, format);
#else
QtGLFramebufferObject fbo(this->size, this->size, QtGLFramebufferObject::Depth);
#endif
this->viewer->renderToFramebuffer(&fbo);
img = fbo.toImage();
static_cast<QtGLWidget*>(this->viewer->getGLWidget())->doneCurrent();

View File

@@ -586,6 +586,9 @@ void View3DInventor::previewFromFramebuffer(const QRect& rect, QImage& img)
QtGLFramebufferObjectFormat format;
format.setSamples(8);
format.setAttachment(QtGLFramebufferObject::Depth);
#if defined(HAVE_QT5_OPENGL)
format.setInternalTextureFormat(GL_RGB32F_ARB);
#endif
QtGLFramebufferObject fbo(rect.width(), rect.height(), format);
#else
QtGLFramebufferObject fbo(rect.width(), rect.height(), QtGLFramebufferObject::Depth);

View File

@@ -931,7 +931,7 @@ void View3DInventorViewer::savePicture(int w, int h, const QColor& bg, QImage& i
#if !defined(HAVE_QT5_OPENGL)
bool useCoinOffscreenRenderer = !QGLPixelBuffer::hasOpenGLPbuffers();
#else
bool useCoinOffscreenRenderer = true;
bool useCoinOffscreenRenderer = false;
#endif
useCoinOffscreenRenderer = App::GetApplication().GetParameterGroupByPath
("User parameter:BaseApp/Preferences/Document")->
@@ -1001,8 +1001,10 @@ void View3DInventorViewer::savePicture(int w, int h, const QColor& bg, QImage& i
root->addChild(gl);
root->addChild(pcViewProviderRoot);
#if !defined(HAVE_QT5_OPENGL)
if (useBackground)
root->addChild(cb);
#endif
root->addChild(foregroundroot);
@@ -1012,7 +1014,7 @@ void View3DInventorViewer::savePicture(int w, int h, const QColor& bg, QImage& i
SoQtOffscreenRenderer renderer(vp);
renderer.setNumPasses(4);
if (bgColor.isValid())
renderer.setBackgroundColor(SbColor(bgColor.redF(), bgColor.greenF(), bgColor.blueF()));
renderer.setBackgroundColor(SbColor4f(bgColor.redF(), bgColor.greenF(), bgColor.blueF(), bgColor.alphaF()));
if (!renderer.render(root))
throw Base::Exception("Offscreen rendering failed");

View File

@@ -708,6 +708,9 @@ void View3DInventorPy::createImageFromFramebuffer(int width, int height, const Q
QtGLFramebufferObjectFormat format;
format.setSamples(8);
format.setAttachment(QtGLFramebufferObject::Depth);
#if defined(HAVE_QT5_OPENGL)
format.setInternalTextureFormat(GL_RGB32F_ARB);
#endif
QtGLFramebufferObject fbo(width, height, format);
#else
QtGLFramebufferObject fbo(width, height, QtGLFramebufferObject::Depth);