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