QGIS API Documentation 4.1.0-Master (3fcefe620d1)
Loading...
Searching...
No Matches
qgsgoochmaterialsettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgoochmaterialsettings.cpp
3 --------------------------------------
4 Date : July 2020
5 Copyright : (C) 2020 by Nyall Dawson
6 Email : nyall dot dawson 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 <QString>
21#include <QUrl>
22
23using namespace Qt::StringLiterals;
24
26{
27 return u"gooch"_s;
28}
29
34
53
58
60{
61 const QgsGoochMaterialSettings *otherGooch = dynamic_cast<const QgsGoochMaterialSettings *>( other );
62 if ( !otherGooch )
63 return false;
64
65 return *this == *otherGooch;
66}
67
72
74{
75 const double kDiffuse = 0.8;
76 const double kSpecular = 0.2;
77
78 double red = 0.5 * kDiffuse * ( ( mCool.redF() + mAlpha * mDiffuse.redF() ) + ( mWarm.redF() + mBeta * mDiffuse.redF() ) ) + kSpecular * mSpecular.redF();
79 double green = 0.5 * kDiffuse * ( ( mCool.greenF() + mAlpha * mDiffuse.greenF() ) + ( mWarm.greenF() + mBeta * mDiffuse.greenF() ) ) + kSpecular * mSpecular.greenF();
80 double blue = 0.5 * kDiffuse * ( ( mCool.blueF() + mAlpha * mDiffuse.blueF() ) + ( mWarm.blueF() + mBeta * mDiffuse.blueF() ) ) + kSpecular * mSpecular.blueF();
81
82 red = std::clamp( red, 0.0, 1.0 );
83 green = std::clamp( green, 0.0, 1.0 );
84 blue = std::clamp( blue, 0.0, 1.0 );
85
86 return QColor::fromRgbF( static_cast<float>( red ), static_cast<float>( green ), static_cast<float>( blue ) );
87}
88
89void QgsGoochMaterialSettings::setColorsFromBase( const QColor &baseColor, double alpha, double beta )
90{
91 mAlpha = std::clamp( alpha, 0.0, 1.0 );
92 mBeta = std::clamp( beta, 0.0, 1.0 );
93
94 const float alphaF = static_cast<float>( mAlpha );
95 const float betaF = static_cast<float>( mBeta );
96
97 // Cool color: linear interpolation between baseColor and blue
98 // with the alpha parameter
99 mCool.setRedF( baseColor.redF() * ( 1.0f - alphaF ) );
100 mCool.setGreenF( baseColor.greenF() * ( 1.0f - alphaF ) );
101 mCool.setBlueF( baseColor.blueF() * ( 1.0f - alphaF ) + alphaF );
102
103 // Warm color: linear interpolation between baseColor and yellow
104 // with the beta parameter
105 mWarm.setRedF( baseColor.redF() * ( 1.0f - betaF ) + betaF );
106 mWarm.setGreenF( baseColor.greenF() * ( 1.0f - betaF ) + betaF );
107 mWarm.setBlueF( baseColor.blueF() * ( 1.0f - betaF ) );
108
109 // Diffuse color: mid-range between warm and cool
110 mDiffuse.setRedF( 0.5f * ( mWarm.redF() + mCool.redF() ) );
111 mDiffuse.setGreenF( 0.5f * ( mWarm.greenF() + mCool.greenF() ) );
112 mDiffuse.setBlueF( 0.5f * ( mWarm.blueF() + mCool.blueF() ) );
113
114 // Specular: soft highlight
115 mSpecular = QColor::fromRgbF( 0.4f, 0.4f, 0.4f );
116}
117
118void QgsGoochMaterialSettings::setColorsFromBase( const QColor &baseColor )
119{
120 return setColorsFromBase( baseColor, mAlpha, mBeta );
121}
122
123void QgsGoochMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
124{
125 mWarm = QgsColorUtils::colorFromString( elem.attribute( u"warm"_s, u"107,0,107"_s ) );
126 mCool = QgsColorUtils::colorFromString( elem.attribute( u"cool"_s, u"255,130,0"_s ) );
127 mDiffuse = QgsColorUtils::colorFromString( elem.attribute( u"diffuse"_s, u"178,178,178"_s ) );
128 mSpecular = QgsColorUtils::colorFromString( elem.attribute( u"specular"_s ) );
129 mShininess = elem.attribute( u"shininess2"_s, u"100"_s ).toDouble();
130 mAlpha = elem.attribute( u"alpha"_s, u"0.25"_s ).toDouble();
131 mBeta = elem.attribute( u"beta"_s, u"0.5"_s ).toDouble();
132
134}
135
136void QgsGoochMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
137{
138 elem.setAttribute( u"warm"_s, QgsColorUtils::colorToString( mWarm ) );
139 elem.setAttribute( u"cool"_s, QgsColorUtils::colorToString( mCool ) );
140 elem.setAttribute( u"diffuse"_s, QgsColorUtils::colorToString( mDiffuse ) );
141 elem.setAttribute( u"specular"_s, QgsColorUtils::colorToString( mSpecular ) );
142 elem.setAttribute( u"shininess2"_s, mShininess );
143 elem.setAttribute( u"alpha"_s, mAlpha );
144 elem.setAttribute( u"beta"_s, mBeta );
145
147}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4375
@ Points
Point based rendering, requires point data.
Definition qgis.h:4379
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4376
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4381
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4377
@ Billboards
Flat billboard rendering.
Definition qgis.h:4383
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4382
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4378
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4380
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.
QgsGoochMaterialSettings * clone() const override
Clones the material settings.
QColor averageColor() const override
Returns an approximate color representing the blended material color.
double alpha() const
Returns the alpha value.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
bool equals(const QgsAbstractMaterialSettings *other) const override
Returns true if this settings exactly matches an other settings.
void setColorsFromBase(const QColor &baseColor, double alpha, double beta)
Decomposes an average color into Gooch material components, and sets the material colors accordingly.
QString type() const override
Returns the unique type name for the material.
double beta() const
Returns the beta value.
QgsGoochMaterialSettings()=default
static bool supportsTechnique(Qgis::MaterialRenderingTechnique technique)
Returns true if the specified technique is supported by the Gooch material.
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsGoochMaterialSettings.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
QSet< QgsAbstractMaterialSettings::Property > supportedProperties() const override
Returns the set of data-defined properties supported by this material.
A container for the context for various read/write operations on objects.