QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsskyboxentity.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsskyboxentity.cpp
3  --------------------------------------
4  Date : August 2020
5  Copyright : (C) 2020 by Belgacem Nedjima
6  Email : gb uderscore nedjima at esi dot dz
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 
16 #include "qgsskyboxentity.h"
17 
18 #include <Qt3DCore/QEntity>
19 #include <Qt3DExtras/QCuboidMesh>
20 #include <Qt3DRender/QEffect>
21 #include <Qt3DRender/QMaterial>
22 #include <Qt3DRender/QShaderProgram>
23 #include <Qt3DRender/QFilterKey>
24 #include <Qt3DRender/QRenderPass>
25 #include <Qt3DRender/QTechnique>
26 #include <Qt3DRender/QGraphicsApiFilter>
27 #include <Qt3DRender/QCullFace>
28 #include <Qt3DRender/QDepthTest>
29 #include <Qt3DRender/QSeamlessCubemap>
30 #include <Qt3DRender/QParameter>
31 #include <Qt3DRender/QTextureImage>
32 
33 #include "qgsimagecache.h"
34 #include "qgsimagetexture.h"
35 #include "qgsproject.h"
36 
38  : Qt3DCore::QEntity( parent )
39  , mEffect( new Qt3DRender::QEffect( this ) )
40  , mMaterial( new Qt3DRender::QMaterial( this ) )
41  , mGl3Technique( new Qt3DRender::QTechnique( this ) )
42  , mFilterKey( new Qt3DRender::QFilterKey( this ) )
43  , mGl3RenderPass( new Qt3DRender::QRenderPass( this ) )
44  , mMesh( new Qt3DExtras::QCuboidMesh( this ) )
45  , mGammaStrengthParameter( new Qt3DRender::QParameter( QStringLiteral( "gammaStrength" ), 0.0f ) )
46  , mTextureParameter( new Qt3DRender::QParameter( this ) )
47 {
48  mGl3Technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
49  mGl3Technique->graphicsApiFilter()->setMajorVersion( 3 );
50  mGl3Technique->graphicsApiFilter()->setMinorVersion( 3 );
51  mGl3Technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
52 
53  mFilterKey->setParent( mEffect );
54  mFilterKey->setName( QStringLiteral( "renderingStyle" ) );
55  mFilterKey->setValue( QStringLiteral( "forward" ) );
56 
57  mGl3Technique->addFilterKey( mFilterKey );
58 
59  Qt3DRender::QCullFace *cullFront = new Qt3DRender::QCullFace();
60  cullFront->setMode( Qt3DRender::QCullFace::Front );
61  Qt3DRender::QDepthTest *depthTest = new Qt3DRender::QDepthTest();
62  depthTest->setDepthFunction( Qt3DRender::QDepthTest::LessOrEqual );
63  Qt3DRender::QSeamlessCubemap *seamlessCubemap = new Qt3DRender::QSeamlessCubemap();
64 
65  mGl3RenderPass->addRenderState( cullFront );
66  mGl3RenderPass->addRenderState( depthTest );
67  mGl3RenderPass->addRenderState( seamlessCubemap );
68 
69  mGl3Technique->addRenderPass( mGl3RenderPass );
70 
71  mEffect->addTechnique( mGl3Technique );
72 
73  mMaterial->setEffect( mEffect );
74  mMaterial->addParameter( mGammaStrengthParameter );
75  mMaterial->addParameter( mTextureParameter );
76 
77  mMesh->setXYMeshResolution( QSize( 2, 2 ) );
78  mMesh->setXZMeshResolution( QSize( 2, 2 ) );
79  mMesh->setYZMeshResolution( QSize( 2, 2 ) );
80 
81  addComponent( mMesh );
82  addComponent( mMaterial );
83 }
84 
85 // Panoramic skybox
86 
87 QgsPanoramicSkyboxEntity::QgsPanoramicSkyboxEntity( const QString &texturePath, QNode *parent )
88  : QgsSkyboxEntity( parent )
89  , mTexturePath( texturePath )
90  , mLoadedTexture( new Qt3DRender::QTextureLoader( parent ) )
91  , mGlShader( new Qt3DRender::QShaderProgram( this ) )
92 {
93  mLoadedTexture->setGenerateMipMaps( false );
94  mGlShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/skybox.vert" ) ) ) );
95  mGlShader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/hdr_skybox.frag" ) ) ) );
96  mGl3RenderPass->setShaderProgram( mGlShader );
97 
98  mTextureParameter->setName( "skyboxTexture" );
99  mTextureParameter->setValue( QVariant::fromValue( mLoadedTexture ) );
100 
101  reloadTexture();
102 }
103 
104 void QgsPanoramicSkyboxEntity::reloadTexture()
105 {
106  mLoadedTexture->setSource( QUrl::fromUserInput( mTexturePath ) );
107 }
108 
109 // 6 faces skybox
110 
111 QgsCubeFacesSkyboxEntity::QgsCubeFacesSkyboxEntity( const QString &posX, const QString &posY, const QString &posZ, const QString &negX, const QString &negY, const QString &negZ, Qt3DCore::QNode *parent )
112  : QgsSkyboxEntity( parent )
113  , mGlShader( new Qt3DRender::QShaderProgram() )
114  , mCubeMap( new Qt3DRender::QTextureCubeMap( this ) )
115 {
116  init();
117  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapPositiveX] = posX;
118  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapPositiveY] = posY;
119  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapPositiveZ] = posZ;
120  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapNegativeX] = negX;
121  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapNegativeY] = negY;
122  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapNegativeZ] = negZ;
123  reloadTexture();
124 }
125 
126 void QgsCubeFacesSkyboxEntity::init()
127 {
128  mGlShader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/skybox.vert" ) ) ) );
129  mGlShader->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/skybox.frag" ) ) ) );
130  mGl3RenderPass->setShaderProgram( mGlShader );
131 
132  mCubeMap->setMagnificationFilter( Qt3DRender::QTextureCubeMap::Linear );
133  mCubeMap->setMinificationFilter( Qt3DRender::QTextureCubeMap::Linear );
134  mCubeMap->setGenerateMipMaps( false );
135  mCubeMap->setWrapMode( Qt3DRender::QTextureWrapMode( Qt3DRender::QTextureWrapMode::Repeat ) );
136 
137  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapPositiveX] = QString();
138  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapPositiveY] = QString();
139  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapPositiveZ] = QString();
140  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapNegativeX] = QString();
141  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapNegativeY] = QString();
142  mCubeFacesPaths[Qt3DRender::QTextureCubeMap::CubeMapNegativeZ] = QString();
143 
144  mTextureParameter->setName( "skyboxTexture" );
145  mTextureParameter->setValue( QVariant::fromValue( mCubeMap ) );
146 }
147 
148 void QgsCubeFacesSkyboxEntity::reloadTexture()
149 {
150  for ( Qt3DRender::QAbstractTextureImage *textureImage : mFacesTextureImages )
151  {
152  mCubeMap->removeTextureImage( textureImage );
153  delete textureImage;
154  }
155  mFacesTextureImages.clear();
156 
157  for ( auto it = mCubeFacesPaths.begin(); it != mCubeFacesPaths.end(); ++it )
158  {
159  Qt3DRender::QTextureCubeMap::CubeMapFace face = it.key();
160  QString texturePath = it.value();
161  Qt3DRender::QTextureImage *image = new Qt3DRender::QTextureImage( this );
162  image->setFace( face );
163  image->setMirrored( false );
164  image->setSource( QUrl::fromUserInput( texturePath ) );
165  mCubeMap->addTextureImage( image );
166  mFacesTextureImages.push_back( image );
167  }
168 }
QgsSkyboxEntity::mGl3Technique
Qt3DRender::QTechnique * mGl3Technique
Definition: qgsskyboxentity.h:63
QgsSkyboxEntity::mMesh
Qt3DExtras::QCuboidMesh * mMesh
Definition: qgsskyboxentity.h:66
QgsSkyboxEntity::mMaterial
Qt3DRender::QMaterial * mMaterial
Definition: qgsskyboxentity.h:62
QgsPanoramicSkyboxEntity::QgsPanoramicSkyboxEntity
QgsPanoramicSkyboxEntity(const QString &texturePath, Qt3DCore::QNode *parent=nullptr)
Construct a skybox from a panoramic 360 image.
Definition: qgsskyboxentity.cpp:87
QgsSkyboxEntity
base class for all skybox types It holds the common member data between different skybox entity types...
Definition: qgsskyboxentity.h:44
QgsSkyboxEntity::mGammaStrengthParameter
Qt3DRender::QParameter * mGammaStrengthParameter
Definition: qgsskyboxentity.h:67
Qt3DCore
Definition: qgsabstract3drenderer.h:31
Qt3DRender
Definition: qgs3dmapscene.h:27
QgsSkyboxEntity::mFilterKey
Qt3DRender::QFilterKey * mFilterKey
Definition: qgsskyboxentity.h:64
qgsskyboxentity.h
qgsimagecache.h
QgsCubeFacesSkyboxEntity::QgsCubeFacesSkyboxEntity
QgsCubeFacesSkyboxEntity(const QString &posX, const QString &posY, const QString &posZ, const QString &negX, const QString &negY, const QString &negZ, Qt3DCore::QNode *parent=nullptr)
Constructs a skybox from 6 different images.
Definition: qgsskyboxentity.cpp:111
QgsSkyboxEntity::QgsSkyboxEntity
QgsSkyboxEntity(QNode *parent=nullptr)
Constructor.
Definition: qgsskyboxentity.cpp:37
qgsimagetexture.h
QgsSkyboxEntity::mGl3RenderPass
Qt3DRender::QRenderPass * mGl3RenderPass
Definition: qgsskyboxentity.h:65
Qt3DExtras
Definition: qgs3dmapscene.h:40
qgsproject.h
QgsSkyboxEntity::mTextureParameter
Qt3DRender::QParameter * mTextureParameter
Definition: qgsskyboxentity.h:68
QgsSkyboxEntity::mEffect
Qt3DRender::QEffect * mEffect
Definition: qgsskyboxentity.h:61