QGIS API Documentation
4.3.0-Master (0cef2469b72)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
3d
materials
qgsmaterial.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgsmaterial.cpp
3
--------------------------------------
4
Date : July 2024
5
Copyright : (C) 2024 by Jean Felder
6
Email : jean dot felder at oslandia 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
16
#include "
qgsmaterial.h
"
17
18
#include "
qgs3dutils.h
"
19
20
#include <QString>
21
#include <Qt3DRender/QEffect>
22
#include <Qt3DRender/QParameter>
23
#include <Qt3DRender/QRenderPass>
24
#include <Qt3DRender/QTechnique>
25
26
#include "moc_qgsmaterial.cpp"
27
28
using namespace
Qt::StringLiterals;
29
30
const
QString QgsMaterial::CLIP_PLANE_ARRAY_PARAMETER_NAME = u
"clipPlane[0]"
_s;
31
const
QString QgsMaterial::CLIP_PLANE_MAX_PLANE_PARAMETER_NAME = u
"max_plane_real"
_s;
32
const
QString QgsMaterial::CLIP_PLANE_DEFINE = u
"CLIPPING"
_s;
33
34
35
QgsMaterial::QgsMaterial
( QNode *parent )
36
: QMaterial( parent )
37
{}
38
39
QgsMaterial::~QgsMaterial
() =
default
;
40
41
bool
QgsMaterial::castsShadows
()
const
42
{
43
return
mCastsShadows;
44
}
45
46
void
QgsMaterial::setCastsShadows
(
bool
enabled )
47
{
48
mCastsShadows = enabled;
49
}
50
51
void
QgsMaterial::enableClipping
(
const
QList<QVector4D> &clipPlanesEquations )
52
{
53
Qt3DRender::QEffect *materialEffect = effect();
54
if
( !materialEffect )
55
return
;
56
57
// First, disable possible existing clipping parameters
58
disableClipping
();
59
if
( clipPlanesEquations.isEmpty() )
60
{
61
return
;
62
}
63
64
// Add #define CLIPPING to the relevant shaders
65
for
( Qt3DRender::QTechnique *technique : materialEffect->techniques() )
66
{
67
for
( Qt3DRender::QRenderPass *renderPass : technique->renderPasses() )
68
{
69
Qt3DRender::QShaderProgram *shaderProgram = renderPass->shaderProgram();
70
71
const
QByteArray vertexCode = shaderProgram->vertexShaderCode();
72
if
( !vertexCode.isEmpty() )
73
{
74
const
QByteArray newVertexCode =
Qgs3DUtils::addDefinesToShaderCode
( vertexCode, QStringList( QgsMaterial::CLIP_PLANE_DEFINE ) );
75
shaderProgram->setVertexShaderCode( newVertexCode );
76
}
77
}
78
}
79
80
// Add the clipping parameters
81
const
int
nrClipPlanes = clipPlanesEquations.size();
82
QVariantList clipPlanesEquationsVariant = QVariantList();
83
for
(
int
i = 0; i < nrClipPlanes; ++i )
84
{
85
clipPlanesEquationsVariant << clipPlanesEquations[i];
86
}
87
Qt3DRender::QParameter *clipPlane =
new
Qt3DRender::QParameter( QgsMaterial::CLIP_PLANE_ARRAY_PARAMETER_NAME, clipPlanesEquationsVariant );
88
Qt3DRender::QParameter *clipPlaneNumber =
new
Qt3DRender::QParameter( QgsMaterial::CLIP_PLANE_MAX_PLANE_PARAMETER_NAME, nrClipPlanes );
89
90
materialEffect->addParameter( clipPlane );
91
materialEffect->addParameter( clipPlaneNumber );
92
93
mClippingEnabled =
true
;
94
}
95
96
void
QgsMaterial::disableClipping
()
97
{
98
Qt3DRender::QEffect *materialEffect = effect();
99
if
( !materialEffect || !mClippingEnabled )
100
return
;
101
102
// Remove #define CLIPPING from the shaders
103
for
( Qt3DRender::QTechnique *technique : materialEffect->techniques() )
104
{
105
for
( Qt3DRender::QRenderPass *renderPass : technique->renderPasses() )
106
{
107
Qt3DRender::QShaderProgram *shaderProgram = renderPass->shaderProgram();
108
109
const
QByteArray vertexCode = shaderProgram->vertexShaderCode();
110
if
( !vertexCode.isEmpty() )
111
{
112
const
QByteArray newVertexCode =
Qgs3DUtils::removeDefinesFromShaderCode
( vertexCode, QStringList( QgsMaterial::CLIP_PLANE_DEFINE ) );
113
shaderProgram->setVertexShaderCode( newVertexCode );
114
}
115
}
116
}
117
118
// Remove the parameters
119
for
( Qt3DRender::QParameter *parameter : materialEffect->parameters() )
120
{
121
const
QString parameterName = parameter->name();
122
if
( parameterName == QgsMaterial::CLIP_PLANE_ARRAY_PARAMETER_NAME || parameterName == QgsMaterial::CLIP_PLANE_MAX_PLANE_PARAMETER_NAME )
123
{
124
materialEffect->removeParameter( parameter );
125
}
126
}
127
128
mClippingEnabled =
false
;
129
}
Qgs3DUtils::removeDefinesFromShaderCode
static QByteArray removeDefinesFromShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Removes some define macros from a shader source code.
Definition
qgs3dutils.cpp:1081
Qgs3DUtils::addDefinesToShaderCode
static QByteArray addDefinesToShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Inserts some define macros into a shader source code.
Definition
qgs3dutils.cpp:1019
QgsMaterial::enableClipping
void enableClipping(const QList< QVector4D > &clipPlanesEquations)
Adds two uniform parameters to define OpenGL clipping from clipPlanesEquations.
Definition
qgsmaterial.cpp:51
QgsMaterial::disableClipping
void disableClipping()
Removes the uniform parameters used to define OpenGL clipping.
Definition
qgsmaterial.cpp:96
QgsMaterial::castsShadows
bool castsShadows() const
Returns true if the material should cast shadows.
Definition
qgsmaterial.cpp:41
QgsMaterial::setCastsShadows
void setCastsShadows(bool enabled)
Sets whether the material should cast shadows.
Definition
qgsmaterial.cpp:46
QgsMaterial::QgsMaterial
QgsMaterial(Qt3DCore::QNode *parent=nullptr)
Constructor for QgsMaterial, with the specified parent node.
Definition
qgsmaterial.cpp:35
QgsMaterial::~QgsMaterial
~QgsMaterial() override
qgs3dutils.h
qgsmaterial.h
Generated on
for QGIS API Documentation by
1.17.0