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