18 #include <QOffscreenSurface>
19 #include <QSurfaceFormat>
20 #include <QOpenGLFunctions>
22 #include <Qt3DCore/QAspectEngine>
23 #include <Qt3DLogic/QLogicAspect>
24 #include <Qt3DRender/QCamera>
25 #include <Qt3DRender/QCameraSelector>
26 #include <Qt3DRender/QClearBuffers>
27 #include <Qt3DRender/QRenderAspect>
28 #include <Qt3DRender/QRenderCapture>
29 #include <Qt3DRender/QRenderSettings>
30 #include <Qt3DRender/QRenderTarget>
31 #include <Qt3DRender/QRenderTargetOutput>
32 #include <Qt3DRender/QRenderTargetSelector>
33 #include <Qt3DRender/QRenderSurfaceSelector>
34 #include <Qt3DRender/QTexture>
35 #include <Qt3DRender/QViewport>
36 #include <QtGui/QOpenGLContext>
42 QSurfaceFormat format;
48 format.setRenderableType( QSurfaceFormat::OpenGLES );
50 if ( QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL )
52 format.setVersion( 4, 3 );
53 format.setProfile( QSurfaceFormat::CoreProfile );
57 format.setMajorVersion( 3 );
58 format.setDepthBufferSize( 32 );
59 format.setSamples( 8 );
60 QSurfaceFormat::setDefaultFormat( format );
63 mCamera =
new Qt3DRender::QCamera;
64 mCamera->lens()->setPerspectiveProjection( 45.0f,
float( mSize.width() ) /
float( mSize.height() ), 0.1f, 1000.0f );
65 mCamera->setPosition( QVector3D( 0, 0, 20.0f ) );
66 mCamera->setUpVector( QVector3D( 0, 1, 0 ) );
67 mCamera->setViewCenter( QVector3D( 0, 0, 0 ) );
70 mAspectEngine =
new Qt3DCore::QAspectEngine();
71 mRenderAspect =
new Qt3DRender::QRenderAspect( Qt3DRender::QRenderAspect::Threaded );
72 mLogicAspect =
new Qt3DLogic::QLogicAspect();
74 mAspectEngine->registerAspect( mRenderAspect );
75 mAspectEngine->registerAspect( mLogicAspect );
82 mRoot =
new Qt3DCore::QEntity();
83 mRenderSettings =
new Qt3DRender::QRenderSettings( mRoot );
84 mRoot->addComponent( mRenderSettings );
86 mCamera->setParent( mRoot );
94 mRenderSettings->setActiveFrameGraph( mSurfaceSelector );
97 mAspectEngine->setRootEntity( Qt3DCore::QEntityPtr( mRoot ) );
103 delete mAspectEngine;
104 delete mOffscreenSurface;
111 mTexture->setSize( mSize.width(), mSize.height() );
112 mDepthTexture->setSize( mSize.width(), mSize.height() );
113 mSurfaceSelector->setExternalRenderTargetSize( mSize );
115 mCamera->setAspectRatio(
float( mSize.width() ) /
float( mSize.height() ) );
120 mClearBuffers->setClearColor( color );
129 void QgsOffscreen3DEngine::createRenderTarget()
131 mTextureTarget =
new Qt3DRender::QRenderTarget;
137 mTextureOutput =
new Qt3DRender::QRenderTargetOutput( mTextureTarget );
138 mTextureOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
141 mTexture =
new Qt3DRender::QTexture2D( mTextureOutput );
142 mTexture->setSize( mSize.width(), mSize.height() );
143 mTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
144 mTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
145 mTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
148 mTextureOutput->setTexture( mTexture );
149 mTextureTarget->addOutput( mTextureOutput );
151 mDepthTextureOutput =
new Qt3DRender::QRenderTargetOutput( mTextureTarget );
152 mDepthTextureOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
153 mDepthTexture =
new Qt3DRender::QTexture2D( mDepthTextureOutput );
154 mDepthTexture->setSize( mSize.width(), mSize.height() );
155 mDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
156 mDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
157 mDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
158 mDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
159 mDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
161 mDepthTextureOutput->setTexture( mDepthTexture );
162 mTextureTarget->addOutput( mDepthTextureOutput );
165 void QgsOffscreen3DEngine::createFrameGraph()
169 mOffscreenSurface =
new QOffscreenSurface();
170 mOffscreenSurface->setFormat( QSurfaceFormat::defaultFormat() );
171 mOffscreenSurface->create();
174 mSurfaceSelector =
new Qt3DRender::QRenderSurfaceSelector( mRenderSettings );
175 mSurfaceSelector->setSurface( mOffscreenSurface );
176 mSurfaceSelector->setExternalRenderTargetSize( mSize );
180 mRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mSurfaceSelector );
181 createRenderTarget();
183 mRenderTargetSelector->setTarget( mTextureTarget );
186 mClearBuffers =
new Qt3DRender::QClearBuffers( mRenderTargetSelector );
187 mClearBuffers->setClearColor( QColor( 100, 100, 100, 255 ) );
188 mClearBuffers->setBuffers( Qt3DRender::QClearBuffers::ColorDepthBuffer );
191 mViewport =
new Qt3DRender::QViewport( mRenderTargetSelector );
192 mViewport->setNormalizedRect( QRectF( 0.0, 0.0, 1.0, 1.0 ) );
195 mCameraSelector =
new Qt3DRender::QCameraSelector( mViewport );
196 mCameraSelector->setCamera( mCamera );
202 mRenderCapture =
new Qt3DRender::QRenderCapture( mRenderTargetSelector );
210 mSceneRoot->setParent(
static_cast<Qt3DCore::QNode *
>(
nullptr ) );
215 mSceneRoot->setParent( mAspectEngine->rootEntity().data() );
220 return mRenderSettings;
235 return mOffscreenSurface;
242 qDebug() <<
"already having a pending capture, skipping";
245 mReply = mRenderCapture->requestCapture();
246 connect( mReply, &Qt3DRender::QRenderCaptureReply::completed,
this, [ = ]
248 QImage image = mReply->image();
249 mReply->deleteLater();