24#include <Qt3DCore/QAttribute>
25#include <Qt3DCore/QBuffer>
26#include <Qt3DCore/QGeometry>
27#include <Qt3DRender/QEffect>
28#include <Qt3DRender/QGraphicsApiFilter>
29#include <Qt3DRender/QParameter>
30#include <Qt3DRender/QTechnique>
32using namespace Qt::StringLiterals;
73 return *
this == *otherPhong;
81 mShininess = elem.attribute( u
"shininess"_s ).toDouble();
82 mOpacity = elem.attribute( u
"opacity"_s, u
"1.0"_s ).toDouble();
83 mAmbientCoefficient = elem.attribute( u
"ka"_s, u
"1.0"_s ).toDouble();
84 mDiffuseCoefficient = elem.attribute( u
"kd"_s, u
"1.0"_s ).toDouble();
85 mSpecularCoefficient = elem.attribute( u
"ks"_s, u
"1.0"_s ).toDouble();
95 elem.setAttribute( u
"shininess"_s, mShininess );
96 elem.setAttribute( u
"opacity"_s, mOpacity );
97 elem.setAttribute( u
"ka"_s, mAmbientCoefficient );
98 elem.setAttribute( u
"kd"_s, mDiffuseCoefficient );
99 elem.setAttribute( u
"ks"_s, mSpecularCoefficient );
118 return new QgsHighlightMaterial( technique );
121 return buildMaterial( context );
132 QMap<QString, QString> parameters;
133 parameters[u
"Kd"_s] = u
"%1 %2 %3"_s.arg( mDiffuse.redF() ).arg( mDiffuse.greenF() ).arg( mDiffuse.blueF() );
134 parameters[u
"Ka"_s] = u
"%1 %2 %3"_s.arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
135 parameters[u
"Ks"_s] = u
"%1 %2 %3"_s.arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
136 parameters[u
"Ns"_s] = QString::number( mShininess );
145 Qt3DRender::QParameter *ambientParameter =
new Qt3DRender::QParameter( u
"ambientColor"_s, QColor::fromRgbF(
ambient.redF() * mAmbientCoefficient,
ambient.greenF() * mAmbientCoefficient,
ambient.blueF() * mAmbientCoefficient ) );
146 Qt3DRender::QParameter *diffuseParameter =
new Qt3DRender::QParameter( u
"diffuseColor"_s, QColor::fromRgbF(
diffuse.redF() * mDiffuseCoefficient,
diffuse.greenF() * mDiffuseCoefficient,
diffuse.blueF() * mDiffuseCoefficient ) );
147 Qt3DRender::QParameter *specularParameter =
new Qt3DRender::QParameter( u
"specularColor"_s, QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) );
148 Qt3DRender::QParameter *shininessParameter =
new Qt3DRender::QParameter( u
"shininess"_s,
static_cast<float>( mShininess ) );
149 Qt3DRender::QParameter *opacityParameter =
new Qt3DRender::QParameter( u
"opacity"_s,
static_cast<float>( mOpacity ) );
151 effect->addParameter( ambientParameter );
152 effect->addParameter( diffuseParameter );
153 effect->addParameter( specularParameter );
154 effect->addParameter( shininessParameter );
155 effect->addParameter( opacityParameter );
165 if ( mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1 )
169 array.resize(
sizeof(
float ) * 9 );
170 float *fptr =
reinterpret_cast<float *
>( array.data() );
172 *fptr++ =
static_cast<float>(
diffuse.redF() * mDiffuseCoefficient );
173 *fptr++ =
static_cast<float>(
diffuse.greenF() * mDiffuseCoefficient );
174 *fptr++ =
static_cast<float>(
diffuse.blueF() * mDiffuseCoefficient );
176 *fptr++ =
static_cast<float>(
ambient.redF() * mAmbientCoefficient );
177 *fptr++ =
static_cast<float>(
ambient.greenF() * mAmbientCoefficient );
178 *fptr++ =
static_cast<float>(
ambient.blueF() * mAmbientCoefficient );
180 *fptr++ =
static_cast<float>(
specular.redF() * mSpecularCoefficient );
181 *fptr++ =
static_cast<float>(
specular.greenF() * mSpecularCoefficient );
182 *fptr++ =
static_cast<float>(
specular.blueF() * mSpecularCoefficient );
186 array.resize(
sizeof(
unsigned char ) * 9 );
187 unsigned char *ptr =
reinterpret_cast<unsigned char *
>( array.data() );
189 *ptr++ =
static_cast<unsigned char>(
diffuse.red() );
190 *ptr++ =
static_cast<unsigned char>(
diffuse.green() );
191 *ptr++ =
static_cast<unsigned char>(
diffuse.blue() );
193 *ptr++ =
static_cast<unsigned char>(
ambient.red() );
194 *ptr++ =
static_cast<unsigned char>(
ambient.green() );
195 *ptr++ =
static_cast<unsigned char>(
ambient.blue() );
197 *ptr++ =
static_cast<unsigned char>(
specular.red() );
198 *ptr++ =
static_cast<unsigned char>(
specular.green() );
199 *ptr++ =
static_cast<unsigned char>(
specular.blue() );
209 Qt3DCore::QBuffer *dataBuffer =
new Qt3DCore::QBuffer( geometry );
213 const bool useFloats = mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1;
215 Qt3DCore::QAttribute *diffuseAttribute =
new Qt3DCore::QAttribute( geometry );
216 diffuseAttribute->setName( u
"dataDefinedDiffuseColor"_s );
217 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
218 diffuseAttribute->setVertexSize( 3 );
219 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
220 diffuseAttribute->setBuffer( dataBuffer );
221 diffuseAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
222 diffuseAttribute->setByteOffset( 0 );
223 diffuseAttribute->setCount( vertexCount );
224 geometry->addAttribute( diffuseAttribute );
226 Qt3DCore::QAttribute *ambientAttribute =
new Qt3DCore::QAttribute( geometry );
227 ambientAttribute->setName( u
"dataDefinedAmbiantColor"_s );
228 ambientAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
229 ambientAttribute->setVertexSize( 3 );
230 ambientAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
231 ambientAttribute->setBuffer( dataBuffer );
232 ambientAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
233 ambientAttribute->setByteOffset( 3 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
234 ambientAttribute->setCount( vertexCount );
235 geometry->addAttribute( ambientAttribute );
237 Qt3DCore::QAttribute *specularAttribute =
new Qt3DCore::QAttribute( geometry );
238 specularAttribute->setName( u
"dataDefinedSpecularColor"_s );
239 specularAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
240 specularAttribute->setVertexSize( 3 );
241 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
242 specularAttribute->setBuffer( dataBuffer );
243 specularAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
244 specularAttribute->setByteOffset( 6 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
245 specularAttribute->setCount( vertexCount );
246 geometry->addAttribute( specularAttribute );
248 dataBuffer->setData( data );
255 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect( material );
257 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
258 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
259 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
260 technique->graphicsApiFilter()->setMajorVersion( 3 );
261 technique->graphicsApiFilter()->setMinorVersion( 3 );
262 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey();
263 filterKey->setName( u
"renderingStyle"_s );
264 filterKey->setValue( u
"forward"_s );
265 technique->addFilterKey( filterKey );
267 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass();
268 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram();
270 renderPass->setShaderProgram( shaderProgram );
271 technique->addRenderPass( renderPass );
273 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( u
"qrc:/shaders/phong.frag"_s ) );
278 const QUrl urlVert( u
"qrc:/shaders/phongDataDefined.vert"_s );
279 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
281 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
286 const QUrl urlVert( u
"qrc:/shaders/default.vert"_s );
287 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
288 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
293 effect->addParameter(
new Qt3DRender::QParameter( u
"ambientColor"_s, QColor::fromRgbF(
ambient.redF() * mAmbientCoefficient,
ambient.greenF() * mAmbientCoefficient,
ambient.blueF() * mAmbientCoefficient ) ) );
294 effect->addParameter(
new Qt3DRender::QParameter( u
"diffuseColor"_s, QColor::fromRgbF(
diffuse.redF() * mDiffuseCoefficient,
diffuse.greenF() * mDiffuseCoefficient,
diffuse.blueF() * mDiffuseCoefficient ) ) );
295 effect->addParameter(
new Qt3DRender::QParameter( u
"specularColor"_s, QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) ) );
298 effect->addParameter(
new Qt3DRender::QParameter( u
"shininess"_s,
static_cast<float>( mShininess ) ) );
299 effect->addParameter(
new Qt3DRender::QParameter( u
"opacity"_s,
static_cast<float>( mOpacity ) ) );
301 effect->addTechnique( technique );
302 material->setEffect( effect );
static QByteArray addDefinesToShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Inserts some define macros into a shader source code.
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.
@ Specular
Specular color.
@ Ambient
Ambient color (phong material).
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.
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.
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.
int dataDefinedByteStride() const override
Returns byte stride of the data defined colors,used to fill the vertex colors data defined buffer for...
QgsMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
bool equals(const QgsAbstractMaterialSettings *other) const override
Returns true if this settings exactly matches an other settings.
QString type() const override
Returns the unique type name for the material.
QColor diffuse() const
Returns diffuse color component.
QMap< QString, QString > toExportParameters() const override
Returns the parameters to be exported to .mtl file.
void applyDataDefinedToGeometry(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...
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsPhongMaterialSettings.
static bool supportsTechnique(QgsMaterialSettingsRenderingTechnique technique)
Returns true if the specified technique is supported by the Phong material.
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.
QByteArray dataDefinedVertexColorsAsByte(const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext) const override
Adds parameters from the material to a destination effect.
A container for the context for various read/write operations on objects.
QgsMaterialSettingsRenderingTechnique
Material rendering techniques.
@ Points
Point based rendering, requires point data.
@ Triangles
Triangle based rendering (default).
@ TrianglesFromModel
Triangle based rendering, using a model object source.
@ Lines
Line based rendering, requires line data.
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).