QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#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 "qgsframegraph.h"
45
46QgsPostprocessingEntity::QgsPostprocessingEntity( QgsFrameGraph *frameGraph, Qt3DRender::QLayer *layer, QNode *parent )
47 : QgsRenderPassQuad( layer, parent )
48{
49 Q_UNUSED( frameGraph )
50 mColorTextureParameter = new Qt3DRender::QParameter( QStringLiteral( "colorTexture" ), frameGraph->forwardRenderColorTexture() );
51 mDepthTextureParameter = new Qt3DRender::QParameter( QStringLiteral( "depthTexture" ), frameGraph->forwardRenderDepthTexture() );
52 mShadowMapParameter = new Qt3DRender::QParameter( QStringLiteral( "shadowTexture" ), frameGraph->shadowMapTexture() );
53 mAmbientOcclusionTextureParameter = new Qt3DRender::QParameter( QStringLiteral( "ssaoTexture" ), frameGraph->blurredAmbientOcclusionFactorMap() );
54 mMaterial->addParameter( mColorTextureParameter );
55 mMaterial->addParameter( mDepthTextureParameter );
56 mMaterial->addParameter( mShadowMapParameter );
57 mMaterial->addParameter( mAmbientOcclusionTextureParameter );
58
59 mMainCamera = frameGraph->mainCamera();
60 mLightCamera = frameGraph->lightCamera();
61
62 mFarPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "farPlane" ), mMainCamera->farPlane() );
63 mMaterial->addParameter( mFarPlaneParameter );
64 connect( mMainCamera, &Qt3DRender::QCamera::farPlaneChanged, mFarPlaneParameter, [&]( float farPlane ) {
65 mFarPlaneParameter->setValue( farPlane );
66 } );
67 mNearPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "nearPlane" ), mMainCamera->nearPlane() );
68 mMaterial->addParameter( mNearPlaneParameter );
69 connect( mMainCamera, &Qt3DRender::QCamera::nearPlaneChanged, mNearPlaneParameter, [&]( float nearPlane ) {
70 mNearPlaneParameter->setValue( nearPlane );
71 } );
72
73 mLightFarPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "lightFarPlane" ), mLightCamera->farPlane() );
74 mMaterial->addParameter( mLightFarPlaneParameter );
75 connect( mLightCamera, &Qt3DRender::QCamera::farPlaneChanged, mLightFarPlaneParameter, [&]( float farPlane ) {
76 mLightFarPlaneParameter->setValue( farPlane );
77 } );
78 mLightNearPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "lightNearPlane" ), mLightCamera->nearPlane() );
79 mMaterial->addParameter( mLightNearPlaneParameter );
80 connect( mLightCamera, &Qt3DRender::QCamera::nearPlaneChanged, mLightNearPlaneParameter, [&]( float nearPlane ) {
81 mLightNearPlaneParameter->setValue( nearPlane );
82 } );
83
84 mMainCameraInvViewMatrixParameter = new Qt3DRender::QParameter( QStringLiteral( "invertedCameraView" ), mMainCamera->viewMatrix().inverted() );
85 mMaterial->addParameter( mMainCameraInvViewMatrixParameter );
86 mMainCameraInvProjMatrixParameter = new Qt3DRender::QParameter( QStringLiteral( "invertedCameraProj" ), mMainCamera->projectionMatrix().inverted() );
87 mMaterial->addParameter( mMainCameraInvProjMatrixParameter );
88 connect( mMainCamera, &Qt3DRender::QCamera::projectionMatrixChanged, mMainCameraInvProjMatrixParameter, [&]( const QMatrix4x4 &projectionMatrix ) {
89 mMainCameraInvProjMatrixParameter->setValue( projectionMatrix.inverted() );
90 } );
91 connect( mMainCamera, &Qt3DRender::QCamera::viewMatrixChanged, mMainCameraInvViewMatrixParameter, [&]() {
92 mMainCameraInvViewMatrixParameter->setValue( mMainCamera->viewMatrix().inverted() );
93 } );
94
95 mShadowMinX = new Qt3DRender::QParameter( QStringLiteral( "shadowMinX" ), QVariant::fromValue( 0.0f ) );
96 mShadowMaxX = new Qt3DRender::QParameter( QStringLiteral( "shadowMaxX" ), QVariant::fromValue( 0.0f ) );
97 mShadowMinY = new Qt3DRender::QParameter( QStringLiteral( "shadowMinY" ), QVariant::fromValue( 0.0f ) );
98 mShadowMaxY = new Qt3DRender::QParameter( QStringLiteral( "shadowMaxY" ), QVariant::fromValue( 0.0f ) );
99 mMaterial->addParameter( mShadowMinX );
100 mMaterial->addParameter( mShadowMaxX );
101 mMaterial->addParameter( mShadowMinY );
102 mMaterial->addParameter( mShadowMaxY );
103
104 mRenderShadowsParameter = new Qt3DRender::QParameter( QStringLiteral( "renderShadows" ), QVariant::fromValue( 0 ) );
105 mMaterial->addParameter( mRenderShadowsParameter );
106
107 mShadowBiasParameter = new Qt3DRender::QParameter( QStringLiteral( "shadowBias" ), QVariant::fromValue( 0.00001f ) );
108 mMaterial->addParameter( mShadowBiasParameter );
109
110 mEyeDomeLightingEnabledParameter = new Qt3DRender::QParameter( QStringLiteral( "edlEnabled" ), QVariant::fromValue( 0 ) );
111 mEyeDomeLightingStrengthParameter = new Qt3DRender::QParameter( QStringLiteral( "edlStrength" ), QVariant::fromValue( 1000.0f ) );
112 mEyeDomeLightingDistanceParameter = new Qt3DRender::QParameter( QStringLiteral( "edlDistance" ), QVariant::fromValue( 2.0f ) );
113 mMaterial->addParameter( mEyeDomeLightingEnabledParameter );
114 mMaterial->addParameter( mEyeDomeLightingStrengthParameter );
115 mMaterial->addParameter( mEyeDomeLightingDistanceParameter );
116
117 mAmbientOcclusionEnabledParameter = new Qt3DRender::QParameter( QStringLiteral( "ssaoEnabled" ), QVariant::fromValue( 0 ) );
118 mMaterial->addParameter( mAmbientOcclusionEnabledParameter );
119
120 mLightPosition = new Qt3DRender::QParameter( QStringLiteral( "lightPosition" ), QVariant::fromValue( QVector3D() ) );
121 mLightDirection = new Qt3DRender::QParameter( QStringLiteral( "lightDirection" ), QVariant::fromValue( QVector3D() ) );
122 mMaterial->addParameter( mLightPosition );
123 mMaterial->addParameter( mLightDirection );
124
125 const QString vertexShaderPath = QStringLiteral( "qrc:/shaders/postprocess.vert" );
126 const QString fragmentShaderPath = QStringLiteral( "qrc:/shaders/postprocess.frag" );
127
128 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
129 mShader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );
130}
131
132void QgsPostprocessingEntity::setupShadowRenderingExtent( float minX, float maxX, float minY, float maxY )
133{
134 mShadowMinX->setValue( minX );
135 mShadowMaxX->setValue( maxX );
136 mShadowMinY->setValue( minY );
137 mShadowMaxY->setValue( maxY );
138}
139
140void QgsPostprocessingEntity::setupDirectionalLight( QVector3D position, QVector3D direction )
141{
142 mLightPosition->setValue( QVariant::fromValue( position ) );
143 mLightDirection->setValue( QVariant::fromValue( direction.normalized() ) );
144}
145
147{
148 mRenderShadowsParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
149}
150
152{
153 mShadowBiasParameter->setValue( QVariant::fromValue( shadowBias ) );
154}
155
157{
158 mEyeDomeLightingEnabledParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
159}
160
162{
163 mEyeDomeLightingStrengthParameter->setValue( QVariant::fromValue( strength ) );
164}
165
167{
168 mEyeDomeLightingDistanceParameter->setValue( QVariant::fromValue( distance ) );
169}
170
172{
173 mAmbientOcclusionEnabledParameter->setValue( enabled );
174}
Qt3DRender::QTexture2D * shadowMapTexture()
Returns the shadow map (a depth texture for the shadow rendering pass)
Qt3DRender::QTexture2D * forwardRenderColorTexture()
Returns the color texture of the forward rendering pass.
Qt3DRender::QCamera * lightCamera()
Returns the light camera.
Qt3DRender::QTexture2D * blurredAmbientOcclusionFactorMap()
Returns blurred ambient occlusion factor values texture.
Qt3DRender::QTexture2D * forwardRenderDepthTexture()
Returns the depth texture of the forward rendering pass.
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 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.
Qt3DRender::QShaderProgram * mShader
Qt3DRender::QMaterial * mMaterial
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry