31#include <Qt3DCore/QAttribute>
32#include <Qt3DCore/QBuffer>
33#include <Qt3DCore/QGeometry>
34#include <Qt3DRender/QAbstractTexture>
35#include <Qt3DRender/QBlendEquation>
36#include <Qt3DRender/QBlendEquationArguments>
37#include <Qt3DRender/QColorMask>
38#include <Qt3DRender/QGeometryRenderer>
39#include <Qt3DRender/QGraphicsApiFilter>
40#include <Qt3DRender/QNoDepthMask>
41#include <Qt3DRender/QNoDraw>
42#include <Qt3DRender/QSortPolicy>
43#include <Qt3DRender/QTechnique>
45#include "moc_qgsframegraph.cpp"
55void QgsFrameGraph::constructForwardRenderPass()
60void QgsFrameGraph::constructHighlightsPass()
65void QgsFrameGraph::constructShadowRenderPass()
70void QgsFrameGraph::constructOverlayTexturePass( Qt3DRender::QFrameGraphNode *topNode )
75Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructSubPostPassForProcessing()
77 Qt3DRender::QLayerFilter *layerFilter =
new Qt3DRender::QLayerFilter();
80 new Qt3DRender::QClearBuffers( layerFilter );
82 Qt3DRender::QLayer *postProcessingLayer =
new Qt3DRender::QLayer();
83 mPostprocessingEntity =
new QgsPostprocessingEntity(
this, postProcessingLayer, mRootEntity );
84 layerFilter->addLayer( postProcessingLayer );
85 mPostprocessingEntity->setObjectName(
"PostProcessingPassEntity" );
90Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructSubPostPassForRenderCapture()
92 Qt3DRender::QFrameGraphNode *top =
new Qt3DRender::QNoDraw;
93 top->setObjectName(
"Sub pass RenderCapture" );
95 mRenderCapture =
new Qt3DRender::QRenderCapture( top );
100Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructPostprocessingPass()
102 mRenderCaptureTargetSelector =
new Qt3DRender::QRenderTargetSelector;
103 mRenderCaptureTargetSelector->setObjectName(
"Postprocessing render pass" );
104 mRenderCaptureTargetSelector->setEnabled( mRenderCaptureEnabled );
106 Qt3DRender::QRenderTarget *renderTarget =
new Qt3DRender::QRenderTarget( mRenderCaptureTargetSelector );
112 Qt3DRender::QRenderTargetOutput *colorOutput =
new Qt3DRender::QRenderTargetOutput( renderTarget );
113 colorOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
116 mRenderCaptureColorTexture =
new Qt3DRender::QTexture2D( colorOutput );
117 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
118 mRenderCaptureColorTexture->setFormat( Qt3DRender::QAbstractTexture::RGB8_UNorm );
119 mRenderCaptureColorTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
120 mRenderCaptureColorTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
121 mRenderCaptureColorTexture->setObjectName(
"PostProcessingPass::ColorTarget" );
124 colorOutput->setTexture( mRenderCaptureColorTexture );
125 renderTarget->addOutput( colorOutput );
127 Qt3DRender::QRenderTargetOutput *depthOutput =
new Qt3DRender::QRenderTargetOutput( renderTarget );
129 depthOutput->setAttachmentPoint( Qt3DRender::QRenderTargetOutput::Depth );
130 mRenderCaptureDepthTexture =
new Qt3DRender::QTexture2D( depthOutput );
131 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
132 mRenderCaptureDepthTexture->setFormat( Qt3DRender::QAbstractTexture::DepthFormat );
133 mRenderCaptureDepthTexture->setMinificationFilter( Qt3DRender::QAbstractTexture::Linear );
134 mRenderCaptureDepthTexture->setMagnificationFilter( Qt3DRender::QAbstractTexture::Linear );
135 mRenderCaptureDepthTexture->setComparisonFunction( Qt3DRender::QAbstractTexture::CompareLessEqual );
136 mRenderCaptureDepthTexture->setComparisonMode( Qt3DRender::QAbstractTexture::CompareRefToTexture );
137 mRenderCaptureDepthTexture->setObjectName(
"PostProcessingPass::DepthTarget" );
139 depthOutput->setTexture( mRenderCaptureDepthTexture );
140 renderTarget->addOutput( depthOutput );
142 mRenderCaptureTargetSelector->setTarget( renderTarget );
145 constructSubPostPassForProcessing()->setParent( mRenderCaptureTargetSelector );
146 constructOverlayTexturePass( mRenderCaptureTargetSelector );
147 constructSubPostPassForRenderCapture()->setParent( mRenderCaptureTargetSelector );
149 return mRenderCaptureTargetSelector;
152void QgsFrameGraph::constructAmbientOcclusionRenderPass()
156 QgsAmbientOcclusionRenderView *aorv =
new QgsAmbientOcclusionRenderView(
AMBIENT_OCCLUSION_RENDERVIEW, mMainCamera, mSize, forwardDepthTexture, mRootEntity );
160Qt3DRender::QFrameGraphNode *QgsFrameGraph::constructRubberBandsPass()
162 mRubberBandsCameraSelector =
new Qt3DRender::QCameraSelector;
163 mRubberBandsCameraSelector->setObjectName(
"RubberBands Pass CameraSelector" );
164 mRubberBandsCameraSelector->setCamera( mMainCamera );
166 mRubberBandsLayerFilter =
new Qt3DRender::QLayerFilter( mRubberBandsCameraSelector );
167 mRubberBandsLayerFilter->addLayer( mRubberBandsLayer );
169 Qt3DRender::QBlendEquationArguments *blendState =
new Qt3DRender::QBlendEquationArguments;
170 blendState->setSourceRgb( Qt3DRender::QBlendEquationArguments::SourceAlpha );
171 blendState->setDestinationRgb( Qt3DRender::QBlendEquationArguments::OneMinusSourceAlpha );
173 Qt3DRender::QBlendEquation *blendEquation =
new Qt3DRender::QBlendEquation;
174 blendEquation->setBlendFunction( Qt3DRender::QBlendEquation::Add );
176 mRubberBandsStateSet =
new Qt3DRender::QRenderStateSet( mRubberBandsLayerFilter );
177 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest;
178 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
179 mRubberBandsStateSet->addRenderState( depthTest );
180 mRubberBandsStateSet->addRenderState( blendState );
181 mRubberBandsStateSet->addRenderState( blendEquation );
186 mRubberBandsRenderTargetSelector =
new Qt3DRender::QRenderTargetSelector( mRubberBandsStateSet );
187 mRubberBandsRenderTargetSelector->setTarget(
forwardRenderView().renderTargetSelector()->target() );
189 return mRubberBandsCameraSelector;
192void QgsFrameGraph::constructDepthRenderPass()
196 QgsDepthRenderView *rv =
new QgsDepthRenderView(
DEPTH_RENDERVIEW, mSize, forwardDepthTexture, mRootEntity );
207 for ( Qt3DRender::QParameter *param : parameters )
209 mGlobalParamsStorage->addParameter( param );
253 mRubberBandsLayer =
new Qt3DRender::QLayer;
254 mRubberBandsLayer->setObjectName(
"mRubberBandsLayer" );
255 mRubberBandsLayer->setRecursive(
true );
257 mRenderSurfaceSelector =
new Qt3DRender::QRenderSurfaceSelector;
259 QObject *surfaceObj =
dynamic_cast<QObject *
>( surface );
260 Q_ASSERT( surfaceObj );
262 mRenderSurfaceSelector->setSurface( surfaceObj );
263 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
265 mMainViewPort =
new Qt3DRender::QViewport( mRenderSurfaceSelector );
266 mMainViewPort->setNormalizedRect( QRectF( 0.0f, 0.0f, 1.0f, 1.0f ) );
268 mGlobalParamsStorage =
new Qt3DRender::QRenderPassFilter( mMainViewPort );
269 mGlobalParamsStorage->setObjectName(
"GlobalParametersStore" );
272 constructForwardRenderPass();
275 constructHighlightsPass();
278 Qt3DRender::QFrameGraphNode *rubberBandsPass = constructRubberBandsPass();
279 rubberBandsPass->setObjectName(
"rubberBandsPass" );
280 rubberBandsPass->setParent( mGlobalParamsStorage );
282 mMsaaBlitNode =
new Qt3DRender::QBlitFramebuffer( mGlobalParamsStorage );
283 mMsaaBlitNode->setObjectName(
"MsaaBlitFramebuffer" );
284 mMsaaBlitNode->setEnabled(
false );
286 mMsaaDepthBlitNode =
new Qt3DRender::QBlitFramebuffer( mGlobalParamsStorage );
287 mMsaaDepthBlitNode->setObjectName(
"MsaaDepthBlitFramebuffer" );
288 mMsaaDepthBlitNode->setEnabled(
false );
291 constructShadowRenderPass();
294 constructDepthRenderPass();
297 constructAmbientOcclusionRenderPass();
300 Qt3DRender::QFrameGraphNode *postprocessingPass = constructPostprocessingPass();
301 postprocessingPass->setParent( mGlobalParamsStorage );
302 postprocessingPass->setObjectName(
"PostProcessingPass" );
304 mRubberBandsRootEntity =
new Qt3DCore::QEntity( mRootEntity );
305 mRubberBandsRootEntity->setObjectName(
"mRubberBandsRootEntity" );
306 mRubberBandsRootEntity->addComponent( mRubberBandsLayer );
311 if ( mRenderViewMap.find( name ) != mRenderViewMap.end() )
313 mRenderViewMap[name]->topGraphNode()->setParent( ( QNode * )
nullptr );
314 mRenderViewMap.erase( name );
321 if ( mRenderViewMap.find( name ) == mRenderViewMap.end() )
323 mRenderViewMap[name] = std::move(
renderView );
324 mRenderViewMap[name]->topGraphNode()->setParent( topNode ? topNode : mGlobalParamsStorage );
325 mRenderViewMap[name]->updateWindowResize( mSize.width(), mSize.height() );
336 if ( mRenderViewMap[name] )
338 mRenderViewMap[name]->setEnabled( enable );
344 if ( mRenderViewMap.find( name ) != mRenderViewMap.end() )
346 return mRenderViewMap[name].get();
353 return mRenderViewMap[name] !=
nullptr && mRenderViewMap[name]->
isEnabled();
365 mPostprocessingEntity->setAmbientOcclusionEnabled( settings.
isEnabled() );
381 for (
int i = 0, dirLight = 0; !light && i < lightSources.size(); i++ )
385 if ( dirLight == selectedLight )
386 light = qgis::down_cast< QgsDirectionalLightSettings * >( lightSources[i] );
396 mPostprocessingEntity->setShadowRenderingEnabled(
true );
397 mPostprocessingEntity->setShadowBias(
static_cast<float>( shadowSettings.
shadowBias() ) );
399 mPostprocessingEntity->setShowCascadingShadowSplits( shadowSettings.
showCascadeSplits() );
405 mPostprocessingEntity->setShadowRenderingEnabled(
false );
412 if ( !debugRenderView )
423 if ( mDepthTextureDebugging )
430 delete mDepthTextureDebugging;
431 mDepthTextureDebugging =
nullptr;
438 QObject *top = mRenderSurfaceSelector;
439 while ( top->parent() &&
dynamic_cast<Qt3DRender::QFrameGraphNode *
>( top->parent() ) )
443 context.
lowestId = mMainCamera->id().id();
446 return strList.join(
"\n" ) + QString(
"\n" );
452 return strList.join(
"\n" ) + QString(
"\n" );
468 for (
auto it = mRenderViewMap.begin(); it != mRenderViewMap.end(); ++it )
474 mRenderCaptureColorTexture->setSize( mSize.width(), mSize.height() );
475 mRenderCaptureDepthTexture->setSize( mSize.width(), mSize.height() );
476 mRenderSurfaceSelector->setExternalRenderTargetSize( mSize );
478 mMsaaBlitNode->setSourceRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
479 mMsaaBlitNode->setDestinationRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
480 mMsaaDepthBlitNode->setSourceRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
481 mMsaaDepthBlitNode->setDestinationRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
486 return mRenderCapture;
491 if ( enabled == mRenderCaptureEnabled )
493 mRenderCaptureEnabled = enabled;
494 mRenderCaptureTargetSelector->setEnabled( mRenderCaptureEnabled );
504 mMsaaEnabled = enabled;
506 if ( !enabled && mMsaaBlitConfigured )
508 mMsaaBlitNode->setSource(
nullptr );
509 mMsaaBlitNode->setDestination(
nullptr );
510 mMsaaDepthBlitNode->setSource(
nullptr );
511 mMsaaDepthBlitNode->setDestination(
nullptr );
512 mMsaaBlitConfigured =
false;
517 if ( enabled && !mMsaaBlitConfigured )
519 mMsaaBlitConfigured =
true;
522 mMsaaBlitNode->setSourceRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
523 mMsaaBlitNode->setDestinationRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
524 mMsaaBlitNode->setSourceAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
525 mMsaaBlitNode->setDestinationAttachmentPoint( Qt3DRender::QRenderTargetOutput::Color0 );
526 mMsaaBlitNode->setInterpolationMethod( Qt3DRender::QBlitFramebuffer::Nearest );
530 mMsaaDepthBlitNode->setSourceRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
531 mMsaaDepthBlitNode->setDestinationRect( QRect( 0, 0, mSize.width(), mSize.height() ) );
532 mMsaaDepthBlitNode->setSourceAttachmentPoint( Qt3DRender::QRenderTargetOutput::DepthStencil );
533 mMsaaDepthBlitNode->setDestinationAttachmentPoint( Qt3DRender::QRenderTargetOutput::DepthStencil );
534 mMsaaDepthBlitNode->setInterpolationMethod( Qt3DRender::QBlitFramebuffer::Nearest );
539 mRubberBandsRenderTargetSelector->setTarget( target );
540 mMsaaBlitNode->setEnabled( enabled );
541 mMsaaDepthBlitNode->setEnabled( enabled );
@ Directional
Directional light source.
Qt::Corner debugDepthMapCorner() const
Returns the corner where the shadow map preview is displayed.
bool debugDepthMapEnabled() const
Returns whether the shadow map debugging is enabled.
double eyeDomeLightingStrength() const
Returns the eye dome lighting strength value.
double debugDepthMapSize() const
Returns the size of the shadow map preview.
bool is2DMapOverlayEnabled() const
Returns whether 2D map overlay is enabled.
int eyeDomeLightingDistance() const
Returns the eye dome lighting distance value (contributes to the contrast of the image).
bool eyeDomeLightingEnabled() const
Returns whether eye dome lighting is used.
Base class for 3D render view.
virtual void setEnabled(bool enable)
Enable or disable via enable the render view sub tree.
virtual bool isEnabled() const
Returns true if render view is enabled.
virtual void updateWindowResize(int width, int height)
Called when 3D window is resized.
Container class that holds different objects related to ambient occlusion rendering.
void setRadius(float radius)
Delegates to QgsAmbientOcclusionRenderEntity::setRadius.
void setEnabled(bool enable) override
Enable or disable via enable the render view sub tree.
void setIntensity(float intensity)
Delegates to QgsAmbientOcclusionRenderEntity::setIntensity.
void setThreshold(float threshold)
Delegates to QgsAmbientOcclusionRenderEntity::setThreshold.
Contains the configuration of ambient occlusion rendering.
float radius() const
Returns the radius parameter of the ambient occlusion effect.
bool isEnabled() const
Returns whether ambient occlusion effect is enabled.
float intensity() const
Returns the shading factor of the ambient occlusion effect.
float threshold() const
Returns at what amount of occlusion the effect will kick in.
Container class that holds different objects related to depth rendering.
Qt3DRender::QRenderCapture * renderCapture()
Returns the render capture object used to take an image of the depth buffer of the scene.
Definition of a directional light in a 3D map scene.
Container class that holds different objects related to forward rendering.
void setClearColor(const QColor &clearColor)
Sets the clear color of the scene (background color).
void setDebugOverlayEnabled(bool enabled)
Sets whether debug overlay is enabled.
void setMsaaEnabled(bool enabled)
Sets whether multisample anti-aliasing (MSAA) is enabled.
void setFrustumCullingEnabled(bool enabled)
Sets whether frustum culling is enabled.
Qt3DRender::QTexture2D * depthTexture() const
Returns forward depth texture.
void addClipPlanes(int nrClipPlanes)
Setups nrClipPlanes clip planes in the forward pass to enable OpenGL clipping.
void removeClipPlanes()
Disables OpenGL clipping.
Qt3DRender::QRenderTarget * msaaRenderTarget() const
Returns the multisampled render target used as blit source when MSAA is enabled.
Qt3DRender::QRenderTarget * regularRenderTarget() const
Returns the regular (single-sample) render target used as blit destination and postprocessing input.
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 addGlobalParameters(const QList< Qt3DRender::QParameter * > ¶meters)
Adds additional global parameters to the graph.
void setMsaaEnabled(bool enabled)
Sets whether multisample anti-aliasing (MSAA) is enabled.
void updateAmbientOcclusionSettings(const QgsAmbientOcclusionSettings &settings)
Updates settings for ambient occlusion.
void updateEyeDomeSettings(const Qgs3DMapSettings &settings)
Updates settings for eye dome lighting.
bool isRenderViewEnabled(const QString &name)
Returns true if the render view named name is found and enabled.
void setRenderViewEnabled(const QString &name, bool enable)
Enables or disables the render view named name according to enable.
void updateShadowSettings(const QgsShadowSettings &shadowSettings, const QList< QgsLightSource * > &lightSources)
Updates shadow bias, light and texture size according to shadowSettings and lightSources.
void addClipPlanes(int nrClipPlanes)
Setups nrClipPlanes clip planes in the forward pass to enable OpenGL clipping.
void unregisterRenderView(const QString &name)
Unregisters the render view named name, if any.
bool registerRenderView(std::unique_ptr< QgsAbstractRenderView > renderView, const QString &name, Qt3DRender::QFrameGraphNode *topNode=nullptr)
Registers a new the render view renderView with name name.
QString dumpFrameGraph() const
Dumps frame graph as string.
void setRenderCaptureEnabled(bool enabled)
Sets whether it will be possible to render to an image.
QgsAmbientOcclusionRenderView & ambientOcclusionRenderView()
Returns ambient occlusion renderview.
Qt3DRender::QRenderCapture * depthRenderCapture()
Returns the render capture object used to take an image of the depth buffer of the scene.
QgsAbstractRenderView * renderView(const QString &name)
Returns the render view named name, if any.
void removeClipPlanes()
Disables OpenGL clipping.
static const QString AMBIENT_OCCLUSION_RENDERVIEW
Ambient occlusion render view name.
QgsDepthRenderView & depthRenderView()
Returns depth renderview.
void setClearColor(const QColor &clearColor)
Sets the clear color of the scene (background color).
static const QString FORWARD_RENDERVIEW
void setFrustumCullingEnabled(bool enabled)
Sets whether frustum culling is enabled.
static const QString SHADOW_RENDERVIEW
void setDebugOverlayEnabled(bool enabled)
Sets whether debug overlay is enabled.
static const QString HIGHLIGHTS_RENDERVIEW
QgsHighlightsRenderView & highlightsRenderView()
Returns the highlights renderview, used for rendering highlight overlays of identified features.
static const QString OVERLAY_RENDERVIEW
void updateDebugDepthMapSettings(const Qgs3DMapSettings &settings)
Updates settings for depth debug map.
static const QString AXIS3D_RENDERVIEW
QgsForwardRenderView & forwardRenderView()
Returns forward renderview.
QgsOverlayTextureRenderView & overlayTextureRenderView()
Returns overlay texture renderview.
QString dumpSceneGraph() const
Dumps scene graph as string.
QgsShadowRenderView & shadowRenderView()
Returns shadow renderview.
void setSize(QSize s)
Sets the size of the buffers used for rendering.
static const QString DEPTH_RENDERVIEW
Qt3DRender::QCamera * mainCamera()
Returns the main camera.
QgsFrameGraph(QSurface *surface, QSize s, Qt3DRender::QCamera *mainCamera, Qt3DCore::QEntity *root)
Constructor.
Qt3DRender::QRenderCapture * renderCapture()
Returns the render capture object used to take an image of the scene.
Container class that holds different objects related to highlighting identified features.
void setRenderTarget(Qt3DRender::QRenderTarget *target)
Switches the render target (called when toggling MSAA on/off).
An entity responsible for rendering an overlay texture in 3D view.
Simple render view to preview overlay textures in 3D view.
Qt3DRender::QLayer * overlayLayer() const
Returns layer in which entities must be added in the in order to be processed by this renderview.
Container class that holds different objects related to shadow rendering.
void setMapSize(int width, int height)
Update shadow depth texture size.
void setEnabled(bool enable) override
Enable or disable via enable the renderview sub tree.
Contains configuration for rendering shadows.
int selectedDirectionalLight() const
Returns the selected direcctional light used to cast shadows.
bool renderShadows() const
Returns whether shadow rendering is enabled.
static int qualityToMapResolution(Qgis::ShadowQuality quality)
Returns the shadow map resolution corresponding to the specified shadow quality.
double maximumShadowRenderingDistance() const
Returns the maximum shadow rendering distance accounted for when rendering shadows Objects further aw...
double shadowBias() const
Returns the shadow bias used to correct the numerical imprecision of shadows (for the depth test) Thi...
Qgis::ShadowQuality shadowQuality() const
Returns the quality of the shadow map texture used to generate the shadows.
bool showCascadeSplits() const
Returns true if the cascading shadow splits should be tinted in the view.