20#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) 
   21#include <Qt3DRender/QAttribute> 
   22#include <Qt3DRender/QBuffer> 
   23#include <Qt3DRender/QGeometry> 
   29#include <Qt3DCore/QAttribute> 
   30#include <Qt3DCore/QBuffer> 
   31#include <Qt3DCore/QGeometry> 
   37#include <Qt3DRender/QParameter> 
   38#include <Qt3DRender/QEffect> 
   39#include <Qt3DRender/QTechnique> 
   40#include <Qt3DRender/QGraphicsApiFilter> 
   46  return QStringLiteral( 
"phong" );
 
 
   83  return *
this == *otherPhong;
 
 
   91  mShininess = elem.attribute( QStringLiteral( 
"shininess" ) ).toDouble();
 
   92  mOpacity = elem.attribute( QStringLiteral( 
"opacity" ), QStringLiteral( 
"1.0" ) ).toDouble();
 
   93  mAmbientCoefficient = elem.attribute( QStringLiteral( 
"ka" ), QStringLiteral( 
"1.0" ) ).toDouble();
 
   94  mDiffuseCoefficient = elem.attribute( QStringLiteral( 
"kd" ), QStringLiteral( 
"1.0" ) ).toDouble();
 
   95  mSpecularCoefficient = elem.attribute( QStringLiteral( 
"ks" ), QStringLiteral( 
"1.0" ) ).toDouble();
 
 
  105  elem.setAttribute( QStringLiteral( 
"shininess" ), mShininess );
 
  106  elem.setAttribute( QStringLiteral( 
"opacity" ), mOpacity );
 
  107  elem.setAttribute( QStringLiteral( 
"ka" ), mAmbientCoefficient );
 
  108  elem.setAttribute( QStringLiteral( 
"kd" ), mDiffuseCoefficient );
 
  109  elem.setAttribute( QStringLiteral( 
"ks" ), mSpecularCoefficient );
 
 
  126      return buildMaterial( context );
 
 
  137  QMap<QString, QString> parameters;
 
  138  parameters[QStringLiteral( 
"Kd" )] = QStringLiteral( 
"%1 %2 %3" ).arg( mDiffuse.redF() ).arg( mDiffuse.greenF() ).arg( mDiffuse.blueF() );
 
  139  parameters[QStringLiteral( 
"Ka" )] = QStringLiteral( 
"%1 %2 %3" ).arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
 
  140  parameters[QStringLiteral( 
"Ks" )] = QStringLiteral( 
"%1 %2 %3" ).arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
 
  141  parameters[QStringLiteral( 
"Ns" )] = QString::number( mShininess );
 
 
  150  Qt3DRender::QParameter *ambientParameter = 
new Qt3DRender::QParameter( QStringLiteral( 
"ambientColor" ), QColor::fromRgbF( 
ambient.redF() * mAmbientCoefficient, 
ambient.greenF() * mAmbientCoefficient, 
ambient.blueF() * mAmbientCoefficient ) );
 
  151  Qt3DRender::QParameter *diffuseParameter = 
new Qt3DRender::QParameter( QStringLiteral( 
"diffuseColor" ), QColor::fromRgbF( 
diffuse.redF() * mDiffuseCoefficient, 
diffuse.greenF() * mDiffuseCoefficient, 
diffuse.blueF() * mDiffuseCoefficient ) );
 
  152  Qt3DRender::QParameter *specularParameter = 
new Qt3DRender::QParameter( QStringLiteral( 
"specularColor" ), QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) );
 
  153  Qt3DRender::QParameter *shininessParameter = 
new Qt3DRender::QParameter( QStringLiteral( 
"shininess" ), 
static_cast<float>( mShininess ) );
 
  154  Qt3DRender::QParameter *opacityParameter = 
new Qt3DRender::QParameter( QStringLiteral( 
"opacity" ), 
static_cast<float>( mOpacity ) );
 
  156  effect->addParameter( ambientParameter );
 
  157  effect->addParameter( diffuseParameter );
 
  158  effect->addParameter( specularParameter );
 
  159  effect->addParameter( shininessParameter );
 
  160  effect->addParameter( opacityParameter );
 
 
  170  if ( mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1 )
 
  174    array.resize( 
sizeof( 
float ) * 9 );
 
  175    float *fptr = 
reinterpret_cast<float *
>( array.data() );
 
  177    *fptr++ = 
static_cast<float>( 
diffuse.redF() * mDiffuseCoefficient );
 
  178    *fptr++ = 
static_cast<float>( 
diffuse.greenF() * mDiffuseCoefficient );
 
  179    *fptr++ = 
static_cast<float>( 
diffuse.blueF() * mDiffuseCoefficient );
 
  181    *fptr++ = 
static_cast<float>( 
ambient.redF() * mAmbientCoefficient );
 
  182    *fptr++ = 
static_cast<float>( 
ambient.greenF() * mAmbientCoefficient );
 
  183    *fptr++ = 
static_cast<float>( 
ambient.blueF() * mAmbientCoefficient );
 
  185    *fptr++ = 
static_cast<float>( 
specular.redF() * mSpecularCoefficient );
 
  186    *fptr++ = 
static_cast<float>( 
specular.greenF() * mSpecularCoefficient );
 
  187    *fptr++ = 
static_cast<float>( 
specular.blueF() * mSpecularCoefficient );
 
  191    array.resize( 
sizeof( 
unsigned char ) * 9 );
 
  192    unsigned char *ptr = 
reinterpret_cast<unsigned char *
>( array.data() );
 
  194    *ptr++ = 
static_cast<unsigned char>( 
diffuse.red() );
 
  195    *ptr++ = 
static_cast<unsigned char>( 
diffuse.green() );
 
  196    *ptr++ = 
static_cast<unsigned char>( 
diffuse.blue() );
 
  198    *ptr++ = 
static_cast<unsigned char>( 
ambient.red() );
 
  199    *ptr++ = 
static_cast<unsigned char>( 
ambient.green() );
 
  200    *ptr++ = 
static_cast<unsigned char>( 
ambient.blue() );
 
  202    *ptr++ = 
static_cast<unsigned char>( 
specular.red() );
 
  203    *ptr++ = 
static_cast<unsigned char>( 
specular.green() );
 
  204    *ptr++ = 
static_cast<unsigned char>( 
specular.blue() );
 
 
  218  const bool useFloats = mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1;
 
  221  diffuseAttribute->setName( QStringLiteral( 
"dataDefinedDiffuseColor" ) );
 
  222  diffuseAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
 
  223  diffuseAttribute->setVertexSize( 3 );
 
  224  diffuseAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
 
  225  diffuseAttribute->setBuffer( dataBuffer );
 
  226  diffuseAttribute->setByteStride( 9 * ( useFloats ? 
sizeof( 
float ) : 
sizeof( 
unsigned char ) ) );
 
  227  diffuseAttribute->setByteOffset( 0 );
 
  228  diffuseAttribute->setCount( vertexCount );
 
  229  geometry->addAttribute( diffuseAttribute );
 
  232  ambientAttribute->setName( QStringLiteral( 
"dataDefinedAmbiantColor" ) );
 
  233  ambientAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
 
  234  ambientAttribute->setVertexSize( 3 );
 
  235  ambientAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
 
  236  ambientAttribute->setBuffer( dataBuffer );
 
  237  ambientAttribute->setByteStride( 9 * ( useFloats ? 
sizeof( 
float ) : 
sizeof( 
unsigned char ) ) );
 
  238  ambientAttribute->setByteOffset( 3 * ( useFloats ? 
sizeof( 
float ) : 
sizeof( 
unsigned char ) ) );
 
  239  ambientAttribute->setCount( vertexCount );
 
  240  geometry->addAttribute( ambientAttribute );
 
  243  specularAttribute->setName( QStringLiteral( 
"dataDefinedSpecularColor" ) );
 
  244  specularAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
 
  245  specularAttribute->setVertexSize( 3 );
 
  246  specularAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
 
  247  specularAttribute->setBuffer( dataBuffer );
 
  248  specularAttribute->setByteStride( 9 * ( useFloats ? 
sizeof( 
float ) : 
sizeof( 
unsigned char ) ) );
 
  249  specularAttribute->setByteOffset( 6 * ( useFloats ? 
sizeof( 
float ) : 
sizeof( 
unsigned char ) ) );
 
  250  specularAttribute->setCount( vertexCount );
 
  251  geometry->addAttribute( specularAttribute );
 
  253  dataBuffer->setData( data );
 
 
  260  Qt3DRender::QEffect *effect = 
new Qt3DRender::QEffect( material );
 
  262  Qt3DRender::QTechnique *technique = 
new Qt3DRender::QTechnique;
 
  263  technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
 
  264  technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
 
  265  technique->graphicsApiFilter()->setMajorVersion( 3 );
 
  266  technique->graphicsApiFilter()->setMinorVersion( 3 );
 
  267  Qt3DRender::QFilterKey *filterKey = 
new Qt3DRender::QFilterKey();
 
  268  filterKey->setName( QStringLiteral( 
"renderingStyle" ) );
 
  269  filterKey->setValue( QStringLiteral( 
"forward" ) );
 
  270  technique->addFilterKey( filterKey );
 
  272  Qt3DRender::QRenderPass *renderPass = 
new Qt3DRender::QRenderPass();
 
  273  Qt3DRender::QShaderProgram *shaderProgram = 
new Qt3DRender::QShaderProgram();
 
  275  renderPass->setShaderProgram( shaderProgram );
 
  276  technique->addRenderPass( renderPass );
 
  278  const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( 
"qrc:/shaders/phong.frag" ) ) );
 
  283    const QUrl urlVert( QStringLiteral( 
"qrc:/shaders/phongDataDefined.vert" ) );
 
  284    shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
 
  286    shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
 
  291    const QUrl urlVert( QStringLiteral( 
"qrc:/shaders/default.vert" ) );
 
  292    shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
 
  293    shaderProgram->setFragmentShaderCode( fragmentShaderCode );
 
  298    effect->addParameter( 
new Qt3DRender::QParameter( QStringLiteral( 
"ambientColor" ), QColor::fromRgbF( 
ambient.redF() * mAmbientCoefficient, 
ambient.greenF() * mAmbientCoefficient, 
ambient.blueF() * mAmbientCoefficient ) ) );
 
  299    effect->addParameter( 
new Qt3DRender::QParameter( QStringLiteral( 
"diffuseColor" ), QColor::fromRgbF( 
diffuse.redF() * mDiffuseCoefficient, 
diffuse.greenF() * mDiffuseCoefficient, 
diffuse.blueF() * mDiffuseCoefficient ) ) );
 
  300    effect->addParameter( 
new Qt3DRender::QParameter( QStringLiteral( 
"specularColor" ), QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) ) );
 
  303  effect->addParameter( 
new Qt3DRender::QParameter( QStringLiteral( 
"shininess" ), 
static_cast<float>( mShininess ) ) );
 
  304  effect->addParameter( 
new Qt3DRender::QParameter( QStringLiteral( 
"opacity" ), 
static_cast<float>( mOpacity ) ) );
 
  306  effect->addTechnique( technique );
 
  307  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.
 
Basic shading material used for rendering based on the Phong shading model with three color component...
 
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
 
Qt3DCore::QAttribute Qt3DQAttribute
 
Qt3DCore::QBuffer Qt3DQBuffer
 
Qt3DCore::QGeometry Qt3DQGeometry