QGIS API Documentation 4.1.0-Master (9af12b5a203)
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 "qgscolorutils.h"
19
20#include <QMap>
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
26{
27 return u"phong"_s;
28}
29
48
53
58
60{
61 const QgsPhongMaterialSettings *otherPhong = dynamic_cast<const QgsPhongMaterialSettings *>( other );
62 if ( !otherPhong )
63 return false;
64
65 return *this == *otherPhong;
66}
67
69{
70 const double avgDiffuseFactor = 0.5;
71 const double avgSpecularFactor = 0.1;
72
73 double red = mAmbientCoefficient * mAmbient.redF() + mDiffuseCoefficient * avgDiffuseFactor * mDiffuse.redF() + mSpecularCoefficient * avgSpecularFactor * mSpecular.redF();
74
75 double green = mAmbientCoefficient * mAmbient.greenF() + mDiffuseCoefficient * avgDiffuseFactor * mDiffuse.greenF() + mSpecularCoefficient * avgSpecularFactor * mSpecular.greenF();
76
77 double blue = mAmbientCoefficient * mAmbient.blueF() + mDiffuseCoefficient * avgDiffuseFactor * mDiffuse.blueF() + mSpecularCoefficient * avgSpecularFactor * mSpecular.blueF();
78
79 red = std::clamp( red, 0.0, 1.0 );
80 green = std::clamp( green, 0.0, 1.0 );
81 blue = std::clamp( blue, 0.0, 1.0 );
82
83 return QColor::fromRgbF( static_cast<float>( red ), static_cast<float>( green ), static_cast<float>( blue ), static_cast<float>( mOpacity ) );
84}
85
86void QgsPhongMaterialSettings::setColorsFromBase( const QColor &baseColor, float metallic )
87{
88 metallic = std::clamp( metallic, 0.0f, 1.0f );
89
90 const float baseR = baseColor.redF();
91 const float baseG = baseColor.greenF();
92 const float baseB = baseColor.blueF();
93
94 // ambient: stable, non-directional lighting
95 constexpr float AMBIENT_FACTOR = 0.2f;
96 mAmbient = QColor::fromRgbF( baseR * AMBIENT_FACTOR, baseG * AMBIENT_FACTOR, baseB * AMBIENT_FACTOR );
97
98 // F0: Fresnel reflectance at normal incidence
99 constexpr float F0_DIELECTRIC = 0.04f;
100 constexpr float F0_METALLIC = 0.6f;
101 const float ks = F0_DIELECTRIC * ( 1.0f - metallic ) + F0_METALLIC * metallic;
102 const float kd = 1.0f - ks;
103
104
105 mDiffuse = QColor::fromRgbF( kd * baseR, kd * baseG, kd * baseB );
106
107 // specular
108 // * Non-metallic surfaces: Independent of base color
109 // * Metallic surfaces:
110 // - Reflect their own color
111 // - Linear interpolation from white to base color as metallic increases
112 mSpecular = QColor::fromRgbF( ( 1.0f - metallic ) * F0_DIELECTRIC + metallic * baseR, ( 1.0f - metallic ) * F0_DIELECTRIC + metallic * baseG, ( 1.0f - metallic ) * F0_DIELECTRIC + metallic * baseB );
113
114 constexpr float MIN_SHININESS = 32.0f;
115 constexpr float MAX_SHININESS = 200.0f;
116 mShininess = MIN_SHININESS + metallic * ( MAX_SHININESS - MIN_SHININESS );
117}
118
119void QgsPhongMaterialSettings::setColorsFromBase( const QColor &baseColor )
120{
121 setColorsFromBase( baseColor, 0.0f );
122}
123
124void QgsPhongMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
125{
126 mAmbient = QgsColorUtils::colorFromString( elem.attribute( u"ambient"_s, u"25,25,25"_s ) );
127 mDiffuse = QgsColorUtils::colorFromString( elem.attribute( u"diffuse"_s, u"178,178,178"_s ) );
128 mSpecular = QgsColorUtils::colorFromString( elem.attribute( u"specular"_s, u"255,255,255"_s ) );
129 mShininess = elem.attribute( u"shininess"_s ).toDouble();
130 mOpacity = elem.attribute( u"opacity"_s, u"1.0"_s ).toDouble();
131 mAmbientCoefficient = elem.attribute( u"ka"_s, u"1.0"_s ).toDouble();
132 mDiffuseCoefficient = elem.attribute( u"kd"_s, u"1.0"_s ).toDouble();
133 mSpecularCoefficient = elem.attribute( u"ks"_s, u"1.0"_s ).toDouble();
134
136}
137
138void QgsPhongMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
139{
140 elem.setAttribute( u"ambient"_s, QgsColorUtils::colorToString( mAmbient ) );
141 elem.setAttribute( u"diffuse"_s, QgsColorUtils::colorToString( mDiffuse ) );
142 elem.setAttribute( u"specular"_s, QgsColorUtils::colorToString( mSpecular ) );
143 elem.setAttribute( u"shininess"_s, mShininess );
144 elem.setAttribute( u"opacity"_s, mOpacity );
145 elem.setAttribute( u"ka"_s, mAmbientCoefficient );
146 elem.setAttribute( u"kd"_s, mDiffuseCoefficient );
147 elem.setAttribute( u"ks"_s, mSpecularCoefficient );
148
150}
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
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.
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.
QColor averageColor() const override
Returns an approximate color representing the blended material color.
bool equals(const QgsAbstractMaterialSettings *other) const override
Returns true if this settings exactly matches an other settings.
void setColorsFromBase(const QColor &baseColor, float metallic)
Decomposes a base color into Phong material components, and sets the material's colors accordingly.
QString type() const override
Returns the unique type name for the material.
static bool supportsTechnique(Qgis::MaterialRenderingTechnique technique)
Returns true if the specified technique is supported by the Phong material.
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsPhongMaterialSettings.
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.
A container for the context for various read/write operations on objects.