QGIS API Documentation  3.6.0-Noosa (5873452)
qgsoffscreen3dengine.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsoffscreen3dengine.cpp
3  --------------------------------------
4  Date : July 2018
5  Copyright : (C) 2018 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsoffscreen3dengine.h"
17 
18 #include <QOffscreenSurface>
19 #include <QSurfaceFormat>
20 
21 #include <Qt3DCore/QAspectEngine>
22 #include <Qt3DLogic/QLogicAspect>
23 #include <Qt3DRender/QCamera>
24 #include <Qt3DRender/QCameraSelector>
25 #include <Qt3DRender/QClearBuffers>
26 #include <Qt3DRender/QRenderAspect>
27 #include <Qt3DRender/QRenderCapture>
28 #include <Qt3DRender/QRenderSettings>
29 #include <Qt3DRender/QRenderTarget>
30 #include <Qt3DRender/QRenderTargetOutput>
31 #include <Qt3DRender/QRenderTargetSelector>
32 #include <Qt3DRender/QRenderSurfaceSelector>
33 #include <Qt3DRender/QTexture>
34 #include <Qt3DRender/QViewport>
35 #include <QtGui/QOpenGLContext>
36 
37 
39 {
40  // Set up the default OpenGL surface format.
41  QSurfaceFormat format;
42 
43  // by default we get just some older version of OpenGL from the system,
44  // but for 3D lines we use "primitive restart" functionality supported in OpenGL >= 3.1
45  // Qt3DWindow uses this - requesting OpenGL 4.3 - so let's request the same version.
46 #ifdef QT_OPENGL_ES_2
47  format.setRenderableType( QSurfaceFormat::OpenGLES );
48 #else
49  if ( QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL )
50  {
51  format.setVersion( 4, 3 );
52  format.setProfile( QSurfaceFormat::CoreProfile );
53  }
54 #endif
55 
56  format.setMajorVersion( 3 );
57  format.setDepthBufferSize( 32 ); // TODO: or 24? (used by QWindow3D)
58  format.setSamples( 8 );
59  QSurfaceFormat::setDefaultFormat( format );
60 
61  // Set up a camera to point at the shapes.
62  mCamera = new Qt3DRender::QCamera;
63  mCamera->lens()->setPerspectiveProjection( 45.0f, float( mSize.width() ) / float( mSize.height() ), 0.1f, 1000.0f );
64  mCamera->setPosition( QVector3D( 0, 0, 20.0f ) );
65  mCamera->setUpVector( QVector3D( 0, 1, 0 ) );
66  mCamera->setViewCenter( QVector3D( 0, 0, 0 ) );
67 
68  // Set up the engine and the aspects that we want to use.
69  mAspectEngine = new Qt3DCore::QAspectEngine();
70  mRenderAspect = new Qt3DRender::QRenderAspect( Qt3DRender::QRenderAspect::Threaded ); // Only threaded mode seems to work right now.
71  mLogicAspect = new Qt3DLogic::QLogicAspect();
72 
73  mAspectEngine->registerAspect( mRenderAspect );
74  mAspectEngine->registerAspect( mLogicAspect );
75 
76  // Create the root entity of the engine.
77  // This is not the same as the 3D scene root: the QRenderSettings
78  // component must be held by the root of the QEntity tree,
79  // so it is added to this one. The 3D scene is added as a subtree later,
80  // in setRootEntity().
81  mRoot = new Qt3DCore::QEntity();
82  mRenderSettings = new Qt3DRender::QRenderSettings( mRoot );
83  mRoot->addComponent( mRenderSettings );
84 
85  mCamera->setParent( mRoot );
86 
87  // Create the offscreen frame graph, which will manage all of the resources required
88  // for rendering without a QWindow.
89  createFrameGraph();
90 
91  // Set this frame graph to be in use.
92  // the render settings also sets itself as the parent of mSurfaceSelector
93  mRenderSettings->setActiveFrameGraph( mSurfaceSelector );
94 
95  // Set the root entity of the engine. This causes the engine to begin running.
96  mAspectEngine->setRootEntity( Qt3DCore::QEntityPtr( mRoot ) );
97 
98 }
99 
101 {
102  delete mAspectEngine;
103  delete mOffscreenSurface;
104 }
105 
107 {
108  mSize = s;
109 
110  mTexture->setSize( mSize.width(), mSize.height() );
111  mDepthTexture->setSize( mSize.width(), mSize.height() );
112  mSurfaceSelector->setExternalRenderTargetSize( mSize );
113 
114  mCamera->setAspectRatio( float( mSize.width() ) / float( mSize.height() ) );
115 }
116 
117 void QgsOffscreen3DEngine::setClearColor( const QColor &color )
118 {
119  mClearBuffers->setClearColor( color );
120 }
121 
123 {
124  // TODO
125  Q_UNUSED( enabled );
126 }
127 
128 void QgsOffscreen3DEngine::createRenderTarget()
129 {
130  mTextureTarget = new Qt3DRender::QRenderTarget;
131 
132  // The lifetime of the objects created here is managed
133  // automatically, as they become children of this object.
134 
135  // Create a render target output for rendering color.
136  mTextureOutput = new Qt3DRender::QRenderTargetOutput( mTextureTarget );
137  mTextureOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
138 
139  // Create a texture to render into.
140  mTexture = new Qt3DRender::QTexture2D( mTextureOutput );
141  mTexture->setSize( mSize.width(), mSize.height() );
142  mTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
143  mTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
144  mTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
145 
146  // Hook the texture up to our output, and the output up to this object.
147  mTextureOutput->setTexture( mTexture );
148  mTextureTarget->addOutput( mTextureOutput );
149 
150  mDepthTextureOutput = new Qt3DRender::QRenderTargetOutput( mTextureTarget );
151  mDepthTextureOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
152  mDepthTexture = new Qt3DRender::QTexture2D( mDepthTextureOutput );
153  mDepthTexture->setSize( mSize.width(), mSize.height() );
154  mDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
155  mDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
156  mDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
157  mDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
158  mDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
159  // Hook up the depth texture
160  mDepthTextureOutput->setTexture( mDepthTexture );
161  mTextureTarget->addOutput( mDepthTextureOutput );
162 }
163 
164 void QgsOffscreen3DEngine::createFrameGraph()
165 {
166  // Firstly, create the offscreen surface. This will take the place
167  // of a QWindow, allowing us to render our scene without one.
168  mOffscreenSurface = new QOffscreenSurface();
169  mOffscreenSurface->setFormat( QSurfaceFormat::defaultFormat() );
170  mOffscreenSurface->create();
171 
172  // Hook it up to the frame graph.
173  mSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector( mRenderSettings );
174  mSurfaceSelector->setSurface( mOffscreenSurface );
175  mSurfaceSelector->setExternalRenderTargetSize( mSize );
176 
177  // Create a texture to render into. This acts as the buffer that
178  // holds the rendered image.
179  mRenderTargetSelector = new Qt3DRender::QRenderTargetSelector( mSurfaceSelector );
180  createRenderTarget();
181  // the target selector also sets itself as the parent of mTextureTarget
182  mRenderTargetSelector->setTarget( mTextureTarget );
183 
184  // Create a node used for clearing the required buffers.
185  mClearBuffers = new Qt3DRender::QClearBuffers( mRenderTargetSelector );
186  mClearBuffers->setClearColor( QColor( 100, 100, 100, 255 ) );
187  mClearBuffers->setBuffers( Qt3DRender::QClearBuffers::ColorDepthBuffer );
188 
189  // Create a viewport node. The viewport here just covers the entire render area.
190  mViewport = new Qt3DRender::QViewport( mRenderTargetSelector );
191  mViewport->setNormalizedRect( QRectF( 0.0, 0.0, 1.0, 1.0 ) );
192 
193  // Create a camera selector node, and tell it to use the camera we've ben given.
194  mCameraSelector = new Qt3DRender::QCameraSelector( mViewport );
195  mCameraSelector->setCamera( mCamera );
196 
197  // Add a render capture node to the frame graph.
198  // This is set as the next child of the render target selector node,
199  // so that the capture will be taken from the specified render target
200  // once all other rendering operations have taken place.
201  mRenderCapture = new Qt3DRender::QRenderCapture( mRenderTargetSelector );
202 }
203 
204 void QgsOffscreen3DEngine::setRootEntity( Qt3DCore::QEntity *root )
205 {
206  // Make sure any existing scene root is unparented.
207  if ( mSceneRoot )
208  {
209  mSceneRoot->setParent( static_cast<Qt3DCore::QNode *>( nullptr ) );
210  }
211 
212  // Parent the incoming scene root to our current root entity.
213  mSceneRoot = root;
214  mSceneRoot->setParent( mAspectEngine->rootEntity().data() );
215 }
216 
217 Qt3DRender::QRenderSettings *QgsOffscreen3DEngine::renderSettings()
218 {
219  return mRenderSettings;
220 }
221 
222 Qt3DRender::QCamera *QgsOffscreen3DEngine::camera()
223 {
224  return mCamera;
225 }
226 
228 {
229  return mSize;
230 }
231 
233 {
234  if ( mReply )
235  {
236  qDebug() << "already having a pending capture, skipping";
237  return;
238  }
239  mReply = mRenderCapture->requestCapture();
240  connect( mReply, &Qt3DRender::QRenderCaptureReply::completed, this, [ = ]
241  {
242  QImage image = mReply->image();
243  mReply->deleteLater();
244  mReply = nullptr;
245  emit imageCaptured( image );
246  } );
247 }
void requestCaptureImage() override
Starts a request for an image rendered by the engine.
void setFrustumCullingEnabled(bool enabled) override
Sets whether frustum culling is enabled (this should make rendering faster by not rendering entities ...
void setRootEntity(Qt3DCore::QEntity *root) override
Sets root entity of the 3D scene.
void setSize(QSize s)
Sets the size of the rendering area (in pixels)
QSize size() const override
Returns size of the engine&#39;s rendering area in pixels.
Qt3DRender::QCamera * camera() override
Returns pointer to the engine&#39;s camera entity.
void imageCaptured(const QImage &image)
Emitted after a call to requestCaptureImage() to return the captured image.
void setClearColor(const QColor &color) override
Sets background color of the scene.
Qt3DRender::QRenderSettings * renderSettings() override
Returns access to the engine&#39;s render settings (the frame graph can be accessed from here) ...