[Interest] Qt3D Rendering offscreen

Sean Harmer sean.harmer at kdab.com
Sat Aug 12 14:40:15 CEST 2017


On Friday 11 August 2017 14:34:34 Andy wrote:
> Goal: With Qt3D (C++), render my scene offscreen, use render capture on it,
> and save image to disk.
> With my Qt3DWindow-based solution the scene looks fine and I can do the
> render capture part (on macOS anyways - Windows doesn't work for me -
> https://bugreports.qt.io/browse/QTBUG-61223 ).
> Now I need to be able to do it offscreen (without the window open).
> I thought this was going to be as simple as setting a QOffscreenSurface as
> my forward renderer surface - something like:
>    QOffscreenSurface *surface = new QOffscreenSurface;
>    surface->create();
>    Qt3DExtras::QForwardRenderer  *forwardRenderer = new
> Qt3DExtras::QForwardRenderer;
>    forwardRenderer->setSurface( surface );
>    forwardRenderer->setExternalRenderTargetSize( QSize( 800, 600 ) );
>    qDebug() << surface->format() << surface->size() << surface->isValid();
> While this gives me a valid surface and the format is correct, its size is
> always (1,1) and the render capture gives me images of that size.
> How do I set the size of the QOffscreenSurface?

You don't :) An offscreen surface is only good for making a GL context 
current. It is not suitable as a render target. If you want to do offscreen 
rendering with QOffscreenSurface, you will need to provide your own render 
target in the form of a framebuffer object.

You will need to create an FBO by way of a QRenderTarget with a 2D texture of 
the desired size attached to the color0 attachment point, and a depth format 
texture of the same size attached to the depth attachment point.

Select this with a QRenderTargetSelector in a custom framegraph to direct 
rendering into the attached textures. You can then do a pixel read back 
operation with the QRenderCapure and save the resulting image.

I suppose this could be wrapped in a convenience that allows the rest of the 
framegraph beyond the render target selector and render capture to be set.



> Or am I going about this the wrong way?
> (It would be nice to have a class like the Qt3DWindow one specifically for
> offscreen rendering.)
> ---
> Andy Maloney  //  https://asmaloney.com
> twitter ~ @asmaloney <https://twitter.com/asmaloney>

Dr Sean Harmer | sean.harmer at kdab.com | Managing Director UK
Klarälvdalens Datakonsult AB, a KDAB Group company
Tel. UK +44 (0)1625 809908, Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-independent software solutions

More information about the Interest mailing list