QGIS API Documentation 4.1.0-Master (64dc32379c2)
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"
20#include "qgsphongmaterial.h"
22
23#include <QMap>
24#include <QString>
25#include <Qt3DCore/QAttribute>
26#include <Qt3DCore/QBuffer>
27#include <Qt3DCore/QGeometry>
28#include <Qt3DRender/QEffect>
29#include <Qt3DRender/QGraphicsApiFilter>
30#include <Qt3DRender/QParameter>
31#include <Qt3DRender/QTechnique>
32
33using namespace Qt::StringLiterals;
34
35
37{
38 switch ( technique )
39 {
46 {
47 if ( context.isHighlighted() )
48 {
49 return new QgsHighlightMaterial( technique );
50 }
51
52 return buildMaterial( settings, context );
53 }
54
57 return nullptr;
58 }
59 return nullptr;
60}
61
63{
64 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
65 Q_ASSERT( phongSettings );
66
67 QMap<QString, QString> parameters;
68 parameters[u"Kd"_s] = u"%1 %2 %3"_s.arg( phongSettings->diffuse().redF() ).arg( phongSettings->diffuse().greenF() ).arg( phongSettings->diffuse().blueF() );
69 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( phongSettings->ambient().redF() ).arg( phongSettings->ambient().greenF() ).arg( phongSettings->ambient().blueF() );
70 parameters[u"Ks"_s] = u"%1 %2 %3"_s.arg( phongSettings->specular().redF() ).arg( phongSettings->specular().greenF() ).arg( phongSettings->specular().blueF() );
71 parameters[u"Ns"_s] = QString::number( phongSettings->shininess() );
72 return parameters;
73}
74
75void QgsPhongMaterial3DHandler::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext ) const
76{
77 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
78 Q_ASSERT( phongSettings );
79
80 const QColor ambient = Qgs3DUtils::srgbToLinear( materialContext.isSelected() ? materialContext.selectionColor().darker() : phongSettings->ambient() );
81 const QColor diffuse = Qgs3DUtils::srgbToLinear( materialContext.isSelected() ? materialContext.selectionColor() : phongSettings->diffuse() );
82
83 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter(
84 u"ambientColor"_s,
85 QColor::fromRgbF(
86 static_cast< float >( ambient.redF() * phongSettings->ambientCoefficient() ),
87 static_cast< float >( ambient.greenF() * phongSettings->ambientCoefficient() ),
88 static_cast< float >( ambient.blueF() * phongSettings->ambientCoefficient() )
89 )
90 );
91 Qt3DRender::QParameter *diffuseParameter = new Qt3DRender::QParameter(
92 u"diffuseColor"_s,
93 QColor::fromRgbF(
94 static_cast<float >( diffuse.redF() * phongSettings->diffuseCoefficient() ),
95 static_cast< float >( diffuse.greenF() * phongSettings->diffuseCoefficient() ),
96 static_cast< float >( diffuse.blueF() * phongSettings->diffuseCoefficient() )
97 )
98 );
99
100 const QColor specular = Qgs3DUtils::srgbToLinear( phongSettings->specular() );
101 Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter(
102 u"specularColor"_s,
103 QColor::fromRgbF(
104 static_cast< float >( specular.redF() * phongSettings->specularCoefficient() ),
105 static_cast< float >( specular.greenF() * phongSettings->specularCoefficient() ),
106 static_cast< float >( specular.blueF() * phongSettings->specularCoefficient() )
107 )
108 );
109 Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( u"shininess"_s, static_cast<float>( phongSettings->shininess() ) );
110 Qt3DRender::QParameter *opacityParameter = new Qt3DRender::QParameter( u"opacity"_s, static_cast<float>( phongSettings->opacity() ) );
111
112 effect->addParameter( ambientParameter );
113 effect->addParameter( diffuseParameter );
114 effect->addParameter( specularParameter );
115 effect->addParameter( shininessParameter );
116 effect->addParameter( opacityParameter );
117}
118
120{
121 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
122 Q_ASSERT( phongSettings );
123
124 const QColor ambient = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, phongSettings->ambient() ) );
125 const QColor diffuse = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, phongSettings->diffuse() ) );
126 const QColor specular = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, phongSettings->specular() ) );
127
128 const double diffuseCoefficient = phongSettings->diffuseCoefficient();
129 const double ambientCoefficient = phongSettings->ambientCoefficient();
130 const double specularCoefficient = phongSettings->specularCoefficient();
131
132 QByteArray array;
133 if ( diffuseCoefficient < 1 || ambientCoefficient < 1 || specularCoefficient < 1 )
134 {
135 // use floats if we are adjusting color component strength, bytes don't
136 // give us enough precision
137 array.resize( sizeof( float ) * 9 );
138 float *fptr = reinterpret_cast<float *>( array.data() );
139
140 *fptr++ = static_cast<float>( diffuse.redF() * diffuseCoefficient );
141 *fptr++ = static_cast<float>( diffuse.greenF() * diffuseCoefficient );
142 *fptr++ = static_cast<float>( diffuse.blueF() * diffuseCoefficient );
143
144 *fptr++ = static_cast<float>( ambient.redF() * ambientCoefficient );
145 *fptr++ = static_cast<float>( ambient.greenF() * ambientCoefficient );
146 *fptr++ = static_cast<float>( ambient.blueF() * ambientCoefficient );
147
148 *fptr++ = static_cast<float>( specular.redF() * specularCoefficient );
149 *fptr++ = static_cast<float>( specular.greenF() * specularCoefficient );
150 *fptr++ = static_cast<float>( specular.blueF() * specularCoefficient );
151 }
152 else
153 {
154 array.resize( sizeof( unsigned char ) * 9 );
155 unsigned char *ptr = reinterpret_cast<unsigned char *>( array.data() );
156
157 *ptr++ = static_cast<unsigned char>( diffuse.red() );
158 *ptr++ = static_cast<unsigned char>( diffuse.green() );
159 *ptr++ = static_cast<unsigned char>( diffuse.blue() );
160
161 *ptr++ = static_cast<unsigned char>( ambient.red() );
162 *ptr++ = static_cast<unsigned char>( ambient.green() );
163 *ptr++ = static_cast<unsigned char>( ambient.blue() );
164
165 *ptr++ = static_cast<unsigned char>( specular.red() );
166 *ptr++ = static_cast<unsigned char>( specular.green() );
167 *ptr++ = static_cast<unsigned char>( specular.blue() );
168 }
169
170 return array;
171}
172
174{
175 return 9 * sizeof( unsigned char );
176}
177
178void QgsPhongMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
179{
180 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
181 Q_ASSERT( phongSettings );
182
183 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
184
185 // use floats if we are adjusting color component strength, bytes don't
186 // give us enough precision
187 const bool useFloats = phongSettings->diffuseCoefficient() < 1 || phongSettings->ambientCoefficient() < 1 || phongSettings->specularCoefficient() < 1;
188
189 Qt3DCore::QAttribute *diffuseAttribute = new Qt3DCore::QAttribute( geometry );
190 diffuseAttribute->setName( u"dataDefinedDiffuseColor"_s );
191 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
192 diffuseAttribute->setVertexSize( 3 );
193 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
194 diffuseAttribute->setBuffer( dataBuffer );
195 diffuseAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
196 diffuseAttribute->setByteOffset( 0 );
197 diffuseAttribute->setCount( vertexCount );
198 geometry->addAttribute( diffuseAttribute );
199
200 Qt3DCore::QAttribute *ambientAttribute = new Qt3DCore::QAttribute( geometry );
201 ambientAttribute->setName( u"dataDefinedAmbiantColor"_s );
202 ambientAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
203 ambientAttribute->setVertexSize( 3 );
204 ambientAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
205 ambientAttribute->setBuffer( dataBuffer );
206 ambientAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
207 ambientAttribute->setByteOffset( 3 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
208 ambientAttribute->setCount( vertexCount );
209 geometry->addAttribute( ambientAttribute );
210
211 Qt3DCore::QAttribute *specularAttribute = new Qt3DCore::QAttribute( geometry );
212 specularAttribute->setName( u"dataDefinedSpecularColor"_s );
213 specularAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
214 specularAttribute->setVertexSize( 3 );
215 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
216 specularAttribute->setBuffer( dataBuffer );
217 specularAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
218 specularAttribute->setByteOffset( 6 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
219 specularAttribute->setCount( vertexCount );
220 geometry->addAttribute( specularAttribute );
221
222 dataBuffer->setData( data );
223}
224
225bool QgsPhongMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
226{
227 const QgsPhongMaterialSettings *phongSettings = qgis::down_cast< const QgsPhongMaterialSettings * >( settings );
228
229 QgsPhongMaterial *material = sceneRoot->findChild<QgsPhongMaterial *>();
230 if ( !material || material->objectName() != "phongMaterial"_L1 )
231 return false;
232
233 material->setAmbient( phongSettings->ambient(), static_cast<float>( phongSettings->ambientCoefficient() ) );
234 material->setDiffuse( phongSettings->diffuse(), static_cast<float>( phongSettings->diffuseCoefficient() ) );
235 material->setSpecular( phongSettings->specular(), static_cast<float>( phongSettings->specularCoefficient() ) );
236 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
237 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
238
239 return true;
240}
241
242QgsMaterial *QgsPhongMaterial3DHandler::buildMaterial( const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context ) const
243{
244 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
245 Q_ASSERT( phongSettings );
246
247 QgsPhongMaterial *material = new QgsPhongMaterial;
248 material->setObjectName( u"phongMaterial"_s );
249
250 material->setDataDefinedEnabled( phongSettings->dataDefinedProperties().hasActiveProperties() );
251
252 if ( !phongSettings->dataDefinedProperties().hasActiveProperties() )
253 {
254 const QColor ambient = context.isSelected() ? context.selectionColor().darker() : phongSettings->ambient();
255 const QColor diffuse = context.isSelected() ? context.selectionColor() : phongSettings->diffuse();
256 const QColor specular = phongSettings->specular();
257
258 material->setAmbient( ambient, static_cast<float>( phongSettings->ambientCoefficient() ) );
259 material->setDiffuse( diffuse, static_cast<float>( phongSettings->diffuseCoefficient() ) );
260 material->setSpecular( specular, static_cast<float>( phongSettings->specularCoefficient() ) );
261 }
262
263 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
264 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
265
266 return material;
267}
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 QColor srgbToLinear(const QColor &color)
Converts a SRGB color to a linear color.
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...