QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsphongmaterialsettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsphongmaterialsettings.cpp
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk 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"
20
21#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
22#include <Qt3DRender/QAttribute>
23#include <Qt3DRender/QBuffer>
24#include <Qt3DRender/QGeometry>
25
26typedef Qt3DRender::QAttribute Qt3DQAttribute;
27typedef Qt3DRender::QBuffer Qt3DQBuffer;
28typedef Qt3DRender::QGeometry Qt3DQGeometry;
29#else
30#include <Qt3DCore/QAttribute>
31#include <Qt3DCore/QBuffer>
32#include <Qt3DCore/QGeometry>
33
34typedef Qt3DCore::QAttribute Qt3DQAttribute;
35typedef Qt3DCore::QBuffer Qt3DQBuffer;
36typedef Qt3DCore::QGeometry Qt3DQGeometry;
37#endif
38#include <Qt3DRender/QParameter>
39#include <Qt3DRender/QEffect>
40#include <Qt3DRender/QTechnique>
41#include <Qt3DRender/QGraphicsApiFilter>
42#include <QMap>
43
44
46{
47 return QStringLiteral( "phong" );
48}
49
67
72
77
79{
80 const QgsPhongMaterialSettings *otherPhong = dynamic_cast<const QgsPhongMaterialSettings *>( other );
81 if ( !otherPhong )
82 return false;
83
84 return *this == *otherPhong;
85}
86
87void QgsPhongMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
88{
89 mAmbient = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "ambient" ), QStringLiteral( "25,25,25" ) ) );
90 mDiffuse = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "diffuse" ), QStringLiteral( "178,178,178" ) ) );
91 mSpecular = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "specular" ), QStringLiteral( "255,255,255" ) ) );
92 mShininess = elem.attribute( QStringLiteral( "shininess" ) ).toDouble();
93 mOpacity = elem.attribute( QStringLiteral( "opacity" ), QStringLiteral( "1.0" ) ).toDouble();
94 mAmbientCoefficient = elem.attribute( QStringLiteral( "ka" ), QStringLiteral( "1.0" ) ).toDouble();
95 mDiffuseCoefficient = elem.attribute( QStringLiteral( "kd" ), QStringLiteral( "1.0" ) ).toDouble();
96 mSpecularCoefficient = elem.attribute( QStringLiteral( "ks" ), QStringLiteral( "1.0" ) ).toDouble();
97
99}
100
101void QgsPhongMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
102{
103 elem.setAttribute( QStringLiteral( "ambient" ), QgsColorUtils::colorToString( mAmbient ) );
104 elem.setAttribute( QStringLiteral( "diffuse" ), QgsColorUtils::colorToString( mDiffuse ) );
105 elem.setAttribute( QStringLiteral( "specular" ), QgsColorUtils::colorToString( mSpecular ) );
106 elem.setAttribute( QStringLiteral( "shininess" ), mShininess );
107 elem.setAttribute( QStringLiteral( "opacity" ), mOpacity );
108 elem.setAttribute( QStringLiteral( "ka" ), mAmbientCoefficient );
109 elem.setAttribute( QStringLiteral( "kd" ), mDiffuseCoefficient );
110 elem.setAttribute( QStringLiteral( "ks" ), mSpecularCoefficient );
111
113}
114
115
135
137{
138 QMap<QString, QString> parameters;
139 parameters[QStringLiteral( "Kd" )] = QStringLiteral( "%1 %2 %3" ).arg( mDiffuse.redF() ).arg( mDiffuse.greenF() ).arg( mDiffuse.blueF() );
140 parameters[QStringLiteral( "Ka" )] = QStringLiteral( "%1 %2 %3" ).arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
141 parameters[QStringLiteral( "Ks" )] = QStringLiteral( "%1 %2 %3" ).arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
142 parameters[QStringLiteral( "Ns" )] = QString::number( mShininess );
143 return parameters;
144}
145
146void QgsPhongMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext ) const
147{
148 const QColor ambient = materialContext.isSelected() ? materialContext.selectionColor().darker() : mAmbient;
149 const QColor diffuse = materialContext.isSelected() ? materialContext.selectionColor() : mDiffuse;
150
151 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( QStringLiteral( "ambientColor" ), QColor::fromRgbF( ambient.redF() * mAmbientCoefficient, ambient.greenF() * mAmbientCoefficient, ambient.blueF() * mAmbientCoefficient ) );
152 Qt3DRender::QParameter *diffuseParameter = new Qt3DRender::QParameter( QStringLiteral( "diffuseColor" ), QColor::fromRgbF( diffuse.redF() * mDiffuseCoefficient, diffuse.greenF() * mDiffuseCoefficient, diffuse.blueF() * mDiffuseCoefficient ) );
153 Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter( QStringLiteral( "specularColor" ), QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) );
154 Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( QStringLiteral( "shininess" ), static_cast<float>( mShininess ) );
155 Qt3DRender::QParameter *opacityParameter = new Qt3DRender::QParameter( QStringLiteral( "opacity" ), static_cast<float>( mOpacity ) );
156
157 effect->addParameter( ambientParameter );
158 effect->addParameter( diffuseParameter );
159 effect->addParameter( specularParameter );
160 effect->addParameter( shininessParameter );
161 effect->addParameter( opacityParameter );
162}
163
165{
166 const QColor ambient = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, mAmbient );
167 const QColor diffuse = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, mDiffuse );
168 const QColor specular = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, mSpecular );
169
170 QByteArray array;
171 if ( mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1 )
172 {
173 // use floats if we are adjusting color component strength, bytes don't
174 // give us enough precision
175 array.resize( sizeof( float ) * 9 );
176 float *fptr = reinterpret_cast<float *>( array.data() );
177
178 *fptr++ = static_cast<float>( diffuse.redF() * mDiffuseCoefficient );
179 *fptr++ = static_cast<float>( diffuse.greenF() * mDiffuseCoefficient );
180 *fptr++ = static_cast<float>( diffuse.blueF() * mDiffuseCoefficient );
181
182 *fptr++ = static_cast<float>( ambient.redF() * mAmbientCoefficient );
183 *fptr++ = static_cast<float>( ambient.greenF() * mAmbientCoefficient );
184 *fptr++ = static_cast<float>( ambient.blueF() * mAmbientCoefficient );
185
186 *fptr++ = static_cast<float>( specular.redF() * mSpecularCoefficient );
187 *fptr++ = static_cast<float>( specular.greenF() * mSpecularCoefficient );
188 *fptr++ = static_cast<float>( specular.blueF() * mSpecularCoefficient );
189 }
190 else
191 {
192 array.resize( sizeof( unsigned char ) * 9 );
193 unsigned char *ptr = reinterpret_cast<unsigned char *>( array.data() );
194
195 *ptr++ = static_cast<unsigned char>( diffuse.red() );
196 *ptr++ = static_cast<unsigned char>( diffuse.green() );
197 *ptr++ = static_cast<unsigned char>( diffuse.blue() );
198
199 *ptr++ = static_cast<unsigned char>( ambient.red() );
200 *ptr++ = static_cast<unsigned char>( ambient.green() );
201 *ptr++ = static_cast<unsigned char>( ambient.blue() );
202
203 *ptr++ = static_cast<unsigned char>( specular.red() );
204 *ptr++ = static_cast<unsigned char>( specular.green() );
205 *ptr++ = static_cast<unsigned char>( specular.blue() );
206 }
207
208 return array;
209}
210
211int QgsPhongMaterialSettings::dataDefinedByteStride() const { return 9 * sizeof( unsigned char ); }
212
213void QgsPhongMaterialSettings::applyDataDefinedToGeometry( Qt3DQGeometry *geometry, int vertexCount, const QByteArray &data ) const
214{
215 Qt3DQBuffer *dataBuffer = new Qt3DQBuffer( geometry );
216
217 // use floats if we are adjusting color component strength, bytes don't
218 // give us enough precision
219 const bool useFloats = mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1;
220
221 Qt3DQAttribute *diffuseAttribute = new Qt3DQAttribute( geometry );
222 diffuseAttribute->setName( QStringLiteral( "dataDefinedDiffuseColor" ) );
223 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
224 diffuseAttribute->setVertexSize( 3 );
225 diffuseAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
226 diffuseAttribute->setBuffer( dataBuffer );
227 diffuseAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
228 diffuseAttribute->setByteOffset( 0 );
229 diffuseAttribute->setCount( vertexCount );
230 geometry->addAttribute( diffuseAttribute );
231
232 Qt3DQAttribute *ambientAttribute = new Qt3DQAttribute( geometry );
233 ambientAttribute->setName( QStringLiteral( "dataDefinedAmbiantColor" ) );
234 ambientAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
235 ambientAttribute->setVertexSize( 3 );
236 ambientAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
237 ambientAttribute->setBuffer( dataBuffer );
238 ambientAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
239 ambientAttribute->setByteOffset( 3 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
240 ambientAttribute->setCount( vertexCount );
241 geometry->addAttribute( ambientAttribute );
242
243 Qt3DQAttribute *specularAttribute = new Qt3DQAttribute( geometry );
244 specularAttribute->setName( QStringLiteral( "dataDefinedSpecularColor" ) );
245 specularAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
246 specularAttribute->setVertexSize( 3 );
247 specularAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
248 specularAttribute->setBuffer( dataBuffer );
249 specularAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
250 specularAttribute->setByteOffset( 6 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
251 specularAttribute->setCount( vertexCount );
252 geometry->addAttribute( specularAttribute );
253
254 dataBuffer->setData( data );
255}
256
257QgsMaterial *QgsPhongMaterialSettings::buildMaterial( const QgsMaterialContext &context ) const
258{
259 QgsMaterial *material = new QgsMaterial;
260
261 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( material );
262
263 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
264 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
265 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
266 technique->graphicsApiFilter()->setMajorVersion( 3 );
267 technique->graphicsApiFilter()->setMinorVersion( 3 );
268 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey();
269 filterKey->setName( QStringLiteral( "renderingStyle" ) );
270 filterKey->setValue( QStringLiteral( "forward" ) );
271 technique->addFilterKey( filterKey );
272
273 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass();
274 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram();
275
276 renderPass->setShaderProgram( shaderProgram );
277 technique->addRenderPass( renderPass );
278
279 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/phong.frag" ) ) );
280
281 if ( dataDefinedProperties().hasActiveProperties() )
282 {
283 // Load shader programs
284 const QUrl urlVert( QStringLiteral( "qrc:/shaders/phongDataDefined.vert" ) );
285 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
286 const QByteArray finalFragmentShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, QStringList( { "DATA_DEFINED" } ) );
287 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
288 }
289 else
290 {
291 // Load shader programs
292 const QUrl urlVert( QStringLiteral( "qrc:/shaders/default.vert" ) );
293 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
294 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
295
296 const QColor ambient = context.isSelected() ? context.selectionColor().darker() : mAmbient;
297 const QColor diffuse = context.isSelected() ? context.selectionColor() : mDiffuse;
298
299 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "ambientColor" ), QColor::fromRgbF( ambient.redF() * mAmbientCoefficient, ambient.greenF() * mAmbientCoefficient, ambient.blueF() * mAmbientCoefficient ) ) );
300 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "diffuseColor" ), QColor::fromRgbF( diffuse.redF() * mDiffuseCoefficient, diffuse.greenF() * mDiffuseCoefficient, diffuse.blueF() * mDiffuseCoefficient ) ) );
301 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "specularColor" ), QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) ) );
302 }
303
304 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "shininess" ), static_cast<float>( mShininess ) ) );
305 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "opacity" ), static_cast<float>( mOpacity ) ) );
306
307 effect->addTechnique( technique );
308 material->setEffect( effect );
309
310 return material;
311}
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...
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.
Base class for all materials used within QGIS 3D views.
Definition qgsmaterial.h:39
int dataDefinedByteStride() const override
Returns byte stride of the data defined colors,used to fill the vertex colors data defined buffer for...
QgsMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
bool equals(const QgsAbstractMaterialSettings *other) const override
Returns true if this settings exactly matches an other settings.
QString type() const override
Returns the unique type name for the material.
QColor diffuse() const
Returns diffuse color component.
QMap< QString, QString > toExportParameters() const override
Returns the parameters to be exported to .mtl file.
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...
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsPhongMaterialSettings.
static bool supportsTechnique(QgsMaterialSettingsRenderingTechnique technique)
Returns true if the specified technique is supported by the Phong material.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
QgsPhongMaterialSettings * clone() const override
Clones the material settings.
QgsPhongMaterialSettings()=default
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
QByteArray dataDefinedVertexColorsAsByte(const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext) const override
Adds parameters from the material to a destination effect.
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).
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry