QGIS API Documentation 3.99.0-Master (a26b91b364d)
qgspostprocessingentity.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspostprocessingentity.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
17#include "moc_qgspostprocessingentity.cpp"
18
19#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
20#include <Qt3DRender/QAttribute>
21#include <Qt3DRender/QBuffer>
22#include <Qt3DRender/QGeometry>
23
24typedef Qt3DRender::QAttribute Qt3DQAttribute;
25typedef Qt3DRender::QBuffer Qt3DQBuffer;
26typedef Qt3DRender::QGeometry Qt3DQGeometry;
27#else
28#include <Qt3DCore/QAttribute>
29#include <Qt3DCore/QBuffer>
30#include <Qt3DCore/QGeometry>
31
32typedef Qt3DCore::QAttribute Qt3DQAttribute;
33typedef Qt3DCore::QBuffer Qt3DQBuffer;
34typedef Qt3DCore::QGeometry Qt3DQGeometry;
35#endif
36
37#include <Qt3DRender/QGeometryRenderer>
38#include <Qt3DRender/QParameter>
39#include <Qt3DRender/QTechnique>
40#include <Qt3DRender/QGraphicsApiFilter>
41#include <Qt3DRender/QDepthTest>
42#include <QUrl>
43
44#include "qgs3dutils.h"
46#include "qgsframegraph.h"
47#include "qgsshadowrenderview.h"
50
51QgsPostprocessingEntity::QgsPostprocessingEntity( QgsFrameGraph *frameGraph, Qt3DRender::QLayer *layer, QNode *parent )
52 : QgsRenderPassQuad( layer, parent )
53{
54 QgsShadowRenderView &shadowRenderView = frameGraph->shadowRenderView();
55 QgsForwardRenderView &forwardRenderView = frameGraph->forwardRenderView();
57
58 mColorTextureParameter = new Qt3DRender::QParameter( QStringLiteral( "colorTexture" ), forwardRenderView.colorTexture() );
59 mDepthTextureParameter = new Qt3DRender::QParameter( QStringLiteral( "depthTexture" ), forwardRenderView.depthTexture() );
60 mShadowMapParameter = new Qt3DRender::QParameter( QStringLiteral( "shadowTexture" ), shadowRenderView.mapTexture() );
61 mAmbientOcclusionTextureParameter = new Qt3DRender::QParameter( QStringLiteral( "ssaoTexture" ), aoRenderView.blurredFactorMapTexture() );
62 mMaterial->addParameter( mColorTextureParameter );
63 mMaterial->addParameter( mDepthTextureParameter );
64 mMaterial->addParameter( mShadowMapParameter );
65 mMaterial->addParameter( mAmbientOcclusionTextureParameter );
66
67 mMainCamera = frameGraph->mainCamera();
68 mLightCamera = shadowRenderView.lightCamera();
69
70 mFarPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "farPlane" ), mMainCamera->farPlane() );
71 mMaterial->addParameter( mFarPlaneParameter );
72 connect( mMainCamera, &Qt3DRender::QCamera::farPlaneChanged, mFarPlaneParameter, [&]( float farPlane ) {
73 mFarPlaneParameter->setValue( farPlane );
74 } );
75 mNearPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "nearPlane" ), mMainCamera->nearPlane() );
76 mMaterial->addParameter( mNearPlaneParameter );
77 connect( mMainCamera, &Qt3DRender::QCamera::nearPlaneChanged, mNearPlaneParameter, [&]( float nearPlane ) {
78 mNearPlaneParameter->setValue( nearPlane );
79 } );
80
81 mLightFarPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "lightFarPlane" ), mLightCamera->farPlane() );
82 mMaterial->addParameter( mLightFarPlaneParameter );
83 connect( mLightCamera, &Qt3DRender::QCamera::farPlaneChanged, mLightFarPlaneParameter, [&]( float farPlane ) {
84 mLightFarPlaneParameter->setValue( farPlane );
85 } );
86 mLightNearPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "lightNearPlane" ), mLightCamera->nearPlane() );
87 mMaterial->addParameter( mLightNearPlaneParameter );
88 connect( mLightCamera, &Qt3DRender::QCamera::nearPlaneChanged, mLightNearPlaneParameter, [&]( float nearPlane ) {
89 mLightNearPlaneParameter->setValue( nearPlane );
90 } );
91
92 mMainCameraInvViewMatrixParameter = new Qt3DRender::QParameter( QStringLiteral( "invertedCameraView" ), mMainCamera->viewMatrix().inverted() );
93 mMaterial->addParameter( mMainCameraInvViewMatrixParameter );
94 mMainCameraInvProjMatrixParameter = new Qt3DRender::QParameter( QStringLiteral( "invertedCameraProj" ), mMainCamera->projectionMatrix().inverted() );
95 mMaterial->addParameter( mMainCameraInvProjMatrixParameter );
96 connect( mMainCamera, &Qt3DRender::QCamera::projectionMatrixChanged, mMainCameraInvProjMatrixParameter, [&]( const QMatrix4x4 &projectionMatrix ) {
97 mMainCameraInvProjMatrixParameter->setValue( projectionMatrix.inverted() );
98 } );
99 connect( mMainCamera, &Qt3DRender::QCamera::viewMatrixChanged, mMainCameraInvViewMatrixParameter, [&]() {
100 mMainCameraInvViewMatrixParameter->setValue( mMainCamera->viewMatrix().inverted() );
101 } );
102
103 mShadowMinX = new Qt3DRender::QParameter( QStringLiteral( "shadowMinX" ), QVariant::fromValue( 0.0f ) );
104 mShadowMaxX = new Qt3DRender::QParameter( QStringLiteral( "shadowMaxX" ), QVariant::fromValue( 0.0f ) );
105 mShadowMinY = new Qt3DRender::QParameter( QStringLiteral( "shadowMinY" ), QVariant::fromValue( 0.0f ) );
106 mShadowMaxY = new Qt3DRender::QParameter( QStringLiteral( "shadowMaxY" ), QVariant::fromValue( 0.0f ) );
107 mMaterial->addParameter( mShadowMinX );
108 mMaterial->addParameter( mShadowMaxX );
109 mMaterial->addParameter( mShadowMinY );
110 mMaterial->addParameter( mShadowMaxY );
111
112 mRenderShadowsParameter = new Qt3DRender::QParameter( QStringLiteral( "renderShadows" ), QVariant::fromValue( 0 ) );
113 mMaterial->addParameter( mRenderShadowsParameter );
114
115 mShadowBiasParameter = new Qt3DRender::QParameter( QStringLiteral( "shadowBias" ), QVariant::fromValue( 0.00001f ) );
116 mMaterial->addParameter( mShadowBiasParameter );
117
118 mEyeDomeLightingEnabledParameter = new Qt3DRender::QParameter( QStringLiteral( "edlEnabled" ), QVariant::fromValue( 0 ) );
119 mEyeDomeLightingStrengthParameter = new Qt3DRender::QParameter( QStringLiteral( "edlStrength" ), QVariant::fromValue( 1000.0f ) );
120 mEyeDomeLightingDistanceParameter = new Qt3DRender::QParameter( QStringLiteral( "edlDistance" ), QVariant::fromValue( 2.0f ) );
121 mMaterial->addParameter( mEyeDomeLightingEnabledParameter );
122 mMaterial->addParameter( mEyeDomeLightingStrengthParameter );
123 mMaterial->addParameter( mEyeDomeLightingDistanceParameter );
124
125 mAmbientOcclusionEnabledParameter = new Qt3DRender::QParameter( QStringLiteral( "ssaoEnabled" ), QVariant::fromValue( 0 ) );
126 mMaterial->addParameter( mAmbientOcclusionEnabledParameter );
127
128 mLightPosition = new Qt3DRender::QParameter( QStringLiteral( "lightPosition" ), QVariant::fromValue( QVector3D() ) );
129 mLightDirection = new Qt3DRender::QParameter( QStringLiteral( "lightDirection" ), QVariant::fromValue( QVector3D() ) );
130 mMaterial->addParameter( mLightPosition );
131 mMaterial->addParameter( mLightDirection );
132
133 const QString vertexShaderPath = QStringLiteral( "qrc:/shaders/postprocess.vert" );
134 const QString fragmentShaderPath = QStringLiteral( "qrc:/shaders/postprocess.frag" );
135
136 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
137 mShader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );
138}
139
140void QgsPostprocessingEntity::setupShadowRenderingExtent( float minX, float maxX, float minY, float maxY )
141{
142 mShadowMinX->setValue( minX );
143 mShadowMaxX->setValue( maxX );
144 mShadowMinY->setValue( minY );
145 mShadowMaxY->setValue( maxY );
146}
147
148void QgsPostprocessingEntity::setupDirectionalLight( QVector3D position, QVector3D direction )
149{
150 mLightPosition->setValue( QVariant::fromValue( position ) );
151 mLightDirection->setValue( QVariant::fromValue( direction.normalized() ) );
152}
153
154void QgsPostprocessingEntity::updateShadowSettings( const QgsDirectionalLightSettings &light, float maximumShadowRenderingDistance )
155{
156 float minX, maxX, minY, maxY, minZ, maxZ;
157 QVector3D lookingAt = mMainCamera->viewCenter();
158 const float d = 2 * ( mMainCamera->position() - mMainCamera->viewCenter() ).length();
159
160 const QVector3D lightDirection = light.direction().toVector3D().normalized();
161 Qgs3DUtils::calculateViewExtent( mMainCamera, maximumShadowRenderingDistance, lookingAt.z(), minX, maxX, minY, maxY, minZ, maxZ );
162
163 lookingAt = QVector3D( 0.5f * ( minX + maxX ), 0.5f * ( minY + maxY ), mMainCamera->viewCenter().z() );
164 const QVector3D lightPosition = lookingAt + QVector3D( 0.0f, 0.0f, d );
165 mLightCamera->setPosition( lightPosition );
166 mLightCamera->setViewCenter( lookingAt );
167 mLightCamera->setUpVector( QVector3D( 0.0f, 1.0f, 0.0f ) );
168 mLightCamera->rotateAboutViewCenter( QQuaternion::rotationTo( QVector3D( 0.0f, 0.0f, -1.0f ), lightDirection ) );
169
170 mLightCamera->setProjectionType( Qt3DRender::QCameraLens::ProjectionType::OrthographicProjection );
171 mLightCamera->lens()->setOrthographicProjection(
172 -0.7f * ( maxX - minX ), 0.7f * ( maxX - minX ),
173 -0.7f * ( maxY - minY ), 0.7f * ( maxY - minY ),
174 1.0f, 2 * ( lookingAt - lightPosition ).length()
175 );
176
177 setupShadowRenderingExtent( minX, maxX, minY, maxY );
178 setupDirectionalLight( lightPosition, lightDirection );
179}
180
182{
183 mRenderShadowsParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
184}
185
187{
188 mShadowBiasParameter->setValue( QVariant::fromValue( shadowBias ) );
189}
190
192{
193 mEyeDomeLightingEnabledParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
194}
195
197{
198 mEyeDomeLightingStrengthParameter->setValue( QVariant::fromValue( strength ) );
199}
200
202{
203 mEyeDomeLightingDistanceParameter->setValue( QVariant::fromValue( distance ) );
204}
205
207{
208 mAmbientOcclusionEnabledParameter->setValue( enabled );
209}
static void calculateViewExtent(const Qt3DRender::QCamera *camera, float maxRenderingDistance, float z, float &minX, float &maxX, float &minY, float &maxY, float &minZ, float &maxZ)
Computes the portion of the Y=y plane the camera is looking at.
Container class that holds different objects related to ambient occlusion rendering.
Qt3DRender::QTexture2D * blurredFactorMapTexture() const
Returns blur pass texture.
Definition of a directional light in a 3D map scene.
QgsVector3D direction() const
Returns the direction of the light in degrees.
Container class that holds different objects related to forward rendering.
Qt3DRender::QTexture2D * colorTexture() const
Returns forward color texture.
Qt3DRender::QTexture2D * depthTexture() const
Returns forward depth texture.
Container class that holds different objects related to frame graphs of 3D scenes.
QgsAmbientOcclusionRenderView & ambientOcclusionRenderView()
Returns ambient occlusion renderview.
QgsForwardRenderView & forwardRenderView()
Returns forward renderview.
QgsShadowRenderView & shadowRenderView()
Returns shadow renderview.
Qt3DRender::QCamera * mainCamera()
Returns the main camera.
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 updateShadowSettings(const QgsDirectionalLightSettings &light, float maximumShadowRenderingDistance)
Sets shadow rendering to use a directional light.
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.
QgsPostprocessingEntity(QgsFrameGraph *frameGraph, Qt3DRender::QLayer *layer, QNode *parent=nullptr)
Constructor.
An entity that is responsible for rendering a screen quad for a specific rendering pass.
Qt3DRender::QShaderProgram * mShader
Qt3DRender::QMaterial * mMaterial
Container class that holds different objects related to shadow rendering.
Qt3DRender::QCamera * lightCamera()
Returns the light camera.
Qt3DRender::QTexture2D * mapTexture() const
Returns shadow depth texture.
QVector3D toVector3D() const
Converts the current object to QVector3D.
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry