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 ) {
32 mFarPlaneParameter->setValue( farPlane );
34 mNearPlaneParameter =
new Qt3DRender::QParameter( QStringLiteral(
"nearPlane" ), camera->nearPlane() );
35 mMaterial->addParameter( mNearPlaneParameter );
36 connect( camera, &Qt3DRender::QCamera::nearPlaneChanged, mNearPlaneParameter, [&](
float nearPlane ) {
37 mNearPlaneParameter->setValue( nearPlane );
39 mProjMatrixParameter =
new Qt3DRender::QParameter( QStringLiteral(
"origProjMatrix" ), camera->projectionMatrix() );
40 mMaterial->addParameter( mProjMatrixParameter );
41 connect( camera, &Qt3DRender::QCamera::projectionMatrixChanged, mProjMatrixParameter, [&](
const QMatrix4x4 &projectionMatrix ) {
42 mProjMatrixParameter->setValue( projectionMatrix );
44 mAspectRatioParameter =
new Qt3DRender::QParameter( QStringLiteral(
"uAspectRatio" ), camera->aspectRatio() );
45 mMaterial->addParameter( mAspectRatioParameter );
46 connect( camera, &Qt3DRender::QCamera::aspectRatioChanged, mAspectRatioParameter, [&](
float ratio ) {
47 mAspectRatioParameter->setValue( ratio );
49 mTanHalfFovParameter =
new Qt3DRender::QParameter( QStringLiteral(
"uTanHalfFov" ), tan( camera->fieldOfView() / 2 * M_PI / 180 ) );
50 mMaterial->addParameter( mTanHalfFovParameter );
51 connect( camera, &Qt3DRender::QCamera::fieldOfViewChanged, mTanHalfFovParameter, [&](
float fov ) {
52 mTanHalfFovParameter->setValue( tan( fov / 2 * M_PI / 180 ) );
55 QVariantList ssaoKernelValues;
57 std::uniform_real_distribution<float> randomFloats( 0.0, 1.0 );
58 std::default_random_engine generator;
59 unsigned int kernelSize = 64;
60 for (
unsigned int i = 0; i < kernelSize; ++i )
63 randomFloats( generator ) * 2.0 - 1.0,
64 randomFloats( generator ) * 2.0 - 1.0,
65 randomFloats( generator ) * 2.0 - 1.0
68 float scale =
static_cast<float>( i ) /
static_cast<float>( kernelSize );
69 scale = 0.1 + 0.9 * scale * scale;
71 ssaoKernelValues.push_back( sample );
75 QVariantList ssaoNoise;
76 for (
unsigned int i = 0; i < 16; ++i )
79 randomFloats( generator ),
80 randomFloats( generator ),
83 ssaoNoise.push_back( sample );
85 mAmbientOcclusionKernelParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ssaoKernel[0]" ), ssaoKernelValues );
86 mMaterial->addParameter( mAmbientOcclusionKernelParameter );
88 Qt3DRender::QParameter *noiseParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ssaoNoise[0]" ), ssaoNoise );
89 mMaterial->addParameter( noiseParameter );
91 mIntensityParameter =
new Qt3DRender::QParameter( QStringLiteral(
"intensity" ), 0.5f );
92 mMaterial->addParameter( mIntensityParameter );
94 mRadiusParameter =
new Qt3DRender::QParameter( QStringLiteral(
"radius" ), 25.0f );
95 mMaterial->addParameter( mRadiusParameter );
97 mThresholdParameter =
new Qt3DRender::QParameter( QStringLiteral(
"threshold" ), 0.5f );
98 mMaterial->addParameter( mThresholdParameter );
100 const QString vertexShaderPath = QStringLiteral(
"qrc:/shaders/ssao_factor_render.vert" );
101 const QString fragmentShaderPath = QStringLiteral(
"qrc:/shaders/ssao_factor_render.frag" );
103 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
104 mShader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );