19#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
20#include <Qt3DRender/QAttribute>
21#include <Qt3DRender/QBuffer>
22#include <Qt3DRender/QGeometry>
28#include <Qt3DCore/QAttribute>
29#include <Qt3DCore/QBuffer>
30#include <Qt3DCore/QGeometry>
36#include <Qt3DRender/QParameter>
37#include <Qt3DRender/QEffect>
38#include <Qt3DRender/QTechnique>
39#include <Qt3DRender/QGraphicsApiFilter>
45 return QStringLiteral(
"phong" );
81 mShininess = elem.attribute( QStringLiteral(
"shininess" ) ).toDouble();
82 mOpacity = elem.attribute( QStringLiteral(
"opacity" ), QStringLiteral(
"1.0" ) ).toDouble();
83 mAmbientCoefficient = elem.attribute( QStringLiteral(
"ka" ), QStringLiteral(
"1.0" ) ).toDouble();
84 mDiffuseCoefficient = elem.attribute( QStringLiteral(
"kd" ), QStringLiteral(
"1.0" ) ).toDouble();
85 mSpecularCoefficient = elem.attribute( QStringLiteral(
"ks" ), QStringLiteral(
"1.0" ) ).toDouble();
95 elem.setAttribute( QStringLiteral(
"shininess" ), mShininess );
96 elem.setAttribute( QStringLiteral(
"opacity" ), mOpacity );
97 elem.setAttribute( QStringLiteral(
"ka" ), mAmbientCoefficient );
98 elem.setAttribute( QStringLiteral(
"kd" ), mDiffuseCoefficient );
99 elem.setAttribute( QStringLiteral(
"ks" ), mSpecularCoefficient );
117 return dataDefinedMaterial();
119 return constantColorMaterial( context );
130 QMap<QString, QString> parameters;
131 parameters[ QStringLiteral(
"Kd" ) ] = QStringLiteral(
"%1 %2 %3" ).arg( mDiffuse.redF() ).arg( mDiffuse.greenF() ).arg( mDiffuse.blueF() );
132 parameters[ QStringLiteral(
"Ka" ) ] = QStringLiteral(
"%1 %2 %3" ).arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
133 parameters[ QStringLiteral(
"Ks" ) ] = QStringLiteral(
"%1 %2 %3" ).arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
134 parameters[ QStringLiteral(
"Ns" ) ] = QString::number( mShininess );
143 Qt3DRender::QParameter *ambientParameter =
new Qt3DRender::QParameter( QStringLiteral(
"ambientColor" ),
144 QColor::fromRgbF(
ambient.redF() * mAmbientCoefficient,
145 ambient.greenF() * mAmbientCoefficient,
146 ambient.blueF() * mAmbientCoefficient ) );
147 Qt3DRender::QParameter *diffuseParameter =
new Qt3DRender::QParameter( QStringLiteral(
"diffuseColor" ),
148 QColor::fromRgbF(
diffuse.redF() * mDiffuseCoefficient,
149 diffuse.greenF() * mDiffuseCoefficient,
150 diffuse.blueF() * mDiffuseCoefficient ) );
151 Qt3DRender::QParameter *specularParameter =
new Qt3DRender::QParameter( QStringLiteral(
"specularColor" ),
152 QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient,
153 mSpecular.greenF() * mSpecularCoefficient,
154 mSpecular.blueF() * mSpecularCoefficient ) );
155 Qt3DRender::QParameter *shininessParameter =
new Qt3DRender::QParameter( QStringLiteral(
"shininess" ),
static_cast< float >( mShininess ) );
156 Qt3DRender::QParameter *opacityParameter =
new Qt3DRender::QParameter( QStringLiteral(
"opacity" ),
static_cast< float >( mOpacity ) );
158 effect->addParameter( ambientParameter );
159 effect->addParameter( diffuseParameter );
160 effect->addParameter( specularParameter );
161 effect->addParameter( shininessParameter );
162 effect->addParameter( opacityParameter );
172 if ( mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1 )
176 array.resize(
sizeof(
float ) * 9 );
177 float *fptr =
reinterpret_cast<float *
>( array.data() );
179 *fptr++ =
static_cast<float>(
diffuse.redF() * mDiffuseCoefficient );
180 *fptr++ =
static_cast<float>(
diffuse.greenF() * mDiffuseCoefficient );
181 *fptr++ =
static_cast<float>(
diffuse.blueF() * mDiffuseCoefficient );
183 *fptr++ =
static_cast<float>(
ambient.redF() * mAmbientCoefficient );
184 *fptr++ =
static_cast<float>(
ambient.greenF() * mAmbientCoefficient );
185 *fptr++ =
static_cast<float>(
ambient.blueF() * mAmbientCoefficient );
187 *fptr++ =
static_cast<float>(
specular.redF() * mSpecularCoefficient );
188 *fptr++ =
static_cast<float>(
specular.greenF() * mSpecularCoefficient );
189 *fptr++ =
static_cast<float>(
specular.blueF() * mSpecularCoefficient );
193 array.resize(
sizeof(
unsigned char ) * 9 );
194 unsigned char *ptr =
reinterpret_cast<unsigned char *
>( array.data() );
196 *ptr++ =
static_cast<unsigned char>(
diffuse.red() );
197 *ptr++ =
static_cast<unsigned char>(
diffuse.green() );
198 *ptr++ =
static_cast<unsigned char>(
diffuse.blue() );
200 *ptr++ =
static_cast<unsigned char>(
ambient.red() );
201 *ptr++ =
static_cast<unsigned char>(
ambient.green() );
202 *ptr++ =
static_cast<unsigned char>(
ambient.blue() );
204 *ptr++ =
static_cast<unsigned char>(
specular.red() );
205 *ptr++ =
static_cast<unsigned char>(
specular.green() );
206 *ptr++ =
static_cast<unsigned char>(
specular.blue() );
220 const bool useFloats = mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1;
223 diffuseAttribute->setName( QStringLiteral(
"dataDefinedDiffuseColor" ) );
224 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
225 diffuseAttribute->setVertexSize( 3 );
226 diffuseAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
227 diffuseAttribute->setBuffer( dataBuffer );
228 diffuseAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
229 diffuseAttribute->setByteOffset( 0 );
230 diffuseAttribute->setCount( vertexCount );
231 geometry->addAttribute( diffuseAttribute );
234 ambientAttribute->setName( QStringLiteral(
"dataDefinedAmbiantColor" ) );
235 ambientAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
236 ambientAttribute->setVertexSize( 3 );
237 ambientAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
238 ambientAttribute->setBuffer( dataBuffer );
239 ambientAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
240 ambientAttribute->setByteOffset( 3 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
241 ambientAttribute->setCount( vertexCount );
242 geometry->addAttribute( ambientAttribute );
245 specularAttribute->setName( QStringLiteral(
"dataDefinedSpecularColor" ) );
246 specularAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
247 specularAttribute->setVertexSize( 3 );
248 specularAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
249 specularAttribute->setBuffer( dataBuffer );
250 specularAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
251 specularAttribute->setByteOffset( 6 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
252 specularAttribute->setCount( vertexCount );
253 geometry->addAttribute( specularAttribute );
255 dataBuffer->setData( data );
258Qt3DRender::QMaterial *QgsPhongMaterialSettings::constantColorMaterial(
const QgsMaterialContext &context )
const
260 Qt3DRender::QMaterial *material =
new Qt3DRender::QMaterial;
262 Qt3DRender::QEffect *eff =
new Qt3DRender::QEffect( material );
264 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
265 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
266 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
267 technique->graphicsApiFilter()->setMajorVersion( 3 );
268 technique->graphicsApiFilter()->setMinorVersion( 3 );
269 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey();
270 filterKey->setName( QStringLiteral(
"renderingStyle" ) );
271 filterKey->setValue( QStringLiteral(
"forward" ) );
272 technique->addFilterKey( filterKey );
274 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass();
275 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram();
278 const QUrl urlVert( QStringLiteral(
"qrc:/shaders/phongConstant.vert" ) );
279 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
280 const QUrl urlFrag( QStringLiteral(
"qrc:/shaders/phongConstant.frag" ) );
281 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Fragment, Qt3DRender::QShaderProgram::loadSource( urlFrag ) );
283 renderPass->setShaderProgram( shaderProgram );
284 technique->addRenderPass( renderPass );
289 eff->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"shininess" ),
static_cast< float >( mShininess ) ) );
290 eff->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"opacity" ),
static_cast< float >( mOpacity ) ) );
291 eff->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"ambientColor" ),
292 QColor::fromRgbF(
ambient.redF() * mAmbientCoefficient,
293 ambient.greenF() * mAmbientCoefficient,
294 ambient.blueF() * mAmbientCoefficient ) ) );
295 eff->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"diffuseColor" ),
296 QColor::fromRgbF(
diffuse.redF() * mDiffuseCoefficient,
297 diffuse.greenF() * mDiffuseCoefficient,
298 diffuse.blueF() * mDiffuseCoefficient ) ) );
299 eff->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"specularColor" ),
300 QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient,
301 mSpecular.greenF() * mSpecularCoefficient,
302 mSpecular.blueF() * mSpecularCoefficient ) ) );
304 eff->addTechnique( technique );
305 material->setEffect( eff );
310Qt3DRender::QMaterial *QgsPhongMaterialSettings::dataDefinedMaterial()
const
312 Qt3DRender::QMaterial *material =
new Qt3DRender::QMaterial;
314 Qt3DRender::QEffect *eff =
new Qt3DRender::QEffect( material );
316 Qt3DRender::QTechnique *technique =
new Qt3DRender::QTechnique;
317 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
318 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
319 technique->graphicsApiFilter()->setMajorVersion( 3 );
320 technique->graphicsApiFilter()->setMinorVersion( 3 );
321 Qt3DRender::QFilterKey *filterKey =
new Qt3DRender::QFilterKey();
322 filterKey->setName( QStringLiteral(
"renderingStyle" ) );
323 filterKey->setValue( QStringLiteral(
"forward" ) );
324 technique->addFilterKey( filterKey );
326 Qt3DRender::QRenderPass *renderPass =
new Qt3DRender::QRenderPass();
327 Qt3DRender::QShaderProgram *shaderProgram =
new Qt3DRender::QShaderProgram();
330 const QUrl urlVert( QStringLiteral(
"qrc:/shaders/phongDataDefined.vert" ) );
331 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
332 const QUrl urlFrag( QStringLiteral(
"qrc:/shaders/phongDataDefined.frag" ) );
333 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Fragment, Qt3DRender::QShaderProgram::loadSource( urlFrag ) );
335 renderPass->setShaderProgram( shaderProgram );
336 technique->addRenderPass( renderPass );
338 eff->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"shininess" ),
static_cast< float >( mShininess ) ) );
339 eff->addParameter(
new Qt3DRender::QParameter( QStringLiteral(
"opacity" ),
static_cast< float >( mOpacity ) ) );
341 eff->addTechnique( technique );
342 material->setEffect( eff );
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...
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.
int dataDefinedByteStride() const override
Returns byte stride of the data defined colors,used to fill the vertex colors data defined buffer for...
Qt3DRender::QMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QMaterial object representing the material 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
Constructor for QgsPhongMaterialSettings.
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.
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