QGIS API Documentation 4.1.0-Master (06b85429c24)
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
76
78{
79 const double avgDiffuseFactor = 0.5;
80 const double avgSpecularFactor = 0.1;
81
82 double red = mAmbientCoefficient * mAmbient.redF() + mDiffuseCoefficient * avgDiffuseFactor * mDiffuse.redF() + mSpecularCoefficient * avgSpecularFactor * mSpecular.redF();
83
84 double green = mAmbientCoefficient * mAmbient.greenF() + mDiffuseCoefficient * avgDiffuseFactor * mDiffuse.greenF() + mSpecularCoefficient * avgSpecularFactor * mSpecular.greenF();
85
86 double blue = mAmbientCoefficient * mAmbient.blueF() + mDiffuseCoefficient * avgDiffuseFactor * mDiffuse.blueF() + mSpecularCoefficient * avgSpecularFactor * mSpecular.blueF();
87
88 red = std::clamp( red, 0.0, 1.0 );
89 green = std::clamp( green, 0.0, 1.0 );
90 blue = std::clamp( blue, 0.0, 1.0 );
91
92 return QColor::fromRgbF( static_cast<float>( red ), static_cast<float>( green ), static_cast<float>( blue ), static_cast<float>( mOpacity ) );
93}
94
95void QgsPhongMaterialSettings::setColorsFromBase( const QColor &baseColor, float metallic )
96{
97 metallic = std::clamp( metallic, 0.0f, 1.0f );
98
99 const float baseR = baseColor.redF();
100 const float baseG = baseColor.greenF();
101 const float baseB = baseColor.blueF();
102
103 // ambient: stable, non-directional lighting
104 constexpr float AMBIENT_FACTOR = 0.2f;
105 mAmbient = QColor::fromRgbF( baseR * AMBIENT_FACTOR, baseG * AMBIENT_FACTOR, baseB * AMBIENT_FACTOR );
106
107 // F0: Fresnel reflectance at normal incidence
108 constexpr float F0_DIELECTRIC = 0.04f;
109 constexpr float F0_METALLIC = 0.6f;
110 const float ks = F0_DIELECTRIC * ( 1.0f - metallic ) + F0_METALLIC * metallic;
111 const float kd = 1.0f - ks;
112
113
114 mDiffuse = QColor::fromRgbF( kd * baseR, kd * baseG, kd * baseB );
115
116 // specular
117 // * Non-metallic surfaces: Independent of base color
118 // * Metallic surfaces:
119 // - Reflect their own color
120 // - Linear interpolation from white to base color as metallic increases
121 mSpecular = QColor::fromRgbF( ( 1.0f - metallic ) * F0_DIELECTRIC + metallic * baseR, ( 1.0f - metallic ) * F0_DIELECTRIC + metallic * baseG, ( 1.0f - metallic ) * F0_DIELECTRIC + metallic * baseB );
122
123 constexpr float MIN_SHININESS = 32.0f;
124 constexpr float MAX_SHININESS = 200.0f;
125 mShininess = MIN_SHININESS + metallic * ( MAX_SHININESS - MIN_SHININESS );
126}
127
128void QgsPhongMaterialSettings::setColorsFromBase( const QColor &baseColor )
129{
130 setColorsFromBase( baseColor, 0.0f );
131}
132
133void QgsPhongMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
134{
135 mAmbient = QgsColorUtils::colorFromString( elem.attribute( u"ambient"_s, u"25,25,25"_s ) );
136 mDiffuse = QgsColorUtils::colorFromString( elem.attribute( u"diffuse"_s, u"178,178,178"_s ) );
137 mSpecular = QgsColorUtils::colorFromString( elem.attribute( u"specular"_s, u"255,255,255"_s ) );
138 mShininess = elem.attribute( u"shininess"_s ).toDouble();
139 mOpacity = elem.attribute( u"opacity"_s, u"1.0"_s ).toDouble();
140 mAmbientCoefficient = elem.attribute( u"ka"_s, u"1.0"_s ).toDouble();
141 mDiffuseCoefficient = elem.attribute( u"kd"_s, u"1.0"_s ).toDouble();
142 mSpecularCoefficient = elem.attribute( u"ks"_s, u"1.0"_s ).toDouble();
143
145}
146
147void QgsPhongMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
148{
149 elem.setAttribute( u"ambient"_s, QgsColorUtils::colorToString( mAmbient ) );
150 elem.setAttribute( u"diffuse"_s, QgsColorUtils::colorToString( mDiffuse ) );
151 elem.setAttribute( u"specular"_s, QgsColorUtils::colorToString( mSpecular ) );
152 elem.setAttribute( u"shininess"_s, mShininess );
153 elem.setAttribute( u"opacity"_s, mOpacity );
154 elem.setAttribute( u"ka"_s, mAmbientCoefficient );
155 elem.setAttribute( u"kd"_s, mDiffuseCoefficient );
156 elem.setAttribute( u"ks"_s, mSpecularCoefficient );
157
159}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4362
@ Points
Point based rendering, requires point data.
Definition qgis.h:4366
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4363
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4368
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4364
@ Billboards
Flat billboard rendering.
Definition qgis.h:4370
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4369
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4365
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4367
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.
QSet< QgsAbstractMaterialSettings::Property > supportedProperties() const override
Returns the set of data-defined properties supported by this material.
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.