26 mDepthTextureParameter =
new Qt3DRender::QParameter( QStringLiteral(
"depthTexture" ), depthTexture );
27 mMaterial->addParameter( mDepthTextureParameter );
29 mFarPlaneParameter =
new Qt3DRender::QParameter( QStringLiteral(
"farPlane" ), camera->farPlane() );
30 mMaterial->addParameter( mFarPlaneParameter );
31 connect( camera, &Qt3DRender::QCamera::farPlaneChanged, mFarPlaneParameter, [&](
float farPlane )
33 mFarPlaneParameter->setValue( farPlane );
35 mNearPlaneParameter =
new Qt3DRender::QParameter( QStringLiteral(
"nearPlane" ), camera->nearPlane() );
36 mMaterial->addParameter( mNearPlaneParameter );
37 connect( camera, &Qt3DRender::QCamera::nearPlaneChanged, mNearPlaneParameter, [&](
float nearPlane )
39 mNearPlaneParameter->setValue( nearPlane );
41 mProjMatrixParameter =
new Qt3DRender::QParameter( QStringLiteral(
"origProjMatrix" ), camera->projectionMatrix() );
42 mMaterial->addParameter( mProjMatrixParameter );
43 connect( camera, &Qt3DRender::QCamera::projectionMatrixChanged, mProjMatrixParameter, [&](
const QMatrix4x4 & projectionMatrix )
45 mProjMatrixParameter->setValue( projectionMatrix );
47 mAspectRatioParameter =
new Qt3DRender::QParameter( QStringLiteral(
"uAspectRatio" ), camera->aspectRatio() );
48 mMaterial->addParameter( mAspectRatioParameter );
49 connect( camera, &Qt3DRender::QCamera::aspectRatioChanged, mAspectRatioParameter, [&](
float ratio )
51 mAspectRatioParameter->setValue( ratio );
53 mTanHalfFovParameter =
new Qt3DRender::QParameter( QStringLiteral(
"uTanHalfFov" ), tan( camera->fieldOfView() / 2 * M_PI / 180 ) );
54 mMaterial->addParameter( mTanHalfFovParameter );
55 connect( camera, &Qt3DRender::QCamera::fieldOfViewChanged, mTanHalfFovParameter, [&](
float fov )
57 mTanHalfFovParameter->setValue( tan( fov / 2 * M_PI / 180 ) );
60 QVariantList ssaoKernelValues;
62 std::uniform_real_distribution<float> randomFloats( 0.0, 1.0 );
63 std::default_random_engine generator;
64 unsigned int kernelSize = 64;
65 for (
unsigned int i = 0; i < kernelSize; ++i )
68 randomFloats( generator ) * 2.0 - 1.0,
69 randomFloats( generator ) * 2.0 - 1.0,
70 randomFloats( generator ) * 2.0 - 1.0
73 float scale =
static_cast< float >( i ) /
static_cast< float >( kernelSize );
74 scale = 0.1 + 0.9 * scale * scale;
76 ssaoKernelValues.push_back( sample );
80 QVariantList ssaoNoise;
81 for (
unsigned int i = 0; i < 16; ++i )
84 randomFloats( generator ),
85 randomFloats( generator ),
88 ssaoNoise.push_back( sample );
90 mAmbientOcclusionKernelParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ssaoKernel[0]" ), ssaoKernelValues );
91 mMaterial->addParameter( mAmbientOcclusionKernelParameter );
93 Qt3DRender::QParameter *noiseParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ssaoNoise[0]" ), ssaoNoise );
94 mMaterial->addParameter( noiseParameter );
96 mIntensityParameter =
new Qt3DRender::QParameter( QStringLiteral(
"intensity" ), 0.5f );
97 mMaterial->addParameter( mIntensityParameter );
99 mRadiusParameter =
new Qt3DRender::QParameter( QStringLiteral(
"radius" ), 25.0f );
100 mMaterial->addParameter( mRadiusParameter );
102 mThresholdParameter =
new Qt3DRender::QParameter( QStringLiteral(
"threshold" ), 0.5f );
103 mMaterial->addParameter( mThresholdParameter );
105 const QString vertexShaderPath = QStringLiteral(
"qrc:/shaders/ssao_factor_render.vert" );
106 const QString fragmentShaderPath = QStringLiteral(
"qrc:/shaders/ssao_factor_render.frag" );
108 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
109 mShader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );