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>
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 };
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 );
42 vertexBuffer =
new Qt3DRender::QBuffer(
this );
44 vertexBuffer->setData( vertexArr );
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 );
55 geom->addAttribute( positionAttribute );
57 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer(
this );
58 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
59 renderer->setGeometry( geom );
61 addComponent( renderer );
63 mMaterial =
new Qt3DRender::QMaterial(
this );
66 mShadowMapParameter =
new Qt3DRender::QParameter(
"shadowTexture", frameGraph->
shadowMapTexture() );
67 mMaterial->addParameter( mColorTextureParameter );
68 mMaterial->addParameter( mDepthTextureParameter );
69 mMaterial->addParameter( mShadowMapParameter );
74 mFarPlaneParameter =
new Qt3DRender::QParameter(
"farPlane", mMainCamera->farPlane() );
75 mMaterial->addParameter( mFarPlaneParameter );
76 connect( mMainCamera, &Qt3DRender::QCamera::farPlaneChanged, [&](
float farPlane )
78 mFarPlaneParameter->setValue( farPlane );
80 mNearPlaneParameter =
new Qt3DRender::QParameter(
"nearPlane", mMainCamera->nearPlane() );
81 mMaterial->addParameter( mNearPlaneParameter );
82 connect( mMainCamera, &Qt3DRender::QCamera::nearPlaneChanged, [&](
float nearPlane )
84 mNearPlaneParameter->setValue( nearPlane );
87 mLightFarPlaneParameter =
new Qt3DRender::QParameter(
"lightFarPlane", mLightCamera->farPlane() );
88 mMaterial->addParameter( mLightFarPlaneParameter );
89 connect( mLightCamera, &Qt3DRender::QCamera::farPlaneChanged, [&](
float farPlane )
91 mLightFarPlaneParameter->setValue( farPlane );
93 mLightNearPlaneParameter =
new Qt3DRender::QParameter(
"lightNearPlane", mLightCamera->nearPlane() );
94 mMaterial->addParameter( mLightNearPlaneParameter );
95 connect( mLightCamera, &Qt3DRender::QCamera::nearPlaneChanged, [&](
float nearPlane )
97 mLightNearPlaneParameter->setValue( nearPlane );
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 )
106 mMainCameraInvProjMatrixParameter->setValue( projectionMatrix.inverted() );
108 connect( mMainCamera, &Qt3DRender::QCamera::viewMatrixChanged, [&]()
110 mMainCameraInvViewMatrixParameter->setValue( mMainCamera->viewMatrix().inverted() );
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 );
122 mRenderShadowsParameter =
new Qt3DRender::QParameter(
"renderShadows", QVariant::fromValue( 0 ) );
123 mMaterial->addParameter( mRenderShadowsParameter );
125 mShadowBiasParameter =
new Qt3DRender::QParameter(
"shadowBias", QVariant::fromValue( 0.00001f ) );
126 mMaterial->addParameter( mShadowBiasParameter );
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 );
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 );
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 );
150 QString vertexShaderPath = QStringLiteral(
"qrc:/shaders/postprocess.vert" );
151 QString fragmentShaderPath = QStringLiteral(
"qrc:/shaders/postprocess.frag" );
153 shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
154 shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );
155 renderPass->setShaderProgram( shader );
157 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest(
this );
158 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
160 renderPass->addRenderState( depthTest );
162 technique->addRenderPass( renderPass );
164 mEffect->addTechnique( technique );
165 mMaterial->setEffect( mEffect );
167 addComponent( mMaterial );
172 mShadowMinX->setValue( minX );
173 mShadowMaxX->setValue( maxX );
174 mShadowMinZ->setValue( minZ );
175 mShadowMaxZ->setValue( maxZ );
180 mLightPosition->setValue( QVariant::fromValue( position ) );
181 mLightDirection->setValue( QVariant::fromValue( direction.normalized() ) );
186 mRenderShadowsParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
191 mShadowBiasParameter->setValue( QVariant::fromValue( shadowBias ) );
196 mEyeDomeLightingEnabledParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
201 mEyeDomeLightingStrengthParameter->setValue( QVariant::fromValue( strength ) );
206 mEyeDomeLightingDistanceParameter->setValue( QVariant::fromValue( distance ) );
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.