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