QGIS API Documentation 3.99.0-Master (d270888f95f)
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 ) {
62 mFarPlaneParameter->setValue( farPlane );
63 } );
64 mNearPlaneParameter = new Qt3DRender::QParameter( u"nearPlane"_s, mMainCamera->nearPlane() );
65 mMaterial->addParameter( mNearPlaneParameter );
66 connect( mMainCamera, &Qt3DRender::QCamera::nearPlaneChanged, mNearPlaneParameter, [&]( float nearPlane ) {
67 mNearPlaneParameter->setValue( nearPlane );
68 } );
69
70 mLightFarPlaneParameter = new Qt3DRender::QParameter( u"lightFarPlane"_s, mLightCamera->farPlane() );
71 mMaterial->addParameter( mLightFarPlaneParameter );
72 connect( mLightCamera, &Qt3DRender::QCamera::farPlaneChanged, mLightFarPlaneParameter, [&]( float farPlane ) {
73 mLightFarPlaneParameter->setValue( farPlane );
74 } );
75 mLightNearPlaneParameter = new Qt3DRender::QParameter( u"lightNearPlane"_s, mLightCamera->nearPlane() );
76 mMaterial->addParameter( mLightNearPlaneParameter );
77 connect( mLightCamera, &Qt3DRender::QCamera::nearPlaneChanged, mLightNearPlaneParameter, [&]( float nearPlane ) {
78 mLightNearPlaneParameter->setValue( nearPlane );
79 } );
80
81 mMainCameraInvViewMatrixParameter = new Qt3DRender::QParameter( u"invertedCameraView"_s, mMainCamera->viewMatrix().inverted() );
82 mMaterial->addParameter( mMainCameraInvViewMatrixParameter );
83 mMainCameraInvProjMatrixParameter = new Qt3DRender::QParameter( u"invertedCameraProj"_s, mMainCamera->projectionMatrix().inverted() );
84 mMaterial->addParameter( mMainCameraInvProjMatrixParameter );
85 connect( mMainCamera, &Qt3DRender::QCamera::projectionMatrixChanged, mMainCameraInvProjMatrixParameter, [&]( const QMatrix4x4 &projectionMatrix ) {
86 mMainCameraInvProjMatrixParameter->setValue( projectionMatrix.inverted() );
87 } );
88 connect( mMainCamera, &Qt3DRender::QCamera::viewMatrixChanged, mMainCameraInvViewMatrixParameter, [&]() {
89 mMainCameraInvViewMatrixParameter->setValue( mMainCamera->viewMatrix().inverted() );
90 } );
91
92 mShadowMinX = new Qt3DRender::QParameter( u"shadowMinX"_s, QVariant::fromValue( 0.0f ) );
93 mShadowMaxX = new Qt3DRender::QParameter( u"shadowMaxX"_s, QVariant::fromValue( 0.0f ) );
94 mShadowMinY = new Qt3DRender::QParameter( u"shadowMinY"_s, QVariant::fromValue( 0.0f ) );
95 mShadowMaxY = new Qt3DRender::QParameter( u"shadowMaxY"_s, QVariant::fromValue( 0.0f ) );
96 mMaterial->addParameter( mShadowMinX );
97 mMaterial->addParameter( mShadowMaxX );
98 mMaterial->addParameter( mShadowMinY );
99 mMaterial->addParameter( mShadowMaxY );
100
101 mRenderShadowsParameter = new Qt3DRender::QParameter( u"renderShadows"_s, QVariant::fromValue( 0 ) );
102 mMaterial->addParameter( mRenderShadowsParameter );
103
104 mShadowBiasParameter = new Qt3DRender::QParameter( u"shadowBias"_s, QVariant::fromValue( 0.00001f ) );
105 mMaterial->addParameter( mShadowBiasParameter );
106
107 mEyeDomeLightingEnabledParameter = new Qt3DRender::QParameter( u"edlEnabled"_s, QVariant::fromValue( 0 ) );
108 mEyeDomeLightingStrengthParameter = new Qt3DRender::QParameter( u"edlStrength"_s, QVariant::fromValue( 1000.0f ) );
109 mEyeDomeLightingDistanceParameter = new Qt3DRender::QParameter( u"edlDistance"_s, QVariant::fromValue( 2.0f ) );
110 mMaterial->addParameter( mEyeDomeLightingEnabledParameter );
111 mMaterial->addParameter( mEyeDomeLightingStrengthParameter );
112 mMaterial->addParameter( mEyeDomeLightingDistanceParameter );
113
114 mAmbientOcclusionEnabledParameter = new Qt3DRender::QParameter( u"ssaoEnabled"_s, QVariant::fromValue( 0 ) );
115 mMaterial->addParameter( mAmbientOcclusionEnabledParameter );
116
117 mLightPosition = new Qt3DRender::QParameter( u"lightPosition"_s, QVariant::fromValue( QVector3D() ) );
118 mLightDirection = new Qt3DRender::QParameter( u"lightDirection"_s, QVariant::fromValue( QVector3D() ) );
119 mMaterial->addParameter( mLightPosition );
120 mMaterial->addParameter( mLightDirection );
121
122 const QString vertexShaderPath = u"qrc:/shaders/postprocess.vert"_s;
123 const QString fragmentShaderPath = u"qrc:/shaders/postprocess.frag"_s;
124
125 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
126 mShader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );
127}
128
129void QgsPostprocessingEntity::setupShadowRenderingExtent( float minX, float maxX, float minY, float maxY )
130{
131 mShadowMinX->setValue( minX );
132 mShadowMaxX->setValue( maxX );
133 mShadowMinY->setValue( minY );
134 mShadowMaxY->setValue( maxY );
135}
136
137void QgsPostprocessingEntity::setupDirectionalLight( QVector3D position, QVector3D direction )
138{
139 mLightPosition->setValue( QVariant::fromValue( position ) );
140 mLightDirection->setValue( QVariant::fromValue( direction.normalized() ) );
141}
142
143void QgsPostprocessingEntity::updateShadowSettings( const QgsDirectionalLightSettings &light, float maximumShadowRenderingDistance )
144{
145 float minX, maxX, minY, maxY, minZ, maxZ;
146 QVector3D lookingAt = mMainCamera->viewCenter();
147 const float d = 2 * ( mMainCamera->position() - mMainCamera->viewCenter() ).length();
148
149 const QVector3D lightDirection = light.direction().toVector3D().normalized();
150 Qgs3DUtils::calculateViewExtent( mMainCamera, maximumShadowRenderingDistance, lookingAt.z(), minX, maxX, minY, maxY, minZ, maxZ );
151
152 lookingAt = QVector3D( 0.5f * ( minX + maxX ), 0.5f * ( minY + maxY ), mMainCamera->viewCenter().z() );
153 const QVector3D lightPosition = lookingAt + QVector3D( 0.0f, 0.0f, d );
154 mLightCamera->setPosition( lightPosition );
155 mLightCamera->setViewCenter( lookingAt );
156 mLightCamera->setUpVector( QVector3D( 0.0f, 1.0f, 0.0f ) );
157 mLightCamera->rotateAboutViewCenter( QQuaternion::rotationTo( QVector3D( 0.0f, 0.0f, -1.0f ), lightDirection ) );
158
159 mLightCamera->setProjectionType( Qt3DRender::QCameraLens::ProjectionType::OrthographicProjection );
160 mLightCamera->lens()->setOrthographicProjection(
161 -0.7f * ( maxX - minX ), 0.7f * ( maxX - minX ),
162 -0.7f * ( maxY - minY ), 0.7f * ( maxY - minY ),
163 1.0f, 2 * ( lookingAt - lightPosition ).length()
164 );
165
166 setupShadowRenderingExtent( minX, maxX, minY, maxY );
167 setupDirectionalLight( lightPosition, lightDirection );
168}
169
171{
172 mRenderShadowsParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
173}
174
176{
177 mShadowBiasParameter->setValue( QVariant::fromValue( shadowBias ) );
178}
179
181{
182 mEyeDomeLightingEnabledParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
183}
184
186{
187 mEyeDomeLightingStrengthParameter->setValue( QVariant::fromValue( strength ) );
188}
189
191{
192 mEyeDomeLightingDistanceParameter->setValue( QVariant::fromValue( distance ) );
193}
194
196{
197 mAmbientOcclusionEnabledParameter->setValue( enabled );
198}
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.