QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 <Qt3DRender/QGeometry>
19 #include <Qt3DRender/QAttribute>
20 #include <Qt3DRender/QBuffer>
21 #include <Qt3DRender/QGeometryRenderer>
22 #include <Qt3DRender/QParameter>
23 #include <Qt3DRender/QTechnique>
24 #include <Qt3DRender/QGraphicsApiFilter>
25 #include <Qt3DRender/QDepthTest>
26 #include <QUrl>
27 
29 
31  : Qt3DCore::QEntity( parent )
32 {
33  Qt3DRender::QGeometry *geom = new Qt3DRender::QGeometry( this );
34  Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute( this );
35  QVector<float> vert = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f };
36 
37  QByteArray vertexArr( ( const char * ) vert.constData(), vert.size() * sizeof( float ) );
38  Qt3DRender::QBuffer *vertexBuffer = nullptr;
39 #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
40  vertexBuffer = new Qt3DRender::QBuffer( Qt3DRender::QBuffer::VertexBuffer, this );
41 #else
42  vertexBuffer = new Qt3DRender::QBuffer( this );
43 #endif
44  vertexBuffer->setData( vertexArr );
45 
46  positionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
47  positionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
48  positionAttribute->setVertexSize( 3 );
49  positionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
50  positionAttribute->setBuffer( vertexBuffer );
51  positionAttribute->setByteOffset( 0 );
52  positionAttribute->setByteStride( 3 * sizeof( float ) );
53  positionAttribute->setCount( 6 );
54 
55  geom->addAttribute( positionAttribute );
56 
57  Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer( this );
58  renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
59  renderer->setGeometry( geom );
60 
61  addComponent( renderer );
62 
63  mMaterial = new Qt3DRender::QMaterial( this );
64  mColorTextureParameter = new Qt3DRender::QParameter( "colorTexture", frameGraph->forwardRenderColorTexture() );
65  mDepthTextureParameter = new Qt3DRender::QParameter( "depthTexture", frameGraph->forwardRenderDepthTexture() );
66  mShadowMapParameter = new Qt3DRender::QParameter( "shadowTexture", frameGraph->shadowMapTexture() );
67  mMaterial->addParameter( mColorTextureParameter );
68  mMaterial->addParameter( mDepthTextureParameter );
69  mMaterial->addParameter( mShadowMapParameter );
70 
71  mMainCamera = frameGraph->mainCamera();
72  mLightCamera = frameGraph->lightCamera();
73 
74  mFarPlaneParameter = new Qt3DRender::QParameter( "farPlane", mMainCamera->farPlane() );
75  mMaterial->addParameter( mFarPlaneParameter );
76  connect( mMainCamera, &Qt3DRender::QCamera::farPlaneChanged, [&]( float farPlane )
77  {
78  mFarPlaneParameter->setValue( farPlane );
79  } );
80  mNearPlaneParameter = new Qt3DRender::QParameter( "nearPlane", mMainCamera->nearPlane() );
81  mMaterial->addParameter( mNearPlaneParameter );
82  connect( mMainCamera, &Qt3DRender::QCamera::nearPlaneChanged, [&]( float nearPlane )
83  {
84  mNearPlaneParameter->setValue( nearPlane );
85  } );
86 
87  mLightFarPlaneParameter = new Qt3DRender::QParameter( "lightFarPlane", mLightCamera->farPlane() );
88  mMaterial->addParameter( mLightFarPlaneParameter );
89  connect( mLightCamera, &Qt3DRender::QCamera::farPlaneChanged, [&]( float farPlane )
90  {
91  mLightFarPlaneParameter->setValue( farPlane );
92  } );
93  mLightNearPlaneParameter = new Qt3DRender::QParameter( "lightNearPlane", mLightCamera->nearPlane() );
94  mMaterial->addParameter( mLightNearPlaneParameter );
95  connect( mLightCamera, &Qt3DRender::QCamera::nearPlaneChanged, [&]( float nearPlane )
96  {
97  mLightNearPlaneParameter->setValue( nearPlane );
98  } );
99 
100  mMainCameraInvViewMatrixParameter = new Qt3DRender::QParameter( "invertedCameraView", mMainCamera->viewMatrix().inverted() );
101  mMaterial->addParameter( mMainCameraInvViewMatrixParameter );
102  mMainCameraInvProjMatrixParameter = new Qt3DRender::QParameter( "invertedCameraProj", mMainCamera->projectionMatrix().inverted() );
103  mMaterial->addParameter( mMainCameraInvProjMatrixParameter );
104  connect( mMainCamera, &Qt3DRender::QCamera::projectionMatrixChanged, [&]( const QMatrix4x4 & projectionMatrix )
105  {
106  mMainCameraInvProjMatrixParameter->setValue( projectionMatrix.inverted() );
107  } );
108  connect( mMainCamera, &Qt3DRender::QCamera::viewMatrixChanged, [&]()
109  {
110  mMainCameraInvViewMatrixParameter->setValue( mMainCamera->viewMatrix().inverted() );
111  } );
112 
113  mShadowMinX = new Qt3DRender::QParameter( "shadowMinX", QVariant::fromValue( 0.0f ) );
114  mShadowMaxX = new Qt3DRender::QParameter( "shadowMaxX", QVariant::fromValue( 0.0f ) );
115  mShadowMinZ = new Qt3DRender::QParameter( "shadowMinZ", QVariant::fromValue( 0.0f ) );
116  mShadowMaxZ = new Qt3DRender::QParameter( "shadowMaxZ", QVariant::fromValue( 0.0f ) );
117  mMaterial->addParameter( mShadowMinX );
118  mMaterial->addParameter( mShadowMaxX );
119  mMaterial->addParameter( mShadowMinZ );
120  mMaterial->addParameter( mShadowMaxZ );
121 
122  mRenderShadowsParameter = new Qt3DRender::QParameter( "renderShadows", QVariant::fromValue( 0 ) );
123  mMaterial->addParameter( mRenderShadowsParameter );
124 
125  mShadowBiasParameter = new Qt3DRender::QParameter( "shadowBias", QVariant::fromValue( 0.00001f ) );
126  mMaterial->addParameter( mShadowBiasParameter );
127 
128  mEyeDomeLightingEnabledParameter = new Qt3DRender::QParameter( "edlEnabled", QVariant::fromValue( 0 ) );
129  mEyeDomeLightingStrengthParameter = new Qt3DRender::QParameter( "edlStrength", QVariant::fromValue( 1000.0f ) );
130  mEyeDomeLightingDistanceParameter = new Qt3DRender::QParameter( "edlDistance", QVariant::fromValue( 2.0f ) );
131  mMaterial->addParameter( mEyeDomeLightingEnabledParameter );
132  mMaterial->addParameter( mEyeDomeLightingStrengthParameter );
133  mMaterial->addParameter( mEyeDomeLightingDistanceParameter );
134 
135  mLightPosition = new Qt3DRender::QParameter( "lightPosition", QVariant::fromValue( QVector3D() ) );
136  mLightDirection = new Qt3DRender::QParameter( "lightDirection", QVariant::fromValue( QVector3D() ) );
137  mMaterial->addParameter( mLightPosition );
138  mMaterial->addParameter( mLightDirection );
139 
140  mEffect = new Qt3DRender::QEffect( this );
141  Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique( this );
142  Qt3DRender::QGraphicsApiFilter *graphicsApiFilter = technique->graphicsApiFilter();
143  graphicsApiFilter->setApi( Qt3DRender::QGraphicsApiFilter::Api::OpenGL );
144  graphicsApiFilter->setProfile( Qt3DRender::QGraphicsApiFilter::OpenGLProfile::CoreProfile );
145  graphicsApiFilter->setMajorVersion( 1 );
146  graphicsApiFilter->setMinorVersion( 5 );
147  Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass( this );
148  Qt3DRender::QShaderProgram *shader = new Qt3DRender::QShaderProgram( this );
149 
150  QString vertexShaderPath = QStringLiteral( "qrc:/shaders/postprocess.vert" );
151  QString fragmentShaderPath = QStringLiteral( "qrc:/shaders/postprocess.frag" );
152 
153  shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
154  shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );
155  renderPass->setShaderProgram( shader );
156 
157  Qt3DRender::QDepthTest *depthTest = new Qt3DRender::QDepthTest( this );
158  depthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
159 
160  renderPass->addRenderState( depthTest );
161 
162  technique->addRenderPass( renderPass );
163 
164  mEffect->addTechnique( technique );
165  mMaterial->setEffect( mEffect );
166 
167  addComponent( mMaterial );
168 }
169 
170 void QgsPostprocessingEntity::setupShadowRenderingExtent( float minX, float maxX, float minZ, float maxZ )
171 {
172  mShadowMinX->setValue( minX );
173  mShadowMaxX->setValue( maxX );
174  mShadowMinZ->setValue( minZ );
175  mShadowMaxZ->setValue( maxZ );
176 }
177 
178 void QgsPostprocessingEntity::setupDirectionalLight( QVector3D position, QVector3D direction )
179 {
180  mLightPosition->setValue( QVariant::fromValue( position ) );
181  mLightDirection->setValue( QVariant::fromValue( direction.normalized() ) );
182 }
183 
185 {
186  mRenderShadowsParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
187 }
188 
190 {
191  mShadowBiasParameter->setValue( QVariant::fromValue( shadowBias ) );
192 }
193 
195 {
196  mEyeDomeLightingEnabledParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
197 }
198 
200 {
201  mEyeDomeLightingStrengthParameter->setValue( QVariant::fromValue( strength ) );
202 }
203 
205 {
206  mEyeDomeLightingDistanceParameter->setValue( QVariant::fromValue( distance ) );
207 }
void setupShadowRenderingExtent(float minX, float maxX, float minZ, float maxZ)
Sets the parts of the scene where objects cast shadows.
QgsPostprocessingEntity(QgsShadowRenderingFrameGraph *frameGraph, QNode *parent=nullptr)
Constructor.
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.
Qt3DRender::QTexture2D * forwardRenderDepthTexture()
Returns the depth texture of the forward rendering pass.
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::QCamera * mainCamera()
Returns the main camera.