QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsframegraph.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsframegraph.cpp
3 --------------------------------------
4 Date : August 2020
5 Copyright : (C) 2020 by Belgacem Nedjima
6 Email : gb underscore nedjima at esi dot dz
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsframegraph.h"
17#include "moc_qgsframegraph.cpp"
20#include "qgspreviewquad.h"
21#include "qgs3dutils.h"
24
25#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
26#include <Qt3DRender/QAttribute>
27#include <Qt3DRender/QBuffer>
28#include <Qt3DRender/QGeometry>
29
30typedef Qt3DRender::QAttribute Qt3DQAttribute;
31typedef Qt3DRender::QBuffer Qt3DQBuffer;
32typedef Qt3DRender::QGeometry Qt3DQGeometry;
33#else
34#include <Qt3DCore/QAttribute>
35#include <Qt3DCore/QBuffer>
36#include <Qt3DCore/QGeometry>
37
38typedef Qt3DCore::QAttribute Qt3DQAttribute;
39typedef Qt3DCore::QBuffer Qt3DQBuffer;
40typedef Qt3DCore::QGeometry Qt3DQGeometry;
41#endif
42
43#include <Qt3DRender/QGeometryRenderer>
44#include <Qt3DRender/QTechnique>
45#include <Qt3DRender/QGraphicsApiFilter>
46#include <Qt3DRender/QBlendEquation>
47#include <Qt3DRender/QColorMask>
48#include <Qt3DRender/QSortPolicy>
49#include <Qt3DRender/QPointSize>
50#include <Qt3DRender/QSeamlessCubemap>
51#include <Qt3DRender/QNoDepthMask>
52#include <Qt3DRender/QBlendEquationArguments>
53#include <Qt3DExtras/QTextureMaterial>
54#include <Qt3DRender/QAbstractTexture>
55#include "qgsfgutils.h"
56#include <Qt3DRender/QNoDraw>
57#include <Qt3DRender/QClipPlane>
58
59Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructForwardRenderPass()
60{
61 // This is where rendering of the 3D scene actually happens.
62 // We define two forward passes: one for solid objects, followed by one for transparent objects.
63 //
64 // |
65 // +-----------------+
66 // | QCameraSelector | (using the main camera)
67 // +-----------------+
68 // |
69 // +-----------------+
70 // | QLayerFilter | (using mForwardRenderLayer)
71 // +-----------------+
72 // |
73 // +-----------------+
74 // | QRenderStateSet | define clip planes
75 // +-----------------+
76 // |
77 // +-----------------------+
78 // | QRenderTargetSelector | (write mForwardColorTexture + mForwardDepthTexture)
79 // +-----------------------+
80 // |
81 // +------------------------+---------------------+
82 // | |
83 // +-----------------+ discard +-----------------+ accept
84 // | QLayerFilter | transparent | QLayerFilter | transparent
85 // +-----------------+ objects +-----------------+ objects
86 // | |
87 // +-----------------+ use depth test +-----------------+ sort entities
88 // | QRenderStateSet | cull back faces | QSortPolicy | back to front
89 // +-----------------+ +-----------------+
90 // | |
91 // +-----------------+ +--------------------+--------------------+
92 // | QFrustumCulling | | |
93 // +-----------------+ +-----------------+ use depth tests +-----------------+ use depth tests
94 // | | QRenderStateSet | don't write depths | QRenderStateSet | write depths
95 // | +-----------------+ write colors +-----------------+ don't write colors
96 // +-----------------+ use alpha blending don't use alpha blending
97 // | QClearBuffers | color and depth no culling no culling
98 // +-----------------+
99
100 mMainCameraSelector = new Qt3DRender::QCameraSelector;
101 mMainCameraSelector->setObjectName( "Forward render pass CameraSelector" );
102 mMainCameraSelector->setCamera( mMainCamera );
103
104 mForwardRenderLayerFilter = new Qt3DRender::QLayerFilter( mMainCameraSelector );
105 mForwardRenderLayerFilter->addLayer( mForwardRenderLayer );
106
107 mClipRenderStateSet = new Qt3DRender::QRenderStateSet( mForwardRenderLayerFilter );
108 mClipRenderStateSet->setObjectName( "Forward render pass Clip Plane RenderStateSet" );
109
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 );
119
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 );
129
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 );
139
140 mForwardRenderTargetSelector = new Qt3DRender::QRenderTargetSelector( mClipRenderStateSet );
141 mForwardRenderTargetSelector->setTarget( forwardRenderTarget );
142
143 // first branch: opaque layer filter
144 Qt3DRender::QLayerFilter *opaqueObjectsFilter = new Qt3DRender::QLayerFilter( mForwardRenderTargetSelector );
145 opaqueObjectsFilter->addLayer( mTransparentObjectsPassLayer );
146 opaqueObjectsFilter->setFilterMode( Qt3DRender::QLayerFilter::DiscardAnyMatchingLayers );
147
148 Qt3DRender::QRenderStateSet *forwardedRenderStateSet = new Qt3DRender::QRenderStateSet( opaqueObjectsFilter );
149
150 Qt3DRender::QDepthTest *depthTest = new Qt3DRender::QDepthTest;
151 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
152 forwardedRenderStateSet->addRenderState( depthTest );
153
154 Qt3DRender::QCullFace *cullFace = new Qt3DRender::QCullFace;
155 cullFace->setMode( Qt3DRender::QCullFace::CullingMode::Back );
156 forwardedRenderStateSet->addRenderState( cullFace );
157
158 mFrustumCulling = new Qt3DRender::QFrustumCulling( forwardedRenderStateSet );
159
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 );
164
165 // second branch: transparent layer filter - color
166 Qt3DRender::QLayerFilter *transparentObjectsLayerFilter = new Qt3DRender::QLayerFilter( mForwardRenderTargetSelector );
167 transparentObjectsLayerFilter->addLayer( mTransparentObjectsPassLayer );
168 transparentObjectsLayerFilter->setFilterMode( Qt3DRender::QLayerFilter::AcceptAnyMatchingLayers );
169
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 );
174
175 Qt3DRender::QRenderStateSet *transparentObjectsRenderStateSetColor = new Qt3DRender::QRenderStateSet( sortPolicy );
176 {
177 Qt3DRender::QDepthTest *depthTest = new Qt3DRender::QDepthTest;
178 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
179 transparentObjectsRenderStateSetColor->addRenderState( depthTest );
180
181 Qt3DRender::QNoDepthMask *noDepthMask = new Qt3DRender::QNoDepthMask;
182 transparentObjectsRenderStateSetColor->addRenderState( noDepthMask );
183
184 Qt3DRender::QCullFace *cullFace = new Qt3DRender::QCullFace;
185 cullFace->setMode( Qt3DRender::QCullFace::CullingMode::NoCulling );
186 transparentObjectsRenderStateSetColor->addRenderState( cullFace );
187
188 Qt3DRender::QBlendEquation *blendEquation = new Qt3DRender::QBlendEquation;
189 blendEquation->setBlendFunction( Qt3DRender::QBlendEquation::Add );
190 transparentObjectsRenderStateSetColor->addRenderState( blendEquation );
191
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 );
196 }
197
198 // third branch: transparent layer filter - depth
199 Qt3DRender::QRenderStateSet *transparentObjectsRenderStateSetDepth = new Qt3DRender::QRenderStateSet( sortPolicy );
200 {
201 Qt3DRender::QDepthTest *depthTest = new Qt3DRender::QDepthTest;
202 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
203 transparentObjectsRenderStateSetDepth->addRenderState( depthTest );
204
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 );
211
212 Qt3DRender::QCullFace *cullFace = new Qt3DRender::QCullFace;
213 cullFace->setMode( Qt3DRender::QCullFace::CullingMode::NoCulling );
214 transparentObjectsRenderStateSetDepth->addRenderState( cullFace );
215 }
216
217 mDebugOverlay = new Qt3DRender::QDebugOverlay( mForwardClearBuffers );
218 mDebugOverlay->setEnabled( false );
219
220 // cppcheck wrongly believes transparentObjectsRenderStateSetColor and transparentObjectsRenderStateSetDepth will leak
221 // cppcheck-suppress memleak
222 return mMainCameraSelector;
223}
224
225Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructShadowRenderPass()
226{
227 mLightCameraSelectorShadowPass = new Qt3DRender::QCameraSelector;
228 mLightCameraSelectorShadowPass->setObjectName( "Shadow render pass CameraSelector" );
229 mLightCameraSelectorShadowPass->setCamera( mLightCamera );
230
231 mShadowSceneEntitiesFilter = new Qt3DRender::QLayerFilter( mLightCameraSelectorShadowPass );
232 mShadowSceneEntitiesFilter->addLayer( mCastShadowsLayer );
233
234 mShadowMapTexture = new Qt3DRender::QTexture2D;
235 mShadowMapTexture->setWidth( mShadowMapResolution );
236 mShadowMapTexture->setHeight( mShadowMapResolution );
237 mShadowMapTexture->setFormat( Qt3DRender::QTexture2D::TextureFormat::DepthFormat );
238 mShadowMapTexture->setGenerateMipMaps( false );
239 mShadowMapTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
240 mShadowMapTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
241 mShadowMapTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
242 mShadowMapTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
243
244 Qt3DRender::QRenderTarget *shadowRenderTarget = new Qt3DRender::QRenderTarget;
245 Qt3DRender::QRenderTargetOutput *shadowRenderTargetOutput = new Qt3DRender::QRenderTargetOutput;
246 shadowRenderTargetOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
247 shadowRenderTargetOutput->setTexture( mShadowMapTexture );
248 shadowRenderTarget->addOutput( shadowRenderTargetOutput );
249
250 mShadowRenderTargetSelector = new Qt3DRender::QRenderTargetSelector( mShadowSceneEntitiesFilter );
251 mShadowRenderTargetSelector->setTarget( shadowRenderTarget );
252
253 mShadowClearBuffers = new Qt3DRender::QClearBuffers( mShadowRenderTargetSelector );
254 mShadowClearBuffers->setBuffers( Qt3DRender::QClearBuffers::BufferType::ColorDepthBuffer );
255 mShadowClearBuffers->setClearColor( QColor::fromRgbF( 0.0f, 1.0f, 0.0f ) );
256
257 mShadowRenderStateSet = new Qt3DRender::QRenderStateSet( mShadowClearBuffers );
258
259 Qt3DRender::QDepthTest *shadowDepthTest = new Qt3DRender::QDepthTest;
260 shadowDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
261 mShadowRenderStateSet->addRenderState( shadowDepthTest );
262
263 Qt3DRender::QCullFace *shadowCullFace = new Qt3DRender::QCullFace;
264 shadowCullFace->setMode( Qt3DRender::QCullFace::CullingMode::Front );
265 mShadowRenderStateSet->addRenderState( shadowCullFace );
266
267 Qt3DRender::QPolygonOffset *polygonOffset = new Qt3DRender::QPolygonOffset;
268 polygonOffset->setDepthSteps( 4.0 );
269 polygonOffset->setScaleFactor( 1.1 );
270 mShadowRenderStateSet->addRenderState( polygonOffset );
271
272 return mLightCameraSelectorShadowPass;
273}
274
275Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructSubPostPassForTexturesPreview()
276{
277 Qt3DRender::QLayerFilter *layerFilter = new Qt3DRender::QLayerFilter;
278 layerFilter->setObjectName( "Sub pass TexturesPreview" );
279 layerFilter->addLayer( mPreviewLayer );
280
281 Qt3DRender::QRenderStateSet *renderStateSet = new Qt3DRender::QRenderStateSet( layerFilter );
282 Qt3DRender::QDepthTest *depthTest = new Qt3DRender::QDepthTest;
283 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
284 renderStateSet->addRenderState( depthTest );
285 Qt3DRender::QCullFace *cullFace = new Qt3DRender::QCullFace;
286 cullFace->setMode( Qt3DRender::QCullFace::NoCulling );
287 renderStateSet->addRenderState( cullFace );
288
289 return layerFilter;
290}
291
292Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructSubPostPassForProcessing()
293{
294 Qt3DRender::QCameraSelector *cameraSelector = new Qt3DRender::QCameraSelector;
295 cameraSelector->setObjectName( "Sub pass Postprocessing" );
296 cameraSelector->setCamera( mLightCamera );
297
298 Qt3DRender::QLayerFilter *layerFilter = new Qt3DRender::QLayerFilter( cameraSelector );
299
300 // could be the first of this branch
301 new Qt3DRender::QClearBuffers( layerFilter );
302
303 Qt3DRender::QLayer *postProcessingLayer = new Qt3DRender::QLayer();
304 mPostprocessingEntity = new QgsPostprocessingEntity( this, postProcessingLayer, mRootEntity );
305 layerFilter->addLayer( postProcessingLayer );
306 mPostprocessingEntity->setObjectName( "PostProcessingPassEntity" );
307
308 return cameraSelector;
309}
310
311Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructSubPostPassForRenderCapture()
312{
313 Qt3DRender::QFrameGraphNode *top = new Qt3DRender::QNoDraw;
314 top->setObjectName( "Sub pass RenderCapture" );
315
316 mRenderCapture = new Qt3DRender::QRenderCapture( top );
317
318 return top;
319}
320
321Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructPostprocessingPass()
322{
323 mRenderCaptureTargetSelector = new Qt3DRender::QRenderTargetSelector;
324 mRenderCaptureTargetSelector->setObjectName( "Postprocessing render pass" );
325 mRenderCaptureTargetSelector->setEnabled( mRenderCaptureEnabled );
326
327 Qt3DRender::QRenderTarget *renderTarget = new Qt3DRender::QRenderTarget( mRenderCaptureTargetSelector );
328
329 // The lifetime of the objects created here is managed
330 // automatically, as they become children of this object.
331
332 // Create a render target output for rendering color.
333 Qt3DRender::QRenderTargetOutput *colorOutput = new Qt3DRender::QRenderTargetOutput( renderTarget );
334 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
335
336 // Create a texture to render into.
337 mRenderCaptureColorTexture = new Qt3DRender::QTexture2D( colorOutput );
338 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
339 mRenderCaptureColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
340 mRenderCaptureColorTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
341 mRenderCaptureColorTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
342 mRenderCaptureColorTexture->setObjectName( "PostProcessingPass::ColorTarget" );
343
344 // Hook the texture up to our output, and the output up to this object.
345 colorOutput->setTexture( mRenderCaptureColorTexture );
346 renderTarget->addOutput( colorOutput );
347
348 Qt3DRender::QRenderTargetOutput *depthOutput = new Qt3DRender::QRenderTargetOutput( renderTarget );
349
350 depthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
351 mRenderCaptureDepthTexture = new Qt3DRender::QTexture2D( depthOutput );
352 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
353 mRenderCaptureDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
354 mRenderCaptureDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
355 mRenderCaptureDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
356 mRenderCaptureDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
357 mRenderCaptureDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
358 mRenderCaptureDepthTexture->setObjectName( "PostProcessingPass::DepthTarget" );
359
360 depthOutput->setTexture( mRenderCaptureDepthTexture );
361 renderTarget->addOutput( depthOutput );
362
363 mRenderCaptureTargetSelector->setTarget( renderTarget );
364
365 // sub passes:
366 constructSubPostPassForProcessing()->setParent( mRenderCaptureTargetSelector );
367 constructSubPostPassForTexturesPreview()->setParent( mRenderCaptureTargetSelector );
368 constructSubPostPassForRenderCapture()->setParent( mRenderCaptureTargetSelector );
369
370 return mRenderCaptureTargetSelector;
371}
372
373Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructAmbientOcclusionRenderPass()
374{
375 mAmbientOcclusionRenderCameraSelector = new Qt3DRender::QCameraSelector;
376 mAmbientOcclusionRenderCameraSelector->setObjectName( "AmbientOcclusion render pass CameraSelector" );
377 mAmbientOcclusionRenderCameraSelector->setCamera( mMainCamera );
378
379 mAmbientOcclusionRenderStateSet = new Qt3DRender::QRenderStateSet( mAmbientOcclusionRenderCameraSelector );
380
381 Qt3DRender::QDepthTest *depthRenderDepthTest = new Qt3DRender::QDepthTest;
382 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
383 Qt3DRender::QCullFace *depthRenderCullFace = new Qt3DRender::QCullFace;
384 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
385
386 mAmbientOcclusionRenderStateSet->addRenderState( depthRenderDepthTest );
387 mAmbientOcclusionRenderStateSet->addRenderState( depthRenderCullFace );
388
389 mAmbientOcclusionRenderLayerFilter = new Qt3DRender::QLayerFilter( mAmbientOcclusionRenderStateSet );
390
391 mAmbientOcclusionRenderCaptureTargetSelector = new Qt3DRender::QRenderTargetSelector( mAmbientOcclusionRenderLayerFilter );
392 Qt3DRender::QRenderTarget *colorRenderTarget = new Qt3DRender::QRenderTarget( mAmbientOcclusionRenderCaptureTargetSelector );
393
394 // The lifetime of the objects created here is managed
395 // automatically, as they become children of this object.
396
397 // Create a render target output for rendering color.
398 Qt3DRender::QRenderTargetOutput *colorOutput = new Qt3DRender::QRenderTargetOutput( colorRenderTarget );
399 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
400
401 // Create a texture to render into.
402 mAmbientOcclusionRenderTexture = new Qt3DRender::QTexture2D( colorOutput );
403 mAmbientOcclusionRenderTexture->setSize( mSize.width(), mSize.height() );
404 mAmbientOcclusionRenderTexture->setFormat( Qt3DRender::QAbstractTexture::R32F );
405 mAmbientOcclusionRenderTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
406 mAmbientOcclusionRenderTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
407
408 // Hook the texture up to our output, and the output up to this object.
409 colorOutput->setTexture( mAmbientOcclusionRenderTexture );
410 colorRenderTarget->addOutput( colorOutput );
411
412 mAmbientOcclusionRenderCaptureTargetSelector->setTarget( colorRenderTarget );
413
414 Qt3DRender::QLayer *ambientOcclusionRenderLayer = new Qt3DRender::QLayer();
415 mAmbientOcclusionRenderEntity = new QgsAmbientOcclusionRenderEntity( mForwardDepthTexture, ambientOcclusionRenderLayer, mMainCamera, mRootEntity );
416 mAmbientOcclusionRenderLayerFilter->addLayer( ambientOcclusionRenderLayer );
417
418 return mAmbientOcclusionRenderCameraSelector;
419}
420
421Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructAmbientOcclusionBlurPass()
422{
423 mAmbientOcclusionBlurCameraSelector = new Qt3DRender::QCameraSelector;
424 mAmbientOcclusionBlurCameraSelector->setObjectName( "AmbientOcclusion blur pass CameraSelector" );
425 mAmbientOcclusionBlurCameraSelector->setCamera( mMainCamera );
426
427 mAmbientOcclusionBlurStateSet = new Qt3DRender::QRenderStateSet( mAmbientOcclusionBlurCameraSelector );
428
429 Qt3DRender::QDepthTest *depthRenderDepthTest = new Qt3DRender::QDepthTest;
430 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
431 Qt3DRender::QCullFace *depthRenderCullFace = new Qt3DRender::QCullFace;
432 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
433
434 mAmbientOcclusionBlurStateSet->addRenderState( depthRenderDepthTest );
435 mAmbientOcclusionBlurStateSet->addRenderState( depthRenderCullFace );
436
437 mAmbientOcclusionBlurLayerFilter = new Qt3DRender::QLayerFilter( mAmbientOcclusionBlurStateSet );
438
439 mAmbientOcclusionBlurRenderCaptureTargetSelector = new Qt3DRender::QRenderTargetSelector( mAmbientOcclusionBlurLayerFilter );
440 Qt3DRender::QRenderTarget *depthRenderTarget = new Qt3DRender::QRenderTarget( mAmbientOcclusionBlurRenderCaptureTargetSelector );
441
442 // The lifetime of the objects created here is managed
443 // automatically, as they become children of this object.
444
445 // Create a render target output for rendering color.
446 Qt3DRender::QRenderTargetOutput *colorOutput = new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
447 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
448
449 // Create a texture to render into.
450 mAmbientOcclusionBlurTexture = new Qt3DRender::QTexture2D( colorOutput );
451 mAmbientOcclusionBlurTexture->setSize( mSize.width(), mSize.height() );
452 mAmbientOcclusionBlurTexture->setFormat( Qt3DRender::QAbstractTexture::R32F );
453 mAmbientOcclusionBlurTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
454 mAmbientOcclusionBlurTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
455
456 // Hook the texture up to our output, and the output up to this object.
457 colorOutput->setTexture( mAmbientOcclusionBlurTexture );
458 depthRenderTarget->addOutput( colorOutput );
459
460 mAmbientOcclusionBlurRenderCaptureTargetSelector->setTarget( depthRenderTarget );
461
462 Qt3DRender::QLayer *ambientOcclusionBlurLayer = new Qt3DRender::QLayer();
463 mAmbientOcclusionBlurEntity = new QgsAmbientOcclusionBlurEntity( mAmbientOcclusionRenderTexture, ambientOcclusionBlurLayer, mRootEntity );
464 mAmbientOcclusionBlurLayerFilter->addLayer( ambientOcclusionBlurLayer );
465
466 return mAmbientOcclusionBlurCameraSelector;
467}
468
469
470Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructRubberBandsPass()
471{
472 mRubberBandsCameraSelector = new Qt3DRender::QCameraSelector;
473 mRubberBandsCameraSelector->setObjectName( "RubberBands Pass CameraSelector" );
474 mRubberBandsCameraSelector->setCamera( mMainCamera );
475
476 mRubberBandsLayerFilter = new Qt3DRender::QLayerFilter( mRubberBandsCameraSelector );
477 mRubberBandsLayerFilter->addLayer( mRubberBandsLayer );
478
479 mRubberBandsStateSet = new Qt3DRender::QRenderStateSet( mRubberBandsLayerFilter );
480 Qt3DRender::QDepthTest *depthTest = new Qt3DRender::QDepthTest;
481 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
482 mRubberBandsStateSet->addRenderState( depthTest );
483
484 // Here we attach our drawings to the render target also used by forward pass.
485 // This is kind of okay, but as a result, post-processing effects get applied
486 // to rubber bands too. Ideally we would want them on top of everything.
487 mRubberBandsRenderTargetSelector = new Qt3DRender::QRenderTargetSelector( mRubberBandsStateSet );
488 mRubberBandsRenderTargetSelector->setTarget( mForwardRenderTargetSelector->target() );
489
490 return mRubberBandsCameraSelector;
491}
492
493
494
495Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructDepthRenderPass()
496{
497 // depth buffer render to copy pass
498
499 mDepthRenderCameraSelector = new Qt3DRender::QCameraSelector;
500 mDepthRenderCameraSelector->setObjectName( "Depth render view CameraSelector" );
501 mDepthRenderCameraSelector->setCamera( mMainCamera );
502
503 mDepthRenderStateSet = new Qt3DRender::QRenderStateSet( mDepthRenderCameraSelector );
504
505 Qt3DRender::QDepthTest *depthRenderDepthTest = new Qt3DRender::QDepthTest;
506 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
507 Qt3DRender::QCullFace *depthRenderCullFace = new Qt3DRender::QCullFace;
508 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
509
510 mDepthRenderStateSet->addRenderState( depthRenderDepthTest );
511 mDepthRenderStateSet->addRenderState( depthRenderCullFace );
512
513 mDepthRenderLayerFilter = new Qt3DRender::QLayerFilter( mDepthRenderStateSet );
514 mDepthRenderLayerFilter->addLayer( mDepthRenderPassLayer );
515
516 mDepthRenderCaptureTargetSelector = new Qt3DRender::QRenderTargetSelector( mDepthRenderLayerFilter );
517 Qt3DRender::QRenderTarget *depthRenderTarget = new Qt3DRender::QRenderTarget( mDepthRenderCaptureTargetSelector );
518
519 // The lifetime of the objects created here is managed
520 // automatically, as they become children of this object.
521
522 // Create a render target output for rendering color.
523 Qt3DRender::QRenderTargetOutput *colorOutput = new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
524 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
525
526 // Create a texture to render into.
527 mDepthRenderCaptureColorTexture = new Qt3DRender::QTexture2D( colorOutput );
528 mDepthRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
529 mDepthRenderCaptureColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
530 mDepthRenderCaptureColorTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
531 mDepthRenderCaptureColorTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
532
533 // Hook the texture up to our output, and the output up to this object.
534 colorOutput->setTexture( mDepthRenderCaptureColorTexture );
535 depthRenderTarget->addOutput( colorOutput );
536
537 Qt3DRender::QRenderTargetOutput *depthOutput = new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
538
539 depthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
540 mDepthRenderCaptureDepthTexture = new Qt3DRender::QTexture2D( depthOutput );
541 mDepthRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
542 mDepthRenderCaptureDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
543 mDepthRenderCaptureDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
544 mDepthRenderCaptureDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
545 mDepthRenderCaptureDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
546 mDepthRenderCaptureDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
547
548 depthOutput->setTexture( mDepthRenderCaptureDepthTexture );
549 depthRenderTarget->addOutput( depthOutput );
550
551 mDepthRenderCaptureTargetSelector->setTarget( depthRenderTarget );
552
553 // Note: We do not a clear buffers node since we are drawing a quad that will override the buffer's content anyway
554 mDepthRenderCapture = new Qt3DRender::QRenderCapture( mDepthRenderCaptureTargetSelector );
555
556 return mDepthRenderCameraSelector;
557}
558
559Qt3DCore::QEntity *QgsFrameGraph::constructDepthRenderQuad()
560{
561 Qt3DCore::QEntity *quad = new Qt3DCore::QEntity;
562 quad->setObjectName( "depthRenderQuad" );
563
564 Qt3DQGeometry *geom = new Qt3DQGeometry;
565 Qt3DQAttribute *positionAttribute = new Qt3DQAttribute;
566 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 };
567
568 const QByteArray vertexArr( ( const char * ) vert.constData(), vert.size() * sizeof( float ) );
569 Qt3DQBuffer *vertexBuffer = nullptr;
570 vertexBuffer = new Qt3DQBuffer( this );
571 vertexBuffer->setData( vertexArr );
572
573 positionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
574 positionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
575 positionAttribute->setVertexSize( 3 );
576 positionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
577 positionAttribute->setBuffer( vertexBuffer );
578 positionAttribute->setByteOffset( 0 );
579 positionAttribute->setByteStride( 3 * sizeof( float ) );
580 positionAttribute->setCount( 6 );
581
582 geom->addAttribute( positionAttribute );
583
584 Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
585 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
586 renderer->setGeometry( geom );
587
588 quad->addComponent( renderer );
589
590 QMatrix4x4 modelMatrix;
591 modelMatrix.setToIdentity();
592
593 // construct material
594
595 Qt3DRender::QMaterial *material = new Qt3DRender::QMaterial;
596 Qt3DRender::QParameter *textureParameter = new Qt3DRender::QParameter( "depthTexture", mForwardDepthTexture );
597 Qt3DRender::QParameter *textureTransformParameter = new Qt3DRender::QParameter( "modelMatrix", QVariant::fromValue( modelMatrix ) );
598 material->addParameter( textureParameter );
599 material->addParameter( textureTransformParameter );
600
601 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect;
602
603 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
604
605 Qt3DRender::QGraphicsApiFilter *graphicsApiFilter = technique->graphicsApiFilter();
606 graphicsApiFilter->setApi( Qt3DRender::QGraphicsApiFilter::Api::OpenGL );
607 graphicsApiFilter->setProfile( Qt3DRender::QGraphicsApiFilter::OpenGLProfile::CoreProfile );
608 graphicsApiFilter->setMajorVersion( 1 );
609 graphicsApiFilter->setMinorVersion( 5 );
610
611 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass;
612
613 Qt3DRender::QShaderProgram *shader = new Qt3DRender::QShaderProgram;
614 shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( "qrc:/shaders/depth_render.vert" ) ) );
615 shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( "qrc:/shaders/depth_render.frag" ) ) );
616 renderPass->setShaderProgram( shader );
617
618 technique->addRenderPass( renderPass );
619
620 effect->addTechnique( technique );
621 material->setEffect( effect );
622
623 quad->addComponent( material );
624
625 return quad;
626}
627
628QgsFrameGraph::QgsFrameGraph( QSurface *surface, QSize s, Qt3DRender::QCamera *mainCamera, Qt3DCore::QEntity *root )
629 : Qt3DCore::QEntity( root )
630 , mSize( s )
631{
632
633 // general overview of how the frame graph looks:
634 //
635 // +------------------------+ using window or
636 // | QRenderSurfaceSelector | offscreen surface
637 // +------------------------+
638 // |
639 // +-----------+
640 // | QViewport | (0,0,1,1)
641 // +-----------+
642 // |
643 // +--------------------------+-------------------+-----------------+
644 // | | | |
645 // +--------------------+ +--------------+ +-----------------+ +-----------------+
646 // | two forward passes | | shadows pass | | depth buffer | | post-processing |
647 // | (solid objects | | | | processing pass | | passes |
648 // | and transparent) | +--------------+ +-----------------+ +-----------------+
649 // +--------------------+
650 //
651 // Notes:
652 // - depth buffer processing pass is used whenever we need depth map information
653 // (for camera navigation) and it converts depth texture to a color texture
654 // so that we can capture it with QRenderCapture - currently it is unable
655 // to capture depth buffer, only colors (see QTBUG-65155)
656 // - there are multiple post-processing passes that take rendered output
657 // of the scene, optionally apply effects (add shadows, ambient occlusion,
658 // eye dome lighting) and finally output to the given surface
659 // - there may be also two more passes when 3D axis is shown - see Qgs3DAxis
660
661 mRootEntity = root;
662 mMainCamera = mainCamera;
663 mLightCamera = new Qt3DRender::QCamera;
664
665 mPreviewLayer = new Qt3DRender::QLayer;
666 mCastShadowsLayer = new Qt3DRender::QLayer;
667 mForwardRenderLayer = new Qt3DRender::QLayer;
668 mDepthRenderPassLayer = new Qt3DRender::QLayer;
669 mTransparentObjectsPassLayer = new Qt3DRender::QLayer;
670 mRubberBandsLayer = new Qt3DRender::QLayer;
671
672 mRubberBandsLayer->setObjectName( "mRubberBandsLayer" );
673
674 mPreviewLayer->setRecursive( true );
675 mCastShadowsLayer->setRecursive( true );
676 mForwardRenderLayer->setRecursive( true );
677 mDepthRenderPassLayer->setRecursive( true );
678 mTransparentObjectsPassLayer->setRecursive( true );
679 mRubberBandsLayer->setRecursive( true );
680
681 mRenderSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector;
682
683 QObject *surfaceObj = dynamic_cast< QObject * >( surface );
684 Q_ASSERT( surfaceObj );
685
686 mRenderSurfaceSelector->setSurface( surfaceObj );
687 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
688
689 mMainViewPort = new Qt3DRender::QViewport( mRenderSurfaceSelector );
690 mMainViewPort->setNormalizedRect( QRectF( 0.0f, 0.0f, 1.0f, 1.0f ) );
691
692 // Forward render
693 Qt3DRender::QFrameGraphNode *forwardRenderPass = constructForwardRenderPass();
694 forwardRenderPass->setParent( mMainViewPort );
695
696 // rubber bands (they should be always on top)
697 Qt3DRender::QFrameGraphNode *rubberBandsPass = constructRubberBandsPass();
698 rubberBandsPass->setObjectName( "rubberBandsPass" );
699 rubberBandsPass->setParent( mMainViewPort );
700
701 // shadow rendering pass
702 Qt3DRender::QFrameGraphNode *shadowRenderPass = constructShadowRenderPass();
703 shadowRenderPass->setParent( mMainViewPort );
704
705 // depth buffer processing
706 Qt3DRender::QFrameGraphNode *depthBufferProcessingPass = constructDepthRenderPass();
707 depthBufferProcessingPass->setParent( mMainViewPort );
708
709 // Ambient occlusion factor render pass
710 Qt3DRender::QFrameGraphNode *ambientOcclusionFactorRender = constructAmbientOcclusionRenderPass();
711 ambientOcclusionFactorRender->setParent( mMainViewPort );
712
713 Qt3DRender::QFrameGraphNode *ambientOcclusionBlurPass = constructAmbientOcclusionBlurPass();
714 ambientOcclusionBlurPass->setParent( mMainViewPort );
715
716 // post process
717 Qt3DRender::QFrameGraphNode *postprocessingPass = constructPostprocessingPass();
718 postprocessingPass->setParent( mMainViewPort );
719 postprocessingPass->setObjectName( "PostProcessingPass" );
720
721 mRubberBandsRootEntity = new Qt3DCore::QEntity( mRootEntity );
722 mRubberBandsRootEntity->setObjectName( "mRubberBandsRootEntity" );
723 mRubberBandsRootEntity->addComponent( mRubberBandsLayer );
724
725 Qt3DRender::QParameter *depthMapIsDepthParam = new Qt3DRender::QParameter( "isDepth", true );
726 Qt3DRender::QParameter *shadowMapIsDepthParam = new Qt3DRender::QParameter( "isDepth", true );
727
728 mDebugDepthMapPreviewQuad = this->addTexturePreviewOverlay( mForwardDepthTexture, QPointF( 0.9f, 0.9f ), QSizeF( 0.1, 0.1 ), QVector<Qt3DRender::QParameter *> { depthMapIsDepthParam } );
729 mDebugShadowMapPreviewQuad = this->addTexturePreviewOverlay( mShadowMapTexture, QPointF( 0.9f, 0.9f ), QSizeF( 0.1, 0.1 ), QVector<Qt3DRender::QParameter *> { shadowMapIsDepthParam } );
730 mDebugDepthMapPreviewQuad->setEnabled( false );
731 mDebugShadowMapPreviewQuad->setEnabled( false );
732
733 mDepthRenderQuad = constructDepthRenderQuad();
734 mDepthRenderQuad->addComponent( mDepthRenderPassLayer );
735 mDepthRenderQuad->setParent( mRootEntity );
736}
737
738QgsPreviewQuad *QgsFrameGraph::addTexturePreviewOverlay( Qt3DRender::QTexture2D *texture, const QPointF &centerTexCoords, const QSizeF &sizeTexCoords, QVector<Qt3DRender::QParameter *> additionalShaderParameters )
739{
740 QgsPreviewQuad *previewQuad = new QgsPreviewQuad( texture, centerTexCoords, sizeTexCoords, additionalShaderParameters );
741 previewQuad->addComponent( mPreviewLayer );
742 previewQuad->setParent( mRootEntity );
743 mPreviewQuads.push_back( previewQuad );
744 return previewQuad;
745}
746
747// computes the portion of the Z=z plane the camera is looking at
748void calculateViewExtent( Qt3DRender::QCamera *camera, float shadowRenderingDistance, float z, float &minX, float &maxX, float &minY, float &maxY, float &minZ, float &maxZ )
749{
750 const QVector3D cameraPos = camera->position();
751 const QMatrix4x4 projectionMatrix = camera->projectionMatrix();
752 const QMatrix4x4 viewMatrix = camera->viewMatrix();
753 float depth = 1.0f;
754 QVector4D viewCenter = viewMatrix * QVector4D( camera->viewCenter(), 1.0f );
755 viewCenter /= viewCenter.w();
756 viewCenter = projectionMatrix * viewCenter;
757 viewCenter /= viewCenter.w();
758 depth = viewCenter.z();
759 QVector<QVector3D> viewFrustumPoints =
760 {
761 QVector3D( 0.0f, 0.0f, depth ),
762 QVector3D( 0.0f, 1.0f, depth ),
763 QVector3D( 1.0f, 0.0f, depth ),
764 QVector3D( 1.0f, 1.0f, depth ),
765 QVector3D( 0.0f, 0.0f, 0 ),
766 QVector3D( 0.0f, 1.0f, 0 ),
767 QVector3D( 1.0f, 0.0f, 0 ),
768 QVector3D( 1.0f, 1.0f, 0 )
769 };
770 maxX = std::numeric_limits<float>::lowest();
771 maxY = std::numeric_limits<float>::lowest();
772 maxZ = std::numeric_limits<float>::lowest();
773 minX = std::numeric_limits<float>::max();
774 minY = std::numeric_limits<float>::max();
775 minZ = std::numeric_limits<float>::max();
776 for ( int i = 0; i < viewFrustumPoints.size(); ++i )
777 {
778 // convert from view port space to world space
779 viewFrustumPoints[i] = viewFrustumPoints[i].unproject( viewMatrix, projectionMatrix, QRect( 0, 0, 1, 1 ) );
780 minX = std::min( minX, viewFrustumPoints[i].x() );
781 maxX = std::max( maxX, viewFrustumPoints[i].x() );
782 minY = std::min( minY, viewFrustumPoints[i].y() );
783 maxY = std::max( maxY, viewFrustumPoints[i].y() );
784 minZ = std::min( minZ, viewFrustumPoints[i].z() );
785 maxZ = std::max( maxZ, viewFrustumPoints[i].z() );
786 // find the intersection between the line going from cameraPos to the frustum quad point
787 // and the horizontal plane Z=z
788 // if the intersection is on the back side of the viewing panel we get a point that is
789 // shadowRenderingDistance units in front of the camera
790 const QVector3D pt = cameraPos;
791 const QVector3D vect = ( viewFrustumPoints[i] - pt ).normalized();
792 float t = ( z - pt.z() ) / vect.z();
793 if ( t < 0 )
794 t = shadowRenderingDistance;
795 else
796 t = std::min( t, shadowRenderingDistance );
797 viewFrustumPoints[i] = pt + t * vect;
798 minX = std::min( minX, viewFrustumPoints[i].x() );
799 maxX = std::max( maxX, viewFrustumPoints[i].x() );
800 minY = std::min( minY, viewFrustumPoints[i].y() );
801 maxY = std::max( maxY, viewFrustumPoints[i].y() );
802 minZ = std::min( minZ, viewFrustumPoints[i].z() );
803 maxZ = std::max( maxZ, viewFrustumPoints[i].z() );
804 }
805}
806
807void QgsFrameGraph::setupDirectionalLight( const QgsDirectionalLightSettings &light, float maximumShadowRenderingDistance )
808{
809 float minX, maxX, minY, maxY, minZ, maxZ;
810 QVector3D lookingAt = mMainCamera->viewCenter();
811 const float d = 2 * ( mMainCamera->position() - mMainCamera->viewCenter() ).length();
812
813 const QVector3D lightDirection = QVector3D( light.direction().x(), light.direction().y(), light.direction().z() ).normalized();
814 calculateViewExtent( mMainCamera, maximumShadowRenderingDistance, lookingAt.z(), minX, maxX, minY, maxY, minZ, maxZ );
815
816 lookingAt = QVector3D( 0.5f * ( minX + maxX ), 0.5f * ( minY + maxY ), mMainCamera->viewCenter().z() );
817 const QVector3D lightPosition = lookingAt + QVector3D( 0.0f, 0.0f, d );
818 mLightCamera->setPosition( lightPosition );
819 mLightCamera->setViewCenter( lookingAt );
820 mLightCamera->setUpVector( QVector3D( 0.0f, 1.0f, 0.0f ) );
821 mLightCamera->rotateAboutViewCenter( QQuaternion::rotationTo( QVector3D( 0.0f, 0.0f, -1.0f ), lightDirection ) );
822
823 mLightCamera->setProjectionType( Qt3DRender::QCameraLens::ProjectionType::OrthographicProjection );
824 mLightCamera->lens()->setOrthographicProjection(
825 - 0.7f * ( maxX - minX ), 0.7f * ( maxX - minX ),
826 - 0.7f * ( maxY - minY ), 0.7f * ( maxY - minY ),
827 1.0f, 2 * ( lookingAt - lightPosition ).length() );
828
829 mPostprocessingEntity->setupShadowRenderingExtent( minX, maxX, minY, maxY );
830 mPostprocessingEntity->setupDirectionalLight( lightPosition, lightDirection );
831}
832
834{
835 QObject *top = mRenderSurfaceSelector;
836 while ( top->parent() && dynamic_cast<Qt3DRender::QFrameGraphNode *>( top->parent() ) )
837 top = top->parent();
838
840 context.lowestId = mMainCamera->id().id();
841 QStringList strList = QgsFgUtils::dumpFrameGraph( dynamic_cast<Qt3DRender::QFrameGraphNode *>( top ), context );
842
843 return qPrintable( strList.join( "\n" ) ) + QString( "\n" );
844}
845
847{
848 QStringList strList = QgsFgUtils::dumpSceneGraph( mRootEntity, QgsFgUtils::FgDumpContext() );
849 return qPrintable( strList.join( "\n" ) ) + QString( "\n" );
850}
851
852void QgsFrameGraph::setClearColor( const QColor &clearColor )
853{
854 mForwardClearBuffers->setClearColor( clearColor );
855}
856
858{
859 mShadowRenderingEnabled = enabled;
860 mPostprocessingEntity->setShadowRenderingEnabled( mShadowRenderingEnabled );
861 if ( mShadowRenderingEnabled )
862 mShadowSceneEntitiesFilter->setEnabled( true );
863 else
864 mShadowSceneEntitiesFilter->setEnabled( false );
865}
866
867void QgsFrameGraph::setShadowBias( float shadowBias )
868{
869 mShadowBias = shadowBias;
870 mPostprocessingEntity->setShadowBias( mShadowBias );
871}
872
874{
875 mShadowMapResolution = resolution;
876 mShadowMapTexture->setWidth( mShadowMapResolution );
877 mShadowMapTexture->setHeight( mShadowMapResolution );
878}
879
881{
882 mAmbientOcclusionEnabled = enabled;
883 mAmbientOcclusionRenderEntity->setEnabled( enabled );
884 mPostprocessingEntity->setAmbientOcclusionEnabled( enabled );
885}
886
888{
889 mAmbientOcclusionIntensity = intensity;
890 mAmbientOcclusionRenderEntity->setIntensity( intensity );
891}
892
894{
895 mAmbientOcclusionRadius = radius;
896 mAmbientOcclusionRenderEntity->setRadius( radius );
897}
898
900{
901 mAmbientOcclusionThreshold = threshold;
902 mAmbientOcclusionRenderEntity->setThreshold( threshold );
903}
904
906{
907 if ( enabled == mFrustumCullingEnabled )
908 return;
909 mFrustumCullingEnabled = enabled;
910 if ( mFrustumCullingEnabled )
911 mFrustumCulling->setParent( mForwardClearBuffers );
912 else
913 mFrustumCulling->setParent( ( Qt3DCore::QNode * )nullptr );
914}
915
916void QgsFrameGraph::setupEyeDomeLighting( bool enabled, double strength, int distance )
917{
918 mEyeDomeLightingEnabled = enabled;
919 mEyeDomeLightingStrength = strength;
920 mEyeDomeLightingDistance = distance;
921 mPostprocessingEntity->setEyeDomeLightingEnabled( enabled );
922 mPostprocessingEntity->setEyeDomeLightingStrength( strength );
923 mPostprocessingEntity->setEyeDomeLightingDistance( distance );
924}
925
926void QgsFrameGraph::setupShadowMapDebugging( bool enabled, Qt::Corner corner, double size )
927{
928 mDebugShadowMapPreviewQuad->setEnabled( enabled );
929 if ( enabled )
930 {
931 switch ( corner )
932 {
933 case Qt::Corner::TopRightCorner:
934 mDebugShadowMapPreviewQuad->setViewPort( QPointF( 1.0f - size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
935 break;
936 case Qt::Corner::TopLeftCorner:
937 mDebugShadowMapPreviewQuad->setViewPort( QPointF( 0.0f + size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
938 break;
939 case Qt::Corner::BottomRightCorner:
940 mDebugShadowMapPreviewQuad->setViewPort( QPointF( 1.0f - size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
941 break;
942 case Qt::Corner::BottomLeftCorner:
943 mDebugShadowMapPreviewQuad->setViewPort( QPointF( 0.0f + size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
944 break;
945 }
946 }
947}
948
949void QgsFrameGraph::setupDepthMapDebugging( bool enabled, Qt::Corner corner, double size )
950{
951 mDebugDepthMapPreviewQuad->setEnabled( enabled );
952
953 if ( enabled )
954 {
955 switch ( corner )
956 {
957 case Qt::Corner::TopRightCorner:
958 mDebugDepthMapPreviewQuad->setViewPort( QPointF( 1.0f - size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
959 break;
960 case Qt::Corner::TopLeftCorner:
961 mDebugDepthMapPreviewQuad->setViewPort( QPointF( 0.0f + size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
962 break;
963 case Qt::Corner::BottomRightCorner:
964 mDebugDepthMapPreviewQuad->setViewPort( QPointF( 1.0f - size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
965 break;
966 case Qt::Corner::BottomLeftCorner:
967 mDebugDepthMapPreviewQuad->setViewPort( QPointF( 0.0f + size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
968 break;
969 }
970 }
971}
972
974{
975 mSize = s;
976 mForwardColorTexture->setSize( mSize.width(), mSize.height() );
977 mForwardDepthTexture->setSize( mSize.width(), mSize.height() );
978 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
979 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
980 mDepthRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
981 mDepthRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
982 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
983
984 mAmbientOcclusionRenderTexture->setSize( mSize.width(), mSize.height() );
985 mAmbientOcclusionBlurTexture->setSize( mSize.width(), mSize.height() );
986}
987
989{
990 if ( enabled == mRenderCaptureEnabled )
991 return;
992 mRenderCaptureEnabled = enabled;
993 mRenderCaptureTargetSelector->setEnabled( mRenderCaptureEnabled );
994}
995
997{
998 mDebugOverlay->setEnabled( enabled );
999}
1000
1002{
1003 for ( Qt3DRender::QRenderState *state : mClipRenderStateSet->renderStates() )
1004 {
1005 if ( qobject_cast<Qt3DRender::QClipPlane *>( state ) )
1006 {
1007 mClipRenderStateSet->removeRenderState( state );
1008 }
1009 }
1010}
1011
1012void QgsFrameGraph::addClipPlanes( int nrClipPlanes )
1013{
1014 // remove existing QClipPlane
1016
1017 // create new QClipPlane
1018 for ( int i = 0; i < nrClipPlanes; ++i )
1019 {
1020 Qt3DRender::QClipPlane *clipPlane = new Qt3DRender::QClipPlane;
1021 clipPlane->setPlaneIndex( i );
1022 mClipRenderStateSet->addRenderState( clipPlane );
1023 }
1024
1025}
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.
static QStringList dumpFrameGraph(const Qt3DCore::QNode *node, FgDumpContext context)
Returns a tree view of the frame graph starting from node. The object ids will be given relatively to...
static QStringList dumpSceneGraph(const Qt3DCore::QNode *node, FgDumpContext context)
Returns a tree view of the scene graph starting from node. The object ids will be given relatively to...
void addClipPlanes(int nrClipPlanes)
Setups nrClipPlanes clip planes in the forward pass to enable OpenGL clipping.
QString dumpFrameGraph() const
Dumps frame graph as string.
void setRenderCaptureEnabled(bool enabled)
Sets whether it will be possible to render to an image.
void removeClipPlanes()
Disables OpenGL clipping.
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 &centerNDC, 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.
QString dumpSceneGraph() const
Dumps scene graph as string.
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 minY, float maxY)
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 &centerNDC, const QSizeF &size)
Sets where the quad will be located on the scene.
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:50
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:52
double x() const
Returns X coordinate.
Definition qgsvector3d.h:48
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry
Qt3DCore::QAttribute Qt3DQAttribute
void calculateViewExtent(Qt3DRender::QCamera *camera, float shadowRenderingDistance, float z, float &minX, float &maxX, float &minY, float &maxY, float &minZ, float &maxZ)
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry