20#include <Qt3DExtras/QGoochMaterial>
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>
46 return QStringLiteral(
"gooch" );
83 mShininess = elem.attribute( QStringLiteral(
"shininess2" ), QStringLiteral(
"100" ) ).toFloat();
84 mAlpha = elem.attribute( QStringLiteral(
"alpha" ), QStringLiteral(
"0.25" ) ).toFloat();
85 mBeta = elem.attribute( QStringLiteral(
"beta" ), QStringLiteral(
"0.5" ) ).toFloat();
96 elem.setAttribute( QStringLiteral(
"shininess2" ), mShininess );
97 elem.setAttribute( QStringLiteral(
"alpha" ), mAlpha );
98 elem.setAttribute( QStringLiteral(
"beta" ), mBeta );
105 return QMap<QString, QString>();
117 return buildMaterial( context );
142 array.resize(
sizeof(
unsigned char ) * 12 );
143 unsigned char *fptr =
reinterpret_cast<unsigned char *
>( array.data() );
145 *fptr++ =
static_cast<unsigned char>(
diffuse.red() );
146 *fptr++ =
static_cast<unsigned char>(
diffuse.green() );
147 *fptr++ =
static_cast<unsigned char>(
diffuse.blue() );
149 *fptr++ =
static_cast<unsigned char>(
warm.red() );
150 *fptr++ =
static_cast<unsigned char>(
warm.green() );
151 *fptr++ =
static_cast<unsigned char>(
warm.blue() );
153 *fptr++ =
static_cast<unsigned char>(
cool.red() );
154 *fptr++ =
static_cast<unsigned char>(
cool.green() );
155 *fptr++ =
static_cast<unsigned char>(
cool.blue() );
157 *fptr++ =
static_cast<unsigned char>(
specular.red() );
158 *fptr++ =
static_cast<unsigned char>(
specular.green() );
159 *fptr++ =
static_cast<unsigned char>(
specular.blue() );
166 return 12 *
sizeof(
unsigned char );
174 diffuseAttribute->setName( QStringLiteral(
"dataDefinedDiffuseColor" ) );
175 diffuseAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
176 diffuseAttribute->setVertexSize( 3 );
177 diffuseAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
178 diffuseAttribute->setBuffer( dataBuffer );
179 diffuseAttribute->setByteStride( 12 *
sizeof(
unsigned char ) );
180 diffuseAttribute->setByteOffset( 0 );
181 diffuseAttribute->setCount( vertexCount );
182 geometry->addAttribute( diffuseAttribute );
185 warmAttribute->setName( QStringLiteral(
"dataDefinedWarmColor" ) );
186 warmAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
187 warmAttribute->setVertexSize( 3 );
188 warmAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
189 warmAttribute->setBuffer( dataBuffer );
190 warmAttribute->setByteStride( 12 *
sizeof(
unsigned char ) );
191 warmAttribute->setByteOffset( 3 *
sizeof(
unsigned char ) );
192 warmAttribute->setCount( vertexCount );
193 geometry->addAttribute( warmAttribute );
196 coolAttribute->setName( QStringLiteral(
"dataDefinedCoolColor" ) );
197 coolAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
198 coolAttribute->setVertexSize( 3 );
199 coolAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
200 coolAttribute->setBuffer( dataBuffer );
201 coolAttribute->setByteStride( 12 *
sizeof(
unsigned char ) );
202 coolAttribute->setByteOffset( 6 *
sizeof(
unsigned char ) );
203 coolAttribute->setCount( vertexCount );
204 geometry->addAttribute( coolAttribute );
208 specularAttribute->setName( QStringLiteral(
"dataDefinedSpecularColor" ) );
209 specularAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
210 specularAttribute->setVertexSize( 3 );
211 specularAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
212 specularAttribute->setBuffer( dataBuffer );
213 specularAttribute->setByteStride( 12 *
sizeof(
unsigned char ) );
214 specularAttribute->setByteOffset( 9 *
sizeof(
unsigned char ) );
215 specularAttribute->setCount( vertexCount );
216 geometry->addAttribute( specularAttribute );
218 dataBuffer->setData( data );
225 Qt3DRender::QEffect *effect =
new Qt3DRender::QEffect( material );
227 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
228 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
229 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
230 technique->graphicsApiFilter()->setMajorVersion( 3 );
231 technique->graphicsApiFilter()->setMinorVersion( 3 );
232 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey();
233 filterKey->setName( QStringLiteral(
"renderingStyle" ) );
234 filterKey->setValue( QStringLiteral(
"forward" ) );
235 technique->addFilterKey( filterKey );
237 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass();
238 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram();
240 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/gooch.frag" ) ) );
245 const QUrl urlVert( QStringLiteral(
"qrc:/shaders/goochDataDefined.vert" ) );
246 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
249 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
254 const QUrl urlVert( QStringLiteral(
"qrc:/shaders/default.vert" ) );
255 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
256 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
259 effect->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"kd" ), diffuseColor ) );
260 effect->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"ks" ), mSpecular ) );
261 effect->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"kblue" ), mCool ) );
262 effect->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"kyellow" ), mWarm ) );
265 renderPass->setShaderProgram( shaderProgram );
266 technique->addRenderPass( renderPass );
268 technique->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"shininess" ), mShininess ) );
269 technique->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"alpha" ), mAlpha ) );
270 technique->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"beta" ), mBeta ) );
272 effect->addTechnique( technique );
273 material->setEffect( effect );
static QByteArray addDefinesToShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Inserts some define macros into a shader source code.
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.
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.
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.
The class is used as a container of context for various read/write operations on other objects.
QgsMaterialSettingsRenderingTechnique
Material rendering techniques 3.
@ 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
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry