24#include <Qt3DCore/QAttribute>
25#include <Qt3DCore/QBuffer>
26#include <Qt3DCore/QGeometry>
27#include <Qt3DExtras/QGoochMaterial>
28#include <Qt3DRender/QEffect>
29#include <Qt3DRender/QGraphicsApiFilter>
30#include <Qt3DRender/QParameter>
31#include <Qt3DRender/QTechnique>
33using namespace Qt::StringLiterals;
74 return *
this == *otherGooch;
83 mShininess = elem.attribute( u
"shininess2"_s, u
"100"_s ).toDouble();
84 mAlpha = elem.attribute( u
"alpha"_s, u
"0.25"_s ).toDouble();
85 mBeta = elem.attribute( u
"beta"_s, u
"0.5"_s ).toDouble();
96 elem.setAttribute( u
"shininess2"_s, mShininess );
97 elem.setAttribute( u
"alpha"_s, mAlpha );
98 elem.setAttribute( u
"beta"_s, mBeta );
105 return QMap<QString, QString>();
119 return new QgsHighlightMaterial( technique );
122 return buildMaterial( context );
145 array.resize(
sizeof(
unsigned char ) * 12 );
146 unsigned char *fptr =
reinterpret_cast<unsigned char *
>( array.data() );
148 *fptr++ =
static_cast<unsigned char>(
diffuse.red() );
149 *fptr++ =
static_cast<unsigned char>(
diffuse.green() );
150 *fptr++ =
static_cast<unsigned char>(
diffuse.blue() );
152 *fptr++ =
static_cast<unsigned char>(
warm.red() );
153 *fptr++ =
static_cast<unsigned char>(
warm.green() );
154 *fptr++ =
static_cast<unsigned char>(
warm.blue() );
156 *fptr++ =
static_cast<unsigned char>(
cool.red() );
157 *fptr++ =
static_cast<unsigned char>(
cool.green() );
158 *fptr++ =
static_cast<unsigned char>(
cool.blue() );
160 *fptr++ =
static_cast<unsigned char>(
specular.red() );
161 *fptr++ =
static_cast<unsigned char>(
specular.green() );
162 *fptr++ =
static_cast<unsigned char>(
specular.blue() );
169 return 12 *
sizeof(
unsigned char );
174 Qt3DCore::QBuffer *dataBuffer =
new Qt3DCore::QBuffer( geometry );
176 Qt3DCore::QAttribute *diffuseAttribute =
new Qt3DCore::QAttribute( geometry );
177 diffuseAttribute->setName( u
"dataDefinedDiffuseColor"_s );
178 diffuseAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
179 diffuseAttribute->setVertexSize( 3 );
180 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
181 diffuseAttribute->setBuffer( dataBuffer );
182 diffuseAttribute->setByteStride( 12 *
sizeof(
unsigned char ) );
183 diffuseAttribute->setByteOffset( 0 );
184 diffuseAttribute->setCount( vertexCount );
185 geometry->addAttribute( diffuseAttribute );
187 Qt3DCore::QAttribute *warmAttribute =
new Qt3DCore::QAttribute( geometry );
188 warmAttribute->setName( u
"dataDefinedWarmColor"_s );
189 warmAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
190 warmAttribute->setVertexSize( 3 );
191 warmAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
192 warmAttribute->setBuffer( dataBuffer );
193 warmAttribute->setByteStride( 12 *
sizeof(
unsigned char ) );
194 warmAttribute->setByteOffset( 3 *
sizeof(
unsigned char ) );
195 warmAttribute->setCount( vertexCount );
196 geometry->addAttribute( warmAttribute );
198 Qt3DCore::QAttribute *coolAttribute =
new Qt3DCore::QAttribute( geometry );
199 coolAttribute->setName( u
"dataDefinedCoolColor"_s );
200 coolAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
201 coolAttribute->setVertexSize( 3 );
202 coolAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
203 coolAttribute->setBuffer( dataBuffer );
204 coolAttribute->setByteStride( 12 *
sizeof(
unsigned char ) );
205 coolAttribute->setByteOffset( 6 *
sizeof(
unsigned char ) );
206 coolAttribute->setCount( vertexCount );
207 geometry->addAttribute( coolAttribute );
210 Qt3DCore::QAttribute *specularAttribute =
new Qt3DCore::QAttribute( geometry );
211 specularAttribute->setName( u
"dataDefinedSpecularColor"_s );
212 specularAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
213 specularAttribute->setVertexSize( 3 );
214 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
215 specularAttribute->setBuffer( dataBuffer );
216 specularAttribute->setByteStride( 12 *
sizeof(
unsigned char ) );
217 specularAttribute->setByteOffset( 9 *
sizeof(
unsigned char ) );
218 specularAttribute->setCount( vertexCount );
219 geometry->addAttribute( specularAttribute );
221 dataBuffer->setData( data );
228 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect( material );
230 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
231 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
232 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
233 technique->graphicsApiFilter()->setMajorVersion( 3 );
234 technique->graphicsApiFilter()->setMinorVersion( 3 );
235 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey();
236 filterKey->setName( u
"renderingStyle"_s );
237 filterKey->setValue( u
"forward"_s );
238 technique->addFilterKey( filterKey );
240 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass();
241 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram();
243 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( u
"qrc:/shaders/gooch.frag"_s ) );
248 const QUrl urlVert( u
"qrc:/shaders/goochDataDefined.vert"_s );
249 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
252 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
257 const QUrl urlVert( u
"qrc:/shaders/default.vert"_s );
258 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
259 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
262 effect->addParameter(
new Qt3DRender::QParameter( u
"kd"_s, diffuseColor ) );
263 effect->addParameter(
new Qt3DRender::QParameter( u
"ks"_s, mSpecular ) );
264 effect->addParameter(
new Qt3DRender::QParameter( u
"kblue"_s, mCool ) );
265 effect->addParameter(
new Qt3DRender::QParameter( u
"kyellow"_s, mWarm ) );
268 renderPass->setShaderProgram( shaderProgram );
269 technique->addRenderPass( renderPass );
271 technique->addParameter(
new Qt3DRender::QParameter( u
"shininess"_s, mShininess ) );
272 technique->addParameter(
new Qt3DRender::QParameter( u
"alpha"_s, mAlpha ) );
273 technique->addParameter(
new Qt3DRender::QParameter( u
"beta"_s, mBeta ) );
275 effect->addTechnique( technique );
276 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.
@ Warm
Warm color (gooch material).
@ Cool
Cool color (gooch material).
@ Specular
Specular color.
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...
QgsGoochMaterialSettings * clone() const override
Clones the material settings.
QColor specular() const
Returns specular color component.
QColor cool() const
Returns cool color component.
int dataDefinedByteStride() const override
Returns byte stride of the data defined colors,used to fill the vertex colors data defined buffer for...
QByteArray dataDefinedVertexColorsAsByte(const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
QMap< QString, QString > toExportParameters() const override
Returns the parameters to be exported to .mtl file.
QColor warm() const
Returns warm color component.
bool equals(const QgsAbstractMaterialSettings *other) const override
Returns true if this settings exactly matches an other settings.
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...
QString type() const override
Returns the unique type name for the material.
QColor diffuse() const
Returns diffuse color component.
QgsGoochMaterialSettings()=default
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsGoochMaterialSettings.
static bool supportsTechnique(QgsMaterialSettingsRenderingTechnique technique)
Returns true if the specified technique is supported by the Gooch material.
QgsMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext) const override
Adds parameters from the material to a destination effect.
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.
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).