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