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 );
60 mColorTextureParameter =
new Qt3DRender::QParameter( QStringLiteral(
"colorTexture" ), frameGraph->
forwardRenderColorTexture() );
61 mDepthTextureParameter =
new Qt3DRender::QParameter( QStringLiteral(
"depthTexture" ), frameGraph->
forwardRenderDepthTexture() );
62 mShadowMapParameter =
new Qt3DRender::QParameter( QStringLiteral(
"shadowTexture" ), frameGraph->
shadowMapTexture() );
63 mMaterial->addParameter( mColorTextureParameter );
64 mMaterial->addParameter( mDepthTextureParameter );
65 mMaterial->addParameter( mShadowMapParameter );
70 mFarPlaneParameter =
new Qt3DRender::QParameter( QStringLiteral(
"farPlane" ), mMainCamera->farPlane() );
71 mMaterial->addParameter( mFarPlaneParameter );
72 connect( mMainCamera, &Qt3DRender::QCamera::farPlaneChanged, mFarPlaneParameter, [&](
float farPlane )
74 mFarPlaneParameter->setValue( farPlane );
76 mNearPlaneParameter =
new Qt3DRender::QParameter( QStringLiteral(
"nearPlane" ), mMainCamera->nearPlane() );
77 mMaterial->addParameter( mNearPlaneParameter );
78 connect( mMainCamera, &Qt3DRender::QCamera::nearPlaneChanged, mNearPlaneParameter, [&](
float nearPlane )
80 mNearPlaneParameter->setValue( nearPlane );
83 mLightFarPlaneParameter =
new Qt3DRender::QParameter( QStringLiteral(
"lightFarPlane" ), mLightCamera->farPlane() );
84 mMaterial->addParameter( mLightFarPlaneParameter );
85 connect( mLightCamera, &Qt3DRender::QCamera::farPlaneChanged, mLightFarPlaneParameter, [&](
float farPlane )
87 mLightFarPlaneParameter->setValue( farPlane );
89 mLightNearPlaneParameter =
new Qt3DRender::QParameter( QStringLiteral(
"lightNearPlane" ), mLightCamera->nearPlane() );
90 mMaterial->addParameter( mLightNearPlaneParameter );
91 connect( mLightCamera, &Qt3DRender::QCamera::nearPlaneChanged, mLightNearPlaneParameter, [&](
float nearPlane )
93 mLightNearPlaneParameter->setValue( nearPlane );
96 mMainCameraInvViewMatrixParameter =
new Qt3DRender::QParameter( QStringLiteral(
"invertedCameraView" ), mMainCamera->viewMatrix().inverted() );
97 mMaterial->addParameter( mMainCameraInvViewMatrixParameter );
98 mMainCameraInvProjMatrixParameter =
new Qt3DRender::QParameter( QStringLiteral(
"invertedCameraProj" ), mMainCamera->projectionMatrix().inverted() );
99 mMaterial->addParameter( mMainCameraInvProjMatrixParameter );
100 connect( mMainCamera, &Qt3DRender::QCamera::projectionMatrixChanged, mMainCameraInvProjMatrixParameter, [&](
const QMatrix4x4 & projectionMatrix )
102 mMainCameraInvProjMatrixParameter->setValue( projectionMatrix.inverted() );
104 connect( mMainCamera, &Qt3DRender::QCamera::viewMatrixChanged, mMainCameraInvViewMatrixParameter, [&]()
106 mMainCameraInvViewMatrixParameter->setValue( mMainCamera->viewMatrix().inverted() );
109 mShadowMinX =
new Qt3DRender::QParameter( QStringLiteral(
"shadowMinX" ), QVariant::fromValue( 0.0f ) );
110 mShadowMaxX =
new Qt3DRender::QParameter( QStringLiteral(
"shadowMaxX" ), QVariant::fromValue( 0.0f ) );
111 mShadowMinZ =
new Qt3DRender::QParameter( QStringLiteral(
"shadowMinZ" ), QVariant::fromValue( 0.0f ) );
112 mShadowMaxZ =
new Qt3DRender::QParameter( QStringLiteral(
"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( QStringLiteral(
"renderShadows" ), QVariant::fromValue( 0 ) );
119 mMaterial->addParameter( mRenderShadowsParameter );
121 mShadowBiasParameter =
new Qt3DRender::QParameter( QStringLiteral(
"shadowBias" ), QVariant::fromValue( 0.00001f ) );
122 mMaterial->addParameter( mShadowBiasParameter );
124 mEyeDomeLightingEnabledParameter =
new Qt3DRender::QParameter( QStringLiteral(
"edlEnabled" ), QVariant::fromValue( 0 ) );
125 mEyeDomeLightingStrengthParameter =
new Qt3DRender::QParameter( QStringLiteral(
"edlStrength" ), QVariant::fromValue( 1000.0f ) );
126 mEyeDomeLightingDistanceParameter =
new Qt3DRender::QParameter( QStringLiteral(
"edlDistance" ), QVariant::fromValue( 2.0f ) );
127 mMaterial->addParameter( mEyeDomeLightingEnabledParameter );
128 mMaterial->addParameter( mEyeDomeLightingStrengthParameter );
129 mMaterial->addParameter( mEyeDomeLightingDistanceParameter );
131 mLightPosition =
new Qt3DRender::QParameter( QStringLiteral(
"lightPosition" ), QVariant::fromValue( QVector3D() ) );
132 mLightDirection =
new Qt3DRender::QParameter( QStringLiteral(
"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 ) );