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 *QgsShadowRenderingFrameGraph::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 *QgsShadowRenderingFrameGraph::constructForwardRenderPass()
77 mMainCameraSelector =
new Qt3DRender::QCameraSelector;
78 mMainCameraSelector->setCamera( mMainCamera );
80 mForwardRenderLayerFilter =
new Qt3DRender::QLayerFilter( mMainCameraSelector );
81 mForwardRenderLayerFilter->addLayer( mForwardRenderLayer );
83 mForwardColorTexture =
new Qt3DRender::QTexture2D;
84 mForwardColorTexture->setWidth( mSize.width() );
85 mForwardColorTexture->setHeight( mSize.height() );
86 mForwardColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
87 mForwardColorTexture->setGenerateMipMaps(
false );
88 mForwardColorTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
89 mForwardColorTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
90 mForwardColorTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
91 mForwardColorTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
93 mForwardDepthTexture =
new Qt3DRender::QTexture2D;
94 mForwardDepthTexture->setWidth( mSize.width() );
95 mForwardDepthTexture->setHeight( mSize.height() );
96 mForwardDepthTexture->setFormat( Qt3DRender::QTexture2D::TextureFormat::DepthFormat );
97 mForwardDepthTexture->setGenerateMipMaps(
false );
98 mForwardDepthTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
99 mForwardDepthTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
100 mForwardDepthTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
101 mForwardDepthTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
103 Qt3DRender::QRenderTarget *forwardRenderTarget =
new Qt3DRender::QRenderTarget;
104 Qt3DRender::QRenderTargetOutput *forwardRenderTargetDepthOutput =
new Qt3DRender::QRenderTargetOutput;
105 forwardRenderTargetDepthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
106 forwardRenderTargetDepthOutput->setTexture( mForwardDepthTexture );
107 forwardRenderTarget->addOutput( forwardRenderTargetDepthOutput );
108 Qt3DRender::QRenderTargetOutput *forwardRenderTargetColorOutput =
new Qt3DRender::QRenderTargetOutput;
109 forwardRenderTargetColorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
110 forwardRenderTargetColorOutput->setTexture( mForwardColorTexture );
111 forwardRenderTarget->addOutput( forwardRenderTargetColorOutput );
113 mForwardRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mForwardRenderLayerFilter );
114 mForwardRenderTargetSelector->setTarget( forwardRenderTarget );
116 Qt3DRender::QLayerFilter *opaqueObjectsFilter =
new Qt3DRender::QLayerFilter( mForwardRenderTargetSelector );
117 opaqueObjectsFilter->addLayer( mTransparentObjectsPassLayer );
118 opaqueObjectsFilter->setFilterMode( Qt3DRender::QLayerFilter::DiscardAnyMatchingLayers );
120 Qt3DRender::QRenderStateSet *forwaredRenderStateSet =
new Qt3DRender::QRenderStateSet( opaqueObjectsFilter );
122 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest;
123 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
124 forwaredRenderStateSet->addRenderState( depthTest );
126 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
127 cullFace->setMode( Qt3DRender::QCullFace::CullingMode::Back );
128 forwaredRenderStateSet->addRenderState( cullFace );
130 mFrustumCulling =
new Qt3DRender::QFrustumCulling( forwaredRenderStateSet );
132 mForwardClearBuffers =
new Qt3DRender::QClearBuffers( mFrustumCulling );
133 mForwardClearBuffers->setClearColor( QColor::fromRgbF( 0.0, 0.0, 1.0, 1.0 ) );
134 mForwardClearBuffers->setBuffers( Qt3DRender::QClearBuffers::ColorDepthBuffer );
135 mForwardClearBuffers->setClearDepthValue( 1.0f );
137 Qt3DRender::QLayerFilter *transparentObjectsLayerFilter =
new Qt3DRender::QLayerFilter( mForwardRenderTargetSelector );
138 transparentObjectsLayerFilter->addLayer( mTransparentObjectsPassLayer );
139 transparentObjectsLayerFilter->setFilterMode( Qt3DRender::QLayerFilter::AcceptAnyMatchingLayers );
141 Qt3DRender::QSortPolicy *sortPolicy =
new Qt3DRender::QSortPolicy( transparentObjectsLayerFilter );
142 QVector<Qt3DRender::QSortPolicy::SortType> sortTypes;
143 sortTypes.push_back( Qt3DRender::QSortPolicy::BackToFront );
144 sortPolicy->setSortTypes( sortTypes );
146 Qt3DRender::QRenderStateSet *transparentObjectsRenderStateSet =
new Qt3DRender::QRenderStateSet( sortPolicy );
148 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest;
149 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
150 transparentObjectsRenderStateSet->addRenderState( depthTest );
152 Qt3DRender::QNoDepthMask *noDepthMask =
new Qt3DRender::QNoDepthMask;
153 transparentObjectsRenderStateSet->addRenderState( noDepthMask );
155 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
156 cullFace->setMode( Qt3DRender::QCullFace::CullingMode::NoCulling );
157 transparentObjectsRenderStateSet->addRenderState( cullFace );
159 Qt3DRender::QBlendEquation *blendEquation =
new Qt3DRender::QBlendEquation;
160 blendEquation->setBlendFunction( Qt3DRender::QBlendEquation::Add );
161 transparentObjectsRenderStateSet->addRenderState( blendEquation );
163 Qt3DRender::QBlendEquationArguments *blenEquationArgs =
new Qt3DRender::QBlendEquationArguments;
164 blenEquationArgs->setSourceRgb( Qt3DRender::QBlendEquationArguments::Blending::One );
165 blenEquationArgs->setDestinationRgb( Qt3DRender::QBlendEquationArguments::Blending::OneMinusSource1Alpha );
166 blenEquationArgs->setSourceAlpha( Qt3DRender::QBlendEquationArguments::Blending::One );
167 blenEquationArgs->setDestinationAlpha( Qt3DRender::QBlendEquationArguments::Blending::OneMinusSource1Alpha );
168 transparentObjectsRenderStateSet->addRenderState( blenEquationArgs );
171#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
172 mDebugOverlay =
new Qt3DRender::QDebugOverlay( mForwardClearBuffers );
173 mDebugOverlay->setEnabled(
false );
178 return mMainCameraSelector;
181Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructShadowRenderPass()
183 mLightCameraSelectorShadowPass =
new Qt3DRender::QCameraSelector;
184 mLightCameraSelectorShadowPass->setCamera( mLightCamera );
186 mShadowSceneEntitiesFilter =
new Qt3DRender::QLayerFilter( mLightCameraSelectorShadowPass );
187 mShadowSceneEntitiesFilter->addLayer( mCastShadowsLayer );
189 mShadowMapTexture =
new Qt3DRender::QTexture2D;
190 mShadowMapTexture->setWidth( mShadowMapResolution );
191 mShadowMapTexture->setHeight( mShadowMapResolution );
192 mShadowMapTexture->setFormat( Qt3DRender::QTexture2D::TextureFormat::DepthFormat );
193 mShadowMapTexture->setGenerateMipMaps(
false );
194 mShadowMapTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
195 mShadowMapTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
196 mShadowMapTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
197 mShadowMapTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
199 Qt3DRender::QRenderTarget *shadowRenderTarget =
new Qt3DRender::QRenderTarget;
200 Qt3DRender::QRenderTargetOutput *shadowRenderTargetOutput =
new Qt3DRender::QRenderTargetOutput;
201 shadowRenderTargetOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
202 shadowRenderTargetOutput->setTexture( mShadowMapTexture );
203 shadowRenderTarget->addOutput( shadowRenderTargetOutput );
205 mShadowRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mShadowSceneEntitiesFilter );
206 mShadowRenderTargetSelector->setTarget( shadowRenderTarget );
208 mShadowClearBuffers =
new Qt3DRender::QClearBuffers( mShadowRenderTargetSelector );
209 mShadowClearBuffers->setBuffers( Qt3DRender::QClearBuffers::BufferType::ColorDepthBuffer );
210 mShadowClearBuffers->setClearColor( QColor::fromRgbF( 0.0f, 1.0f, 0.0f ) );
212 mShadowRenderStateSet =
new Qt3DRender::QRenderStateSet( mShadowClearBuffers );
214 Qt3DRender::QDepthTest *shadowDepthTest =
new Qt3DRender::QDepthTest;
215 shadowDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
216 mShadowRenderStateSet->addRenderState( shadowDepthTest );
218 Qt3DRender::QCullFace *shadowCullFace =
new Qt3DRender::QCullFace;
219 shadowCullFace->setMode( Qt3DRender::QCullFace::CullingMode::Front );
220 mShadowRenderStateSet->addRenderState( shadowCullFace );
222 Qt3DRender::QPolygonOffset *polygonOffset =
new Qt3DRender::QPolygonOffset;
223 polygonOffset->setDepthSteps( 4.0 );
224 polygonOffset->setScaleFactor( 1.1 );
225 mShadowRenderStateSet->addRenderState( polygonOffset );
227 return mLightCameraSelectorShadowPass;
230Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructPostprocessingPass()
232 mPostProcessingCameraSelector =
new Qt3DRender::QCameraSelector;
233 mPostProcessingCameraSelector->setCamera( mLightCamera );
235 mPostprocessPassLayerFilter =
new Qt3DRender::QLayerFilter( mPostProcessingCameraSelector );
237 mPostprocessClearBuffers =
new Qt3DRender::QClearBuffers( mPostprocessPassLayerFilter );
239 mRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mPostprocessClearBuffers );
241 Qt3DRender::QRenderTarget *renderTarget =
new Qt3DRender::QRenderTarget( mRenderCaptureTargetSelector );
247 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( renderTarget );
248 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
251 mRenderCaptureColorTexture =
new Qt3DRender::QTexture2D( colorOutput );
252 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
253 mRenderCaptureColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
254 mRenderCaptureColorTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
255 mRenderCaptureColorTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
258 colorOutput->setTexture( mRenderCaptureColorTexture );
259 renderTarget->addOutput( colorOutput );
261 Qt3DRender::QRenderTargetOutput *depthOutput =
new Qt3DRender::QRenderTargetOutput( renderTarget );
263 depthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
264 mRenderCaptureDepthTexture =
new Qt3DRender::QTexture2D( depthOutput );
265 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
266 mRenderCaptureDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
267 mRenderCaptureDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
268 mRenderCaptureDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
269 mRenderCaptureDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
270 mRenderCaptureDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
272 depthOutput->setTexture( mRenderCaptureDepthTexture );
273 renderTarget->addOutput( depthOutput );
275 mRenderCaptureTargetSelector->setTarget( renderTarget );
277 mRenderCapture =
new Qt3DRender::QRenderCapture( mRenderCaptureTargetSelector );
280 mPostprocessPassLayerFilter->addLayer( mPostprocessingEntity->
layer() );
282 return mPostProcessingCameraSelector;
285Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructAmbientOcclusionRenderPass()
287 mAmbientOcclusionRenderCameraSelector =
new Qt3DRender::QCameraSelector;
288 mAmbientOcclusionRenderCameraSelector->setCamera( mMainCamera );
290 mAmbientOcclusionRenderStateSet =
new Qt3DRender::QRenderStateSet( mAmbientOcclusionRenderCameraSelector );
292 Qt3DRender::QDepthTest *depthRenderDepthTest =
new Qt3DRender::QDepthTest;
293 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
294 Qt3DRender::QCullFace *depthRenderCullFace =
new Qt3DRender::QCullFace;
295 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
297 mAmbientOcclusionRenderStateSet->addRenderState( depthRenderDepthTest );
298 mAmbientOcclusionRenderStateSet->addRenderState( depthRenderCullFace );
300 mAmbientOcclusionRenderLayerFilter =
new Qt3DRender::QLayerFilter( mAmbientOcclusionRenderStateSet );
302 mAmbientOcclusionRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mAmbientOcclusionRenderLayerFilter );
303 Qt3DRender::QRenderTarget *depthRenderTarget =
new Qt3DRender::QRenderTarget( mAmbientOcclusionRenderCaptureTargetSelector );
309 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
310 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
313 mAmbientOcclusionRenderTexture =
new Qt3DRender::QTexture2D( colorOutput );
314 mAmbientOcclusionRenderTexture->setSize( mSize.width(), mSize.height() );
315 mAmbientOcclusionRenderTexture->setFormat( Qt3DRender::QAbstractTexture::R32F );
316 mAmbientOcclusionRenderTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
317 mAmbientOcclusionRenderTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
320 colorOutput->setTexture( mAmbientOcclusionRenderTexture );
321 depthRenderTarget->addOutput( colorOutput );
323 mAmbientOcclusionRenderCaptureTargetSelector->setTarget( depthRenderTarget );
326 mAmbientOcclusionRenderLayerFilter->addLayer( mAmbientOcclusionRenderEntity->
layer() );
328 return mAmbientOcclusionRenderCameraSelector;
331Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructAmbientOcclusionBlurPass()
333 mAmbientOcclusionBlurCameraSelector =
new Qt3DRender::QCameraSelector;
334 mAmbientOcclusionBlurCameraSelector->setCamera( mMainCamera );
336 mAmbientOcclusionBlurStateSet =
new Qt3DRender::QRenderStateSet( mAmbientOcclusionBlurCameraSelector );
338 Qt3DRender::QDepthTest *depthRenderDepthTest =
new Qt3DRender::QDepthTest;
339 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
340 Qt3DRender::QCullFace *depthRenderCullFace =
new Qt3DRender::QCullFace;
341 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
343 mAmbientOcclusionBlurStateSet->addRenderState( depthRenderDepthTest );
344 mAmbientOcclusionBlurStateSet->addRenderState( depthRenderCullFace );
346 mAmbientOcclusionBlurLayerFilter =
new Qt3DRender::QLayerFilter( mAmbientOcclusionBlurStateSet );
348 mAmbientOcclusionBlurRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mAmbientOcclusionBlurLayerFilter );
349 Qt3DRender::QRenderTarget *depthRenderTarget =
new Qt3DRender::QRenderTarget( mAmbientOcclusionBlurRenderCaptureTargetSelector );
355 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
356 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
359 mAmbientOcclusionBlurTexture =
new Qt3DRender::QTexture2D( colorOutput );
360 mAmbientOcclusionBlurTexture->setSize( mSize.width(), mSize.height() );
361 mAmbientOcclusionBlurTexture->setFormat( Qt3DRender::QAbstractTexture::R32F );
362 mAmbientOcclusionBlurTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
363 mAmbientOcclusionBlurTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
366 colorOutput->setTexture( mAmbientOcclusionBlurTexture );
367 depthRenderTarget->addOutput( colorOutput );
369 mAmbientOcclusionBlurRenderCaptureTargetSelector->setTarget( depthRenderTarget );
372 mAmbientOcclusionBlurLayerFilter->addLayer( mAmbientOcclusionBlurEntity->
layer() );
374 return mAmbientOcclusionBlurCameraSelector;
379Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructDepthRenderPass()
383 mDepthRenderCameraSelector =
new Qt3DRender::QCameraSelector;
384 mDepthRenderCameraSelector->setCamera( mMainCamera );
386 mDepthRenderStateSet =
new Qt3DRender::QRenderStateSet( mDepthRenderCameraSelector );
388 Qt3DRender::QDepthTest *depthRenderDepthTest =
new Qt3DRender::QDepthTest;
389 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
390 Qt3DRender::QCullFace *depthRenderCullFace =
new Qt3DRender::QCullFace;
391 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
393 mDepthRenderStateSet->addRenderState( depthRenderDepthTest );
394 mDepthRenderStateSet->addRenderState( depthRenderCullFace );
396 mDepthRenderLayerFilter =
new Qt3DRender::QLayerFilter( mDepthRenderStateSet );
397 mDepthRenderLayerFilter->addLayer( mDepthRenderPassLayer );
399 mDepthRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mDepthRenderLayerFilter );
400 Qt3DRender::QRenderTarget *depthRenderTarget =
new Qt3DRender::QRenderTarget( mDepthRenderCaptureTargetSelector );
406 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
407 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
410 mDepthRenderCaptureColorTexture =
new Qt3DRender::QTexture2D( colorOutput );
411 mDepthRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
412 mDepthRenderCaptureColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
413 mDepthRenderCaptureColorTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
414 mDepthRenderCaptureColorTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
417 colorOutput->setTexture( mDepthRenderCaptureColorTexture );
418 depthRenderTarget->addOutput( colorOutput );
420 Qt3DRender::QRenderTargetOutput *depthOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
422 depthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
423 mDepthRenderCaptureDepthTexture =
new Qt3DRender::QTexture2D( depthOutput );
424 mDepthRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
425 mDepthRenderCaptureDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
426 mDepthRenderCaptureDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
427 mDepthRenderCaptureDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
428 mDepthRenderCaptureDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
429 mDepthRenderCaptureDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
431 depthOutput->setTexture( mDepthRenderCaptureDepthTexture );
432 depthRenderTarget->addOutput( depthOutput );
434 mDepthRenderCaptureTargetSelector->setTarget( depthRenderTarget );
437 mDepthRenderCapture =
new Qt3DRender::QRenderCapture( mDepthRenderCaptureTargetSelector );
439 return mDepthRenderCameraSelector;
442Qt3DCore::QEntity *QgsShadowRenderingFrameGraph::constructDepthRenderQuad()
444 Qt3DCore::QEntity *quad =
new Qt3DCore::QEntity;
445 quad->setObjectName(
"depthRenderQuad" );
449 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 };
451 const QByteArray vertexArr( (
const char * ) vert.constData(), vert.size() *
sizeof(
float ) );
454 vertexBuffer->setData( vertexArr );
456 positionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
457 positionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
458 positionAttribute->setVertexSize( 3 );
459 positionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
460 positionAttribute->setBuffer( vertexBuffer );
461 positionAttribute->setByteOffset( 0 );
462 positionAttribute->setByteStride( 3 *
sizeof(
float ) );
463 positionAttribute->setCount( 6 );
465 geom->addAttribute( positionAttribute );
467 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
468 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
469 renderer->setGeometry( geom );
471 quad->addComponent( renderer );
473 QMatrix4x4 modelMatrix;
474 modelMatrix.setToIdentity();
478 Qt3DRender::QMaterial *material =
new Qt3DRender::QMaterial;
479 Qt3DRender::QParameter *textureParameter =
new Qt3DRender::QParameter(
"depthTexture", mForwardDepthTexture );
480 Qt3DRender::QParameter *textureTransformParameter =
new Qt3DRender::QParameter(
"modelMatrix", QVariant::fromValue( modelMatrix ) );
481 material->addParameter( textureParameter );
482 material->addParameter( textureTransformParameter );
484 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect;
486 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
488 Qt3DRender::QGraphicsApiFilter *graphicsApiFilter = technique->graphicsApiFilter();
489 graphicsApiFilter->setApi( Qt3DRender::QGraphicsApiFilter::Api::OpenGL );
490 graphicsApiFilter->setProfile( Qt3DRender::QGraphicsApiFilter::OpenGLProfile::CoreProfile );
491 graphicsApiFilter->setMajorVersion( 1 );
492 graphicsApiFilter->setMinorVersion( 5 );
494 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass;
496 Qt3DRender::QShaderProgram *shader =
new Qt3DRender::QShaderProgram;
497 shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"qrc:/shaders/depth_render.vert" ) ) );
498 shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"qrc:/shaders/depth_render.frag" ) ) );
499 renderPass->setShaderProgram( shader );
501 technique->addRenderPass( renderPass );
503 effect->addTechnique( technique );
504 material->setEffect( effect );
506 quad->addComponent( material );
517 mLightCamera =
new Qt3DRender::QCamera;
519 mPreviewLayer =
new Qt3DRender::QLayer;
520 mCastShadowsLayer =
new Qt3DRender::QLayer;
521 mForwardRenderLayer =
new Qt3DRender::QLayer;
522 mDepthRenderPassLayer =
new Qt3DRender::QLayer;
523 mTransparentObjectsPassLayer =
new Qt3DRender::QLayer;
525 mPreviewLayer->setRecursive(
true );
526 mCastShadowsLayer->setRecursive(
true );
527 mForwardRenderLayer->setRecursive(
true );
528 mDepthRenderPassLayer->setRecursive(
true );
529 mTransparentObjectsPassLayer->setRecursive(
true );
531 mRenderSurfaceSelector =
new Qt3DRender::QRenderSurfaceSelector;
533 QObject *surfaceObj =
dynamic_cast< QObject *
>( surface );
534 Q_ASSERT( surfaceObj );
536 mRenderSurfaceSelector->setSurface( surfaceObj );
537 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
539 mMainViewPort =
new Qt3DRender::QViewport( mRenderSurfaceSelector );
540 mMainViewPort->setNormalizedRect( QRectF( 0.0f, 0.0f, 1.0f, 1.0f ) );
543 Qt3DRender::QFrameGraphNode *forwardRenderPass = constructForwardRenderPass();
544 forwardRenderPass->setParent( mMainViewPort );
548 Qt3DRender::QFrameGraphNode *shadowRenderPass = constructShadowRenderPass();
549 shadowRenderPass->setParent( mMainViewPort );
552 Qt3DRender::QFrameGraphNode *depthBufferProcessingPass = constructDepthRenderPass();
553 depthBufferProcessingPass->setParent( mMainViewPort );
556 Qt3DRender::QFrameGraphNode *ambientOcclusionFactorRender = constructAmbientOcclusionRenderPass();
557 ambientOcclusionFactorRender->setParent( mMainViewPort );
559 Qt3DRender::QFrameGraphNode *ambientOcclusionBlurPass = constructAmbientOcclusionBlurPass();
560 ambientOcclusionBlurPass->setParent( mMainViewPort );
563 Qt3DRender::QFrameGraphNode *postprocessingPass = constructPostprocessingPass();
564 postprocessingPass->setParent( mMainViewPort );
567 Qt3DRender::QFrameGraphNode *previewPass = constructTexturesPreviewPass();
568 previewPass->setParent( mMainViewPort );
570 Qt3DRender::QParameter *depthMapIsDepthParam =
new Qt3DRender::QParameter(
"isDepth",
true );
571 Qt3DRender::QParameter *shadowMapIsDepthParam =
new Qt3DRender::QParameter(
"isDepth",
true );
573 mDebugDepthMapPreviewQuad = this->
addTexturePreviewOverlay( mForwardDepthTexture, QPointF( 0.9f, 0.9f ), QSizeF( 0.1, 0.1 ), QVector<Qt3DRender::QParameter *> { depthMapIsDepthParam } );
574 mDebugShadowMapPreviewQuad = this->
addTexturePreviewOverlay( mShadowMapTexture, QPointF( 0.9f, 0.9f ), QSizeF( 0.1, 0.1 ), QVector<Qt3DRender::QParameter *> { shadowMapIsDepthParam } );
575 mDebugDepthMapPreviewQuad->setEnabled(
false );
576 mDebugShadowMapPreviewQuad->setEnabled(
false );
578 mDepthRenderQuad = constructDepthRenderQuad();
579 mDepthRenderQuad->addComponent( mDepthRenderPassLayer );
580 mDepthRenderQuad->setParent( mRootEntity );
586 previewQuad->addComponent( mPreviewLayer );
587 previewQuad->setParent( mRootEntity );
588 mPreviewQuads.push_back( previewQuad );
593void calculateViewExtent( Qt3DRender::QCamera *camera,
float shadowRenderingDistance,
float y,
float &minX,
float &maxX,
float &minY,
float &maxY,
float &minZ,
float &maxZ )
595 const QVector3D cameraPos = camera->position();
596 const QMatrix4x4 projectionMatrix = camera->projectionMatrix();
597 const QMatrix4x4 viewMatrix = camera->viewMatrix();
599 QVector4D viewCenter = viewMatrix * QVector4D( camera->viewCenter(), 1.0f );
600 viewCenter /= viewCenter.w();
601 viewCenter = projectionMatrix * viewCenter;
602 viewCenter /= viewCenter.w();
603 depth = viewCenter.z();
604 QVector<QVector3D> viewFrustumPoints =
606 QVector3D( 0.0f, 0.0f, depth ),
607 QVector3D( 0.0f, 1.0f, depth ),
608 QVector3D( 1.0f, 0.0f, depth ),
609 QVector3D( 1.0f, 1.0f, depth ),
610 QVector3D( 0.0f, 0.0f, 0 ),
611 QVector3D( 0.0f, 1.0f, 0 ),
612 QVector3D( 1.0f, 0.0f, 0 ),
613 QVector3D( 1.0f, 1.0f, 0 )
615 maxX = std::numeric_limits<float>::lowest();
616 maxY = std::numeric_limits<float>::lowest();
617 maxZ = std::numeric_limits<float>::lowest();
618 minX = std::numeric_limits<float>::max();
619 minY = std::numeric_limits<float>::max();
620 minZ = std::numeric_limits<float>::max();
621 for (
int i = 0; i < viewFrustumPoints.size(); ++i )
624 viewFrustumPoints[i] = viewFrustumPoints[i].unproject( viewMatrix, projectionMatrix, QRect( 0, 0, 1, 1 ) );
625 minX = std::min( minX, viewFrustumPoints[i].x() );
626 maxX = std::max( maxX, viewFrustumPoints[i].x() );
627 minY = std::min( minY, viewFrustumPoints[i].y() );
628 maxY = std::max( maxY, viewFrustumPoints[i].y() );
629 minZ = std::min( minZ, viewFrustumPoints[i].z() );
630 maxZ = std::max( maxZ, viewFrustumPoints[i].z() );
635 const QVector3D pt = cameraPos;
636 const QVector3D vect = ( viewFrustumPoints[i] - pt ).normalized();
637 float t = ( y - pt.y() ) / vect.y();
639 t = shadowRenderingDistance;
641 t = std::min( t, shadowRenderingDistance );
642 viewFrustumPoints[i] = pt + t * vect;
643 minX = std::min( minX, viewFrustumPoints[i].x() );
644 maxX = std::max( maxX, viewFrustumPoints[i].x() );
645 minY = std::min( minY, viewFrustumPoints[i].y() );
646 maxY = std::max( maxY, viewFrustumPoints[i].y() );
647 minZ = std::min( minZ, viewFrustumPoints[i].z() );
648 maxZ = std::max( maxZ, viewFrustumPoints[i].z() );
654 float minX, maxX, minY, maxY, minZ, maxZ;
655 QVector3D lookingAt = mMainCamera->viewCenter();
656 const float d = 2 * ( mMainCamera->position() - mMainCamera->viewCenter() ).length();
658 const QVector3D vertical = QVector3D( 0.0f, d, 0.0f );
660 calculateViewExtent( mMainCamera, maximumShadowRenderingDistance, lookingAt.y(), minX, maxX, minY, maxY, minZ, maxZ );
662 lookingAt = QVector3D( 0.5 * ( minX + maxX ), mMainCamera->viewCenter().y(), 0.5 * ( minZ + maxZ ) );
663 const QVector3D lightPosition = lookingAt + vertical;
664 mLightCamera->setPosition( lightPosition );
665 mLightCamera->setViewCenter( lookingAt );
666 mLightCamera->setUpVector( QVector3D( 0.0f, 1.0f, 0.0f ) );
667 mLightCamera->rotateAboutViewCenter( QQuaternion::rotationTo( vertical.normalized(), -lightDirection.normalized() ) );
669 mLightCamera->setProjectionType( Qt3DRender::QCameraLens::ProjectionType::OrthographicProjection );
670 mLightCamera->lens()->setOrthographicProjection(
671 - 0.7 * ( maxX - minX ), 0.7 * ( maxX - minX ),
672 - 0.7 * ( maxZ - minZ ), 0.7 * ( maxZ - minZ ),
673 1.0f, 2 * ( lookingAt - lightPosition ).length() );
681 mForwardClearBuffers->setClearColor( clearColor );
686 mShadowRenderingEnabled = enabled;
688 if ( mShadowRenderingEnabled )
689 mShadowSceneEntitiesFilter->setEnabled(
true );
691 mShadowSceneEntitiesFilter->setEnabled(
false );
702 mShadowMapResolution = resolution;
703 mShadowMapTexture->setWidth( mShadowMapResolution );
704 mShadowMapTexture->setHeight( mShadowMapResolution );
709 mAmbientOcclusionEnabled = enabled;
710 mAmbientOcclusionRenderEntity->setEnabled( enabled );
716 mAmbientOcclusionIntensity = intensity;
717 mAmbientOcclusionRenderEntity->
setIntensity( intensity );
722 mAmbientOcclusionRadius = radius;
723 mAmbientOcclusionRenderEntity->
setRadius( radius );
728 mAmbientOcclusionThreshold = threshold;
729 mAmbientOcclusionRenderEntity->
setThreshold( threshold );
734 if ( enabled == mFrustumCullingEnabled )
736 mFrustumCullingEnabled = enabled;
737 if ( mFrustumCullingEnabled )
738 mFrustumCulling->setParent( mForwardClearBuffers );
740 mFrustumCulling->setParent( ( Qt3DCore::QNode * )
nullptr );
745 mEyeDomeLightingEnabled = enabled;
746 mEyeDomeLightingStrength = strength;
747 mEyeDomeLightingDistance = distance;
755 mDebugShadowMapPreviewQuad->setEnabled( enabled );
760 case Qt::Corner::TopRightCorner:
761 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
763 case Qt::Corner::TopLeftCorner:
764 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
766 case Qt::Corner::BottomRightCorner:
767 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
769 case Qt::Corner::BottomLeftCorner:
770 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
778 mDebugDepthMapPreviewQuad->setEnabled( enabled );
784 case Qt::Corner::TopRightCorner:
785 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
787 case Qt::Corner::TopLeftCorner:
788 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
790 case Qt::Corner::BottomRightCorner:
791 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
793 case Qt::Corner::BottomLeftCorner:
794 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
803 mForwardColorTexture->setSize( mSize.width(), mSize.height() );
804 mForwardDepthTexture->setSize( mSize.width(), mSize.height() );
805 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
806 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
807 mDepthRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
808 mDepthRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
809 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
811 mAmbientOcclusionRenderTexture->setSize( mSize.width(), mSize.height() );
812 mAmbientOcclusionBlurTexture->setSize( mSize.width(), mSize.height() );
817 if ( enabled == mRenderCaptureEnabled )
819 mRenderCaptureEnabled = enabled;
820 mRenderCaptureTargetSelector->setEnabled( mRenderCaptureEnabled );
825#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
826 mDebugOverlay->setEnabled( enabled );
829 qDebug() <<
"Cannot display debug overlay. Qt version 5.15 or above is needed.";
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 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.
void setupDepthMapDebugging(bool enabled, Qt::Corner corner, double size)
Sets the depth map debugging view port.
void setAmbientOcclusionThreshold(float threshold)
Sets the ambient occlusion threshold.
QgsShadowRenderingFrameGraph(QSurface *surface, QSize s, Qt3DRender::QCamera *mainCamera, Qt3DCore::QEntity *root)
Constructor.
void setupShadowMapDebugging(bool enabled, Qt::Corner corner, double size)
Sets the shadow map debugging view port.
void setShadowBias(float shadowBias)
Sets the shadow bias value.
void setAmbientOcclusionIntensity(float intensity)
Sets the ambient occlusion intensity.
void setShadowMapResolution(int resolution)
Sets the resolution of the shadow map.
void setSize(QSize s)
Sets the size of the buffers used for rendering.
void setupEyeDomeLighting(bool enabled, double strength, int distance)
Sets eye dome lighting shading related settings.
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 setAmbientOcclusionRadius(float radius)
Sets the ambient occlusion radius.
void setDebugOverlayEnabled(bool enabled)
Sets whether debug overlay is enabled.
void setAmbientOcclusionEnabled(bool enabled)
Sets whether Screen Space Ambient Occlusion will be enabled.
void setFrustumCullingEnabled(bool enabled)
Sets whether frustum culling is enabled.
void setClearColor(const QColor &clearColor)
Sets the clear color of the scene (background color)
void setRenderCaptureEnabled(bool enabled)
Sets whether it will be possible to render to an image.
void setupDirectionalLight(const QgsDirectionalLightSettings &light, float maximumShadowRenderingDistance)
Sets shadow rendering to use a directional light.
float shadowBias() const
Returns the shadow bias value.
Qt3DRender::QCamera * mainCamera()
Returns the main camera.
void setShadowRenderingEnabled(bool enabled)
Sets whether the shadow rendering is enabled.
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