QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsambientocclusionrenderentity.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsambientocclusionrenderentity.cpp
3 --------------------------------------
4 Date : June 2022
5 Copyright : (C) 2022 by Belgacem Nedjima
6 Email : belgacem dot nedjima at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsambientocclusionrenderentity.cpp"
18
19#include <random>
20
21#include <Qt3DRender/QParameter>
22
23QgsAmbientOcclusionRenderEntity::QgsAmbientOcclusionRenderEntity( Qt3DRender::QTexture2D *depthTexture, Qt3DRender::QLayer *layer, Qt3DRender::QCamera *camera, QNode *parent )
24 : QgsRenderPassQuad( layer, parent )
25{
26 mDepthTextureParameter = new Qt3DRender::QParameter( QStringLiteral( "depthTexture" ), depthTexture );
27 mMaterial->addParameter( mDepthTextureParameter );
28
29 mFarPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "farPlane" ), camera->farPlane() );
30 mMaterial->addParameter( mFarPlaneParameter );
31 connect( camera, &Qt3DRender::QCamera::farPlaneChanged, mFarPlaneParameter, [&]( float farPlane )
32 {
33 mFarPlaneParameter->setValue( farPlane );
34 } );
35 mNearPlaneParameter = new Qt3DRender::QParameter( QStringLiteral( "nearPlane" ), camera->nearPlane() );
36 mMaterial->addParameter( mNearPlaneParameter );
37 connect( camera, &Qt3DRender::QCamera::nearPlaneChanged, mNearPlaneParameter, [&]( float nearPlane )
38 {
39 mNearPlaneParameter->setValue( nearPlane );
40 } );
41 mProjMatrixParameter = new Qt3DRender::QParameter( QStringLiteral( "origProjMatrix" ), camera->projectionMatrix() );
42 mMaterial->addParameter( mProjMatrixParameter );
43 connect( camera, &Qt3DRender::QCamera::projectionMatrixChanged, mProjMatrixParameter, [&]( const QMatrix4x4 & projectionMatrix )
44 {
45 mProjMatrixParameter->setValue( projectionMatrix );
46 } );
47 mAspectRatioParameter = new Qt3DRender::QParameter( QStringLiteral( "uAspectRatio" ), camera->aspectRatio() );
48 mMaterial->addParameter( mAspectRatioParameter );
49 connect( camera, &Qt3DRender::QCamera::aspectRatioChanged, mAspectRatioParameter, [&]( float ratio )
50 {
51 mAspectRatioParameter->setValue( ratio );
52 } );
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 )
56 {
57 mTanHalfFovParameter->setValue( tan( fov / 2 * M_PI / 180 ) );
58 } );
59
60 QVariantList ssaoKernelValues;
61
62 std::uniform_real_distribution<float> randomFloats( 0.0, 1.0 ); // random floats between [0.0, 1.0]
63 std::default_random_engine generator;
64 unsigned int kernelSize = 64;
65 for ( unsigned int i = 0; i < kernelSize; ++i )
66 {
67 QVector3D sample(
68 randomFloats( generator ) * 2.0 - 1.0,
69 randomFloats( generator ) * 2.0 - 1.0,
70 randomFloats( generator ) * 2.0 - 1.0
71 );
72 sample.normalize();
73 float scale = static_cast< float >( i ) / static_cast< float >( kernelSize );
74 scale = 0.1 + 0.9 * scale * scale;
75 sample *= scale;
76 ssaoKernelValues.push_back( sample );
77 }
78
79 // 4x4 array of random rotation vectors
80 QVariantList ssaoNoise;
81 for ( unsigned int i = 0; i < 16; ++i )
82 {
83 QVector3D sample(
84 randomFloats( generator ),
85 randomFloats( generator ),
86 0.0
87 );
88 ssaoNoise.push_back( sample );
89 }
90 mAmbientOcclusionKernelParameter = new Qt3DRender::QParameter( QStringLiteral( "ssaoKernel[0]" ), ssaoKernelValues );
91 mMaterial->addParameter( mAmbientOcclusionKernelParameter );
92
93 Qt3DRender::QParameter *noiseParameter = new Qt3DRender::QParameter( QStringLiteral( "ssaoNoise[0]" ), ssaoNoise );
94 mMaterial->addParameter( noiseParameter );
95
96 mIntensityParameter = new Qt3DRender::QParameter( QStringLiteral( "intensity" ), 0.5f );
97 mMaterial->addParameter( mIntensityParameter );
98
99 mRadiusParameter = new Qt3DRender::QParameter( QStringLiteral( "radius" ), 25.0f );
100 mMaterial->addParameter( mRadiusParameter );
101
102 mThresholdParameter = new Qt3DRender::QParameter( QStringLiteral( "threshold" ), 0.5f );
103 mMaterial->addParameter( mThresholdParameter );
104
105 const QString vertexShaderPath = QStringLiteral( "qrc:/shaders/ssao_factor_render.vert" );
106 const QString fragmentShaderPath = QStringLiteral( "qrc:/shaders/ssao_factor_render.frag" );
107
108 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
109 mShader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );
110}
111
113{
114 mIntensityParameter->setValue( intensity );
115}
116
118{
119 mRadiusParameter->setValue( radius );
120}
121
123{
124 mThresholdParameter->setValue( threshold );
125}
void setThreshold(float threshold)
Sets the amount of occlusion when the effects starts to kick in.
void setRadius(float radius)
Sets the radius for the ambient occlusion effect.
void setIntensity(float intensity)
Sets the intensity for the ambient occlusion effect.
QgsAmbientOcclusionRenderEntity(Qt3DRender::QTexture2D *depthTexture, Qt3DRender::QLayer *layer, Qt3DRender::QCamera *camera, QNode *parent=nullptr)
Constructor.
Qt3DRender::QShaderProgram * mShader
Qt3DRender::QMaterial * mMaterial