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