Skip to content

Commit

Permalink
3D material color data defined (#39683)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcloarec committed Nov 4, 2020
1 parent 0e758df commit b725a04
Show file tree
Hide file tree
Showing 34 changed files with 1,131 additions and 108 deletions.
Expand Up @@ -124,12 +124,12 @@ Clones the material settings.
Caller takes ownership of the returned object.
%End

virtual void readXml( const QDomElement &element, const QgsReadWriteContext &context ) = 0;
virtual void readXml( const QDomElement &element, const QgsReadWriteContext & );
%Docstring
Reads settings from a DOM ``element``
%End

virtual void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const = 0;
virtual void writeXml( QDomElement &element, const QgsReadWriteContext & ) const;
%Docstring
Writes settings to a DOM ``element``
%End
Expand Down
Expand Up @@ -10,6 +10,7 @@




class QgsPhongMaterialSettings : QgsAbstractMaterialSettings
{
%Docstring
Expand Down Expand Up @@ -92,6 +93,7 @@ Sets shininess of the surface
virtual void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const;



bool operator==( const QgsPhongMaterialSettings &other ) const;

};
Expand Down
1 change: 1 addition & 0 deletions src/3d/CMakeLists.txt
Expand Up @@ -47,6 +47,7 @@ SET(QGIS_3D_SRCS
chunks/qgschunknode_p.cpp
chunks/qgschunkqueuejob_p.cpp

materials/qgsabstractmaterialsettings.cpp
materials/qgsgoochmaterialsettings.cpp
materials/qgsmaterialregistry.cpp
materials/qgsphongmaterialsettings.cpp
Expand Down
76 changes: 76 additions & 0 deletions src/3d/materials/qgsabstractmaterialsettings.cpp
@@ -0,0 +1,76 @@
/***************************************************************************
qgsabstractmaterialsettings.cpp
--------------------------------------
Date : July 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <qgsabstractmaterialsettings.h>

QgsPropertiesDefinition QgsAbstractMaterialSettings::sPropertyDefinitions;

void QgsAbstractMaterialSettings::readXml( const QDomElement &element, const QgsReadWriteContext & )
{
QDomElement elemDataDefinedProperties = element.firstChildElement( QStringLiteral( "data-defined-properties" ) );
if ( !elemDataDefinedProperties.isNull() )
mDataDefinedProperties.readXml( elemDataDefinedProperties, propertyDefinitions() );
}

void QgsAbstractMaterialSettings::writeXml( QDomElement &element, const QgsReadWriteContext & ) const
{
QDomElement elemDataDefinedProperties = element.ownerDocument().createElement( QStringLiteral( "data-defined-properties" ) );
mDataDefinedProperties.writeXml( elemDataDefinedProperties, propertyDefinitions() );
element.appendChild( elemDataDefinedProperties );
}

void QgsAbstractMaterialSettings::setDataDefinedProperties( const QgsPropertyCollection &collection )
{
mDataDefinedProperties = collection;
}

QgsPropertyCollection QgsAbstractMaterialSettings::dataDefinedProperties() const {return mDataDefinedProperties;}

const QgsPropertiesDefinition &QgsAbstractMaterialSettings::propertyDefinitions() const
{
if ( sPropertyDefinitions.isEmpty() )
initPropertyDefinitions();
return sPropertyDefinitions;
}

QByteArray QgsAbstractMaterialSettings::dataDefinedVertexColorsAsByte( const QgsExpressionContext &expressionContext ) const
{
Q_UNUSED( expressionContext )
return QByteArray();
}

void QgsAbstractMaterialSettings::applyDataDefinedToGeometry( Qt3DRender::QGeometry *geometry, int vertexCount, const QByteArray &dataDefinedBytes ) const
{
Q_UNUSED( geometry )
Q_UNUSED( vertexCount )
Q_UNUSED( dataDefinedBytes )
}

void QgsAbstractMaterialSettings::initPropertyDefinitions() const
{
if ( !sPropertyDefinitions.isEmpty() )
return;

QString origin = QStringLiteral( "material3d" );

sPropertyDefinitions = QgsPropertiesDefinition
{
{ Diffuse, QgsPropertyDefinition( "diffuse", QObject::tr( "Diffuse" ), QgsPropertyDefinition::ColorNoAlpha, origin ) },
{ Ambient, QgsPropertyDefinition( "ambient", QObject::tr( "Ambient" ), QgsPropertyDefinition::ColorNoAlpha, origin ) },
{ Warm, QgsPropertyDefinition( "warm", QObject::tr( "Warm" ), QgsPropertyDefinition::ColorNoAlpha, origin ) },
{ Cool, QgsPropertyDefinition( "cool", QObject::tr( "Cool" ), QgsPropertyDefinition::ColorNoAlpha, origin ) },
{ Specular, QgsPropertyDefinition( "specular", QObject::tr( "Specular" ), QgsPropertyDefinition::ColorNoAlpha, origin ) }
};
}
64 changes: 62 additions & 2 deletions src/3d/materials/qgsabstractmaterialsettings.h
Expand Up @@ -21,11 +21,19 @@

#include <QColor>
#include <Qt3DRender/qmaterial.h>
#include "qgspropertycollection.h"

class QDomElement;
class QgsReadWriteContext;
class QgsLineMaterial;
class QgsExpressionContext;

#ifndef SIP_RUN
namespace Qt3DRender
{
class QGeometry;
}
#endif //SIP_RUN

/**
* Material rendering techniques
Expand Down Expand Up @@ -146,10 +154,10 @@ class _3D_EXPORT QgsAbstractMaterialSettings SIP_ABSTRACT
virtual QgsAbstractMaterialSettings *clone() const = 0 SIP_FACTORY;

//! Reads settings from a DOM \a element
virtual void readXml( const QDomElement &element, const QgsReadWriteContext &context ) = 0;
virtual void readXml( const QDomElement &element, const QgsReadWriteContext & );

//! Writes settings to a DOM \a element
virtual void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const = 0;
virtual void writeXml( QDomElement &element, const QgsReadWriteContext & ) const;

#ifndef SIP_RUN

Expand All @@ -170,8 +178,60 @@ class _3D_EXPORT QgsAbstractMaterialSettings SIP_ABSTRACT
* Adds parameters from the material to a destination \a effect.
*/
virtual void addParametersToEffect( Qt3DRender::QEffect *effect ) const = 0;

//! Data definable properties.
enum Property
{
Diffuse, //!< Diffuse color
Ambient, //!< Ambient color (phong material)
Warm, //!< Warm color (gooch material)
Cool,//!< Cool color (gooch material)
Specular //!< Specular color
};

/**
* Sets the material property collection, used for data defined overrides.
* \since QGIS 3.18
*/
void setDataDefinedProperties( const QgsPropertyCollection &collection );

/**
* Returns the symbol material property collection, used for data defined overrides.
* \since QGIS 3.18
*/
QgsPropertyCollection dataDefinedProperties() const;

/**
* Returns a reference to the material properties definition, used for data defined overrides.
* \since QGIS 3.18
*/
const QgsPropertiesDefinition &propertyDefinitions() const;

/**
* Applies the data defined bytes, \a dataDefinedBytes, on the \a geometry by filling a specific vertex buffer that will be used by the shader.
* \since QGIS 3.18
*/
virtual void applyDataDefinedToGeometry( Qt3DRender::QGeometry *geometry, int vertexCount, const QByteArray &dataDefinedBytes ) const;

/**
* Returns byte array corresponding to the data defined colors depending of the \a expressionContext,
* used to fill the specific vertex buffer used for rendering the geometry
* \see applyDataDefinedToGeometry()
* \since QGIS 3.18
*/
virtual QByteArray dataDefinedVertexColorsAsByte( const QgsExpressionContext &expressionContext ) const;

/**
* Returns byte stride of the data defined colors,used to fill the vertex colors data defined buffer for rendering
* \since QGIS 3.18
*/
virtual int dataDefinedByteStride() const {return 0;}
#endif

private:
QgsPropertyCollection mDataDefinedProperties;
static QgsPropertiesDefinition sPropertyDefinitions;
void initPropertyDefinitions() const;
};


Expand Down

0 comments on commit b725a04

Please sign in to comment.