QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 "qgssymbollayerutils.h"
19 #include "qgsapplication.h"
20 #include "qgsimagecache.h"
21 #include <Qt3DExtras/QDiffuseMapMaterial>
22 #include <Qt3DExtras/QPhongMaterial>
23 #include <Qt3DRender/QParameter>
24 #include <Qt3DRender/QEffect>
25 #include <QMap>
26 
27 
29 {
30  return QStringLiteral( "phong" );
31 }
32 
34 {
35  switch ( technique )
36  {
37  case QgsMaterialSettingsRenderingTechnique::Triangles:
38  case QgsMaterialSettingsRenderingTechnique::InstancedPoints:
41  return true;
42 
43  case QgsMaterialSettingsRenderingTechnique::Lines:
44  return false;
45  }
46  return false;
47 }
48 
50 {
51  return new QgsPhongMaterialSettings();
52 }
53 
55 {
56  return new QgsPhongMaterialSettings( *this );
57 }
58 
59 void QgsPhongMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext & )
60 {
61  mAmbient = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "ambient" ), QStringLiteral( "25,25,25" ) ) );
62  mDiffuse = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "diffuse" ), QStringLiteral( "178,178,178" ) ) );
63  mSpecular = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "specular" ), QStringLiteral( "255,255,255" ) ) );
64  mShininess = elem.attribute( QStringLiteral( "shininess" ) ).toFloat();
65 }
66 
67 void QgsPhongMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext & ) const
68 {
69  elem.setAttribute( QStringLiteral( "ambient" ), QgsSymbolLayerUtils::encodeColor( mAmbient ) );
70  elem.setAttribute( QStringLiteral( "diffuse" ), QgsSymbolLayerUtils::encodeColor( mDiffuse ) );
71  elem.setAttribute( QStringLiteral( "specular" ), QgsSymbolLayerUtils::encodeColor( mSpecular ) );
72  elem.setAttribute( QStringLiteral( "shininess" ), mShininess );
73 }
74 
75 
77 {
78  switch ( technique )
79  {
80  case QgsMaterialSettingsRenderingTechnique::Triangles:
81  case QgsMaterialSettingsRenderingTechnique::InstancedPoints:
84  {
85  Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial;
86  material->setDiffuse( mDiffuse );
87  material->setAmbient( mAmbient );
88  material->setSpecular( mSpecular );
89  material->setShininess( mShininess );
90 
91  if ( context.isSelected() )
92  {
93  // update the material with selection colors
94  material->setDiffuse( context.selectionColor() );
95  material->setAmbient( context.selectionColor().darker() );
96  }
97  return material;
98  }
99 
100  case QgsMaterialSettingsRenderingTechnique::Lines:
101  return nullptr;
102 
103  }
104  return nullptr;
105 }
106 
107 QMap<QString, QString> QgsPhongMaterialSettings::toExportParameters() const
108 {
109  QMap<QString, QString> parameters;
110  parameters[ QStringLiteral( "Kd" ) ] = QStringLiteral( "%1 %2 %3" ).arg( mDiffuse.redF() ).arg( mDiffuse.greenF() ).arg( mDiffuse.blueF() );
111  parameters[ QStringLiteral( "Ka" ) ] = QStringLiteral( "%1 %2 %3" ).arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
112  parameters[ QStringLiteral( "Ks" ) ] = QStringLiteral( "%1 %2 %3" ).arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
113  parameters[ QStringLiteral( "Ns" ) ] = QString::number( mShininess );
114  return parameters;
115 }
116 
117 void QgsPhongMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *effect ) const
118 {
119  Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( QStringLiteral( "ka" ), QColor::fromRgbF( 0.05f, 0.05f, 0.05f, 1.0f ) );
120  Qt3DRender::QParameter *diffuseParameter = new Qt3DRender::QParameter( QStringLiteral( "kd" ), QColor::fromRgbF( 0.7f, 0.7f, 0.7f, 1.0f ) );
121  Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter( QStringLiteral( "ks" ), QColor::fromRgbF( 0.01f, 0.01f, 0.01f, 1.0f ) );
122  Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( QStringLiteral( "shininess" ), 150.0f );
123 
124  diffuseParameter->setValue( mDiffuse );
125  ambientParameter->setValue( mAmbient );
126  specularParameter->setValue( mSpecular );
127  shininessParameter->setValue( mShininess );
128 
129  effect->addParameter( ambientParameter );
130  effect->addParameter( diffuseParameter );
131  effect->addParameter( specularParameter );
132  effect->addParameter( shininessParameter );
133 }
QgsSymbolLayerUtils::encodeColor
static QString encodeColor(const QColor &color)
Definition: qgssymbollayerutils.cpp:52
QgsPhongMaterialSettings::QgsPhongMaterialSettings
QgsPhongMaterialSettings()=default
Constructor for QgsPhongMaterialSettings.
QgsPhongMaterialSettings::writeXml
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
Definition: qgsphongmaterialsettings.cpp:67
QgsPhongMaterialSettings::clone
QgsPhongMaterialSettings * clone() const override
Clones the material settings.
Definition: qgsphongmaterialsettings.cpp:54
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
qgssymbollayerutils.h
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:57
QgsMaterialContext::selectionColor
QColor selectionColor() const
Returns the color for representing materials in a selected state.
Definition: qgsabstractmaterialsettings.h:77
qgsapplication.h
QgsMaterialContext
3 Context settings for a material.
Definition: qgsabstractmaterialsettings.h:55
QgsPhongMaterialSettings
3 Basic shading material used for rendering based on the Phong shading model with three color compone...
Definition: qgsphongmaterialsettings.h:37
QgsAbstractMaterialSettings
3 Abstract base class for material settings.
Definition: qgsabstractmaterialsettings.h:105
QgsPhongMaterialSettings::toMaterial
Qt3DRender::QMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QMaterial object representing the material settings.
Definition: qgsphongmaterialsettings.cpp:76
QgsPhongMaterialSettings::type
QString type() const override
Returns the unique type name for the material.
Definition: qgsphongmaterialsettings.cpp:28
QgsPhongMaterialSettings::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
Definition: qgsphongmaterialsettings.cpp:59
QgsMaterialSettingsRenderingTechnique::Triangles
@ Triangles
Triangle based rendering (default)
QgsPhongMaterialSettings::create
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsPhongMaterialSettings.
Definition: qgsphongmaterialsettings.cpp:49
QgsMaterialSettingsRenderingTechnique
QgsMaterialSettingsRenderingTechnique
Material rendering techniques 3.
Definition: qgsabstractmaterialsettings.h:36
qgsimagecache.h
QgsPhongMaterialSettings::addParametersToEffect
void addParametersToEffect(Qt3DRender::QEffect *effect) const override
Adds parameters from the material to a destination effect.
Definition: qgsphongmaterialsettings.cpp:117
qgsphongmaterialsettings.h
QgsMaterialSettingsRenderingTechnique::Points
@ Points
Point based rendering, requires point data.
QgsPhongMaterialSettings::toExportParameters
QMap< QString, QString > toExportParameters() const override
Returns the parameters to be exported to .mtl file.
Definition: qgsphongmaterialsettings.cpp:107
QgsPhongMaterialSettings::supportsTechnique
static bool supportsTechnique(QgsMaterialSettingsRenderingTechnique technique)
Returns true if the specified technique is supported by the Phong material.
Definition: qgsphongmaterialsettings.cpp:33
QgsMaterialContext::isSelected
bool isSelected() const
Returns true if the material should represent a selected state.
Definition: qgsabstractmaterialsettings.h:63