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>
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 };
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 );
41 vertexBuffer =
new Qt3DRender::QBuffer(
this );
43 vertexBuffer->setData( vertexArr );
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 );
54 geom->addAttribute( positionAttribute );
56 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer(
this );
57 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::PrimitiveType::Triangles );
58 renderer->setGeometry( geom );
60 addComponent( renderer );
62 mMaterial =
new Qt3DRender::QMaterial(
this );
65 mShadowMapParameter =
new Qt3DRender::QParameter(
"shadowTexture", frameGraph->
shadowMapTexture() );
66 mMaterial->addParameter( mColorTextureParameter );
67 mMaterial->addParameter( mDepthTextureParameter );
68 mMaterial->addParameter( mShadowMapParameter );
73 mFarPlaneParameter =
new Qt3DRender::QParameter(
"farPlane", mMainCamera->farPlane() );
74 mMaterial->addParameter( mFarPlaneParameter );
75 connect( mMainCamera, &Qt3DRender::QCamera::farPlaneChanged, [&](
float farPlane )
77 mFarPlaneParameter->setValue( farPlane );
79 mNearPlaneParameter =
new Qt3DRender::QParameter(
"nearPlane", mMainCamera->nearPlane() );
80 mMaterial->addParameter( mNearPlaneParameter );
81 connect( mMainCamera, &Qt3DRender::QCamera::nearPlaneChanged, [&](
float nearPlane )
83 mNearPlaneParameter->setValue( nearPlane );
86 mLightFarPlaneParameter =
new Qt3DRender::QParameter(
"lightFarPlane", mLightCamera->farPlane() );
87 mMaterial->addParameter( mLightFarPlaneParameter );
88 connect( mLightCamera, &Qt3DRender::QCamera::farPlaneChanged, [&](
float farPlane )
90 mLightFarPlaneParameter->setValue( farPlane );
92 mLightNearPlaneParameter =
new Qt3DRender::QParameter(
"lightNearPlane", mLightCamera->nearPlane() );
93 mMaterial->addParameter( mLightNearPlaneParameter );
94 connect( mLightCamera, &Qt3DRender::QCamera::nearPlaneChanged, [&](
float nearPlane )
96 mLightNearPlaneParameter->setValue( nearPlane );
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 )
105 mMainCameraInvProjMatrixParameter->setValue( projectionMatrix.inverted() );
107 connect( mMainCamera, &Qt3DRender::QCamera::viewMatrixChanged, [&]()
109 mMainCameraInvViewMatrixParameter->setValue( mMainCamera->viewMatrix().inverted() );
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 );
121 mRenderShadowsParameter =
new Qt3DRender::QParameter(
"renderShadows", QVariant::fromValue( 0 ) );
122 mMaterial->addParameter( mRenderShadowsParameter );
124 mShadowBiasParameter =
new Qt3DRender::QParameter(
"shadowBias", QVariant::fromValue( 0.00001f ) );
125 mMaterial->addParameter( mShadowBiasParameter );
127 mLightPosition =
new Qt3DRender::QParameter(
"lightPosition", QVariant::fromValue( QVector3D() ) );
128 mLightDirection =
new Qt3DRender::QParameter(
"lightDirection", QVariant::fromValue( QVector3D() ) );
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 );
140 QString vertexShaderPath = QStringLiteral(
"qrc:/shaders/postprocess.vert" );
141 QString fragmentShaderPath = QStringLiteral(
"qrc:/shaders/postprocess.frag" );
143 shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
144 shader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );
145 renderPass->setShaderProgram( shader );
147 Qt3DRender::QDepthTest *depthTest =
new Qt3DRender::QDepthTest(
this );
148 depthTest->setDepthFunction( Qt3DRender::QDepthTest::Always );
150 renderPass->addRenderState( depthTest );
152 technique->addRenderPass( renderPass );
154 mEffect->addTechnique( technique );
155 mMaterial->setEffect( mEffect );
157 addComponent( mMaterial );
162 mShadowMinX->setValue( minX );
163 mShadowMaxX->setValue( maxX );
164 mShadowMinZ->setValue( minZ );
165 mShadowMaxZ->setValue( maxZ );
170 mLightPosition->setValue( QVariant::fromValue( position ) );
171 mLightDirection->setValue( QVariant::fromValue( direction.normalized() ) );
176 mRenderShadowsParameter->setValue( QVariant::fromValue( enabled ? 1 : 0 ) );
181 mShadowBiasParameter->setValue( QVariant::fromValue( shadowBias ) );