21#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
22#include <Qt3DRender/QAttribute>
23#include <Qt3DRender/QBuffer>
24#include <Qt3DRender/QGeometry>
30#include <Qt3DCore/QAttribute>
31#include <Qt3DCore/QBuffer>
32#include <Qt3DCore/QGeometry>
38#include <Qt3DRender/QParameter>
39#include <Qt3DRender/QEffect>
40#include <Qt3DRender/QTechnique>
41#include <Qt3DRender/QGraphicsApiFilter>
47 return QStringLiteral(
"phong" );
84 return *
this == *otherPhong;
92 mShininess = elem.attribute( QStringLiteral(
"shininess" ) ).toDouble();
93 mOpacity = elem.attribute( QStringLiteral(
"opacity" ), QStringLiteral(
"1.0" ) ).toDouble();
94 mAmbientCoefficient = elem.attribute( QStringLiteral(
"ka" ), QStringLiteral(
"1.0" ) ).toDouble();
95 mDiffuseCoefficient = elem.attribute( QStringLiteral(
"kd" ), QStringLiteral(
"1.0" ) ).toDouble();
96 mSpecularCoefficient = elem.attribute( QStringLiteral(
"ks" ), QStringLiteral(
"1.0" ) ).toDouble();
106 elem.setAttribute( QStringLiteral(
"shininess" ), mShininess );
107 elem.setAttribute( QStringLiteral(
"opacity" ), mOpacity );
108 elem.setAttribute( QStringLiteral(
"ka" ), mAmbientCoefficient );
109 elem.setAttribute( QStringLiteral(
"kd" ), mDiffuseCoefficient );
110 elem.setAttribute( QStringLiteral(
"ks" ), mSpecularCoefficient );
127 return buildMaterial( context );
138 QMap<QString, QString> parameters;
139 parameters[QStringLiteral(
"Kd" )] = QStringLiteral(
"%1 %2 %3" ).arg( mDiffuse.redF() ).arg( mDiffuse.greenF() ).arg( mDiffuse.blueF() );
140 parameters[QStringLiteral(
"Ka" )] = QStringLiteral(
"%1 %2 %3" ).arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
141 parameters[QStringLiteral(
"Ks" )] = QStringLiteral(
"%1 %2 %3" ).arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
142 parameters[QStringLiteral(
"Ns" )] = QString::number( mShininess );
151 Qt3DRender::QParameter *ambientParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ambientColor" ), QColor::fromRgbF(
ambient.redF() * mAmbientCoefficient,
ambient.greenF() * mAmbientCoefficient,
ambient.blueF() * mAmbientCoefficient ) );
152 Qt3DRender::QParameter *diffuseParameter =
new Qt3DRender::QParameter( QStringLiteral(
"diffuseColor" ), QColor::fromRgbF(
diffuse.redF() * mDiffuseCoefficient,
diffuse.greenF() * mDiffuseCoefficient,
diffuse.blueF() * mDiffuseCoefficient ) );
153 Qt3DRender::QParameter *specularParameter =
new Qt3DRender::QParameter( QStringLiteral(
"specularColor" ), QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) );
154 Qt3DRender::QParameter *shininessParameter =
new Qt3DRender::QParameter( QStringLiteral(
"shininess" ),
static_cast<float>( mShininess ) );
155 Qt3DRender::QParameter *opacityParameter =
new Qt3DRender::QParameter( QStringLiteral(
"opacity" ),
static_cast<float>( mOpacity ) );
157 effect->addParameter( ambientParameter );
158 effect->addParameter( diffuseParameter );
159 effect->addParameter( specularParameter );
160 effect->addParameter( shininessParameter );
161 effect->addParameter( opacityParameter );
171 if ( mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1 )
175 array.resize(
sizeof(
float ) * 9 );
176 float *fptr =
reinterpret_cast<float *
>( array.data() );
178 *fptr++ =
static_cast<float>(
diffuse.redF() * mDiffuseCoefficient );
179 *fptr++ =
static_cast<float>(
diffuse.greenF() * mDiffuseCoefficient );
180 *fptr++ =
static_cast<float>(
diffuse.blueF() * mDiffuseCoefficient );
182 *fptr++ =
static_cast<float>(
ambient.redF() * mAmbientCoefficient );
183 *fptr++ =
static_cast<float>(
ambient.greenF() * mAmbientCoefficient );
184 *fptr++ =
static_cast<float>(
ambient.blueF() * mAmbientCoefficient );
186 *fptr++ =
static_cast<float>(
specular.redF() * mSpecularCoefficient );
187 *fptr++ =
static_cast<float>(
specular.greenF() * mSpecularCoefficient );
188 *fptr++ =
static_cast<float>(
specular.blueF() * mSpecularCoefficient );
192 array.resize(
sizeof(
unsigned char ) * 9 );
193 unsigned char *ptr =
reinterpret_cast<unsigned char *
>( array.data() );
195 *ptr++ =
static_cast<unsigned char>(
diffuse.red() );
196 *ptr++ =
static_cast<unsigned char>(
diffuse.green() );
197 *ptr++ =
static_cast<unsigned char>(
diffuse.blue() );
199 *ptr++ =
static_cast<unsigned char>(
ambient.red() );
200 *ptr++ =
static_cast<unsigned char>(
ambient.green() );
201 *ptr++ =
static_cast<unsigned char>(
ambient.blue() );
203 *ptr++ =
static_cast<unsigned char>(
specular.red() );
204 *ptr++ =
static_cast<unsigned char>(
specular.green() );
205 *ptr++ =
static_cast<unsigned char>(
specular.blue() );
219 const bool useFloats = mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1;
222 diffuseAttribute->setName( QStringLiteral(
"dataDefinedDiffuseColor" ) );
223 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
224 diffuseAttribute->setVertexSize( 3 );
225 diffuseAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
226 diffuseAttribute->setBuffer( dataBuffer );
227 diffuseAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
228 diffuseAttribute->setByteOffset( 0 );
229 diffuseAttribute->setCount( vertexCount );
230 geometry->addAttribute( diffuseAttribute );
233 ambientAttribute->setName( QStringLiteral(
"dataDefinedAmbiantColor" ) );
234 ambientAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
235 ambientAttribute->setVertexSize( 3 );
236 ambientAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
237 ambientAttribute->setBuffer( dataBuffer );
238 ambientAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
239 ambientAttribute->setByteOffset( 3 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
240 ambientAttribute->setCount( vertexCount );
241 geometry->addAttribute( ambientAttribute );
244 specularAttribute->setName( QStringLiteral(
"dataDefinedSpecularColor" ) );
245 specularAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
246 specularAttribute->setVertexSize( 3 );
247 specularAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
248 specularAttribute->setBuffer( dataBuffer );
249 specularAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
250 specularAttribute->setByteOffset( 6 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
251 specularAttribute->setCount( vertexCount );
252 geometry->addAttribute( specularAttribute );
254 dataBuffer->setData( data );
261 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect( material );
263 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
264 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
265 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
266 technique->graphicsApiFilter()->setMajorVersion( 3 );
267 technique->graphicsApiFilter()->setMinorVersion( 3 );
268 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey();
269 filterKey->setName( QStringLiteral(
"renderingStyle" ) );
270 filterKey->setValue( QStringLiteral(
"forward" ) );
271 technique->addFilterKey( filterKey );
273 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass();
274 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram();
276 renderPass->setShaderProgram( shaderProgram );
277 technique->addRenderPass( renderPass );
279 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/phong.frag" ) ) );
284 const QUrl urlVert( QStringLiteral(
"qrc:/shaders/phongDataDefined.vert" ) );
285 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
287 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
292 const QUrl urlVert( QStringLiteral(
"qrc:/shaders/default.vert" ) );
293 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
294 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
299 effect->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"ambientColor" ), QColor::fromRgbF(
ambient.redF() * mAmbientCoefficient,
ambient.greenF() * mAmbientCoefficient,
ambient.blueF() * mAmbientCoefficient ) ) );
300 effect->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"diffuseColor" ), QColor::fromRgbF(
diffuse.redF() * mDiffuseCoefficient,
diffuse.greenF() * mDiffuseCoefficient,
diffuse.blueF() * mDiffuseCoefficient ) ) );
301 effect->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"specularColor" ), QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) ) );
304 effect->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"shininess" ),
static_cast<float>( mShininess ) ) );
305 effect->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"opacity" ),
static_cast<float>( mOpacity ) ) );
307 effect->addTechnique( technique );
308 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).
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry