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/QSortPolicy>
47#include <Qt3DRender/QNoDepthMask>
48#include <Qt3DRender/QBlendEquationArguments>
50Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructTexturesPreviewPass()
52 mPreviewLayerFilter =
new Qt3DRender::QLayerFilter;
53 mPreviewLayerFilter->addLayer( mPreviewLayer );
55 mPreviewRenderStateSet =
new Qt3DRender::QRenderStateSet( mPreviewLayerFilter );
56 mPreviewDepthTest =
new Qt3DRender::QDepthTest;
57 mPreviewDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
58 mPreviewRenderStateSet->addRenderState( mPreviewDepthTest );
59 mPreviewCullFace =
new Qt3DRender::QCullFace;
60 mPreviewCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
61 mPreviewRenderStateSet->addRenderState( mPreviewCullFace );
63 return mPreviewLayerFilter;
66Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructForwardRenderPass()
102 mMainCameraSelector =
new Qt3DRender::QCameraSelector;
103 mMainCameraSelector->setCamera( mMainCamera );
105 mForwardRenderLayerFilter =
new Qt3DRender::QLayerFilter( mMainCameraSelector );
106 mForwardRenderLayerFilter->addLayer( mForwardRenderLayer );
108 mForwardColorTexture =
new Qt3DRender::QTexture2D;
109 mForwardColorTexture->setWidth( mSize.width() );
110 mForwardColorTexture->setHeight( mSize.height() );
111 mForwardColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
112 mForwardColorTexture->setGenerateMipMaps(
false );
113 mForwardColorTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
114 mForwardColorTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
115 mForwardColorTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
116 mForwardColorTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
118 mForwardDepthTexture =
new Qt3DRender::QTexture2D;
119 mForwardDepthTexture->setWidth( mSize.width() );
120 mForwardDepthTexture->setHeight( mSize.height() );
121 mForwardDepthTexture->setFormat( Qt3DRender::QTexture2D::TextureFormat::DepthFormat );
122 mForwardDepthTexture->setGenerateMipMaps(
false );
123 mForwardDepthTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
124 mForwardDepthTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
125 mForwardDepthTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
126 mForwardDepthTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
128 Qt3DRender::QRenderTarget *forwardRenderTarget =
new Qt3DRender::QRenderTarget;
129 Qt3DRender::QRenderTargetOutput *forwardRenderTargetDepthOutput =
new Qt3DRender::QRenderTargetOutput;
130 forwardRenderTargetDepthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
131 forwardRenderTargetDepthOutput->setTexture( mForwardDepthTexture );
132 forwardRenderTarget->addOutput( forwardRenderTargetDepthOutput );
133 Qt3DRender::QRenderTargetOutput *forwardRenderTargetColorOutput =
new Qt3DRender::QRenderTargetOutput;
134 forwardRenderTargetColorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
135 forwardRenderTargetColorOutput->setTexture( mForwardColorTexture );
136 forwardRenderTarget->addOutput( forwardRenderTargetColorOutput );
138 mForwardRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mForwardRenderLayerFilter );
139 mForwardRenderTargetSelector->setTarget( forwardRenderTarget );
141 Qt3DRender::QLayerFilter *opaqueObjectsFilter =
new Qt3DRender::QLayerFilter( mForwardRenderTargetSelector );
142 opaqueObjectsFilter->addLayer( mTransparentObjectsPassLayer );
143 opaqueObjectsFilter->setFilterMode( Qt3DRender::QLayerFilter::DiscardAnyMatchingLayers );
145 Qt3DRender::QRenderStateSet *forwaredRenderStateSet =
new Qt3DRender::QRenderStateSet( opaqueObjectsFilter );
147 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest;
148 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
149 forwaredRenderStateSet->addRenderState( depthTest );
151 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
152 cullFace->setMode( Qt3DRender::QCullFace::CullingMode::Back );
153 forwaredRenderStateSet->addRenderState( cullFace );
155 mFrustumCulling =
new Qt3DRender::QFrustumCulling( forwaredRenderStateSet );
157 mForwardClearBuffers =
new Qt3DRender::QClearBuffers( mFrustumCulling );
158 mForwardClearBuffers->setClearColor( QColor::fromRgbF( 0.0, 0.0, 1.0, 1.0 ) );
159 mForwardClearBuffers->setBuffers( Qt3DRender::QClearBuffers::ColorDepthBuffer );
160 mForwardClearBuffers->setClearDepthValue( 1.0f );
162 Qt3DRender::QLayerFilter *transparentObjectsLayerFilter =
new Qt3DRender::QLayerFilter( mForwardRenderTargetSelector );
163 transparentObjectsLayerFilter->addLayer( mTransparentObjectsPassLayer );
164 transparentObjectsLayerFilter->setFilterMode( Qt3DRender::QLayerFilter::AcceptAnyMatchingLayers );
166 Qt3DRender::QSortPolicy *sortPolicy =
new Qt3DRender::QSortPolicy( transparentObjectsLayerFilter );
167 QVector<Qt3DRender::QSortPolicy::SortType> sortTypes;
168 sortTypes.push_back( Qt3DRender::QSortPolicy::BackToFront );
169 sortPolicy->setSortTypes( sortTypes );
171 Qt3DRender::QRenderStateSet *transparentObjectsRenderStateSet =
new Qt3DRender::QRenderStateSet( sortPolicy );
173 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest;
174 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
175 transparentObjectsRenderStateSet->addRenderState( depthTest );
177 Qt3DRender::QNoDepthMask *noDepthMask =
new Qt3DRender::QNoDepthMask;
178 transparentObjectsRenderStateSet->addRenderState( noDepthMask );
180 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
181 cullFace->setMode( Qt3DRender::QCullFace::CullingMode::NoCulling );
182 transparentObjectsRenderStateSet->addRenderState( cullFace );
184 Qt3DRender::QBlendEquation *blendEquation =
new Qt3DRender::QBlendEquation;
185 blendEquation->setBlendFunction( Qt3DRender::QBlendEquation::Add );
186 transparentObjectsRenderStateSet->addRenderState( blendEquation );
188 Qt3DRender::QBlendEquationArguments *blenEquationArgs =
new Qt3DRender::QBlendEquationArguments;
189 blenEquationArgs->setSourceRgb( Qt3DRender::QBlendEquationArguments::Blending::One );
190 blenEquationArgs->setDestinationRgb( Qt3DRender::QBlendEquationArguments::Blending::OneMinusSource1Alpha );
191 blenEquationArgs->setSourceAlpha( Qt3DRender::QBlendEquationArguments::Blending::One );
192 blenEquationArgs->setDestinationAlpha( Qt3DRender::QBlendEquationArguments::Blending::OneMinusSource1Alpha );
193 transparentObjectsRenderStateSet->addRenderState( blenEquationArgs );
196 mDebugOverlay =
new Qt3DRender::QDebugOverlay( mForwardClearBuffers );
197 mDebugOverlay->setEnabled(
false );
201 return mMainCameraSelector;
204Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructShadowRenderPass()
206 mLightCameraSelectorShadowPass =
new Qt3DRender::QCameraSelector;
207 mLightCameraSelectorShadowPass->setCamera( mLightCamera );
209 mShadowSceneEntitiesFilter =
new Qt3DRender::QLayerFilter( mLightCameraSelectorShadowPass );
210 mShadowSceneEntitiesFilter->addLayer( mCastShadowsLayer );
212 mShadowMapTexture =
new Qt3DRender::QTexture2D;
213 mShadowMapTexture->setWidth( mShadowMapResolution );
214 mShadowMapTexture->setHeight( mShadowMapResolution );
215 mShadowMapTexture->setFormat( Qt3DRender::QTexture2D::TextureFormat::DepthFormat );
216 mShadowMapTexture->setGenerateMipMaps(
false );
217 mShadowMapTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
218 mShadowMapTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
219 mShadowMapTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
220 mShadowMapTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
222 Qt3DRender::QRenderTarget *shadowRenderTarget =
new Qt3DRender::QRenderTarget;
223 Qt3DRender::QRenderTargetOutput *shadowRenderTargetOutput =
new Qt3DRender::QRenderTargetOutput;
224 shadowRenderTargetOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
225 shadowRenderTargetOutput->setTexture( mShadowMapTexture );
226 shadowRenderTarget->addOutput( shadowRenderTargetOutput );
228 mShadowRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mShadowSceneEntitiesFilter );
229 mShadowRenderTargetSelector->setTarget( shadowRenderTarget );
231 mShadowClearBuffers =
new Qt3DRender::QClearBuffers( mShadowRenderTargetSelector );
232 mShadowClearBuffers->setBuffers( Qt3DRender::QClearBuffers::BufferType::ColorDepthBuffer );
233 mShadowClearBuffers->setClearColor( QColor::fromRgbF( 0.0f, 1.0f, 0.0f ) );
235 mShadowRenderStateSet =
new Qt3DRender::QRenderStateSet( mShadowClearBuffers );
237 Qt3DRender::QDepthTest *shadowDepthTest =
new Qt3DRender::QDepthTest;
238 shadowDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
239 mShadowRenderStateSet->addRenderState( shadowDepthTest );
241 Qt3DRender::QCullFace *shadowCullFace =
new Qt3DRender::QCullFace;
242 shadowCullFace->setMode( Qt3DRender::QCullFace::CullingMode::Front );
243 mShadowRenderStateSet->addRenderState( shadowCullFace );
245 Qt3DRender::QPolygonOffset *polygonOffset =
new Qt3DRender::QPolygonOffset;
246 polygonOffset->setDepthSteps( 4.0 );
247 polygonOffset->setScaleFactor( 1.1 );
248 mShadowRenderStateSet->addRenderState( polygonOffset );
250 return mLightCameraSelectorShadowPass;
253Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructPostprocessingPass()
255 mPostProcessingCameraSelector =
new Qt3DRender::QCameraSelector;
256 mPostProcessingCameraSelector->setCamera( mLightCamera );
258 mPostprocessPassLayerFilter =
new Qt3DRender::QLayerFilter( mPostProcessingCameraSelector );
260 mPostprocessClearBuffers =
new Qt3DRender::QClearBuffers( mPostprocessPassLayerFilter );
262 mRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mPostprocessClearBuffers );
264 Qt3DRender::QRenderTarget *renderTarget =
new Qt3DRender::QRenderTarget( mRenderCaptureTargetSelector );
270 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( renderTarget );
271 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
274 mRenderCaptureColorTexture =
new Qt3DRender::QTexture2D( colorOutput );
275 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
276 mRenderCaptureColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
277 mRenderCaptureColorTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
278 mRenderCaptureColorTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
281 colorOutput->setTexture( mRenderCaptureColorTexture );
282 renderTarget->addOutput( colorOutput );
284 Qt3DRender::QRenderTargetOutput *depthOutput =
new Qt3DRender::QRenderTargetOutput( renderTarget );
286 depthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
287 mRenderCaptureDepthTexture =
new Qt3DRender::QTexture2D( depthOutput );
288 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
289 mRenderCaptureDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
290 mRenderCaptureDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
291 mRenderCaptureDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
292 mRenderCaptureDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
293 mRenderCaptureDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
295 depthOutput->setTexture( mRenderCaptureDepthTexture );
296 renderTarget->addOutput( depthOutput );
298 mRenderCaptureTargetSelector->setTarget( renderTarget );
300 mRenderCapture =
new Qt3DRender::QRenderCapture( mRenderCaptureTargetSelector );
303 mPostprocessPassLayerFilter->addLayer( mPostprocessingEntity->
layer() );
305 return mPostProcessingCameraSelector;
308Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructAmbientOcclusionRenderPass()
310 mAmbientOcclusionRenderCameraSelector =
new Qt3DRender::QCameraSelector;
311 mAmbientOcclusionRenderCameraSelector->setCamera( mMainCamera );
313 mAmbientOcclusionRenderStateSet =
new Qt3DRender::QRenderStateSet( mAmbientOcclusionRenderCameraSelector );
315 Qt3DRender::QDepthTest *depthRenderDepthTest =
new Qt3DRender::QDepthTest;
316 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
317 Qt3DRender::QCullFace *depthRenderCullFace =
new Qt3DRender::QCullFace;
318 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
320 mAmbientOcclusionRenderStateSet->addRenderState( depthRenderDepthTest );
321 mAmbientOcclusionRenderStateSet->addRenderState( depthRenderCullFace );
323 mAmbientOcclusionRenderLayerFilter =
new Qt3DRender::QLayerFilter( mAmbientOcclusionRenderStateSet );
325 mAmbientOcclusionRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mAmbientOcclusionRenderLayerFilter );
326 Qt3DRender::QRenderTarget *depthRenderTarget =
new Qt3DRender::QRenderTarget( mAmbientOcclusionRenderCaptureTargetSelector );
332 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
333 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
336 mAmbientOcclusionRenderTexture =
new Qt3DRender::QTexture2D( colorOutput );
337 mAmbientOcclusionRenderTexture->setSize( mSize.width(), mSize.height() );
338 mAmbientOcclusionRenderTexture->setFormat( Qt3DRender::QAbstractTexture::R32F );
339 mAmbientOcclusionRenderTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
340 mAmbientOcclusionRenderTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
343 colorOutput->setTexture( mAmbientOcclusionRenderTexture );
344 depthRenderTarget->addOutput( colorOutput );
346 mAmbientOcclusionRenderCaptureTargetSelector->setTarget( depthRenderTarget );
349 mAmbientOcclusionRenderLayerFilter->addLayer( mAmbientOcclusionRenderEntity->
layer() );
351 return mAmbientOcclusionRenderCameraSelector;
354Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructAmbientOcclusionBlurPass()
356 mAmbientOcclusionBlurCameraSelector =
new Qt3DRender::QCameraSelector;
357 mAmbientOcclusionBlurCameraSelector->setCamera( mMainCamera );
359 mAmbientOcclusionBlurStateSet =
new Qt3DRender::QRenderStateSet( mAmbientOcclusionBlurCameraSelector );
361 Qt3DRender::QDepthTest *depthRenderDepthTest =
new Qt3DRender::QDepthTest;
362 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
363 Qt3DRender::QCullFace *depthRenderCullFace =
new Qt3DRender::QCullFace;
364 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
366 mAmbientOcclusionBlurStateSet->addRenderState( depthRenderDepthTest );
367 mAmbientOcclusionBlurStateSet->addRenderState( depthRenderCullFace );
369 mAmbientOcclusionBlurLayerFilter =
new Qt3DRender::QLayerFilter( mAmbientOcclusionBlurStateSet );
371 mAmbientOcclusionBlurRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mAmbientOcclusionBlurLayerFilter );
372 Qt3DRender::QRenderTarget *depthRenderTarget =
new Qt3DRender::QRenderTarget( mAmbientOcclusionBlurRenderCaptureTargetSelector );
378 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
379 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
382 mAmbientOcclusionBlurTexture =
new Qt3DRender::QTexture2D( colorOutput );
383 mAmbientOcclusionBlurTexture->setSize( mSize.width(), mSize.height() );
384 mAmbientOcclusionBlurTexture->setFormat( Qt3DRender::QAbstractTexture::R32F );
385 mAmbientOcclusionBlurTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
386 mAmbientOcclusionBlurTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
389 colorOutput->setTexture( mAmbientOcclusionBlurTexture );
390 depthRenderTarget->addOutput( colorOutput );
392 mAmbientOcclusionBlurRenderCaptureTargetSelector->setTarget( depthRenderTarget );
395 mAmbientOcclusionBlurLayerFilter->addLayer( mAmbientOcclusionBlurEntity->
layer() );
397 return mAmbientOcclusionBlurCameraSelector;
401Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructRubberBandsPass()
403 mRubberBandsCameraSelector =
new Qt3DRender::QCameraSelector;
404 mRubberBandsCameraSelector->setCamera( mMainCamera );
406 mRubberBandsLayerFilter =
new Qt3DRender::QLayerFilter( mRubberBandsCameraSelector );
407 mRubberBandsLayerFilter->addLayer( mRubberBandsLayer );
409 mRubberBandsStateSet =
new Qt3DRender::QRenderStateSet( mRubberBandsLayerFilter );
410 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest;
411 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
412 mRubberBandsStateSet->addRenderState( depthTest );
417 mRubberBandsRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mRubberBandsStateSet );
418 mRubberBandsRenderTargetSelector->setTarget( mForwardRenderTargetSelector->target() );
420 return mRubberBandsCameraSelector;
425Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructDepthRenderPass()
429 mDepthRenderCameraSelector =
new Qt3DRender::QCameraSelector;
430 mDepthRenderCameraSelector->setCamera( mMainCamera );
432 mDepthRenderStateSet =
new Qt3DRender::QRenderStateSet( mDepthRenderCameraSelector );
434 Qt3DRender::QDepthTest *depthRenderDepthTest =
new Qt3DRender::QDepthTest;
435 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
436 Qt3DRender::QCullFace *depthRenderCullFace =
new Qt3DRender::QCullFace;
437 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
439 mDepthRenderStateSet->addRenderState( depthRenderDepthTest );
440 mDepthRenderStateSet->addRenderState( depthRenderCullFace );
442 mDepthRenderLayerFilter =
new Qt3DRender::QLayerFilter( mDepthRenderStateSet );
443 mDepthRenderLayerFilter->addLayer( mDepthRenderPassLayer );
445 mDepthRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mDepthRenderLayerFilter );
446 Qt3DRender::QRenderTarget *depthRenderTarget =
new Qt3DRender::QRenderTarget( mDepthRenderCaptureTargetSelector );
452 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
453 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
456 mDepthRenderCaptureColorTexture =
new Qt3DRender::QTexture2D( colorOutput );
457 mDepthRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
458 mDepthRenderCaptureColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
459 mDepthRenderCaptureColorTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
460 mDepthRenderCaptureColorTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
463 colorOutput->setTexture( mDepthRenderCaptureColorTexture );
464 depthRenderTarget->addOutput( colorOutput );
466 Qt3DRender::QRenderTargetOutput *depthOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
468 depthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
469 mDepthRenderCaptureDepthTexture =
new Qt3DRender::QTexture2D( depthOutput );
470 mDepthRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
471 mDepthRenderCaptureDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
472 mDepthRenderCaptureDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
473 mDepthRenderCaptureDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
474 mDepthRenderCaptureDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
475 mDepthRenderCaptureDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
477 depthOutput->setTexture( mDepthRenderCaptureDepthTexture );
478 depthRenderTarget->addOutput( depthOutput );
480 mDepthRenderCaptureTargetSelector->setTarget( depthRenderTarget );
483 mDepthRenderCapture =
new Qt3DRender::QRenderCapture( mDepthRenderCaptureTargetSelector );
485 return mDepthRenderCameraSelector;
488Qt3DCore::QEntity *QgsFrameGraph::constructDepthRenderQuad()
490 Qt3DCore::QEntity *quad =
new Qt3DCore::QEntity;
491 quad->setObjectName(
"depthRenderQuad" );
495 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 };
497 const QByteArray vertexArr( (
const char * ) vert.constData(), vert.size() *
sizeof(
float ) );
500 vertexBuffer->setData( vertexArr );
502 positionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
503 positionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
504 positionAttribute->setVertexSize( 3 );
505 positionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
506 positionAttribute->setBuffer( vertexBuffer );
507 positionAttribute->setByteOffset( 0 );
508 positionAttribute->setByteStride( 3 *
sizeof(
float ) );
509 positionAttribute->setCount( 6 );
511 geom->addAttribute( positionAttribute );
513 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
514 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
515 renderer->setGeometry( geom );
517 quad->addComponent( renderer );
519 QMatrix4x4 modelMatrix;
520 modelMatrix.setToIdentity();
524 Qt3DRender::QMaterial *material =
new Qt3DRender::QMaterial;
525 Qt3DRender::QParameter *textureParameter =
new Qt3DRender::QParameter(
"depthTexture", mForwardDepthTexture );
526 Qt3DRender::QParameter *textureTransformParameter =
new Qt3DRender::QParameter(
"modelMatrix", QVariant::fromValue( modelMatrix ) );
527 material->addParameter( textureParameter );
528 material->addParameter( textureTransformParameter );
530 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect;
532 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
534 Qt3DRender::QGraphicsApiFilter *graphicsApiFilter = technique->graphicsApiFilter();
535 graphicsApiFilter->setApi( Qt3DRender::QGraphicsApiFilter::Api::OpenGL );
536 graphicsApiFilter->setProfile( Qt3DRender::QGraphicsApiFilter::OpenGLProfile::CoreProfile );
537 graphicsApiFilter->setMajorVersion( 1 );
538 graphicsApiFilter->setMinorVersion( 5 );
540 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass;
542 Qt3DRender::QShaderProgram *shader =
new Qt3DRender::QShaderProgram;
543 shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"qrc:/shaders/depth_render.vert" ) ) );
544 shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"qrc:/shaders/depth_render.frag" ) ) );
545 renderPass->setShaderProgram( shader );
547 technique->addRenderPass( renderPass );
549 effect->addTechnique( technique );
550 material->setEffect( effect );
552 quad->addComponent( material );
592 mLightCamera =
new Qt3DRender::QCamera;
594 mPreviewLayer =
new Qt3DRender::QLayer;
595 mCastShadowsLayer =
new Qt3DRender::QLayer;
596 mForwardRenderLayer =
new Qt3DRender::QLayer;
597 mDepthRenderPassLayer =
new Qt3DRender::QLayer;
598 mTransparentObjectsPassLayer =
new Qt3DRender::QLayer;
599 mRubberBandsLayer =
new Qt3DRender::QLayer;
601 mPreviewLayer->setRecursive(
true );
602 mCastShadowsLayer->setRecursive(
true );
603 mForwardRenderLayer->setRecursive(
true );
604 mDepthRenderPassLayer->setRecursive(
true );
605 mTransparentObjectsPassLayer->setRecursive(
true );
606 mRubberBandsLayer->setRecursive(
true );
608 mRenderSurfaceSelector =
new Qt3DRender::QRenderSurfaceSelector;
610 QObject *surfaceObj =
dynamic_cast< QObject *
>( surface );
611 Q_ASSERT( surfaceObj );
613 mRenderSurfaceSelector->setSurface( surfaceObj );
614 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
616 mMainViewPort =
new Qt3DRender::QViewport( mRenderSurfaceSelector );
617 mMainViewPort->setNormalizedRect( QRectF( 0.0f, 0.0f, 1.0f, 1.0f ) );
620 Qt3DRender::QFrameGraphNode *forwardRenderPass = constructForwardRenderPass();
621 forwardRenderPass->setParent( mMainViewPort );
624 Qt3DRender::QFrameGraphNode *rubberBandsPass = constructRubberBandsPass();
625 rubberBandsPass->setParent( mMainViewPort );
628 Qt3DRender::QFrameGraphNode *shadowRenderPass = constructShadowRenderPass();
629 shadowRenderPass->setParent( mMainViewPort );
632 Qt3DRender::QFrameGraphNode *depthBufferProcessingPass = constructDepthRenderPass();
633 depthBufferProcessingPass->setParent( mMainViewPort );
636 Qt3DRender::QFrameGraphNode *ambientOcclusionFactorRender = constructAmbientOcclusionRenderPass();
637 ambientOcclusionFactorRender->setParent( mMainViewPort );
639 Qt3DRender::QFrameGraphNode *ambientOcclusionBlurPass = constructAmbientOcclusionBlurPass();
640 ambientOcclusionBlurPass->setParent( mMainViewPort );
643 Qt3DRender::QFrameGraphNode *postprocessingPass = constructPostprocessingPass();
644 postprocessingPass->setParent( mMainViewPort );
646 mRubberBandsRootEntity =
new Qt3DCore::QEntity( mRootEntity );
647 mRubberBandsRootEntity->addComponent( mRubberBandsLayer );
650 Qt3DRender::QFrameGraphNode *previewPass = constructTexturesPreviewPass();
651 previewPass->setParent( mMainViewPort );
653 Qt3DRender::QParameter *depthMapIsDepthParam =
new Qt3DRender::QParameter(
"isDepth",
true );
654 Qt3DRender::QParameter *shadowMapIsDepthParam =
new Qt3DRender::QParameter(
"isDepth",
true );
656 mDebugDepthMapPreviewQuad = this->
addTexturePreviewOverlay( mForwardDepthTexture, QPointF( 0.9f, 0.9f ), QSizeF( 0.1, 0.1 ), QVector<Qt3DRender::QParameter *> { depthMapIsDepthParam } );
657 mDebugShadowMapPreviewQuad = this->
addTexturePreviewOverlay( mShadowMapTexture, QPointF( 0.9f, 0.9f ), QSizeF( 0.1, 0.1 ), QVector<Qt3DRender::QParameter *> { shadowMapIsDepthParam } );
658 mDebugDepthMapPreviewQuad->setEnabled(
false );
659 mDebugShadowMapPreviewQuad->setEnabled(
false );
661 mDepthRenderQuad = constructDepthRenderQuad();
662 mDepthRenderQuad->addComponent( mDepthRenderPassLayer );
663 mDepthRenderQuad->setParent( mRootEntity );
669 previewQuad->addComponent( mPreviewLayer );
670 previewQuad->setParent( mRootEntity );
671 mPreviewQuads.push_back( previewQuad );
676void calculateViewExtent( Qt3DRender::QCamera *camera,
float shadowRenderingDistance,
float y,
float &minX,
float &maxX,
float &minY,
float &maxY,
float &minZ,
float &maxZ )
678 const QVector3D cameraPos = camera->position();
679 const QMatrix4x4 projectionMatrix = camera->projectionMatrix();
680 const QMatrix4x4 viewMatrix = camera->viewMatrix();
682 QVector4D viewCenter = viewMatrix * QVector4D( camera->viewCenter(), 1.0f );
683 viewCenter /= viewCenter.w();
684 viewCenter = projectionMatrix * viewCenter;
685 viewCenter /= viewCenter.w();
686 depth = viewCenter.z();
687 QVector<QVector3D> viewFrustumPoints =
689 QVector3D( 0.0f, 0.0f, depth ),
690 QVector3D( 0.0f, 1.0f, depth ),
691 QVector3D( 1.0f, 0.0f, depth ),
692 QVector3D( 1.0f, 1.0f, depth ),
693 QVector3D( 0.0f, 0.0f, 0 ),
694 QVector3D( 0.0f, 1.0f, 0 ),
695 QVector3D( 1.0f, 0.0f, 0 ),
696 QVector3D( 1.0f, 1.0f, 0 )
698 maxX = std::numeric_limits<float>::lowest();
699 maxY = std::numeric_limits<float>::lowest();
700 maxZ = std::numeric_limits<float>::lowest();
701 minX = std::numeric_limits<float>::max();
702 minY = std::numeric_limits<float>::max();
703 minZ = std::numeric_limits<float>::max();
704 for (
int i = 0; i < viewFrustumPoints.size(); ++i )
707 viewFrustumPoints[i] = viewFrustumPoints[i].unproject( viewMatrix, projectionMatrix, QRect( 0, 0, 1, 1 ) );
708 minX = std::min( minX, viewFrustumPoints[i].x() );
709 maxX = std::max( maxX, viewFrustumPoints[i].x() );
710 minY = std::min( minY, viewFrustumPoints[i].y() );
711 maxY = std::max( maxY, viewFrustumPoints[i].y() );
712 minZ = std::min( minZ, viewFrustumPoints[i].z() );
713 maxZ = std::max( maxZ, viewFrustumPoints[i].z() );
718 const QVector3D pt = cameraPos;
719 const QVector3D vect = ( viewFrustumPoints[i] - pt ).normalized();
720 float t = ( y - pt.y() ) / vect.y();
722 t = shadowRenderingDistance;
724 t = std::min( t, shadowRenderingDistance );
725 viewFrustumPoints[i] = pt + t * vect;
726 minX = std::min( minX, viewFrustumPoints[i].x() );
727 maxX = std::max( maxX, viewFrustumPoints[i].x() );
728 minY = std::min( minY, viewFrustumPoints[i].y() );
729 maxY = std::max( maxY, viewFrustumPoints[i].y() );
730 minZ = std::min( minZ, viewFrustumPoints[i].z() );
731 maxZ = std::max( maxZ, viewFrustumPoints[i].z() );
737 float minX, maxX, minY, maxY, minZ, maxZ;
738 QVector3D lookingAt = mMainCamera->viewCenter();
739 const float d = 2 * ( mMainCamera->position() - mMainCamera->viewCenter() ).length();
741 const QVector3D vertical = QVector3D( 0.0f, d, 0.0f );
743 calculateViewExtent( mMainCamera, maximumShadowRenderingDistance, lookingAt.y(), minX, maxX, minY, maxY, minZ, maxZ );
745 lookingAt = QVector3D( 0.5 * ( minX + maxX ), mMainCamera->viewCenter().y(), 0.5 * ( minZ + maxZ ) );
746 const QVector3D lightPosition = lookingAt + vertical;
747 mLightCamera->setPosition( lightPosition );
748 mLightCamera->setViewCenter( lookingAt );
749 mLightCamera->setUpVector( QVector3D( 0.0f, 1.0f, 0.0f ) );
750 mLightCamera->rotateAboutViewCenter( QQuaternion::rotationTo( vertical.normalized(), -lightDirection.normalized() ) );
752 mLightCamera->setProjectionType( Qt3DRender::QCameraLens::ProjectionType::OrthographicProjection );
753 mLightCamera->lens()->setOrthographicProjection(
754 - 0.7 * ( maxX - minX ), 0.7 * ( maxX - minX ),
755 - 0.7 * ( maxZ - minZ ), 0.7 * ( maxZ - minZ ),
756 1.0f, 2 * ( lookingAt - lightPosition ).length() );
764 mForwardClearBuffers->setClearColor( clearColor );
769 mShadowRenderingEnabled = enabled;
771 if ( mShadowRenderingEnabled )
772 mShadowSceneEntitiesFilter->setEnabled(
true );
774 mShadowSceneEntitiesFilter->setEnabled(
false );
785 mShadowMapResolution = resolution;
786 mShadowMapTexture->setWidth( mShadowMapResolution );
787 mShadowMapTexture->setHeight( mShadowMapResolution );
792 mAmbientOcclusionEnabled = enabled;
793 mAmbientOcclusionRenderEntity->setEnabled( enabled );
799 mAmbientOcclusionIntensity = intensity;
800 mAmbientOcclusionRenderEntity->
setIntensity( intensity );
805 mAmbientOcclusionRadius = radius;
806 mAmbientOcclusionRenderEntity->
setRadius( radius );
811 mAmbientOcclusionThreshold = threshold;
812 mAmbientOcclusionRenderEntity->
setThreshold( threshold );
817 if ( enabled == mFrustumCullingEnabled )
819 mFrustumCullingEnabled = enabled;
820 if ( mFrustumCullingEnabled )
821 mFrustumCulling->setParent( mForwardClearBuffers );
823 mFrustumCulling->setParent( ( Qt3DCore::QNode * )
nullptr );
828 mEyeDomeLightingEnabled = enabled;
829 mEyeDomeLightingStrength = strength;
830 mEyeDomeLightingDistance = distance;
838 mDebugShadowMapPreviewQuad->setEnabled( enabled );
843 case Qt::Corner::TopRightCorner:
844 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
846 case Qt::Corner::TopLeftCorner:
847 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
849 case Qt::Corner::BottomRightCorner:
850 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
852 case Qt::Corner::BottomLeftCorner:
853 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
861 mDebugDepthMapPreviewQuad->setEnabled( enabled );
867 case Qt::Corner::TopRightCorner:
868 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
870 case Qt::Corner::TopLeftCorner:
871 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
873 case Qt::Corner::BottomRightCorner:
874 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
876 case Qt::Corner::BottomLeftCorner:
877 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
886 mForwardColorTexture->setSize( mSize.width(), mSize.height() );
887 mForwardDepthTexture->setSize( mSize.width(), mSize.height() );
888 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
889 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
890 mDepthRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
891 mDepthRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
892 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
894 mAmbientOcclusionRenderTexture->setSize( mSize.width(), mSize.height() );
895 mAmbientOcclusionBlurTexture->setSize( mSize.width(), mSize.height() );
900 if ( enabled == mRenderCaptureEnabled )
902 mRenderCaptureEnabled = enabled;
903 mRenderCaptureTargetSelector->setEnabled( mRenderCaptureEnabled );
908 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