QGIS API Documentation 3.39.0-Master (8f4da32b6b8)
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#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
19#include <Qt3DRender/QAttribute>
20#include <Qt3DRender/QBuffer>
21#include <Qt3DRender/QGeometry>
22
23typedef Qt3DRender::QAttribute Qt3DQAttribute;
24typedef Qt3DRender::QBuffer Qt3DQBuffer;
25typedef Qt3DRender::QGeometry Qt3DQGeometry;
26#else
27#include <Qt3DCore/QAttribute>
28#include <Qt3DCore/QBuffer>
29#include <Qt3DCore/QGeometry>
30
31typedef Qt3DCore::QAttribute Qt3DQAttribute;
32typedef Qt3DCore::QBuffer Qt3DQBuffer;
33typedef Qt3DCore::QGeometry Qt3DQGeometry;
34#endif
35
36#include <Qt3DRender/QGeometryRenderer>
37#include <Qt3DRender/QParameter>
38#include <Qt3DRender/QTechnique>
39#include <Qt3DRender/QGraphicsApiFilter>
40#include <Qt3DRender/QDepthTest>
41#include <QUrl>
42
43#include "qgsframegraph.h"
44
45QgsPostprocessingEntity::QgsPostprocessingEntity( QgsFrameGraph *frameGraph, Qt3DRender::QLayer *layer, QNode *parent )
46 : QgsRenderPassQuad( layer, parent )
47 , mFrameGraph( frameGraph )
48{
49 mColorTextureParameter = new Qt3DRender::QParameter( QStringLiteral( "colorTexture" ), frameGraph->forwardRenderColorTexture() );
50 mDepthTextureParameter = new Qt3DRender::QParameter( QStringLiteral( "depthTexture" ), frameGraph->forwardRenderDepthTexture() );
51 mShadowMapParameter = new Qt3DRender::QParameter( QStringLiteral( "shadowTexture" ), frameGraph->shadowMapTexture() );
52 mAmbientOcclusionTextureParameter = new Qt3DRender::QParameter( QStringLiteral( "ssaoTexture" ), frameGraph->blurredAmbientOcclusionFactorMap() );
53 mMaterial->addParameter( mColorTextureParameter );
54 mMaterial->addParameter( mDepthTextureParameter );
55 mMaterial->addParameter( mShadowMapParameter );
56 mMaterial->addParameter( mAmbientOcclusionTextureParameter );
57
58 mMainCamera = frameGraph->mainCamera();
59 mLightCamera = frameGraph->lightCamera();
60
61 mFarPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "farPlane" ), mMainCamera->farPlane() );
62 mMaterial->addParameter( mFarPlaneParameter );
63 connect( mMainCamera, &Qt3DRender::QCamera::farPlaneChanged, mFarPlaneParameter, [&]( float farPlane )
64 {
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 {
71 mNearPlaneParameter->setValue( nearPlane );
72 } );
73
74 mLightFarPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "lightFarPlane" ), mLightCamera->farPlane() );
75 mMaterial->addParameter( mLightFarPlaneParameter );
76 connect( mLightCamera, &Qt3DRender::QCamera::farPlaneChanged, mLightFarPlaneParameter, [&]( float farPlane )
77 {
78 mLightFarPlaneParameter->setValue( farPlane );
79 } );
80 mLightNearPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "lightNearPlane" ), mLightCamera->nearPlane() );
81 mMaterial->addParameter( mLightNearPlaneParameter );
82 connect( mLightCamera, &Qt3DRender::QCamera::nearPlaneChanged, mLightNearPlaneParameter, [&]( float nearPlane )
83 {
84 mLightNearPlaneParameter->setValue( nearPlane );
85 } );
86
87 mMainCameraInvViewMatrixParameter = new Qt3DRender::QParameter( QStringLiteral( "invertedCameraView" ), mMainCamera->viewMatrix().inverted() );
88 mMaterial->addParameter( mMainCameraInvViewMatrixParameter );
89 mMainCameraInvProjMatrixParameter = new Qt3DRender::QParameter( QStringLiteral( "invertedCameraProj" ), mMainCamera->projectionMatrix().inverted() );
90 mMaterial->addParameter( mMainCameraInvProjMatrixParameter );
91 connect( mMainCamera, &Qt3DRender::QCamera::projectionMatrixChanged, mMainCameraInvProjMatrixParameter, [&]( const QMatrix4x4 & projectionMatrix )
92 {
93 mMainCameraInvProjMatrixParameter->setValue( projectionMatrix.inverted() );
94 } );
95 connect( mMainCamera, &Qt3DRender::QCamera::viewMatrixChanged, mMainCameraInvViewMatrixParameter, [&]()
96 {
97 mMainCameraInvViewMatrixParameter->setValue( mMainCamera->viewMatrix().inverted() );
98 } );
99
100 mShadowMinX = new Qt3DRender::QParameter( QStringLiteral( "shadowMinX" ), QVariant::fromValue( 0.0f ) );
101 mShadowMaxX = new Qt3DRender::QParameter( QStringLiteral( "shadowMaxX" ), QVariant::fromValue( 0.0f ) );
102 mShadowMinZ = new Qt3DRender::QParameter( QStringLiteral( "shadowMinZ" ), QVariant::fromValue( 0.0f ) );
103 mShadowMaxZ = new Qt3DRender::QParameter( QStringLiteral( "shadowMaxZ" ), QVariant::fromValue( 0.0f ) );
104 mMaterial->addParameter( mShadowMinX );
105 mMaterial->addParameter( mShadowMaxX );
106 mMaterial->addParameter( mShadowMinZ );
107 mMaterial->addParameter( mShadowMaxZ );
108
109 mRenderShadowsParameter = new Qt3DRender::QParameter( QStringLiteral( "renderShadows" ), QVariant::fromValue( 0 ) );
110 mMaterial->addParameter( mRenderShadowsParameter );
111
112 mShadowBiasParameter = new Qt3DRender::QParameter( QStringLiteral( "shadowBias" ), QVariant::fromValue( 0.00001f ) );
113 mMaterial->addParameter( mShadowBiasParameter );
114
115 mEyeDomeLightingEnabledParameter = new Qt3DRender::QParameter( QStringLiteral( "edlEnabled" ), QVariant::fromValue( 0 ) );
116 mEyeDomeLightingStrengthParameter = new Qt3DRender::QParameter( QStringLiteral( "edlStrength" ), QVariant::fromValue( 1000.0f ) );
117 mEyeDomeLightingDistanceParameter = new Qt3DRender::QParameter( QStringLiteral( "edlDistance" ), QVariant::fromValue( 2.0f ) );
118 mMaterial->addParameter( mEyeDomeLightingEnabledParameter );
119 mMaterial->addParameter( mEyeDomeLightingStrengthParameter );
120 mMaterial->addParameter( mEyeDomeLightingDistanceParameter );
121
122 mAmbientOcclusionEnabledParameter = new Qt3DRender::QParameter( QStringLiteral( "ssaoEnabled" ), QVariant::fromValue( 0 ) );
123 mMaterial->addParameter( mAmbientOcclusionEnabledParameter );
124
125 mLightPosition = new Qt3DRender::QParameter( QStringLiteral( "lightPosition" ), QVariant::fromValue( QVector3D() ) );
126 mLightDirection = new Qt3DRender::QParameter( QStringLiteral( "lightDirection" ), QVariant::fromValue( QVector3D() ) );
127 mMaterial->addParameter( mLightPosition );
128 mMaterial->addParameter( mLightDirection );
129
130 const QString vertexShaderPath = QStringLiteral( "qrc:/shaders/postprocess.vert" );
131 const QString fragmentShaderPath = QStringLiteral( "qrc:/shaders/postprocess.frag" );
132
133 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
134 mShader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );
135}
136
137void QgsPostprocessingEntity::setupShadowRenderingExtent( float minX, float maxX, float minZ, float maxZ )
138{
139 mShadowMinX->setValue( minX );
140 mShadowMaxX->setValue( maxX );
141 mShadowMinZ->setValue( minZ );
142 mShadowMaxZ->setValue( maxZ );
143}
144
145void QgsPostprocessingEntity::setupDirectionalLight( QVector3D position, QVector3D direction )
146{
147 mLightPosition->setValue( QVariant::fromValue( position ) );
148 mLightDirection->setValue( QVariant::fromValue( direction.normalized() ) );
149}
150
152{
153 mRenderShadowsParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
154}
155
157{
158 mShadowBiasParameter->setValue( QVariant::fromValue( shadowBias ) );
159}
160
162{
163 mEyeDomeLightingEnabledParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
164}
165
167{
168 mEyeDomeLightingStrengthParameter->setValue( QVariant::fromValue( strength ) );
169}
170
172{
173 mEyeDomeLightingDistanceParameter->setValue( QVariant::fromValue( distance ) );
174}
175
177{
178 mAmbientOcclusionEnabledParameter->setValue( enabled );
179}
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 minZ, float maxZ)
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