18 #include <QOffscreenSurface> 19 #include <QSurfaceFormat> 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> 41 QSurfaceFormat format;
47 format.setRenderableType( QSurfaceFormat::OpenGLES );
49 if ( QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL )
51 format.setVersion( 4, 3 );
52 format.setProfile( QSurfaceFormat::CoreProfile );
56 format.setMajorVersion( 3 );
57 format.setDepthBufferSize( 32 );
58 format.setSamples( 8 );
59 QSurfaceFormat::setDefaultFormat( format );
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 ) );
69 mAspectEngine =
new Qt3DCore::QAspectEngine();
70 mRenderAspect =
new Qt3DRender::QRenderAspect( Qt3DRender::QRenderAspect::Threaded );
71 mLogicAspect =
new Qt3DLogic::QLogicAspect();
73 mAspectEngine->registerAspect( mRenderAspect );
74 mAspectEngine->registerAspect( mLogicAspect );
81 mRoot =
new Qt3DCore::QEntity();
82 mRenderSettings =
new Qt3DRender::QRenderSettings( mRoot );
83 mRoot->addComponent( mRenderSettings );
85 mCamera->setParent( mRoot );
93 mRenderSettings->setActiveFrameGraph( mSurfaceSelector );
96 mAspectEngine->setRootEntity( Qt3DCore::QEntityPtr( mRoot ) );
102 delete mAspectEngine;
103 delete mOffscreenSurface;
110 mTexture->setSize( mSize.width(), mSize.height() );
111 mDepthTexture->setSize( mSize.width(), mSize.height() );
112 mSurfaceSelector->setExternalRenderTargetSize( mSize );
114 mCamera->setAspectRatio(
float( mSize.width() ) /
float( mSize.height() ) );
119 mClearBuffers->setClearColor( color );
128 void QgsOffscreen3DEngine::createRenderTarget()
130 mTextureTarget =
new Qt3DRender::QRenderTarget;
136 mTextureOutput =
new Qt3DRender::QRenderTargetOutput( mTextureTarget );
137 mTextureOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
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 );
147 mTextureOutput->setTexture( mTexture );
148 mTextureTarget->addOutput( mTextureOutput );
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 );
160 mDepthTextureOutput->setTexture( mDepthTexture );
161 mTextureTarget->addOutput( mDepthTextureOutput );
164 void QgsOffscreen3DEngine::createFrameGraph()
168 mOffscreenSurface =
new QOffscreenSurface();
169 mOffscreenSurface->setFormat( QSurfaceFormat::defaultFormat() );
170 mOffscreenSurface->create();
173 mSurfaceSelector =
new Qt3DRender::QRenderSurfaceSelector( mRenderSettings );
174 mSurfaceSelector->setSurface( mOffscreenSurface );
175 mSurfaceSelector->setExternalRenderTargetSize( mSize );
179 mRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mSurfaceSelector );
180 createRenderTarget();
182 mRenderTargetSelector->setTarget( mTextureTarget );
185 mClearBuffers =
new Qt3DRender::QClearBuffers( mRenderTargetSelector );
186 mClearBuffers->setClearColor( QColor( 100, 100, 100, 255 ) );
187 mClearBuffers->setBuffers( Qt3DRender::QClearBuffers::ColorDepthBuffer );
190 mViewport =
new Qt3DRender::QViewport( mRenderTargetSelector );
191 mViewport->setNormalizedRect( QRectF( 0.0, 0.0, 1.0, 1.0 ) );
194 mCameraSelector =
new Qt3DRender::QCameraSelector( mViewport );
195 mCameraSelector->setCamera( mCamera );
201 mRenderCapture =
new Qt3DRender::QRenderCapture( mRenderTargetSelector );
209 mSceneRoot->setParent( static_cast<Qt3DCore::QNode *>(
nullptr ) );
214 mSceneRoot->setParent( mAspectEngine->rootEntity().data() );
219 return mRenderSettings;
236 qDebug() <<
"already having a pending capture, skipping";
239 mReply = mRenderCapture->requestCapture();
240 connect( mReply, &Qt3DRender::QRenderCaptureReply::completed,
this, [ = ]
242 QImage image = mReply->image();
243 mReply->deleteLater();
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.
~QgsOffscreen3DEngine() override
void setSize(QSize s)
Sets the size of the rendering area (in pixels)
QSize size() const override
Returns size of the engine's rendering area in pixels.
Qt3DRender::QCamera * camera() override
Returns pointer to the engine'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's render settings (the frame graph can be accessed from here) ...