QGIS API Documentation 4.1.0-Master (31622b25bb0)
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 {
41 {
42 Q_ASSERT( false );
43 return nullptr;
44 }
50 {
51 if ( context.isHighlighted() )
52 {
53 return new QgsHighlightMaterial( technique );
54 }
55
56 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
57 Q_ASSERT( phongSettings );
58
59 QgsPhongMaterial *material = new QgsPhongMaterial();
60 material->setObjectName( u"phongMaterial"_s );
61
62 const bool dataDefined = phongSettings->dataDefinedProperties().hasActiveProperties();
63 if ( !dataDefined )
64 {
65 const QColor ambient = context.isSelected() ? context.selectionColor().darker() : phongSettings->ambient();
66 const QColor diffuse = context.isSelected() ? context.selectionColor() : phongSettings->diffuse();
67 material->setAmbient( ambient, static_cast<float>( phongSettings->ambientCoefficient() ) );
68 material->setDiffuse( diffuse, static_cast<float>( phongSettings->diffuseCoefficient() ) );
69 material->setSpecular( phongSettings->specular(), static_cast<float>( phongSettings->specularCoefficient() ) );
70 }
71 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
72 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
73 material->setDataDefinedEnabled( dataDefined );
74
75 return material;
76 }
77
80 return nullptr;
81 }
82 return nullptr;
83}
84
86{
87 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
88 Q_ASSERT( phongSettings );
89
90 QMap<QString, QString> parameters;
91 parameters[u"Kd"_s] = u"%1 %2 %3"_s.arg( phongSettings->diffuse().redF() ).arg( phongSettings->diffuse().greenF() ).arg( phongSettings->diffuse().blueF() );
92 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( phongSettings->ambient().redF() ).arg( phongSettings->ambient().greenF() ).arg( phongSettings->ambient().blueF() );
93 parameters[u"Ks"_s] = u"%1 %2 %3"_s.arg( phongSettings->specular().redF() ).arg( phongSettings->specular().greenF() ).arg( phongSettings->specular().blueF() );
94 parameters[u"Ns"_s] = QString::number( phongSettings->shininess() );
95 return parameters;
96}
97
99{
100 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
101 Q_ASSERT( phongSettings );
102
103 const QColor ambient = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, phongSettings->ambient() ) );
104 const QColor diffuse = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, phongSettings->diffuse() ) );
105 const QColor specular = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, phongSettings->specular() ) );
106
107 const double diffuseCoefficient = phongSettings->diffuseCoefficient();
108 const double ambientCoefficient = phongSettings->ambientCoefficient();
109 const double specularCoefficient = phongSettings->specularCoefficient();
110
111 QByteArray array;
112 if ( diffuseCoefficient < 1 || ambientCoefficient < 1 || specularCoefficient < 1 )
113 {
114 // use floats if we are adjusting color component strength, bytes don't
115 // give us enough precision
116 array.resize( sizeof( float ) * 9 );
117 float *fptr = reinterpret_cast<float *>( array.data() );
118
119 *fptr++ = static_cast<float>( diffuse.redF() * diffuseCoefficient );
120 *fptr++ = static_cast<float>( diffuse.greenF() * diffuseCoefficient );
121 *fptr++ = static_cast<float>( diffuse.blueF() * diffuseCoefficient );
122
123 *fptr++ = static_cast<float>( ambient.redF() * ambientCoefficient );
124 *fptr++ = static_cast<float>( ambient.greenF() * ambientCoefficient );
125 *fptr++ = static_cast<float>( ambient.blueF() * ambientCoefficient );
126
127 *fptr++ = static_cast<float>( specular.redF() * specularCoefficient );
128 *fptr++ = static_cast<float>( specular.greenF() * specularCoefficient );
129 *fptr++ = static_cast<float>( specular.blueF() * specularCoefficient );
130 }
131 else
132 {
133 array.resize( sizeof( unsigned char ) * 9 );
134 unsigned char *ptr = reinterpret_cast<unsigned char *>( array.data() );
135
136 *ptr++ = static_cast<unsigned char>( diffuse.red() );
137 *ptr++ = static_cast<unsigned char>( diffuse.green() );
138 *ptr++ = static_cast<unsigned char>( diffuse.blue() );
139
140 *ptr++ = static_cast<unsigned char>( ambient.red() );
141 *ptr++ = static_cast<unsigned char>( ambient.green() );
142 *ptr++ = static_cast<unsigned char>( ambient.blue() );
143
144 *ptr++ = static_cast<unsigned char>( specular.red() );
145 *ptr++ = static_cast<unsigned char>( specular.green() );
146 *ptr++ = static_cast<unsigned char>( specular.blue() );
147 }
148
149 return array;
150}
151
152void QgsPhongMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
153{
154 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
155 Q_ASSERT( phongSettings );
156
157 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
158
159 // use floats if we are adjusting color component strength, bytes don't
160 // give us enough precision
161 const bool useFloats = phongSettings->diffuseCoefficient() < 1 || phongSettings->ambientCoefficient() < 1 || phongSettings->specularCoefficient() < 1;
162
163 Qt3DCore::QAttribute *diffuseAttribute = new Qt3DCore::QAttribute( geometry );
164 diffuseAttribute->setName( u"dataDefinedDiffuseColor"_s );
165 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
166 diffuseAttribute->setVertexSize( 3 );
167 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
168 diffuseAttribute->setBuffer( dataBuffer );
169 diffuseAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
170 diffuseAttribute->setByteOffset( 0 );
171 diffuseAttribute->setCount( vertexCount );
172 geometry->addAttribute( diffuseAttribute );
173
174 Qt3DCore::QAttribute *ambientAttribute = new Qt3DCore::QAttribute( geometry );
175 ambientAttribute->setName( u"dataDefinedAmbiantColor"_s );
176 ambientAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
177 ambientAttribute->setVertexSize( 3 );
178 ambientAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
179 ambientAttribute->setBuffer( dataBuffer );
180 ambientAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
181 ambientAttribute->setByteOffset( 3 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
182 ambientAttribute->setCount( vertexCount );
183 geometry->addAttribute( ambientAttribute );
184
185 Qt3DCore::QAttribute *specularAttribute = new Qt3DCore::QAttribute( geometry );
186 specularAttribute->setName( u"dataDefinedSpecularColor"_s );
187 specularAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
188 specularAttribute->setVertexSize( 3 );
189 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
190 specularAttribute->setBuffer( dataBuffer );
191 specularAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
192 specularAttribute->setByteOffset( 6 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
193 specularAttribute->setCount( vertexCount );
194 geometry->addAttribute( specularAttribute );
195
196 dataBuffer->setData( data );
197}
198
199bool QgsPhongMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
200{
201 const QgsPhongMaterialSettings *phongSettings = qgis::down_cast< const QgsPhongMaterialSettings * >( settings );
202
203 QgsPhongMaterial *material = sceneRoot->findChild<QgsPhongMaterial *>();
204 if ( !material || material->objectName() != "phongMaterial"_L1 )
205 return false;
206
207 material->setAmbient( phongSettings->ambient(), static_cast<float>( phongSettings->ambientCoefficient() ) );
208 material->setDiffuse( phongSettings->diffuse(), static_cast<float>( phongSettings->diffuseCoefficient() ) );
209 material->setSpecular( phongSettings->specular(), static_cast<float>( phongSettings->specularCoefficient() ) );
210 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
211 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
212
213 return true;
214}
215
217{
218 const QgsPhongMaterialSettings *phongSettings = qgis::down_cast< const QgsPhongMaterialSettings * >( settings );
219
220 QgsPhongMaterial *material = new QgsPhongMaterial();
221 material->setInstancingEnabled( true, flags );
222 material->setObjectName( u"phongMaterial"_s );
223
224 const QColor ambient = context.isSelected() ? context.selectionColor().darker() : phongSettings->ambient();
225 const QColor diffuse = context.isSelected() ? context.selectionColor() : phongSettings->diffuse();
226 material->setAmbient( ambient, static_cast<float>( phongSettings->ambientCoefficient() ) );
227 material->setDiffuse( diffuse, static_cast<float>( phongSettings->diffuseCoefficient() ) );
228 material->setSpecular( phongSettings->specular(), static_cast<float>( phongSettings->specularCoefficient() ) );
229 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
230 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
231
232 return material;
233}
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
QFlags< InstancedMaterialFlag > InstancedMaterialFlags
Definition qgis.h:4365
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,...
QgsMaterial * toInstancedMaterial(const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags) const override
Creates a QgsMaterial for instanced point rendering.
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.
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...