QGIS API Documentation 4.1.0-Master (01362494303)
Loading...
Searching...
No Matches
qgsphongmaterial3dhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsphongmaterial3dhandler.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"
21
22#include <QMap>
23#include <QString>
24#include <Qt3DCore/QAttribute>
25#include <Qt3DCore/QBuffer>
26#include <Qt3DCore/QGeometry>
27#include <Qt3DRender/QEffect>
28#include <Qt3DRender/QGraphicsApiFilter>
29#include <Qt3DRender/QParameter>
30#include <Qt3DRender/QTechnique>
31
32using namespace Qt::StringLiterals;
33
34
36{
37 switch ( technique )
38 {
45 {
46 if ( context.isHighlighted() )
47 {
48 return new QgsHighlightMaterial( technique );
49 }
50
51 return buildMaterial( settings, context );
52 }
53
56 return nullptr;
57 }
58 return nullptr;
59}
60
62{
63 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
64 Q_ASSERT( phongSettings );
65
66 QMap<QString, QString> parameters;
67 parameters[u"Kd"_s] = u"%1 %2 %3"_s.arg( phongSettings->diffuse().redF() ).arg( phongSettings->diffuse().greenF() ).arg( phongSettings->diffuse().blueF() );
68 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( phongSettings->ambient().redF() ).arg( phongSettings->ambient().greenF() ).arg( phongSettings->ambient().blueF() );
69 parameters[u"Ks"_s] = u"%1 %2 %3"_s.arg( phongSettings->specular().redF() ).arg( phongSettings->specular().greenF() ).arg( phongSettings->specular().blueF() );
70 parameters[u"Ns"_s] = QString::number( phongSettings->shininess() );
71 return parameters;
72}
73
74void QgsPhongMaterial3DHandler::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext ) const
75{
76 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
77 Q_ASSERT( phongSettings );
78
79 const QColor ambient = Qgs3DUtils::srgbToLinear( materialContext.isSelected() ? materialContext.selectionColor().darker() : phongSettings->ambient() );
80 const QColor diffuse = Qgs3DUtils::srgbToLinear( materialContext.isSelected() ? materialContext.selectionColor() : phongSettings->diffuse() );
81
82 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter(
83 u"ambientColor"_s,
84 QColor::fromRgbF(
85 static_cast< float >( ambient.redF() * phongSettings->ambientCoefficient() ),
86 static_cast< float >( ambient.greenF() * phongSettings->ambientCoefficient() ),
87 static_cast< float >( ambient.blueF() * phongSettings->ambientCoefficient() )
88 )
89 );
90 Qt3DRender::QParameter *diffuseParameter = new Qt3DRender::QParameter(
91 u"diffuseColor"_s,
92 QColor::fromRgbF(
93 static_cast<float >( diffuse.redF() * phongSettings->diffuseCoefficient() ),
94 static_cast< float >( diffuse.greenF() * phongSettings->diffuseCoefficient() ),
95 static_cast< float >( diffuse.blueF() * phongSettings->diffuseCoefficient() )
96 )
97 );
98
99 const QColor specular = Qgs3DUtils::srgbToLinear( phongSettings->specular() );
100 Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter(
101 u"specularColor"_s,
102 QColor::fromRgbF(
103 static_cast< float >( specular.redF() * phongSettings->specularCoefficient() ),
104 static_cast< float >( specular.greenF() * phongSettings->specularCoefficient() ),
105 static_cast< float >( specular.blueF() * phongSettings->specularCoefficient() )
106 )
107 );
108 Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( u"shininess"_s, static_cast<float>( phongSettings->shininess() ) );
109 Qt3DRender::QParameter *opacityParameter = new Qt3DRender::QParameter( u"opacity"_s, static_cast<float>( phongSettings->opacity() ) );
110
111 effect->addParameter( ambientParameter );
112 effect->addParameter( diffuseParameter );
113 effect->addParameter( specularParameter );
114 effect->addParameter( shininessParameter );
115 effect->addParameter( opacityParameter );
116}
117
119{
120 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
121 Q_ASSERT( phongSettings );
122
123 const QColor ambient = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, phongSettings->ambient() ) );
124 const QColor diffuse = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, phongSettings->diffuse() ) );
125 const QColor specular = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, phongSettings->specular() ) );
126
127 const double diffuseCoefficient = phongSettings->diffuseCoefficient();
128 const double ambientCoefficient = phongSettings->ambientCoefficient();
129 const double specularCoefficient = phongSettings->specularCoefficient();
130
131 QByteArray array;
132 if ( diffuseCoefficient < 1 || ambientCoefficient < 1 || specularCoefficient < 1 )
133 {
134 // use floats if we are adjusting color component strength, bytes don't
135 // give us enough precision
136 array.resize( sizeof( float ) * 9 );
137 float *fptr = reinterpret_cast<float *>( array.data() );
138
139 *fptr++ = static_cast<float>( diffuse.redF() * diffuseCoefficient );
140 *fptr++ = static_cast<float>( diffuse.greenF() * diffuseCoefficient );
141 *fptr++ = static_cast<float>( diffuse.blueF() * diffuseCoefficient );
142
143 *fptr++ = static_cast<float>( ambient.redF() * ambientCoefficient );
144 *fptr++ = static_cast<float>( ambient.greenF() * ambientCoefficient );
145 *fptr++ = static_cast<float>( ambient.blueF() * ambientCoefficient );
146
147 *fptr++ = static_cast<float>( specular.redF() * specularCoefficient );
148 *fptr++ = static_cast<float>( specular.greenF() * specularCoefficient );
149 *fptr++ = static_cast<float>( specular.blueF() * specularCoefficient );
150 }
151 else
152 {
153 array.resize( sizeof( unsigned char ) * 9 );
154 unsigned char *ptr = reinterpret_cast<unsigned char *>( array.data() );
155
156 *ptr++ = static_cast<unsigned char>( diffuse.red() );
157 *ptr++ = static_cast<unsigned char>( diffuse.green() );
158 *ptr++ = static_cast<unsigned char>( diffuse.blue() );
159
160 *ptr++ = static_cast<unsigned char>( ambient.red() );
161 *ptr++ = static_cast<unsigned char>( ambient.green() );
162 *ptr++ = static_cast<unsigned char>( ambient.blue() );
163
164 *ptr++ = static_cast<unsigned char>( specular.red() );
165 *ptr++ = static_cast<unsigned char>( specular.green() );
166 *ptr++ = static_cast<unsigned char>( specular.blue() );
167 }
168
169 return array;
170}
171
173{
174 return 9 * sizeof( unsigned char );
175}
176
177void QgsPhongMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
178{
179 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
180 Q_ASSERT( phongSettings );
181
182 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
183
184 // use floats if we are adjusting color component strength, bytes don't
185 // give us enough precision
186 const bool useFloats = phongSettings->diffuseCoefficient() < 1 || phongSettings->ambientCoefficient() < 1 || phongSettings->specularCoefficient() < 1;
187
188 Qt3DCore::QAttribute *diffuseAttribute = new Qt3DCore::QAttribute( geometry );
189 diffuseAttribute->setName( u"dataDefinedDiffuseColor"_s );
190 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
191 diffuseAttribute->setVertexSize( 3 );
192 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
193 diffuseAttribute->setBuffer( dataBuffer );
194 diffuseAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
195 diffuseAttribute->setByteOffset( 0 );
196 diffuseAttribute->setCount( vertexCount );
197 geometry->addAttribute( diffuseAttribute );
198
199 Qt3DCore::QAttribute *ambientAttribute = new Qt3DCore::QAttribute( geometry );
200 ambientAttribute->setName( u"dataDefinedAmbiantColor"_s );
201 ambientAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
202 ambientAttribute->setVertexSize( 3 );
203 ambientAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
204 ambientAttribute->setBuffer( dataBuffer );
205 ambientAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
206 ambientAttribute->setByteOffset( 3 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
207 ambientAttribute->setCount( vertexCount );
208 geometry->addAttribute( ambientAttribute );
209
210 Qt3DCore::QAttribute *specularAttribute = new Qt3DCore::QAttribute( geometry );
211 specularAttribute->setName( u"dataDefinedSpecularColor"_s );
212 specularAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
213 specularAttribute->setVertexSize( 3 );
214 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
215 specularAttribute->setBuffer( dataBuffer );
216 specularAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
217 specularAttribute->setByteOffset( 6 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
218 specularAttribute->setCount( vertexCount );
219 geometry->addAttribute( specularAttribute );
220
221 dataBuffer->setData( data );
222}
223
224bool QgsPhongMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
225{
226 const QgsPhongMaterialSettings *phongSettings = qgis::down_cast< const QgsPhongMaterialSettings * >( settings );
227
228 QgsMaterial *material = sceneRoot->findChild<QgsMaterial *>();
229 if ( material->objectName() != "phongMaterial"_L1 )
230 return false;
231
232 Qt3DRender::QEffect *effect = material->effect();
233
234 const QColor ambient = Qgs3DUtils::srgbToLinear( phongSettings->ambient() );
235 if ( Qt3DRender::QParameter *p = findParameter( effect, u"ambientColor"_s ) )
236 {
237 p->setValue(
238 QColor::fromRgbF(
239 static_cast< float >( ambient.redF() * phongSettings->ambientCoefficient() ),
240 static_cast< float >( ambient.greenF() * phongSettings->ambientCoefficient() ),
241 static_cast< float >( ambient.blueF() * phongSettings->ambientCoefficient() )
242 )
243 );
244 }
245 const QColor diffuse = Qgs3DUtils::srgbToLinear( phongSettings->diffuse() );
246 if ( Qt3DRender::QParameter *p = findParameter( effect, u"diffuseColor"_s ) )
247 {
248 p->setValue(
249 QColor::fromRgbF(
250 static_cast<float >( diffuse.redF() * phongSettings->diffuseCoefficient() ),
251 static_cast< float >( diffuse.greenF() * phongSettings->diffuseCoefficient() ),
252 static_cast< float >( diffuse.blueF() * phongSettings->diffuseCoefficient() )
253 )
254 );
255 }
256
257 const QColor specularColor = Qgs3DUtils::srgbToLinear( phongSettings->specular() );
258 if ( Qt3DRender::QParameter *p = findParameter( effect, u"specularColor"_s ) )
259 {
260 p->setValue(
261 QColor::fromRgbF(
262 static_cast< float >( specularColor.redF() * phongSettings->specularCoefficient() ),
263 static_cast< float >( specularColor.greenF() * phongSettings->specularCoefficient() ),
264 static_cast< float >( specularColor.blueF() * phongSettings->specularCoefficient() )
265 )
266 );
267 }
268
269 if ( Qt3DRender::QParameter *p = findParameter( effect, u"shininess"_s ) )
270 p->setValue( static_cast<float>( phongSettings->shininess() ) );
271
272 if ( Qt3DRender::QParameter *p = findParameter( effect, u"opacity"_s ) )
273 p->setValue( static_cast<float>( phongSettings->opacity() ) );
274
275 return true;
276}
277
278QgsMaterial *QgsPhongMaterial3DHandler::buildMaterial( const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context ) const
279{
280 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
281 Q_ASSERT( phongSettings );
282
283 QgsMaterial *material = new QgsMaterial;
284 material->setObjectName( u"phongMaterial"_s );
285
286 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( material );
287
288 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
289 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
290 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
291 technique->graphicsApiFilter()->setMajorVersion( 3 );
292 technique->graphicsApiFilter()->setMinorVersion( 3 );
293 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey();
294 filterKey->setName( u"renderingStyle"_s );
295 filterKey->setValue( u"forward"_s );
296 technique->addFilterKey( filterKey );
297
298 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass();
299 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram();
300
301 renderPass->setShaderProgram( shaderProgram );
302 technique->addRenderPass( renderPass );
303
304 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( u"qrc:/shaders/phong.frag"_s ) );
305
306 if ( phongSettings->dataDefinedProperties().hasActiveProperties() )
307 {
308 // Load shader programs
309 const QUrl urlVert( u"qrc:/shaders/phongDataDefined.vert"_s );
310 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
311 const QByteArray finalFragmentShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, QStringList( { "DATA_DEFINED" } ) );
312 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
313 }
314 else
315 {
316 // Load shader programs
317 const QUrl urlVert( u"qrc:/shaders/default.vert"_s );
318 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
319 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
320
321 const QColor ambient = Qgs3DUtils::srgbToLinear( context.isSelected() ? context.selectionColor().darker() : phongSettings->ambient() );
322 const QColor diffuse = Qgs3DUtils::srgbToLinear( context.isSelected() ? context.selectionColor() : phongSettings->diffuse() );
323
324 effect->addParameter( new Qt3DRender::QParameter(
325 u"ambientColor"_s,
326 QColor::fromRgbF(
327 static_cast< float >( ambient.redF() * phongSettings->ambientCoefficient() ),
328 static_cast< float >( ambient.greenF() * phongSettings->ambientCoefficient() ),
329 static_cast< float >( ambient.blueF() * phongSettings->ambientCoefficient() )
330 )
331 ) );
332 effect->addParameter( new Qt3DRender::QParameter(
333 u"diffuseColor"_s,
334 QColor::fromRgbF(
335 static_cast< float >( diffuse.redF() * phongSettings->diffuseCoefficient() ),
336 static_cast< float >( diffuse.greenF() * phongSettings->diffuseCoefficient() ),
337 static_cast< float >( diffuse.blueF() * phongSettings->diffuseCoefficient() )
338 )
339 ) );
340 const QColor specular = Qgs3DUtils::srgbToLinear( phongSettings->specular() );
341 effect->addParameter( new Qt3DRender::QParameter(
342 u"specularColor"_s,
343 QColor::fromRgbF(
344 static_cast< float >( specular.redF() * phongSettings->specularCoefficient() ),
345 static_cast< float >( specular.greenF() * phongSettings->specularCoefficient() ),
346 static_cast< float >( specular.blueF() * phongSettings->specularCoefficient() )
347 )
348 ) );
349 }
350
351 effect->addParameter( new Qt3DRender::QParameter( u"shininess"_s, static_cast<float>( phongSettings->shininess() ) ) );
352 effect->addParameter( new Qt3DRender::QParameter( u"opacity"_s, static_cast<float>( phongSettings->opacity() ) ) );
353
354 effect->addTechnique( technique );
355 material->setEffect( effect );
356
357 return material;
358}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4342
@ Points
Point based rendering, requires point data.
Definition qgis.h:4346
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4343
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4348
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4344
@ Billboards
Flat billboard rendering.
Definition qgis.h:4350
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4349
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4345
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4347
static QByteArray addDefinesToShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Inserts some define macros into a shader source code.
static QColor srgbToLinear(const QColor &color)
Converts a SRGB color to a linear color.
static Qt3DRender::QParameter * findParameter(Qt3DRender::QEffect *effect, const QString &name)
Finds an existing parameter in an effect by name.
Abstract base class for material settings.
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.
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.
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:40
QByteArray dataDefinedVertexColorsAsByte(const QgsAbstractMaterialSettings *settings, const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
void applyDataDefinedToGeometry(const QgsAbstractMaterialSettings *settings, 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...
bool updatePreviewScene(Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context) const override
Updates an existing material preview scene with new material settings.
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext) const override
Adds parameters from the material settings to a destination effect.
int dataDefinedByteStride(const QgsAbstractMaterialSettings *settings) const override
Returns byte stride of the data defined colors,used to fill the vertex colors data defined buffer for...
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
Basic shading material used for rendering based on the Phong shading model with three color component...
double ambientCoefficient() const
Returns the coefficient for the ambient color contribution (ie strength factor of the ambient color).
QColor diffuse() const
Returns diffuse color component.
double opacity() const
Returns the opacity of the surface.
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
double specularCoefficient() const
Returns the coefficient for the specular color contribution (ie strength factor of the specular color...
double shininess() const
Returns shininess of the surface.
double diffuseCoefficient() const
Returns the coefficient for the diffuse color contribution (ie strength factor of the diffuse color).
bool hasActiveProperties() const final
Returns true if the collection has any active properties, or false if all properties within the colle...