23#include <Qt3DCore/QAttribute>
24#include <Qt3DCore/QBuffer>
25#include <Qt3DCore/QGeometry>
26#include <Qt3DRender/QEffect>
27#include <Qt3DRender/QGraphicsApiFilter>
28#include <Qt3DRender/QParameter>
29#include <Qt3DRender/QTechnique>
31using namespace Qt::StringLiterals;
72 return *
this == *otherPhong;
80 mShininess = elem.attribute( u
"shininess"_s ).toDouble();
81 mOpacity = elem.attribute( u
"opacity"_s, u
"1.0"_s ).toDouble();
82 mAmbientCoefficient = elem.attribute( u
"ka"_s, u
"1.0"_s ).toDouble();
83 mDiffuseCoefficient = elem.attribute( u
"kd"_s, u
"1.0"_s ).toDouble();
84 mSpecularCoefficient = elem.attribute( u
"ks"_s, u
"1.0"_s ).toDouble();
94 elem.setAttribute( u
"shininess"_s, mShininess );
95 elem.setAttribute( u
"opacity"_s, mOpacity );
96 elem.setAttribute( u
"ka"_s, mAmbientCoefficient );
97 elem.setAttribute( u
"kd"_s, mDiffuseCoefficient );
98 elem.setAttribute( u
"ks"_s, mSpecularCoefficient );
115 return buildMaterial( context );
126 QMap<QString, QString> parameters;
127 parameters[u
"Kd"_s] = u
"%1 %2 %3"_s.arg( mDiffuse.redF() ).arg( mDiffuse.greenF() ).arg( mDiffuse.blueF() );
128 parameters[u
"Ka"_s] = u
"%1 %2 %3"_s.arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
129 parameters[u
"Ks"_s] = u
"%1 %2 %3"_s.arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
130 parameters[u
"Ns"_s] = QString::number( mShininess );
139 Qt3DRender::QParameter *ambientParameter =
new Qt3DRender::QParameter( u
"ambientColor"_s, QColor::fromRgbF(
ambient.redF() * mAmbientCoefficient,
ambient.greenF() * mAmbientCoefficient,
ambient.blueF() * mAmbientCoefficient ) );
140 Qt3DRender::QParameter *diffuseParameter =
new Qt3DRender::QParameter( u
"diffuseColor"_s, QColor::fromRgbF(
diffuse.redF() * mDiffuseCoefficient,
diffuse.greenF() * mDiffuseCoefficient,
diffuse.blueF() * mDiffuseCoefficient ) );
141 Qt3DRender::QParameter *specularParameter =
new Qt3DRender::QParameter( u
"specularColor"_s, QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) );
142 Qt3DRender::QParameter *shininessParameter =
new Qt3DRender::QParameter( u
"shininess"_s,
static_cast<float>( mShininess ) );
143 Qt3DRender::QParameter *opacityParameter =
new Qt3DRender::QParameter( u
"opacity"_s,
static_cast<float>( mOpacity ) );
145 effect->addParameter( ambientParameter );
146 effect->addParameter( diffuseParameter );
147 effect->addParameter( specularParameter );
148 effect->addParameter( shininessParameter );
149 effect->addParameter( opacityParameter );
159 if ( mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1 )
163 array.resize(
sizeof(
float ) * 9 );
164 float *fptr =
reinterpret_cast<float *
>( array.data() );
166 *fptr++ =
static_cast<float>(
diffuse.redF() * mDiffuseCoefficient );
167 *fptr++ =
static_cast<float>(
diffuse.greenF() * mDiffuseCoefficient );
168 *fptr++ =
static_cast<float>(
diffuse.blueF() * mDiffuseCoefficient );
170 *fptr++ =
static_cast<float>(
ambient.redF() * mAmbientCoefficient );
171 *fptr++ =
static_cast<float>(
ambient.greenF() * mAmbientCoefficient );
172 *fptr++ =
static_cast<float>(
ambient.blueF() * mAmbientCoefficient );
174 *fptr++ =
static_cast<float>(
specular.redF() * mSpecularCoefficient );
175 *fptr++ =
static_cast<float>(
specular.greenF() * mSpecularCoefficient );
176 *fptr++ =
static_cast<float>(
specular.blueF() * mSpecularCoefficient );
180 array.resize(
sizeof(
unsigned char ) * 9 );
181 unsigned char *ptr =
reinterpret_cast<unsigned char *
>( array.data() );
183 *ptr++ =
static_cast<unsigned char>(
diffuse.red() );
184 *ptr++ =
static_cast<unsigned char>(
diffuse.green() );
185 *ptr++ =
static_cast<unsigned char>(
diffuse.blue() );
187 *ptr++ =
static_cast<unsigned char>(
ambient.red() );
188 *ptr++ =
static_cast<unsigned char>(
ambient.green() );
189 *ptr++ =
static_cast<unsigned char>(
ambient.blue() );
191 *ptr++ =
static_cast<unsigned char>(
specular.red() );
192 *ptr++ =
static_cast<unsigned char>(
specular.green() );
193 *ptr++ =
static_cast<unsigned char>(
specular.blue() );
203 Qt3DCore::QBuffer *dataBuffer =
new Qt3DCore::QBuffer( geometry );
207 const bool useFloats = mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1;
209 Qt3DCore::QAttribute *diffuseAttribute =
new Qt3DCore::QAttribute( geometry );
210 diffuseAttribute->setName( u
"dataDefinedDiffuseColor"_s );
211 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
212 diffuseAttribute->setVertexSize( 3 );
213 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
214 diffuseAttribute->setBuffer( dataBuffer );
215 diffuseAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
216 diffuseAttribute->setByteOffset( 0 );
217 diffuseAttribute->setCount( vertexCount );
218 geometry->addAttribute( diffuseAttribute );
220 Qt3DCore::QAttribute *ambientAttribute =
new Qt3DCore::QAttribute( geometry );
221 ambientAttribute->setName( u
"dataDefinedAmbiantColor"_s );
222 ambientAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
223 ambientAttribute->setVertexSize( 3 );
224 ambientAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
225 ambientAttribute->setBuffer( dataBuffer );
226 ambientAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
227 ambientAttribute->setByteOffset( 3 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
228 ambientAttribute->setCount( vertexCount );
229 geometry->addAttribute( ambientAttribute );
231 Qt3DCore::QAttribute *specularAttribute =
new Qt3DCore::QAttribute( geometry );
232 specularAttribute->setName( u
"dataDefinedSpecularColor"_s );
233 specularAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
234 specularAttribute->setVertexSize( 3 );
235 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
236 specularAttribute->setBuffer( dataBuffer );
237 specularAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
238 specularAttribute->setByteOffset( 6 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
239 specularAttribute->setCount( vertexCount );
240 geometry->addAttribute( specularAttribute );
242 dataBuffer->setData( data );
249 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect( material );
251 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
252 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
253 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
254 technique->graphicsApiFilter()->setMajorVersion( 3 );
255 technique->graphicsApiFilter()->setMinorVersion( 3 );
256 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey();
257 filterKey->setName( u
"renderingStyle"_s );
258 filterKey->setValue( u
"forward"_s );
259 technique->addFilterKey( filterKey );
261 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass();
262 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram();
264 renderPass->setShaderProgram( shaderProgram );
265 technique->addRenderPass( renderPass );
267 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( u
"qrc:/shaders/phong.frag"_s ) );
272 const QUrl urlVert( u
"qrc:/shaders/phongDataDefined.vert"_s );
273 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
275 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
280 const QUrl urlVert( u
"qrc:/shaders/default.vert"_s );
281 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
282 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
287 effect->addParameter(
new Qt3DRender::QParameter( u
"ambientColor"_s, QColor::fromRgbF(
ambient.redF() * mAmbientCoefficient,
ambient.greenF() * mAmbientCoefficient,
ambient.blueF() * mAmbientCoefficient ) ) );
288 effect->addParameter(
new Qt3DRender::QParameter( u
"diffuseColor"_s, QColor::fromRgbF(
diffuse.redF() * mDiffuseCoefficient,
diffuse.greenF() * mDiffuseCoefficient,
diffuse.blueF() * mDiffuseCoefficient ) ) );
289 effect->addParameter(
new Qt3DRender::QParameter( u
"specularColor"_s, QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) ) );
292 effect->addParameter(
new Qt3DRender::QParameter( u
"shininess"_s,
static_cast<float>( mShininess ) ) );
293 effect->addParameter(
new Qt3DRender::QParameter( u
"opacity"_s,
static_cast<float>( mOpacity ) ) );
295 effect->addTechnique( technique );
296 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.
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).