25 #include <Qt3DRender/QAttribute>
26 #include <Qt3DRender/QBuffer>
27 #include <Qt3DRender/QTechnique>
28 #include <Qt3DRender/QGraphicsApiFilter>
31 Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructTexturesPreviewPass()
33 mPreviewLayerFilter =
new Qt3DRender::QLayerFilter;
34 mPreviewLayerFilter->addLayer( mPreviewLayer );
36 mPreviewRenderStateSet =
new Qt3DRender::QRenderStateSet( mPreviewLayerFilter );
37 mPreviewDepthTest =
new Qt3DRender::QDepthTest;
38 mPreviewDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
39 mPreviewRenderStateSet->addRenderState( mPreviewDepthTest );
40 mPreviewCullFace =
new Qt3DRender::QCullFace;
41 mPreviewCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
42 mPreviewRenderStateSet->addRenderState( mPreviewCullFace );
44 return mPreviewLayerFilter;
47 Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructForwardRenderPass()
49 mMainCameraSelector =
new Qt3DRender::QCameraSelector;
50 mMainCameraSelector->setCamera( mMainCamera );
52 mForwardRenderLayerFilter =
new Qt3DRender::QLayerFilter( mMainCameraSelector );
53 mForwardRenderLayerFilter->addLayer( mForwardRenderLayer );
55 mForwardColorTexture =
new Qt3DRender::QTexture2D;
56 mForwardColorTexture->setWidth( mSize.width() );
57 mForwardColorTexture->setHeight( mSize.height() );
58 mForwardColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
59 mForwardColorTexture->setGenerateMipMaps(
false );
60 mForwardColorTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
61 mForwardColorTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
62 mForwardColorTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
63 mForwardColorTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
65 mForwardDepthTexture =
new Qt3DRender::QTexture2D;
66 mForwardDepthTexture->setWidth( mSize.width() );
67 mForwardDepthTexture->setHeight( mSize.height() );
68 mForwardDepthTexture->setFormat( Qt3DRender::QTexture2D::TextureFormat::DepthFormat );
69 mForwardDepthTexture->setGenerateMipMaps(
false );
70 mForwardDepthTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
71 mForwardDepthTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
72 mForwardDepthTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
73 mForwardDepthTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
75 Qt3DRender::QRenderTarget *forwardRenderTarget =
new Qt3DRender::QRenderTarget;
76 Qt3DRender::QRenderTargetOutput *forwardRenderTargetDepthOutput =
new Qt3DRender::QRenderTargetOutput;
77 forwardRenderTargetDepthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
78 forwardRenderTargetDepthOutput->setTexture( mForwardDepthTexture );
79 forwardRenderTarget->addOutput( forwardRenderTargetDepthOutput );
80 Qt3DRender::QRenderTargetOutput *forwardRenderTargetColorOutput =
new Qt3DRender::QRenderTargetOutput;
81 forwardRenderTargetColorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
82 forwardRenderTargetColorOutput->setTexture( mForwardColorTexture );
83 forwardRenderTarget->addOutput( forwardRenderTargetColorOutput );
85 mForwardRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mForwardRenderLayerFilter );
86 mForwardRenderTargetSelector->setTarget( forwardRenderTarget );
88 mForwardClearBuffers =
new Qt3DRender::QClearBuffers( mForwardRenderTargetSelector );
89 mForwardClearBuffers->setClearColor( QColor::fromRgbF( 0.0, 0.0, 1.0, 1.0 ) );
90 mForwardClearBuffers->setBuffers( Qt3DRender::QClearBuffers::ColorDepthBuffer );
91 mForwardClearBuffers->setClearDepthValue( 1.0f );
93 Qt3DRender::QRenderStateSet *forwaredRenderStateSet =
new Qt3DRender::QRenderStateSet( mForwardClearBuffers );
95 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest;
96 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
97 forwaredRenderStateSet->addRenderState( depthTest );
99 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
100 cullFace->setMode( Qt3DRender::QCullFace::CullingMode::Back );
101 forwaredRenderStateSet->addRenderState( cullFace );
103 mFrustumCulling =
new Qt3DRender::QFrustumCulling( forwaredRenderStateSet );
105 return mMainCameraSelector;
108 Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructShadowRenderPass()
110 mLightCameraSelectorShadowPass =
new Qt3DRender::QCameraSelector;
111 mLightCameraSelectorShadowPass->setCamera( mLightCamera );
113 mShadowSceneEntitiesFilter =
new Qt3DRender::QLayerFilter( mLightCameraSelectorShadowPass );
114 mShadowSceneEntitiesFilter->addLayer( mCastShadowsLayer );
116 mShadowMapTexture =
new Qt3DRender::QTexture2D;
117 mShadowMapTexture->setWidth( mShadowMapResolution );
118 mShadowMapTexture->setHeight( mShadowMapResolution );
119 mShadowMapTexture->setFormat( Qt3DRender::QTexture2D::TextureFormat::DepthFormat );
120 mShadowMapTexture->setGenerateMipMaps(
false );
121 mShadowMapTexture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
122 mShadowMapTexture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
123 mShadowMapTexture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::ClampToEdge );
124 mShadowMapTexture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::ClampToEdge );
126 Qt3DRender::QRenderTarget *shadowRenderTarget =
new Qt3DRender::QRenderTarget;
127 Qt3DRender::QRenderTargetOutput *shadowRenderTargetOutput =
new Qt3DRender::QRenderTargetOutput;
128 shadowRenderTargetOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
129 shadowRenderTargetOutput->setTexture( mShadowMapTexture );
130 shadowRenderTarget->addOutput( shadowRenderTargetOutput );
132 mShadowRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mShadowSceneEntitiesFilter );
133 mShadowRenderTargetSelector->setTarget( shadowRenderTarget );
135 mShadowClearBuffers =
new Qt3DRender::QClearBuffers( mShadowRenderTargetSelector );
136 mShadowClearBuffers->setBuffers( Qt3DRender::QClearBuffers::BufferType::ColorDepthBuffer );
137 mShadowClearBuffers->setClearColor( QColor::fromRgbF( 0.0f, 1.0f, 0.0f ) );
139 mShadowRenderStateSet =
new Qt3DRender::QRenderStateSet( mShadowClearBuffers );
141 Qt3DRender::QDepthTest *shadowDepthTest =
new Qt3DRender::QDepthTest;
142 shadowDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Less );
143 mShadowRenderStateSet->addRenderState( shadowDepthTest );
145 Qt3DRender::QCullFace *shadowCullFace =
new Qt3DRender::QCullFace;
146 shadowCullFace->setMode( Qt3DRender::QCullFace::CullingMode::Front );
147 mShadowRenderStateSet->addRenderState( shadowCullFace );
149 Qt3DRender::QPolygonOffset *polygonOffset =
new Qt3DRender::QPolygonOffset;
150 polygonOffset->setDepthSteps( 4.0 );
151 polygonOffset->setScaleFactor( 1.1 );
152 mShadowRenderStateSet->addRenderState( polygonOffset );
154 return mLightCameraSelectorShadowPass;
157 Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructPostprocessingPass()
159 mPostProcessingCameraSelector =
new Qt3DRender::QCameraSelector;
160 mPostProcessingCameraSelector->setCamera( mLightCamera );
162 mPostprocessPassLayerFilter =
new Qt3DRender::QLayerFilter( mPostProcessingCameraSelector );
163 mPostprocessPassLayerFilter->addLayer( mPostprocessPassLayer );
165 mPostprocessClearBuffers =
new Qt3DRender::QClearBuffers( mPostprocessPassLayerFilter );
167 mRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mPostprocessClearBuffers );
169 Qt3DRender::QRenderTarget *renderTarget =
new Qt3DRender::QRenderTarget( mRenderCaptureTargetSelector );
175 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( renderTarget );
176 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
179 mRenderCaptureColorTexture =
new Qt3DRender::QTexture2D( colorOutput );
180 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
181 mRenderCaptureColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
182 mRenderCaptureColorTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
183 mRenderCaptureColorTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
186 colorOutput->setTexture( mRenderCaptureColorTexture );
187 renderTarget->addOutput( colorOutput );
189 Qt3DRender::QRenderTargetOutput *depthOutput =
new Qt3DRender::QRenderTargetOutput( renderTarget );
191 depthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
192 mRenderCaptureDepthTexture =
new Qt3DRender::QTexture2D( depthOutput );
193 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
194 mRenderCaptureDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
195 mRenderCaptureDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
196 mRenderCaptureDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
197 mRenderCaptureDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
198 mRenderCaptureDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
200 depthOutput->setTexture( mRenderCaptureDepthTexture );
201 renderTarget->addOutput( depthOutput );
203 mRenderCaptureTargetSelector->setTarget( renderTarget );
205 mRenderCapture =
new Qt3DRender::QRenderCapture( mRenderCaptureTargetSelector );
207 return mPostProcessingCameraSelector;
210 Qt3DRender::QFrameGraphNode *QgsShadowRenderingFrameGraph::constructDepthRenderPass()
214 mDepthRenderCameraSelector =
new Qt3DRender::QCameraSelector;
215 mDepthRenderCameraSelector->setCamera( mMainCamera );
217 mDepthRenderStateSet =
new Qt3DRender::QRenderStateSet( mDepthRenderCameraSelector );
219 Qt3DRender::QDepthTest *depthRenderDepthTest =
new Qt3DRender::QDepthTest;
220 depthRenderDepthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );;
221 Qt3DRender::QCullFace *depthRenderCullFace =
new Qt3DRender::QCullFace;
222 depthRenderCullFace->setMode( Qt3DRender::QCullFace::NoCulling );
224 mDepthRenderStateSet->addRenderState( depthRenderDepthTest );
225 mDepthRenderStateSet->addRenderState( depthRenderCullFace );
227 mDepthRenderLayerFilter =
new Qt3DRender::QLayerFilter( mDepthRenderStateSet );
228 mDepthRenderLayerFilter->addLayer( mDepthRenderPassLayer );
230 mDepthRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector( mDepthRenderLayerFilter );
231 Qt3DRender::QRenderTarget *depthRenderTarget =
new Qt3DRender::QRenderTarget( mDepthRenderCaptureTargetSelector );
237 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
238 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
241 mDepthRenderCaptureColorTexture =
new Qt3DRender::QTexture2D( colorOutput );
242 mDepthRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
243 mDepthRenderCaptureColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
244 mDepthRenderCaptureColorTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
245 mDepthRenderCaptureColorTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
248 colorOutput->setTexture( mDepthRenderCaptureColorTexture );
249 depthRenderTarget->addOutput( colorOutput );
251 Qt3DRender::QRenderTargetOutput *depthOutput =
new Qt3DRender::QRenderTargetOutput( depthRenderTarget );
253 depthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
254 mDepthRenderCaptureDepthTexture =
new Qt3DRender::QTexture2D( depthOutput );
255 mDepthRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
256 mDepthRenderCaptureDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
257 mDepthRenderCaptureDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
258 mDepthRenderCaptureDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
259 mDepthRenderCaptureDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
260 mDepthRenderCaptureDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
262 depthOutput->setTexture( mDepthRenderCaptureDepthTexture );
263 depthRenderTarget->addOutput( depthOutput );
265 mDepthRenderCaptureTargetSelector->setTarget( depthRenderTarget );
268 mDepthRenderCapture =
new Qt3DRender::QRenderCapture( mDepthRenderCaptureTargetSelector );
270 return mDepthRenderCameraSelector;
273 Qt3DCore::QEntity *QgsShadowRenderingFrameGraph::constructDepthRenderQuad()
275 Qt3DCore::QEntity *quad =
new Qt3DCore::QEntity;
276 quad->setObjectName(
"depthRenderQuad" );
278 Qt3DRender::QGeometry *geom =
new Qt3DRender::QGeometry;
279 Qt3DRender::QAttribute *positionAttribute =
new Qt3DRender::QAttribute;
280 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 };
282 const QByteArray vertexArr( (
const char * ) vert.constData(), vert.size() *
sizeof(
float ) );
283 Qt3DRender::QBuffer *vertexBuffer =
nullptr;
284 vertexBuffer =
new Qt3DRender::QBuffer(
this );
285 vertexBuffer->setData( vertexArr );
287 positionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
288 positionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
289 positionAttribute->setVertexSize( 3 );
290 positionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
291 positionAttribute->setBuffer( vertexBuffer );
292 positionAttribute->setByteOffset( 0 );
293 positionAttribute->setByteStride( 3 *
sizeof(
float ) );
294 positionAttribute->setCount( 6 );
296 geom->addAttribute( positionAttribute );
298 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
299 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
300 renderer->setGeometry( geom );
302 quad->addComponent( renderer );
304 QMatrix4x4 modelMatrix;
305 modelMatrix.setToIdentity();
309 Qt3DRender::QMaterial *material =
new Qt3DRender::QMaterial;
310 Qt3DRender::QParameter *textureParameter =
new Qt3DRender::QParameter(
"depthTexture", mForwardDepthTexture );
311 Qt3DRender::QParameter *textureTransformParameter =
new Qt3DRender::QParameter(
"modelMatrix", QVariant::fromValue( modelMatrix ) );
312 material->addParameter( textureParameter );
313 material->addParameter( textureTransformParameter );
315 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect;
317 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
319 Qt3DRender::QGraphicsApiFilter *graphicsApiFilter = technique->graphicsApiFilter();
320 graphicsApiFilter->setApi( Qt3DRender::QGraphicsApiFilter::Api::OpenGL );
321 graphicsApiFilter->setProfile( Qt3DRender::QGraphicsApiFilter::OpenGLProfile::CoreProfile );
322 graphicsApiFilter->setMajorVersion( 1 );
323 graphicsApiFilter->setMinorVersion( 5 );
325 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass;
327 Qt3DRender::QShaderProgram *shader =
new Qt3DRender::QShaderProgram;
328 shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"qrc:/shaders/depth_render.vert" ) ) );
329 shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl(
"qrc:/shaders/depth_render.frag" ) ) );
330 renderPass->setShaderProgram( shader );
332 technique->addRenderPass( renderPass );
334 effect->addTechnique( technique );
335 material->setEffect( effect );
337 quad->addComponent( material );
348 mLightCamera =
new Qt3DRender::QCamera;
350 mPostprocessPassLayer =
new Qt3DRender::QLayer;
351 mPreviewLayer =
new Qt3DRender::QLayer;
352 mCastShadowsLayer =
new Qt3DRender::QLayer;
353 mForwardRenderLayer =
new Qt3DRender::QLayer;
354 mDepthRenderPassLayer =
new Qt3DRender::QLayer;
357 mPostprocessPassLayer->setRecursive(
true );
358 mPreviewLayer->setRecursive(
true );
359 mCastShadowsLayer->setRecursive(
true );
360 mForwardRenderLayer->setRecursive(
true );
361 mDepthRenderPassLayer->setRecursive(
true );
363 mRenderSurfaceSelector =
new Qt3DRender::QRenderSurfaceSelector;
365 QObject *surfaceObj =
dynamic_cast< QObject *
>( surface );
366 Q_ASSERT( surfaceObj );
368 mRenderSurfaceSelector->setSurface( surfaceObj );
369 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
371 mMainViewPort =
new Qt3DRender::QViewport( mRenderSurfaceSelector );
372 mMainViewPort->setNormalizedRect( QRectF( 0.0f, 0.0f, 1.0f, 1.0f ) );
375 Qt3DRender::QFrameGraphNode *forwardRenderPass = constructForwardRenderPass();
376 forwardRenderPass->setParent( mMainViewPort );
380 Qt3DRender::QFrameGraphNode *shadowRenderPass = constructShadowRenderPass();
381 shadowRenderPass->setParent( mMainViewPort );
384 Qt3DRender::QFrameGraphNode *depthBufferProcessingPass = constructDepthRenderPass();
385 depthBufferProcessingPass->setParent( mMainViewPort );
388 Qt3DRender::QFrameGraphNode *postprocessingPass = constructPostprocessingPass();
389 postprocessingPass->setParent( mMainViewPort );
392 Qt3DRender::QFrameGraphNode *previewPass = constructTexturesPreviewPass();
393 previewPass->setParent( mMainViewPort );
396 mPostprocessingEntity->addComponent( mPostprocessPassLayer );
398 Qt3DRender::QParameter *depthMapIsDepthParam =
new Qt3DRender::QParameter(
"isDepth",
true );
399 Qt3DRender::QParameter *shadowMapIsDepthParam =
new Qt3DRender::QParameter(
"isDepth",
true );
401 mDebugDepthMapPreviewQuad = this->
addTexturePreviewOverlay( mForwardDepthTexture, QPointF( 0.9f, 0.9f ), QSizeF( 0.1, 0.1 ), QVector<Qt3DRender::QParameter *> { depthMapIsDepthParam } );
402 mDebugShadowMapPreviewQuad = this->
addTexturePreviewOverlay( mShadowMapTexture, QPointF( 0.9f, 0.9f ), QSizeF( 0.1, 0.1 ), QVector<Qt3DRender::QParameter *> { shadowMapIsDepthParam } );
403 mDebugDepthMapPreviewQuad->setEnabled(
false );
404 mDebugShadowMapPreviewQuad->setEnabled(
false );
406 mDepthRenderQuad = constructDepthRenderQuad();
407 mDepthRenderQuad->addComponent( mDepthRenderPassLayer );
408 mDepthRenderQuad->setParent( mRootEntity );
414 previewQuad->addComponent( mPreviewLayer );
415 previewQuad->setParent( mRootEntity );
416 mPreviewQuads.push_back( previewQuad );
421 void calculateViewExtent( Qt3DRender::QCamera *camera,
float shadowRenderingDistance,
float y,
float &minX,
float &maxX,
float &minY,
float &maxY,
float &minZ,
float &maxZ )
423 const QVector3D cameraPos = camera->position();
424 const QMatrix4x4 projectionMatrix = camera->projectionMatrix();
425 const QMatrix4x4 viewMatrix = camera->viewMatrix();
427 QVector4D viewCenter = viewMatrix * QVector4D( camera->viewCenter(), 1.0f );
428 viewCenter /= viewCenter.w();
429 viewCenter = projectionMatrix * viewCenter;
430 viewCenter /= viewCenter.w();
431 depth = viewCenter.z();
432 QVector<QVector3D> viewFrustumPoints =
434 QVector3D( 0.0f, 0.0f, depth ),
435 QVector3D( 0.0f, 1.0f, depth ),
436 QVector3D( 1.0f, 0.0f, depth ),
437 QVector3D( 1.0f, 1.0f, depth ),
438 QVector3D( 0.0f, 0.0f, 0 ),
439 QVector3D( 0.0f, 1.0f, 0 ),
440 QVector3D( 1.0f, 0.0f, 0 ),
441 QVector3D( 1.0f, 1.0f, 0 )
443 maxX = std::numeric_limits<float>::lowest();
444 maxY = std::numeric_limits<float>::lowest();
445 maxZ = std::numeric_limits<float>::lowest();
446 minX = std::numeric_limits<float>::max();
447 minY = std::numeric_limits<float>::max();
448 minZ = std::numeric_limits<float>::max();
449 for (
int i = 0; i < viewFrustumPoints.size(); ++i )
452 viewFrustumPoints[i] = viewFrustumPoints[i].unproject( viewMatrix, projectionMatrix, QRect( 0, 0, 1, 1 ) );
453 minX = std::min( minX, viewFrustumPoints[i].x() );
454 maxX = std::max( maxX, viewFrustumPoints[i].x() );
455 minY = std::min( minY, viewFrustumPoints[i].y() );
456 maxY = std::max( maxY, viewFrustumPoints[i].y() );
457 minZ = std::min( minZ, viewFrustumPoints[i].z() );
458 maxZ = std::max( maxZ, viewFrustumPoints[i].z() );
463 const QVector3D pt = cameraPos;
464 const QVector3D vect = ( viewFrustumPoints[i] - pt ).normalized();
465 float t = ( y - pt.y() ) / vect.y();
467 t = shadowRenderingDistance;
469 t = std::min( t, shadowRenderingDistance );
470 viewFrustumPoints[i] = pt + t * vect;
471 minX = std::min( minX, viewFrustumPoints[i].x() );
472 maxX = std::max( maxX, viewFrustumPoints[i].x() );
473 minY = std::min( minY, viewFrustumPoints[i].y() );
474 maxY = std::max( maxY, viewFrustumPoints[i].y() );
475 minZ = std::min( minZ, viewFrustumPoints[i].z() );
476 maxZ = std::max( maxZ, viewFrustumPoints[i].z() );
482 float minX, maxX, minY, maxY, minZ, maxZ;
483 QVector3D lookingAt = mMainCamera->viewCenter();
484 const float d = 2 * ( mMainCamera->position() - mMainCamera->viewCenter() ).length();
486 const QVector3D vertical = QVector3D( 0.0f, d, 0.0f );
488 calculateViewExtent( mMainCamera, maximumShadowRenderingDistance, lookingAt.y(), minX, maxX, minY, maxY, minZ, maxZ );
490 lookingAt = QVector3D( 0.5 * ( minX + maxX ), mMainCamera->viewCenter().y(), 0.5 * ( minZ + maxZ ) );
491 const QVector3D lightPosition = lookingAt + vertical;
492 mLightCamera->setPosition( lightPosition );
493 mLightCamera->setViewCenter( lookingAt );
494 mLightCamera->setUpVector( QVector3D( 0.0f, 1.0f, 0.0f ) );
495 mLightCamera->rotateAboutViewCenter( QQuaternion::rotationTo( vertical.normalized(), -lightDirection.normalized() ) );
497 mLightCamera->setProjectionType( Qt3DRender::QCameraLens::ProjectionType::OrthographicProjection );
498 mLightCamera->lens()->setOrthographicProjection(
499 - 0.7 * ( maxX - minX ), 0.7 * ( maxX - minX ),
500 - 0.7 * ( maxZ - minZ ), 0.7 * ( maxZ - minZ ),
501 1.0f, 2 * ( lookingAt - lightPosition ).length() );
509 mForwardClearBuffers->setClearColor( clearColor );
514 mShadowRenderingEnabled = enabled;
516 if ( mShadowRenderingEnabled )
517 mShadowSceneEntitiesFilter->setEnabled(
true );
519 mShadowSceneEntitiesFilter->setEnabled(
false );
530 mShadowMapResolution = resolution;
531 mShadowMapTexture->setWidth( mShadowMapResolution );
532 mShadowMapTexture->setHeight( mShadowMapResolution );
537 if ( enabled == mFrustumCullingEnabled )
539 mFrustumCullingEnabled = enabled;
540 if ( mFrustumCullingEnabled )
541 mFrustumCulling->setParent( mForwardClearBuffers );
543 mFrustumCulling->setParent( ( Qt3DCore::QNode * )
nullptr );
548 mEyeDomeLightingEnabled = enabled;
549 mEyeDomeLightingStrength = strength;
550 mEyeDomeLightingDistance = distance;
558 mDebugShadowMapPreviewQuad->setEnabled( enabled );
563 case Qt::Corner::TopRightCorner:
564 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
566 case Qt::Corner::TopLeftCorner:
567 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
569 case Qt::Corner::BottomRightCorner:
570 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
572 case Qt::Corner::BottomLeftCorner:
573 mDebugShadowMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
581 mDebugDepthMapPreviewQuad->setEnabled( enabled );
587 case Qt::Corner::TopRightCorner:
588 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
590 case Qt::Corner::TopLeftCorner:
591 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 0.0f + size / 2 ), 0.5 * QSizeF( size, size ) );
593 case Qt::Corner::BottomRightCorner:
594 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 1.0f - size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
596 case Qt::Corner::BottomLeftCorner:
597 mDebugDepthMapPreviewQuad->
setViewPort( QPointF( 0.0f + size / 2, 1.0f - size / 2 ), 0.5 * QSizeF( size, size ) );
606 mForwardColorTexture->setSize( mSize.width(), mSize.height() );
607 mForwardDepthTexture->setSize( mSize.width(), mSize.height() );
608 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
609 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
610 mDepthRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
611 mDepthRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
612 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
617 if ( enabled == mRenderCaptureEnabled )
619 mRenderCaptureEnabled = enabled;
620 mRenderCaptureTargetSelector->setEnabled( mRenderCaptureEnabled );
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 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.
void setupDepthMapDebugging(bool enabled, Qt::Corner corner, double size)
Sets the depth map debugging view port.
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 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.
Qt3DRender::QCamera * mainCamera()
Returns the main camera.
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.
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.
void calculateViewExtent(Qt3DRender::QCamera *camera, float shadowRenderingDistance, float y, float &minX, float &maxX, float &minY, float &maxY, float &minZ, float &maxZ)