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 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 };
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 );
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 );
51 geom->addAttribute( positionAttribute );
53 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer(
this );
54 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
55 renderer->setGeometry( geom );
57 addComponent( renderer );
59 mMaterial =
new Qt3DRender::QMaterial(
this );
62 mShadowMapParameter =
new Qt3DRender::QParameter(
"shadowTexture", frameGraph->
shadowMapTexture() );
63 mMaterial->addParameter( mColorTextureParameter );
64 mMaterial->addParameter( mDepthTextureParameter );
65 mMaterial->addParameter( mShadowMapParameter );
70 mFarPlaneParameter =
new Qt3DRender::QParameter(
"farPlane", mMainCamera->farPlane() );
71 mMaterial->addParameter( mFarPlaneParameter );
72 connect( mMainCamera, &Qt3DRender::QCamera::farPlaneChanged, [&](
float farPlane )
74 mFarPlaneParameter->setValue( farPlane );
76 mNearPlaneParameter =
new Qt3DRender::QParameter(
"nearPlane", mMainCamera->nearPlane() );
77 mMaterial->addParameter( mNearPlaneParameter );
78 connect( mMainCamera, &Qt3DRender::QCamera::nearPlaneChanged, [&](
float nearPlane )
80 mNearPlaneParameter->setValue( nearPlane );
83 mLightFarPlaneParameter =
new Qt3DRender::QParameter(
"lightFarPlane", mLightCamera->farPlane() );
84 mMaterial->addParameter( mLightFarPlaneParameter );
85 connect( mLightCamera, &Qt3DRender::QCamera::farPlaneChanged, [&](
float farPlane )
87 mLightFarPlaneParameter->setValue( farPlane );
89 mLightNearPlaneParameter =
new Qt3DRender::QParameter(
"lightNearPlane", mLightCamera->nearPlane() );
90 mMaterial->addParameter( mLightNearPlaneParameter );
91 connect( mLightCamera, &Qt3DRender::QCamera::nearPlaneChanged, [&](
float nearPlane )
93 mLightNearPlaneParameter->setValue( nearPlane );
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 )
102 mMainCameraInvProjMatrixParameter->setValue( projectionMatrix.inverted() );
104 connect( mMainCamera, &Qt3DRender::QCamera::viewMatrixChanged, [&]()
106 mMainCameraInvViewMatrixParameter->setValue( mMainCamera->viewMatrix().inverted() );
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 );
118 mRenderShadowsParameter =
new Qt3DRender::QParameter(
"renderShadows", QVariant::fromValue( 0 ) );
119 mMaterial->addParameter( mRenderShadowsParameter );
121 mShadowBiasParameter =
new Qt3DRender::QParameter(
"shadowBias", QVariant::fromValue( 0.00001f ) );
122 mMaterial->addParameter( mShadowBiasParameter );
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 );
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 );
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 );
146 const QString vertexShaderPath = QStringLiteral(
"qrc:/shaders/postprocess.vert" );
147 const QString fragmentShaderPath = QStringLiteral(
"qrc:/shaders/postprocess.frag" );
149 shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
150 shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );
151 renderPass->setShaderProgram( shader );
153 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest(
this );
154 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
156 renderPass->addRenderState( depthTest );
158 technique->addRenderPass( renderPass );
160 mEffect->addTechnique( technique );
161 mMaterial->setEffect( mEffect );
163 addComponent( mMaterial );
168 mShadowMinX->setValue( minX );
169 mShadowMaxX->setValue( maxX );
170 mShadowMinZ->setValue( minZ );
171 mShadowMaxZ->setValue( maxZ );
176 mLightPosition->setValue( QVariant::fromValue( position ) );
177 mLightDirection->setValue( QVariant::fromValue( direction.normalized() ) );
182 mRenderShadowsParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
187 mShadowBiasParameter->setValue( QVariant::fromValue( shadowBias ) );
192 mEyeDomeLightingEnabledParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
197 mEyeDomeLightingStrengthParameter->setValue( QVariant::fromValue( strength ) );
202 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.