24#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
25#include <Qt3DRender/QAttribute>
26#include <Qt3DRender/QBuffer>
27#include <Qt3DRender/QGeometry>
33#include <Qt3DCore/QAttribute>
34#include <Qt3DCore/QBuffer>
35#include <Qt3DCore/QGeometry>
42#include <Qt3DRender/QGeometryRenderer>
43#include <Qt3DRender/QTechnique>
44#include <Qt3DRender/QGraphicsApiFilter>
45#include <Qt3DRender/QBlendEquation>
46#include <Qt3DRender/QColorMask>
47#include <Qt3DRender/QSortPolicy>
48#include <Qt3DRender/QNoDepthMask>
49#include <Qt3DRender/QBlendEquationArguments>
51Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructTexturesPreviewPass()
53 mPreviewLayerFilter =
new Qt3DRender::QLayerFilter;
54 mPreviewLayerFilter->addLayer( mPreviewLayer );
56 mPreviewRenderStateSet =
new Qt3DRender::QRenderStateSet( mPreviewLayerFilter );
57 mPreviewDepthTest =
new Qt3DRender::QDepthTest;
58 mPreviewDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
59 mPreviewRenderStateSet->addRenderState( mPreviewDepthTest );
60 mPreviewCullFace =
new Qt3DRender::QCullFace;
61 mPreviewCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
62 mPreviewRenderStateSet->addRenderState( mPreviewCullFace );
64 return mPreviewLayerFilter;
67Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructForwardRenderPass()
104 mMainCameraSelector =
new Qt3DRender::QCameraSelector;
105 mMainCameraSelector->setCamera( mMainCamera );
107 mForwardRenderLayerFilter =
new Qt3DRender::QLayerFilter( mMainCameraSelector );
108 mForwardRenderLayerFilter->addLayer( mForwardRenderLayer );
110 mForwardColorTexture =
new Qt3DRender::QTexture2D;
111 mForwardColorTexture->setWidth( mSize.width() );
112 mForwardColorTexture->setHeight( mSize.height() );
113 mForwardColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
114 mForwardColorTexture->setGenerateMipMaps(
false );
115 mForwardColorTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
116 mForwardColorTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
117 mForwardColorTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
118 mForwardColorTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
120 mForwardDepthTexture =
new Qt3DRender::QTexture2D;
121 mForwardDepthTexture->setWidth( mSize.width() );
122 mForwardDepthTexture->setHeight( mSize.height() );
123 mForwardDepthTexture->setFormat( Qt3DRender::QTexture2D::TextureFormat::DepthFormat );
124 mForwardDepthTexture->setGenerateMipMaps(
false );
125 mForwardDepthTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
126 mForwardDepthTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
127 mForwardDepthTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
128 mForwardDepthTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
130 Qt3DRender::QRenderTarget *forwardRenderTarget =
new Qt3DRender::QRenderTarget;
131 Qt3DRender::QRenderTargetOutput *forwardRenderTargetDepthOutput =
new Qt3DRender::QRenderTargetOutput;
132 forwardRenderTargetDepthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
133 forwardRenderTargetDepthOutput->setTexture( mForwardDepthTexture );
134 forwardRenderTarget->addOutput( forwardRenderTargetDepthOutput );
135 Qt3DRender::QRenderTargetOutput *forwardRenderTargetColorOutput =
new Qt3DRender::QRenderTargetOutput;
136 forwardRenderTargetColorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
137 forwardRenderTargetColorOutput->setTexture( mForwardColorTexture );
138 forwardRenderTarget->addOutput( forwardRenderTargetColorOutput );
140 mForwardRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mForwardRenderLayerFilter );
141 mForwardRenderTargetSelector->setTarget( forwardRenderTarget );
144 Qt3DRender::QLayerFilter *opaqueObjectsFilter =
new Qt3DRender::QLayerFilter( mForwardRenderTargetSelector );
145 opaqueObjectsFilter->addLayer( mTransparentObjectsPassLayer );
146 opaqueObjectsFilter->setFilterMode( Qt3DRender::QLayerFilter::DiscardAnyMatchingLayers );
148 Qt3DRender::QRenderStateSet *forwardedRenderStateSet =
new Qt3DRender::QRenderStateSet( opaqueObjectsFilter );
150 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest;
151 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
152 forwardedRenderStateSet->addRenderState( depthTest );
154 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
155 cullFace->setMode( Qt3DRender::QCullFace::CullingMode::Back );
156 forwardedRenderStateSet->addRenderState( cullFace );
158 mFrustumCulling =
new Qt3DRender::QFrustumCulling( forwardedRenderStateSet );
160 mForwardClearBuffers =
new Qt3DRender::QClearBuffers( mFrustumCulling );
161 mForwardClearBuffers->setClearColor( QColor::fromRgbF( 0.0, 0.0, 1.0, 1.0 ) );
162 mForwardClearBuffers->setBuffers( Qt3DRender::QClearBuffers::ColorDepthBuffer );
163 mForwardClearBuffers->setClearDepthValue( 1.0f );
166 Qt3DRender::QLayerFilter *transparentObjectsLayerFilter =
new Qt3DRender::QLayerFilter( mForwardRenderTargetSelector );
167 transparentObjectsLayerFilter->addLayer( mTransparentObjectsPassLayer );
168 transparentObjectsLayerFilter->setFilterMode( Qt3DRender::QLayerFilter::AcceptAnyMatchingLayers );
170 Qt3DRender::QSortPolicy *sortPolicy =
new Qt3DRender::QSortPolicy( transparentObjectsLayerFilter );
171 QVector<Qt3DRender::QSortPolicy::SortType> sortTypes;
172 sortTypes.push_back( Qt3DRender::QSortPolicy::BackToFront );
173 sortPolicy->setSortTypes( sortTypes );
175 Qt3DRender::QRenderStateSet *transparentObjectsRenderStateSetColor =
new Qt3DRender::QRenderStateSet( sortPolicy );
177 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest;
178 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
179 transparentObjectsRenderStateSetColor->addRenderState( depthTest );
181 Qt3DRender::QNoDepthMask *noDepthMask =
new Qt3DRender::QNoDepthMask;
182 transparentObjectsRenderStateSetColor->addRenderState( noDepthMask );
184 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
185 cullFace->setMode( Qt3DRender::QCullFace::CullingMode::NoCulling );
186 transparentObjectsRenderStateSetColor->addRenderState( cullFace );
188 Qt3DRender::QBlendEquation *blendEquation =
new Qt3DRender::QBlendEquation;
189 blendEquation->setBlendFunction( Qt3DRender::QBlendEquation::Add );
190 transparentObjectsRenderStateSetColor->addRenderState( blendEquation );
192 Qt3DRender::QBlendEquationArguments *blendEquationArgs =
new Qt3DRender::QBlendEquationArguments;
193 blendEquationArgs->setSourceRgb( Qt3DRender::QBlendEquationArguments::Blending::SourceAlpha );
194 blendEquationArgs->setDestinationRgb( Qt3DRender::QBlendEquationArguments::Blending::OneMinusSourceAlpha );
195 transparentObjectsRenderStateSetColor->addRenderState( blendEquationArgs );
199 Qt3DRender::QRenderStateSet *transparentObjectsRenderStateSetDepth =
new Qt3DRender::QRenderStateSet( sortPolicy );
201 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest;
202 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
203 transparentObjectsRenderStateSetDepth->addRenderState( depthTest );
205 Qt3DRender::QColorMask *noColorMask =
new Qt3DRender::QColorMask;
206 noColorMask->setAlphaMasked(
false );
207 noColorMask->setRedMasked(
false );
208 noColorMask->setGreenMasked(
false );
209 noColorMask->setBlueMasked(
false );
210 transparentObjectsRenderStateSetDepth->addRenderState( noColorMask );
212 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
213 cullFace->setMode( Qt3DRender::QCullFace::CullingMode::NoCulling );
214 transparentObjectsRenderStateSetDepth->addRenderState( cullFace );
217 mDebugOverlay =
new Qt3DRender::QDebugOverlay( mForwardClearBuffers );
218 mDebugOverlay->setEnabled(
false );
222 return mMainCameraSelector;
225Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructShadowRenderPass()
227 mLightCameraSelectorShadowPass =
new Qt3DRender::QCameraSelector;
228 mLightCameraSelectorShadowPass->setCamera( mLightCamera );
230 mShadowSceneEntitiesFilter =
new Qt3DRender::QLayerFilter( mLightCameraSelectorShadowPass );
231 mShadowSceneEntitiesFilter->addLayer( mCastShadowsLayer );
233 mShadowMapTexture =
new Qt3DRender::QTexture2D;
234 mShadowMapTexture->setWidth( mShadowMapResolution );
235 mShadowMapTexture->setHeight( mShadowMapResolution );
236 mShadowMapTexture->setFormat( Qt3DRender::QTexture2D::TextureFormat::DepthFormat );
237 mShadowMapTexture->setGenerateMipMaps(
false );
238 mShadowMapTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
239 mShadowMapTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
240 mShadowMapTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
241 mShadowMapTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
243 Qt3DRender::QRenderTarget *shadowRenderTarget =
new Qt3DRender::QRenderTarget;
244 Qt3DRender::QRenderTargetOutput *shadowRenderTargetOutput =
new Qt3DRender::QRenderTargetOutput;
245 shadowRenderTargetOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
246 shadowRenderTargetOutput->setTexture( mShadowMapTexture );
247 shadowRenderTarget->addOutput( shadowRenderTargetOutput );
249 mShadowRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mShadowSceneEntitiesFilter );
250 mShadowRenderTargetSelector->setTarget( shadowRenderTarget );
252 mShadowClearBuffers =
new Qt3DRender::QClearBuffers( mShadowRenderTargetSelector );
253 mShadowClearBuffers->setBuffers( Qt3DRender::QClearBuffers::BufferType::ColorDepthBuffer );
254 mShadowClearBuffers->setClearColor( QColor::fromRgbF( 0.0f, 1.0f, 0.0f ) );
256 mShadowRenderStateSet =
new Qt3DRender::QRenderStateSet( mShadowClearBuffers );
258 Qt3DRender::QDepthTest *shadowDepthTest =
new Qt3DRender::QDepthTest;
259 shadowDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
260 mShadowRenderStateSet->addRenderState( shadowDepthTest );
262 Qt3DRender::QCullFace *shadowCullFace =
new Qt3DRender::QCullFace;
263 shadowCullFace->setMode( Qt3DRender::QCullFace::CullingMode::Front );
264 mShadowRenderStateSet->addRenderState( shadowCullFace );
266 Qt3DRender::QPolygonOffset *polygonOffset =
new Qt3DRender::QPolygonOffset;
267 polygonOffset->setDepthSteps( 4.0 );
268 polygonOffset->setScaleFactor( 1.1 );
269 mShadowRenderStateSet->addRenderState( polygonOffset );
271 return mLightCameraSelectorShadowPass;
274Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructPostprocessingPass()
276 mPostProcessingCameraSelector =
new Qt3DRender::QCameraSelector;
277 mPostProcessingCameraSelector->setCamera( mLightCamera );
279 mPostprocessPassLayerFilter =
new Qt3DRender::QLayerFilter( mPostProcessingCameraSelector );
281 mPostprocessClearBuffers =
new Qt3DRender::QClearBuffers( mPostprocessPassLayerFilter );
283 mRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mPostprocessClearBuffers );
285 Qt3DRender::QRenderTarget *renderTarget =
new Qt3DRender::QRenderTarget( mRenderCaptureTargetSelector );
291 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( renderTarget );
292 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
295 mRenderCaptureColorTexture =
new Qt3DRender::QTexture2D( colorOutput );
296 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
297 mRenderCaptureColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
298 mRenderCaptureColorTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
299 mRenderCaptureColorTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
302 colorOutput->setTexture( mRenderCaptureColorTexture );
303 renderTarget->addOutput( colorOutput );
305 Qt3DRender::QRenderTargetOutput *depthOutput =
new Qt3DRender::QRenderTargetOutput( renderTarget );
307 depthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
308 mRenderCaptureDepthTexture =
new Qt3DRender::QTexture2D( depthOutput );
309 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
310 mRenderCaptureDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
311 mRenderCaptureDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
312 mRenderCaptureDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
313 mRenderCaptureDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
314 mRenderCaptureDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
316 depthOutput->setTexture( mRenderCaptureDepthTexture );
317 renderTarget->addOutput( depthOutput );
319 mRenderCaptureTargetSelector->setTarget( renderTarget );
321 mRenderCapture =
new Qt3DRender::QRenderCapture( mRenderCaptureTargetSelector );
324 mPostprocessPassLayerFilter->addLayer( mPostprocessingEntity->
layer() );
326 return mPostProcessingCameraSelector;
329Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructAmbientOcclusionRenderPass()
331 mAmbientOcclusionRenderCameraSelector =
new Qt3DRender::QCameraSelector;
332 mAmbientOcclusionRenderCameraSelector->setCamera( mMainCamera );
334 mAmbientOcclusionRenderStateSet =
new Qt3DRender::QRenderStateSet( mAmbientOcclusionRenderCameraSelector );
336 Qt3DRender::QDepthTest *depthRenderDepthTest =
new Qt3DRender::QDepthTest;
337 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
338 Qt3DRender::QCullFace *depthRenderCullFace =
new Qt3DRender::QCullFace;
339 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
341 mAmbientOcclusionRenderStateSet->addRenderState( depthRenderDepthTest );
342 mAmbientOcclusionRenderStateSet->addRenderState( depthRenderCullFace );
344 mAmbientOcclusionRenderLayerFilter =
new Qt3DRender::QLayerFilter( mAmbientOcclusionRenderStateSet );
346 mAmbientOcclusionRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mAmbientOcclusionRenderLayerFilter );
347 Qt3DRender::QRenderTarget *depthRenderTarget =
new Qt3DRender::QRenderTarget( mAmbientOcclusionRenderCaptureTargetSelector );
353 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
354 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
357 mAmbientOcclusionRenderTexture =
new Qt3DRender::QTexture2D( colorOutput );
358 mAmbientOcclusionRenderTexture->setSize( mSize.width(), mSize.height() );
359 mAmbientOcclusionRenderTexture->setFormat( Qt3DRender::QAbstractTexture::R32F );
360 mAmbientOcclusionRenderTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
361 mAmbientOcclusionRenderTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
364 colorOutput->setTexture( mAmbientOcclusionRenderTexture );
365 depthRenderTarget->addOutput( colorOutput );
367 mAmbientOcclusionRenderCaptureTargetSelector->setTarget( depthRenderTarget );
370 mAmbientOcclusionRenderLayerFilter->addLayer( mAmbientOcclusionRenderEntity->
layer() );
372 return mAmbientOcclusionRenderCameraSelector;
375Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructAmbientOcclusionBlurPass()
377 mAmbientOcclusionBlurCameraSelector =
new Qt3DRender::QCameraSelector;
378 mAmbientOcclusionBlurCameraSelector->setCamera( mMainCamera );
380 mAmbientOcclusionBlurStateSet =
new Qt3DRender::QRenderStateSet( mAmbientOcclusionBlurCameraSelector );
382 Qt3DRender::QDepthTest *depthRenderDepthTest =
new Qt3DRender::QDepthTest;
383 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
384 Qt3DRender::QCullFace *depthRenderCullFace =
new Qt3DRender::QCullFace;
385 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
387 mAmbientOcclusionBlurStateSet->addRenderState( depthRenderDepthTest );
388 mAmbientOcclusionBlurStateSet->addRenderState( depthRenderCullFace );
390 mAmbientOcclusionBlurLayerFilter =
new Qt3DRender::QLayerFilter( mAmbientOcclusionBlurStateSet );
392 mAmbientOcclusionBlurRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mAmbientOcclusionBlurLayerFilter );
393 Qt3DRender::QRenderTarget *depthRenderTarget =
new Qt3DRender::QRenderTarget( mAmbientOcclusionBlurRenderCaptureTargetSelector );
399 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
400 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
403 mAmbientOcclusionBlurTexture =
new Qt3DRender::QTexture2D( colorOutput );
404 mAmbientOcclusionBlurTexture->setSize( mSize.width(), mSize.height() );
405 mAmbientOcclusionBlurTexture->setFormat( Qt3DRender::QAbstractTexture::R32F );
406 mAmbientOcclusionBlurTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
407 mAmbientOcclusionBlurTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
410 colorOutput->setTexture( mAmbientOcclusionBlurTexture );
411 depthRenderTarget->addOutput( colorOutput );
413 mAmbientOcclusionBlurRenderCaptureTargetSelector->setTarget( depthRenderTarget );
416 mAmbientOcclusionBlurLayerFilter->addLayer( mAmbientOcclusionBlurEntity->
layer() );
418 return mAmbientOcclusionBlurCameraSelector;
422Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructRubberBandsPass()
424 mRubberBandsCameraSelector =
new Qt3DRender::QCameraSelector;
425 mRubberBandsCameraSelector->setCamera( mMainCamera );
427 mRubberBandsLayerFilter =
new Qt3DRender::QLayerFilter( mRubberBandsCameraSelector );
428 mRubberBandsLayerFilter->addLayer( mRubberBandsLayer );
430 mRubberBandsStateSet =
new Qt3DRender::QRenderStateSet( mRubberBandsLayerFilter );
431 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest;
432 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
433 mRubberBandsStateSet->addRenderState( depthTest );
438 mRubberBandsRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mRubberBandsStateSet );
439 mRubberBandsRenderTargetSelector->setTarget( mForwardRenderTargetSelector->target() );
441 return mRubberBandsCameraSelector;
446Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructDepthRenderPass()
450 mDepthRenderCameraSelector =
new Qt3DRender::QCameraSelector;
451 mDepthRenderCameraSelector->setCamera( mMainCamera );
453 mDepthRenderStateSet =
new Qt3DRender::QRenderStateSet( mDepthRenderCameraSelector );
455 Qt3DRender::QDepthTest *depthRenderDepthTest =
new Qt3DRender::QDepthTest;
456 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
457 Qt3DRender::QCullFace *depthRenderCullFace =
new Qt3DRender::QCullFace;
458 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
460 mDepthRenderStateSet->addRenderState( depthRenderDepthTest );
461 mDepthRenderStateSet->addRenderState( depthRenderCullFace );
463 mDepthRenderLayerFilter =
new Qt3DRender::QLayerFilter( mDepthRenderStateSet );
464 mDepthRenderLayerFilter->addLayer( mDepthRenderPassLayer );
466 mDepthRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mDepthRenderLayerFilter );
467 Qt3DRender::QRenderTarget *depthRenderTarget =
new Qt3DRender::QRenderTarget( mDepthRenderCaptureTargetSelector );
473 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
474 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
477 mDepthRenderCaptureColorTexture =
new Qt3DRender::QTexture2D( colorOutput );
478 mDepthRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
479 mDepthRenderCaptureColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
480 mDepthRenderCaptureColorTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
481 mDepthRenderCaptureColorTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
484 colorOutput->setTexture( mDepthRenderCaptureColorTexture );
485 depthRenderTarget->addOutput( colorOutput );
487 Qt3DRender::QRenderTargetOutput *depthOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
489 depthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
490 mDepthRenderCaptureDepthTexture =
new Qt3DRender::QTexture2D( depthOutput );
491 mDepthRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
492 mDepthRenderCaptureDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
493 mDepthRenderCaptureDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
494 mDepthRenderCaptureDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
495 mDepthRenderCaptureDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
496 mDepthRenderCaptureDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
498 depthOutput->setTexture( mDepthRenderCaptureDepthTexture );
499 depthRenderTarget->addOutput( depthOutput );
501 mDepthRenderCaptureTargetSelector->setTarget( depthRenderTarget );
504 mDepthRenderCapture =
new Qt3DRender::QRenderCapture( mDepthRenderCaptureTargetSelector );
506 return mDepthRenderCameraSelector;
509Qt3DCore::QEntity *QgsFrameGraph::constructDepthRenderQuad()
511 Qt3DCore::QEntity *quad =
new Qt3DCore::QEntity;
512 quad->setObjectName(
"depthRenderQuad" );
516 const QVector<float> vert = { -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
518 const QByteArray vertexArr( (
const char * ) vert.constData(), vert.size() *
sizeof(
float ) );
521 vertexBuffer->setData( vertexArr );
523 positionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
524 positionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
525 positionAttribute->setVertexSize( 3 );
526 positionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
527 positionAttribute->setBuffer( vertexBuffer );
528 positionAttribute->setByteOffset( 0 );
529 positionAttribute->setByteStride( 3 *
sizeof(
float ) );
530 positionAttribute->setCount( 6 );
532 geom->addAttribute( positionAttribute );
534 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
535 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
536 renderer->setGeometry( geom );
538 quad->addComponent( renderer );
540 QMatrix4x4 modelMatrix;
541 modelMatrix.setToIdentity();
545 Qt3DRender::QMaterial *material =
new Qt3DRender::QMaterial;
546 Qt3DRender::QParameter *textureParameter =
new Qt3DRender::QParameter(
"depthTexture", mForwardDepthTexture );
547 Qt3DRender::QParameter *textureTransformParameter =
new Qt3DRender::QParameter(
"modelMatrix", QVariant::fromValue( modelMatrix ) );
548 material->addParameter( textureParameter );
549 material->addParameter( textureTransformParameter );
551 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect;
553 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
555 Qt3DRender::QGraphicsApiFilter *graphicsApiFilter = technique->graphicsApiFilter();
556 graphicsApiFilter->setApi( Qt3DRender::QGraphicsApiFilter::Api::OpenGL );
557 graphicsApiFilter->setProfile( Qt3DRender::QGraphicsApiFilter::OpenGLProfile::CoreProfile );
558 graphicsApiFilter->setMajorVersion( 1 );
559 graphicsApiFilter->setMinorVersion( 5 );
561 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass;
563 Qt3DRender::QShaderProgram *shader =
new Qt3DRender::QShaderProgram;
564 shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"qrc:/shaders/depth_render.vert" ) ) );
565 shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"qrc:/shaders/depth_render.frag" ) ) );
566 renderPass->setShaderProgram( shader );
568 technique->addRenderPass( renderPass );
570 effect->addTechnique( technique );
571 material->setEffect( effect );
573 quad->addComponent( material );
613 mLightCamera =
new Qt3DRender::QCamera;
615 mPreviewLayer =
new Qt3DRender::QLayer;
616 mCastShadowsLayer =
new Qt3DRender::QLayer;
617 mForwardRenderLayer =
new Qt3DRender::QLayer;
618 mDepthRenderPassLayer =
new Qt3DRender::QLayer;
619 mTransparentObjectsPassLayer =
new Qt3DRender::QLayer;
620 mRubberBandsLayer =
new Qt3DRender::QLayer;
622 mPreviewLayer->setRecursive(
true );
623 mCastShadowsLayer->setRecursive(
true );
624 mForwardRenderLayer->setRecursive(
true );
625 mDepthRenderPassLayer->setRecursive(
true );
626 mTransparentObjectsPassLayer->setRecursive(
true );
627 mRubberBandsLayer->setRecursive(
true );
629 mRenderSurfaceSelector =
new Qt3DRender::QRenderSurfaceSelector;
631 QObject *surfaceObj =
dynamic_cast< QObject *
>( surface );
632 Q_ASSERT( surfaceObj );
634 mRenderSurfaceSelector->setSurface( surfaceObj );
635 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
637 mMainViewPort =
new Qt3DRender::QViewport( mRenderSurfaceSelector );
638 mMainViewPort->setNormalizedRect( QRectF( 0.0f, 0.0f, 1.0f, 1.0f ) );
641 Qt3DRender::QFrameGraphNode *forwardRenderPass = constructForwardRenderPass();
642 forwardRenderPass->setParent( mMainViewPort );
645 Qt3DRender::QFrameGraphNode *rubberBandsPass = constructRubberBandsPass();
646 rubberBandsPass->setParent( mMainViewPort );
649 Qt3DRender::QFrameGraphNode *shadowRenderPass = constructShadowRenderPass();
650 shadowRenderPass->setParent( mMainViewPort );
653 Qt3DRender::QFrameGraphNode *depthBufferProcessingPass = constructDepthRenderPass();
654 depthBufferProcessingPass->setParent( mMainViewPort );
657 Qt3DRender::QFrameGraphNode *ambientOcclusionFactorRender = constructAmbientOcclusionRenderPass();
658 ambientOcclusionFactorRender->setParent( mMainViewPort );
660 Qt3DRender::QFrameGraphNode *ambientOcclusionBlurPass = constructAmbientOcclusionBlurPass();
661 ambientOcclusionBlurPass->setParent( mMainViewPort );
664 Qt3DRender::QFrameGraphNode *postprocessingPass = constructPostprocessingPass();
665 postprocessingPass->setParent( mMainViewPort );
667 mRubberBandsRootEntity =
new Qt3DCore::QEntity( mRootEntity );
668 mRubberBandsRootEntity->addComponent( mRubberBandsLayer );
671 Qt3DRender::QFrameGraphNode *previewPass = constructTexturesPreviewPass();
672 previewPass->setParent( mMainViewPort );
674 Qt3DRender::QParameter *depthMapIsDepthParam =
new Qt3DRender::QParameter(
"isDepth",
true );
675 Qt3DRender::QParameter *shadowMapIsDepthParam =
new Qt3DRender::QParameter(
"isDepth",
true );
677 mDebugDepthMapPreviewQuad = this->
addTexturePreviewOverlay( mForwardDepthTexture, QPointF( 0.9f, 0.9f ), QSizeF( 0.1, 0.1 ), QVector<Qt3DRender::QParameter *> { depthMapIsDepthParam } );
678 mDebugShadowMapPreviewQuad = this->
addTexturePreviewOverlay( mShadowMapTexture, QPointF( 0.9f, 0.9f ), QSizeF( 0.1, 0.1 ), QVector<Qt3DRender::QParameter *> { shadowMapIsDepthParam } );
679 mDebugDepthMapPreviewQuad->setEnabled(
false );
680 mDebugShadowMapPreviewQuad->setEnabled(
false );
682 mDepthRenderQuad = constructDepthRenderQuad();
683 mDepthRenderQuad->addComponent( mDepthRenderPassLayer );
684 mDepthRenderQuad->setParent( mRootEntity );
690 previewQuad->addComponent( mPreviewLayer );
691 previewQuad->setParent( mRootEntity );
692 mPreviewQuads.push_back( previewQuad );
697void calculateViewExtent( Qt3DRender::QCamera *camera,
float shadowRenderingDistance,
float y,
float &minX,
float &maxX,
float &minY,
float &maxY,
float &minZ,
float &maxZ )
699 const QVector3D cameraPos = camera->position();
700 const QMatrix4x4 projectionMatrix = camera->projectionMatrix();
701 const QMatrix4x4 viewMatrix = camera->viewMatrix();
703 QVector4D viewCenter = viewMatrix * QVector4D( camera->viewCenter(), 1.0f );
704 viewCenter /= viewCenter.w();
705 viewCenter = projectionMatrix * viewCenter;
706 viewCenter /= viewCenter.w();
707 depth = viewCenter.z();
708 QVector<QVector3D> viewFrustumPoints =
710 QVector3D( 0.0f, 0.0f, depth ),
711 QVector3D( 0.0f, 1.0f, depth ),
712 QVector3D( 1.0f, 0.0f, depth ),
713 QVector3D( 1.0f, 1.0f, depth ),
714 QVector3D( 0.0f, 0.0f, 0 ),
715 QVector3D( 0.0f, 1.0f, 0 ),
716 QVector3D( 1.0f, 0.0f, 0 ),
717 QVector3D( 1.0f, 1.0f, 0 )
719 maxX = std::numeric_limits<float>::lowest();
720 maxY = std::numeric_limits<float>::lowest();
721 maxZ = std::numeric_limits<float>::lowest();
722 minX = std::numeric_limits<float>::max();
723 minY = std::numeric_limits<float>::max();
724 minZ = std::numeric_limits<float>::max();
725 for (
int i = 0; i < viewFrustumPoints.size(); ++i )
728 viewFrustumPoints[i] = viewFrustumPoints[i].unproject( viewMatrix, projectionMatrix, QRect( 0, 0, 1, 1 ) );
729 minX = std::min( minX, viewFrustumPoints[i].x() );
730 maxX = std::max( maxX, viewFrustumPoints[i].x() );
731 minY = std::min( minY, viewFrustumPoints[i].y() );
732 maxY = std::max( maxY, viewFrustumPoints[i].y() );
733 minZ = std::min( minZ, viewFrustumPoints[i].z() );
734 maxZ = std::max( maxZ, viewFrustumPoints[i].z() );
739 const QVector3D pt = cameraPos;
740 const QVector3D vect = ( viewFrustumPoints[i] - pt ).normalized();
741 float t = ( y - pt.y() ) / vect.y();
743 t = shadowRenderingDistance;
745 t = std::min( t, shadowRenderingDistance );
746 viewFrustumPoints[i] = pt + t * vect;
747 minX = std::min( minX, viewFrustumPoints[i].x() );
748 maxX = std::max( maxX, viewFrustumPoints[i].x() );
749 minY = std::min( minY, viewFrustumPoints[i].y() );
750 maxY = std::max( maxY, viewFrustumPoints[i].y() );
751 minZ = std::min( minZ, viewFrustumPoints[i].z() );
752 maxZ = std::max( maxZ, viewFrustumPoints[i].z() );
758 float minX, maxX, minY, maxY, minZ, maxZ;
759 QVector3D lookingAt = mMainCamera->viewCenter();
760 const float d = 2 * ( mMainCamera->position() - mMainCamera->viewCenter() ).length();
762 const QVector3D vertical = QVector3D( 0.0f, d, 0.0f );
764 calculateViewExtent( mMainCamera, maximumShadowRenderingDistance, lookingAt.y(), minX, maxX, minY, maxY, minZ, maxZ );
766 lookingAt = QVector3D( 0.5 * ( minX + maxX ), mMainCamera->viewCenter().y(), 0.5 * ( minZ + maxZ ) );
767 const QVector3D lightPosition = lookingAt + vertical;
768 mLightCamera->setPosition( lightPosition );
769 mLightCamera->setViewCenter( lookingAt );
770 mLightCamera->setUpVector( QVector3D( 0.0f, 1.0f, 0.0f ) );
771 mLightCamera->rotateAboutViewCenter( QQuaternion::rotationTo( vertical.normalized(), -lightDirection.normalized() ) );
773 mLightCamera->setProjectionType( Qt3DRender::QCameraLens::ProjectionType::OrthographicProjection );
774 mLightCamera->lens()->setOrthographicProjection(
775 - 0.7 * ( maxX - minX ), 0.7 * ( maxX - minX ),
776 - 0.7 * ( maxZ - minZ ), 0.7 * ( maxZ - minZ ),
777 1.0f, 2 * ( lookingAt - lightPosition ).length() );
785 mForwardClearBuffers->setClearColor( clearColor );
790 mShadowRenderingEnabled = enabled;
792 if ( mShadowRenderingEnabled )
793 mShadowSceneEntitiesFilter->setEnabled(
true );
795 mShadowSceneEntitiesFilter->setEnabled(
false );
806 mShadowMapResolution = resolution;
807 mShadowMapTexture->setWidth( mShadowMapResolution );
808 mShadowMapTexture->setHeight( mShadowMapResolution );
813 mAmbientOcclusionEnabled = enabled;
814 mAmbientOcclusionRenderEntity->setEnabled( enabled );
820 mAmbientOcclusionIntensity = intensity;
821 mAmbientOcclusionRenderEntity->
setIntensity( intensity );
826 mAmbientOcclusionRadius = radius;
827 mAmbientOcclusionRenderEntity->
setRadius( radius );
832 mAmbientOcclusionThreshold = threshold;
833 mAmbientOcclusionRenderEntity->
setThreshold( threshold );
838 if ( enabled == mFrustumCullingEnabled )
840 mFrustumCullingEnabled = enabled;
841 if ( mFrustumCullingEnabled )
842 mFrustumCulling->setParent( mForwardClearBuffers );
844 mFrustumCulling->setParent( ( Qt3DCore::QNode * )
nullptr );
849 mEyeDomeLightingEnabled = enabled;
850 mEyeDomeLightingStrength = strength;
851 mEyeDomeLightingDistance = distance;
859 mDebugShadowMapPreviewQuad->setEnabled( enabled );
864 case Qt::Corner::TopRightCorner:
865 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
867 case Qt::Corner::TopLeftCorner:
868 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
870 case Qt::Corner::BottomRightCorner:
871 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
873 case Qt::Corner::BottomLeftCorner:
874 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
882 mDebugDepthMapPreviewQuad->setEnabled( enabled );
888 case Qt::Corner::TopRightCorner:
889 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
891 case Qt::Corner::TopLeftCorner:
892 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
894 case Qt::Corner::BottomRightCorner:
895 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
897 case Qt::Corner::BottomLeftCorner:
898 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
907 mForwardColorTexture->setSize( mSize.width(), mSize.height() );
908 mForwardDepthTexture->setSize( mSize.width(), mSize.height() );
909 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
910 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
911 mDepthRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
912 mDepthRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
913 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
915 mAmbientOcclusionRenderTexture->setSize( mSize.width(), mSize.height() );
916 mAmbientOcclusionBlurTexture->setSize( mSize.width(), mSize.height() );
921 if ( enabled == mRenderCaptureEnabled )
923 mRenderCaptureEnabled = enabled;
924 mRenderCaptureTargetSelector->setEnabled( mRenderCaptureEnabled );
929 mDebugOverlay->setEnabled( enabled );
void setThreshold(float threshold)
Sets the amount of occlusion when the effects starts to kick in.
void setRadius(float radius)
Sets the radius for the ambient occlusion effect.
void setIntensity(float intensity)
Sets the intensity for the ambient occlusion effect.
QgsVector3D direction() const
Returns the direction of the light in degrees.
void setRenderCaptureEnabled(bool enabled)
Sets whether it will be possible to render to an image.
void setShadowBias(float shadowBias)
Sets the shadow bias value.
void setupShadowMapDebugging(bool enabled, Qt::Corner corner, double size)
Sets the shadow map debugging view port.
void setClearColor(const QColor &clearColor)
Sets the clear color of the scene (background color)
void setShadowMapResolution(int resolution)
Sets the resolution of the shadow map.
QgsPreviewQuad * addTexturePreviewOverlay(Qt3DRender::QTexture2D *texture, const QPointF ¢erNDC, const QSizeF &size, QVector< Qt3DRender::QParameter * > additionalShaderParameters=QVector< Qt3DRender::QParameter * >())
Adds an preview entity that shows a texture in real time for debugging purposes.
void setAmbientOcclusionIntensity(float intensity)
Sets the ambient occlusion intensity.
float shadowBias() const
Returns the shadow bias value.
void setFrustumCullingEnabled(bool enabled)
Sets whether frustum culling is enabled.
void setDebugOverlayEnabled(bool enabled)
Sets whether debug overlay is enabled.
void setShadowRenderingEnabled(bool enabled)
Sets whether the shadow rendering is enabled.
void setupDepthMapDebugging(bool enabled, Qt::Corner corner, double size)
Sets the depth map debugging view port.
void setupDirectionalLight(const QgsDirectionalLightSettings &light, float maximumShadowRenderingDistance)
Sets shadow rendering to use a directional light.
void setAmbientOcclusionThreshold(float threshold)
Sets the ambient occlusion threshold.
void setupEyeDomeLighting(bool enabled, double strength, int distance)
Sets eye dome lighting shading related settings.
void setSize(QSize s)
Sets the size of the buffers used for rendering.
void setAmbientOcclusionEnabled(bool enabled)
Sets whether Screen Space Ambient Occlusion will be enabled.
void setAmbientOcclusionRadius(float radius)
Sets the ambient occlusion radius.
Qt3DRender::QCamera * mainCamera()
Returns the main camera.
QgsFrameGraph(QSurface *surface, QSize s, Qt3DRender::QCamera *mainCamera, Qt3DCore::QEntity *root)
Constructor.
void setupShadowRenderingExtent(float minX, float maxX, float minZ, float maxZ)
Sets the parts of the scene where objects cast shadows.
void setAmbientOcclusionEnabled(bool enabled)
Sets whether screen space ambient occlusion is enabled.
void setShadowRenderingEnabled(bool enabled)
Sets whether shadow rendering is enabled.
void setEyeDomeLightingDistance(int distance)
Sets the eye dome lighting distance (contributes to the contrast of the image)
void setShadowBias(float shadowBias)
Sets the shadow bias value.
void setEyeDomeLightingStrength(double strength)
Sets the eye dome lighting strength.
void setupDirectionalLight(QVector3D position, QVector3D direction)
Sets up a directional light that is used to render shadows.
void setEyeDomeLightingEnabled(bool enabled)
Sets whether eye dome lighting is enabled.
void setViewPort(const QPointF ¢erNDC, const QSizeF &size)
Sets where the quad will be located on the scene.
Qt3DRender::QLayer * layer()
Returns the layer object used to select this entity for rendering in a specific rendering pass.
double y() const
Returns Y coordinate.
double z() const
Returns Z coordinate.
double x() const
Returns X coordinate.
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
void calculateViewExtent(Qt3DRender::QCamera *camera, float shadowRenderingDistance, float y, float &minX, float &maxX, float &minY, float &maxY, float &minZ, float &maxZ)
Qt3DCore::QGeometry Qt3DQGeometry