QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
qgsgoochmaterialsettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgoochmaterialsettings.cpp
3 --------------------------------------
4 Date : July 2020
5 Copyright : (C) 2020 by Nyall Dawson
6 Email : nyall dot dawson 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
18#include "qgs3dutils.h"
19#include "qgscolorutils.h"
21
22#include <QString>
23#include <QUrl>
24#include <Qt3DCore/QAttribute>
25#include <Qt3DCore/QBuffer>
26#include <Qt3DCore/QGeometry>
27#include <Qt3DExtras/QGoochMaterial>
28#include <Qt3DRender/QEffect>
29#include <Qt3DRender/QGraphicsApiFilter>
30#include <Qt3DRender/QParameter>
31#include <Qt3DRender/QTechnique>
32
33using namespace Qt::StringLiterals;
34
36{
37 return u"gooch"_s;
38}
39
44
62
67
69{
70 const QgsGoochMaterialSettings *otherGooch = dynamic_cast<const QgsGoochMaterialSettings *>( other );
71 if ( !otherGooch )
72 return false;
73
74 return *this == *otherGooch;
75}
76
77void QgsGoochMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
78{
79 mWarm = QgsColorUtils::colorFromString( elem.attribute( u"warm"_s, u"107,0,107"_s ) );
80 mCool = QgsColorUtils::colorFromString( elem.attribute( u"cool"_s, u"255,130,0"_s ) );
81 mDiffuse = QgsColorUtils::colorFromString( elem.attribute( u"diffuse"_s, u"178,178,178"_s ) );
82 mSpecular = QgsColorUtils::colorFromString( elem.attribute( u"specular"_s ) );
83 mShininess = elem.attribute( u"shininess2"_s, u"100"_s ).toDouble();
84 mAlpha = elem.attribute( u"alpha"_s, u"0.25"_s ).toDouble();
85 mBeta = elem.attribute( u"beta"_s, u"0.5"_s ).toDouble();
86
88}
89
90void QgsGoochMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
91{
92 elem.setAttribute( u"warm"_s, QgsColorUtils::colorToString( mWarm ) );
93 elem.setAttribute( u"cool"_s, QgsColorUtils::colorToString( mCool ) );
94 elem.setAttribute( u"diffuse"_s, QgsColorUtils::colorToString( mDiffuse ) );
95 elem.setAttribute( u"specular"_s, QgsColorUtils::colorToString( mSpecular ) );
96 elem.setAttribute( u"shininess2"_s, mShininess );
97 elem.setAttribute( u"alpha"_s, mAlpha );
98 elem.setAttribute( u"beta"_s, mBeta );
99
101}
102
104{
105 return QMap<QString, QString>();
106}
107
109{
110 switch ( technique )
111 {
116 {
117 if ( context.isHighlighted() )
118 {
119 return new QgsHighlightMaterial( technique );
120 }
121
122 return buildMaterial( context );
123 }
124
128 return nullptr;
129 }
130 return nullptr;
131}
132
133void QgsGoochMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *, const QgsMaterialContext & ) const
134{
135}
136
138{
139 const QColor diffuse = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, mDiffuse );
140 const QColor warm = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Warm, expressionContext, mWarm );
141 const QColor cool = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Cool, expressionContext, mCool );
142 const QColor specular = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, mSpecular );
143
144
145 QByteArray array;
146 array.resize( sizeof( unsigned char ) * 12 );
147 unsigned char *fptr = reinterpret_cast<unsigned char *>( array.data() );
148
149 *fptr++ = static_cast<unsigned char>( diffuse.red() );
150 *fptr++ = static_cast<unsigned char>( diffuse.green() );
151 *fptr++ = static_cast<unsigned char>( diffuse.blue() );
152
153 *fptr++ = static_cast<unsigned char>( warm.red() );
154 *fptr++ = static_cast<unsigned char>( warm.green() );
155 *fptr++ = static_cast<unsigned char>( warm.blue() );
156
157 *fptr++ = static_cast<unsigned char>( cool.red() );
158 *fptr++ = static_cast<unsigned char>( cool.green() );
159 *fptr++ = static_cast<unsigned char>( cool.blue() );
160
161 *fptr++ = static_cast<unsigned char>( specular.red() );
162 *fptr++ = static_cast<unsigned char>( specular.green() );
163 *fptr++ = static_cast<unsigned char>( specular.blue() );
164
165 return array;
166}
167
169{
170 return 12 * sizeof( unsigned char );
171}
172
173void QgsGoochMaterialSettings::applyDataDefinedToGeometry( Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
174{
175 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
176
177 Qt3DCore::QAttribute *diffuseAttribute = new Qt3DCore::QAttribute( geometry );
178 diffuseAttribute->setName( u"dataDefinedDiffuseColor"_s );
179 diffuseAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
180 diffuseAttribute->setVertexSize( 3 );
181 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
182 diffuseAttribute->setBuffer( dataBuffer );
183 diffuseAttribute->setByteStride( 12 * sizeof( unsigned char ) );
184 diffuseAttribute->setByteOffset( 0 );
185 diffuseAttribute->setCount( vertexCount );
186 geometry->addAttribute( diffuseAttribute );
187
188 Qt3DCore::QAttribute *warmAttribute = new Qt3DCore::QAttribute( geometry );
189 warmAttribute->setName( u"dataDefinedWarmColor"_s );
190 warmAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
191 warmAttribute->setVertexSize( 3 );
192 warmAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
193 warmAttribute->setBuffer( dataBuffer );
194 warmAttribute->setByteStride( 12 * sizeof( unsigned char ) );
195 warmAttribute->setByteOffset( 3 * sizeof( unsigned char ) );
196 warmAttribute->setCount( vertexCount );
197 geometry->addAttribute( warmAttribute );
198
199 Qt3DCore::QAttribute *coolAttribute = new Qt3DCore::QAttribute( geometry );
200 coolAttribute->setName( u"dataDefinedCoolColor"_s );
201 coolAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
202 coolAttribute->setVertexSize( 3 );
203 coolAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
204 coolAttribute->setBuffer( dataBuffer );
205 coolAttribute->setByteStride( 12 * sizeof( unsigned char ) );
206 coolAttribute->setByteOffset( 6 * sizeof( unsigned char ) );
207 coolAttribute->setCount( vertexCount );
208 geometry->addAttribute( coolAttribute );
209
210
211 Qt3DCore::QAttribute *specularAttribute = new Qt3DCore::QAttribute( geometry );
212 specularAttribute->setName( u"dataDefinedSpecularColor"_s );
213 specularAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
214 specularAttribute->setVertexSize( 3 );
215 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
216 specularAttribute->setBuffer( dataBuffer );
217 specularAttribute->setByteStride( 12 * sizeof( unsigned char ) );
218 specularAttribute->setByteOffset( 9 * sizeof( unsigned char ) );
219 specularAttribute->setCount( vertexCount );
220 geometry->addAttribute( specularAttribute );
221
222 dataBuffer->setData( data );
223}
224
225QgsMaterial *QgsGoochMaterialSettings::buildMaterial( const QgsMaterialContext &context ) const
226{
227 QgsMaterial *material = new QgsMaterial;
228
229 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( material );
230
231 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
232 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
233 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
234 technique->graphicsApiFilter()->setMajorVersion( 3 );
235 technique->graphicsApiFilter()->setMinorVersion( 3 );
236 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey();
237 filterKey->setName( u"renderingStyle"_s );
238 filterKey->setValue( u"forward"_s );
239 technique->addFilterKey( filterKey );
240
241 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass();
242 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram();
243
244 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( u"qrc:/shaders/gooch.frag"_s ) );
245
246 if ( dataDefinedProperties().hasActiveProperties() )
247 {
248 //Load shader programs
249 const QUrl urlVert( u"qrc:/shaders/goochDataDefined.vert"_s );
250 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
251
252 const QByteArray finalFragmentShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, QStringList( { "DATA_DEFINED" } ) );
253 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
254 }
255 else
256 {
257 //Load shader programs
258 const QUrl urlVert( u"qrc:/shaders/default.vert"_s );
259 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
260 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
261
262 const QColor diffuseColor = context.isSelected() ? context.selectionColor() : mDiffuse;
263 effect->addParameter( new Qt3DRender::QParameter( u"kd"_s, diffuseColor ) );
264 effect->addParameter( new Qt3DRender::QParameter( u"ks"_s, mSpecular ) );
265 effect->addParameter( new Qt3DRender::QParameter( u"kblue"_s, mCool ) );
266 effect->addParameter( new Qt3DRender::QParameter( u"kyellow"_s, mWarm ) );
267 }
268
269 renderPass->setShaderProgram( shaderProgram );
270 technique->addRenderPass( renderPass );
271
272 technique->addParameter( new Qt3DRender::QParameter( u"shininess"_s, mShininess ) );
273 technique->addParameter( new Qt3DRender::QParameter( u"alpha"_s, mAlpha ) );
274 technique->addParameter( new Qt3DRender::QParameter( u"beta"_s, mBeta ) );
275
276 effect->addTechnique( technique );
277 material->setEffect( effect );
278
279 return material;
280}
static QByteArray addDefinesToShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Inserts some define macros into a shader source code.
Abstract base class for material settings.
virtual void writeXml(QDomElement &element, const QgsReadWriteContext &) const
Writes settings to a DOM element.
virtual void readXml(const QDomElement &element, const QgsReadWriteContext &)
Reads settings from a DOM element.
QgsPropertyCollection dataDefinedProperties() const
Returns the symbol material property collection, used for data defined overrides.
QColor valueAsColor(int key, const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a color.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsGoochMaterialSettings * clone() const override
Clones the material settings.
QColor specular() const
Returns specular color component.
QColor cool() const
Returns cool color component.
int dataDefinedByteStride() const override
Returns byte stride of the data defined colors,used to fill the vertex colors data defined buffer for...
QByteArray dataDefinedVertexColorsAsByte(const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
QMap< QString, QString > toExportParameters() const override
Returns the parameters to be exported to .mtl file.
QColor warm() const
Returns warm color component.
bool equals(const QgsAbstractMaterialSettings *other) const override
Returns true if this settings exactly matches an other settings.
void applyDataDefinedToGeometry(Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data) const override
Applies the data defined bytes, dataDefinedBytes, on the geometry by filling a specific vertex buffer...
QString type() const override
Returns the unique type name for the material.
QColor diffuse() const
Returns diffuse color component.
QgsGoochMaterialSettings()=default
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsGoochMaterialSettings.
static bool supportsTechnique(QgsMaterialSettingsRenderingTechnique technique)
Returns true if the specified technique is supported by the Gooch material.
QgsMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext) const override
Adds parameters from the material to a destination effect.
Context settings for a material.
QColor selectionColor() const
Returns the color for representing materials in a selected state.
bool isSelected() const
Returns true if the material should represent a selected state.
bool isHighlighted() const
Returns true if the material should represent a highlighted state.
Base class for all materials used within QGIS 3D views.
Definition qgsmaterial.h:39
A container for the context for various read/write operations on objects.
QgsMaterialSettingsRenderingTechnique
Material rendering techniques.
@ Points
Point based rendering, requires point data.
@ Triangles
Triangle based rendering (default).
@ TrianglesFromModel
Triangle based rendering, using a model object source.
@ Lines
Line based rendering, requires line data.
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).