QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 mFarPlaneParameter->setValue( farPlane );
33 } );
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 );
38 } );
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 );
43 } );
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 );
48 } );
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 ) );
53 } );
54
55 QVariantList ssaoKernelValues;
56
57 std::uniform_real_distribution<float> randomFloats( 0.0, 1.0 ); // random floats between [0.0, 1.0]
58 std::default_random_engine generator;
59 unsigned int kernelSize = 64;
60 for ( unsigned int i = 0; i < kernelSize; ++i )
61 {
62 QVector3D sample(
63 randomFloats( generator ) * 2.0 - 1.0,
64 randomFloats( generator ) * 2.0 - 1.0,
65 randomFloats( generator ) * 2.0 - 1.0
66 );
67 sample.normalize();
68 float scale = static_cast<float>( i ) / static_cast<float>( kernelSize );
69 scale = 0.1 + 0.9 * scale * scale;
70 sample *= scale;
71 ssaoKernelValues.push_back( sample );
72 }
73
74 // 4x4 array of random rotation vectors
75 QVariantList ssaoNoise;
76 for ( unsigned int i = 0; i < 16; ++i )
77 {
78 QVector3D sample(
79 randomFloats( generator ),
80 randomFloats( generator ),
81 0.0
82 );
83 ssaoNoise.push_back( sample );
84 }
85 mAmbientOcclusionKernelParameter = new Qt3DRender::QParameter( QStringLiteral( "ssaoKernel[0]" ), ssaoKernelValues );
86 mMaterial->addParameter( mAmbientOcclusionKernelParameter );
87
88 Qt3DRender::QParameter *noiseParameter = new Qt3DRender::QParameter( QStringLiteral( "ssaoNoise[0]" ), ssaoNoise );
89 mMaterial->addParameter( noiseParameter );
90
91 mIntensityParameter = new Qt3DRender::QParameter( QStringLiteral( "intensity" ), 0.5f );
92 mMaterial->addParameter( mIntensityParameter );
93
94 mRadiusParameter = new Qt3DRender::QParameter( QStringLiteral( "radius" ), 25.0f );
95 mMaterial->addParameter( mRadiusParameter );
96
97 mThresholdParameter = new Qt3DRender::QParameter( QStringLiteral( "threshold" ), 0.5f );
98 mMaterial->addParameter( mThresholdParameter );
99
100 const QString vertexShaderPath = QStringLiteral( "qrc:/shaders/ssao_factor_render.vert" );
101 const QString fragmentShaderPath = QStringLiteral( "qrc:/shaders/ssao_factor_render.frag" );
102
103 mShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( vertexShaderPath ) ) );
104 mShader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( fragmentShaderPath ) ) );
105}
106
108{
109 mIntensityParameter->setValue( intensity );
110}
111
113{
114 mRadiusParameter->setValue( radius );
115}
116
118{
119 mThresholdParameter->setValue( threshold );
120}
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