Skip to content

Commit

Permalink
move material stuff to symbol classes
Browse files Browse the repository at this point in the history
  • Loading branch information
NEDJIMAbelgacem authored and wonder-sk committed Dec 3, 2020
1 parent f4b9f72 commit 7b9725a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 91 deletions.
28 changes: 23 additions & 5 deletions python/3d/auto_generated/symbols/qgspointcloud3dsymbol.sip.in
Expand Up @@ -9,6 +9,10 @@



namespace Qt3DRender
{
};

class QgsPointCloud3DSymbol : QgsAbstract3DSymbol
{
%Docstring
Expand Down Expand Up @@ -45,11 +49,6 @@ Constructor for QgsPointCloud3DSymbol
%End
~QgsPointCloud3DSymbol();

virtual QgsAbstract3DSymbol *clone() const /Factory/;

virtual void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const;
virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context );

virtual QString type() const;

QgsPointCloudLayer *layer();
Expand Down Expand Up @@ -83,6 +82,12 @@ Sets the point size
Returns the rendering style used to render the point cloud
%End

virtual unsigned int byteStride() = 0;
%Docstring
Returns the byte stride for the geometries used to for the vertex buffer
%End
virtual void fillMaterial( Qt3DRender::QMaterial *material ) = 0;

protected:
float mPointSize = 2.0f;
};
Expand Down Expand Up @@ -131,6 +136,11 @@ Sets the color used by the renderer when using SingleColor rendering mode
.. seealso:: :py:func:`singleColor`
%End

virtual unsigned int byteStride();
virtual void fillMaterial( Qt3DRender::QMaterial *material );



};

class QgsColorRampPointCloud3DSymbol : QgsPointCloud3DSymbol
Expand Down Expand Up @@ -212,6 +222,10 @@ Sets the minimum and maximum values used when classifying colors in the color ra
.. seealso:: :py:func:`colorRampShaderMin`
%End

virtual unsigned int byteStride();
virtual void fillMaterial( Qt3DRender::QMaterial *material );


};

class QgsRGBPointCloud3DSymbol : QgsPointCloud3DSymbol
Expand Down Expand Up @@ -243,6 +257,10 @@ Constructor for QgsRGBPointCloud3DSymbol

virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context );


virtual unsigned int byteStride();
virtual void fillMaterial( Qt3DRender::QMaterial *material );

};

/************************************************************************
Expand Down
85 changes: 9 additions & 76 deletions src/3d/qgspointcloudlayerchunkloader_p.cpp
Expand Up @@ -55,24 +55,24 @@ QgsPointCloud3DGeometry::QgsPointCloud3DGeometry( Qt3DCore::QNode *parent, const
#else
, mVertexBuffer( new Qt3DRender::QBuffer( this ) )
#endif
, mByteStride( symbol->byteStride() )
, mRenderingStyle( symbol->renderingStyle() )
{
unsigned int byte_stride = mRenderingStyle == QgsPointCloud3DSymbol::RenderingStyle::RGBRendering ? 28 : 16;
mPositionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
mPositionAttribute->setBuffer( mVertexBuffer );
mPositionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
mPositionAttribute->setVertexSize( 3 );
mPositionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
mPositionAttribute->setByteOffset( 0 );
mPositionAttribute->setByteStride( byte_stride );
mPositionAttribute->setByteStride( mByteStride );

mParameterAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
mParameterAttribute->setBuffer( mVertexBuffer );
mParameterAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
mParameterAttribute->setVertexSize( 1 );
mParameterAttribute->setName( "vertexParameter" );
mParameterAttribute->setByteOffset( 12 );
mParameterAttribute->setByteStride( byte_stride );
mParameterAttribute->setByteStride( mByteStride );

addAttribute( mPositionAttribute );
addAttribute( mParameterAttribute );
Expand All @@ -85,7 +85,7 @@ QgsPointCloud3DGeometry::QgsPointCloud3DGeometry( Qt3DCore::QNode *parent, const
mColorAttribute->setVertexSize( 3 );
mColorAttribute->setName( QStringLiteral( "vertexColor" ) );
mColorAttribute->setByteOffset( 16 );
mColorAttribute->setByteStride( byte_stride );
mColorAttribute->setByteStride( mByteStride );
addAttribute( mColorAttribute );
}

Expand All @@ -96,9 +96,9 @@ void QgsPointCloud3DGeometry::makeVertexBuffer( const QgsPointCloud3DSymbolHandl
{
QByteArray vertexBufferData;
if ( mRenderingStyle == QgsPointCloud3DSymbol::RenderingStyle::RGBRendering )
vertexBufferData.resize( data.positions.size() * 7 * sizeof( float ) );
vertexBufferData.resize( data.positions.size() * mByteStride );
else
vertexBufferData.resize( data.positions.size() * 4 * sizeof( float ) );
vertexBufferData.resize( data.positions.size() * mByteStride );
float *rawVertexArray = reinterpret_cast<float *>( vertexBufferData.data() );
int idx = 0;
Q_ASSERT( data.positions.count() == data.parameter.count() );
Expand Down Expand Up @@ -265,22 +265,9 @@ void QgsPointCloud3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent, const
Qt3DCore::QTransform *tr = new Qt3DCore::QTransform;

// Material
Qt3DRender::QMaterial *mat = nullptr;
switch ( mSymbol->renderingStyle() )
{
case QgsPointCloud3DSymbol::RenderingStyle::NoRendering:
mat = new Qt3DRender::QMaterial;
break;
case QgsPointCloud3DSymbol::RenderingStyle::SingleColor:
mat = constructMaterial( dynamic_cast<QgsSingleColorPointCloud3DSymbol *>( mSymbol.get() ) );
break;
case QgsPointCloud3DSymbol::RenderingStyle::ColorRamp:
mat = constructMaterial( dynamic_cast<QgsColorRampPointCloud3DSymbol *>( mSymbol.get() ) );
break;
case QgsPointCloud3DSymbol::RenderingStyle::RGBRendering:
mat = constructMaterial( dynamic_cast<QgsRGBPointCloud3DSymbol *>( mSymbol.get() ) );
break;
}
Qt3DRender::QMaterial *mat = new Qt3DRender::QMaterial;
if ( mSymbol )
mSymbol->fillMaterial( mat );

Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram( mat );
shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/pointcloud.vert" ) ) ) );
Expand Down Expand Up @@ -335,60 +322,6 @@ void QgsPointCloud3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent, const
// cppcheck-suppress memleak
}


Qt3DRender::QMaterial *QgsPointCloud3DSymbolHandler::constructMaterial( QgsSingleColorPointCloud3DSymbol *symbol )
{
Qt3DRender::QMaterial *mat = new Qt3DRender::QMaterial;
Qt3DRender::QParameter *renderingStyle = new Qt3DRender::QParameter( "u_renderingStyle", symbol->renderingStyle() );
mat->addParameter( renderingStyle );
Qt3DRender::QParameter *pointSizeParameter = new Qt3DRender::QParameter( "u_pointSize", QVariant::fromValue( symbol->pointSize() ) );
mat->addParameter( pointSizeParameter );
QColor singleColor = symbol->singleColor();
Qt3DRender::QParameter *singleColorParameter = new Qt3DRender::QParameter( "u_singleColor", QVector3D( singleColor.redF(), singleColor.greenF(), singleColor.blueF() ) );
mat->addParameter( singleColorParameter );
return mat;
}

Qt3DRender::QMaterial *QgsPointCloud3DSymbolHandler::constructMaterial( QgsColorRampPointCloud3DSymbol *symbol )
{
Qt3DRender::QMaterial *mat = new Qt3DRender::QMaterial;
Qt3DRender::QParameter *renderingStyle = new Qt3DRender::QParameter( "u_renderingStyle", symbol->renderingStyle() );
mat->addParameter( renderingStyle );
Qt3DRender::QParameter *pointSizeParameter = new Qt3DRender::QParameter( "u_pointSize", QVariant::fromValue( symbol->pointSize() ) );
mat->addParameter( pointSizeParameter );
QgsColorRampShader colorRampShader = symbol->colorRampShader();

// Create the texture to pass the color ramp
Qt3DRender::QTexture1D *colorRampTexture = nullptr;
if ( colorRampShader.colorRampItemList().count() > 0 )
{
colorRampTexture = new Qt3DRender::QTexture1D( mat );
colorRampTexture->addTextureImage( new QgsColorRampTexture( colorRampShader, 1 ) );
colorRampTexture->setMinificationFilter( Qt3DRender::QTexture1D::Linear );
colorRampTexture->setMagnificationFilter( Qt3DRender::QTexture1D::Linear );
}

// Parameters
Qt3DRender::QParameter *colorRampTextureParameter = new Qt3DRender::QParameter( "u_colorRampTexture", colorRampTexture );
mat->addParameter( colorRampTextureParameter );
Qt3DRender::QParameter *colorRampCountParameter = new Qt3DRender::QParameter( "u_colorRampCount", colorRampShader.colorRampItemList().count() );
mat->addParameter( colorRampCountParameter );
int colorRampType = colorRampShader.colorRampType();
Qt3DRender::QParameter *colorRampTypeParameter = new Qt3DRender::QParameter( "u_colorRampType", colorRampType );
mat->addParameter( colorRampTypeParameter );
return mat;
}

Qt3DRender::QMaterial *QgsPointCloud3DSymbolHandler::constructMaterial( QgsRGBPointCloud3DSymbol *symbol )
{
Qt3DRender::QMaterial *mat = new Qt3DRender::QMaterial;
Qt3DRender::QParameter *renderingStyle = new Qt3DRender::QParameter( "u_renderingStyle", symbol->renderingStyle() );
mat->addParameter( renderingStyle );
Qt3DRender::QParameter *pointSizeParameter = new Qt3DRender::QParameter( "u_pointSize", QVariant::fromValue( symbol->pointSize() ) );
mat->addParameter( pointSizeParameter );
return mat;
}

QgsPointCloudLayerChunkLoader::QgsPointCloudLayerChunkLoader( const QgsPointCloudLayerChunkLoaderFactory *factory, QgsChunkNode *node, QgsPointCloud3DSymbol *symbol )
: QgsChunkLoader( node )
, mFactory( factory )
Expand Down
6 changes: 1 addition & 5 deletions src/3d/qgspointcloudlayerchunkloader_p.h
Expand Up @@ -78,11 +78,6 @@ class QgsPointCloud3DSymbolHandler // : public QgsFeature3DHandler
//static void addMeshEntities( const Qgs3DMapSettings &map, const QVector<QVector3D> &positions, const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent, bool are_selected );
//static Qt3DCore::QTransform *transform( QVector3D position, const QgsPoint3DSymbol *symbol );

private:
Qt3DRender::QMaterial *constructMaterial( QgsSingleColorPointCloud3DSymbol *symbol );
Qt3DRender::QMaterial *constructMaterial( QgsColorRampPointCloud3DSymbol *symbol );
Qt3DRender::QMaterial *constructMaterial( QgsRGBPointCloud3DSymbol *symbol );

void makeEntity( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context, PointData &out, bool selected );

// input specific for this class
Expand Down Expand Up @@ -111,6 +106,7 @@ class QgsPointCloud3DGeometry: public Qt3DRender::QGeometry
Qt3DRender::QBuffer *mVertexBuffer = nullptr;
int mVertexCount = 0;

unsigned int mByteStride = 16;
QgsPointCloud3DSymbol::RenderingStyle mRenderingStyle;
};

Expand Down
49 changes: 49 additions & 0 deletions src/3d/symbols/qgspointcloud3dsymbol.cpp
Expand Up @@ -15,6 +15,12 @@

#include "qgspointcloud3dsymbol.h"

#include "qgscolorramptexture.h"

#include <Qt3DRender/QMaterial>
#include <Qt3DRender/QParameter>
#include <Qt3DRender/QTexture>

// QgsPointCloud3DSymbol

QgsPointCloud3DSymbol::QgsPointCloud3DSymbol( QgsPointCloudLayer *layer, QgsPointCloud3DSymbol::RenderingStyle style )
Expand Down Expand Up @@ -80,6 +86,16 @@ void QgsSingleColorPointCloud3DSymbol::setSingleColor( QColor color )
mSingleColor = color;
}

void QgsSingleColorPointCloud3DSymbol::fillMaterial( Qt3DRender::QMaterial *mat )
{
Qt3DRender::QParameter *renderingStyle = new Qt3DRender::QParameter( "u_renderingStyle", mRenderingStyle );
mat->addParameter( renderingStyle );
Qt3DRender::QParameter *pointSizeParameter = new Qt3DRender::QParameter( "u_pointSize", QVariant::fromValue( mPointSize ) );
mat->addParameter( pointSizeParameter );
Qt3DRender::QParameter *singleColorParameter = new Qt3DRender::QParameter( "u_singleColor", QVector3D( mSingleColor.redF(), mSingleColor.greenF(), mSingleColor.blueF() ) );
mat->addParameter( singleColorParameter );
}

// QgsColorRampPointCloud3DSymbol

QgsColorRampPointCloud3DSymbol::QgsColorRampPointCloud3DSymbol( QgsPointCloudLayer *layer )
Expand Down Expand Up @@ -152,6 +168,32 @@ void QgsColorRampPointCloud3DSymbol::setColorRampShaderMinMax( double min, doubl
mColorRampShaderMax = max;
}

void QgsColorRampPointCloud3DSymbol::fillMaterial( Qt3DRender::QMaterial *mat )
{
Qt3DRender::QParameter *renderingStyle = new Qt3DRender::QParameter( "u_renderingStyle", mRenderingStyle );
mat->addParameter( renderingStyle );
Qt3DRender::QParameter *pointSizeParameter = new Qt3DRender::QParameter( "u_pointSize", QVariant::fromValue( mPointSize ) );
mat->addParameter( pointSizeParameter );
// Create the texture to pass the color ramp
Qt3DRender::QTexture1D *colorRampTexture = nullptr;
if ( mColorRampShader.colorRampItemList().count() > 0 )
{
colorRampTexture = new Qt3DRender::QTexture1D( mat );
colorRampTexture->addTextureImage( new QgsColorRampTexture( mColorRampShader, 1 ) );
colorRampTexture->setMinificationFilter( Qt3DRender::QTexture1D::Linear );
colorRampTexture->setMagnificationFilter( Qt3DRender::QTexture1D::Linear );
}

// Parameters
Qt3DRender::QParameter *colorRampTextureParameter = new Qt3DRender::QParameter( "u_colorRampTexture", colorRampTexture );
mat->addParameter( colorRampTextureParameter );
Qt3DRender::QParameter *colorRampCountParameter = new Qt3DRender::QParameter( "u_colorRampCount", mColorRampShader.colorRampItemList().count() );
mat->addParameter( colorRampCountParameter );
int colorRampType = mColorRampShader.colorRampType();
Qt3DRender::QParameter *colorRampTypeParameter = new Qt3DRender::QParameter( "u_colorRampType", colorRampType );
mat->addParameter( colorRampTypeParameter );
}

// QgsRGBPointCloud3DSymbol

QgsRGBPointCloud3DSymbol::QgsRGBPointCloud3DSymbol( QgsPointCloudLayer *layer )
Expand Down Expand Up @@ -182,3 +224,10 @@ void QgsRGBPointCloud3DSymbol::readXml( const QDomElement &elem, const QgsReadWr
mPointSize = elem.attribute( "point-size", QStringLiteral( "2.0" ) ).toFloat();
}

void QgsRGBPointCloud3DSymbol::fillMaterial( Qt3DRender::QMaterial *mat )
{
Qt3DRender::QParameter *renderingStyle = new Qt3DRender::QParameter( "u_renderingStyle", mRenderingStyle );
mat->addParameter( renderingStyle );
Qt3DRender::QParameter *pointSizeParameter = new Qt3DRender::QParameter( "u_pointSize", QVariant::fromValue( mPointSize ) );
mat->addParameter( pointSizeParameter );
}
24 changes: 19 additions & 5 deletions src/3d/symbols/qgspointcloud3dsymbol.h
Expand Up @@ -22,6 +22,11 @@
#include "qgscolorrampshader.h"
#include "qgspointcloudlayer.h"

namespace Qt3DRender
{
class QMaterial;
};

/**
* \ingroup 3d
* 3D symbol that draws point cloud geometries as 3D objects.
Expand Down Expand Up @@ -55,11 +60,6 @@ class _3D_EXPORT QgsPointCloud3DSymbol : public QgsAbstract3DSymbol
//! Destructor for QgsPointCloud3DSymbol
~QgsPointCloud3DSymbol() override;

QgsAbstract3DSymbol *clone() const override SIP_FACTORY { return nullptr; }

void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const override { Q_UNUSED( elem ); Q_UNUSED( context ); }
void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override { Q_UNUSED( elem ); Q_UNUSED( context ); }

QString type() const override { return "pointcloud"; }

/**
Expand Down Expand Up @@ -89,6 +89,10 @@ class _3D_EXPORT QgsPointCloud3DSymbol : public QgsAbstract3DSymbol
//! Returns the rendering style used to render the point cloud
QgsPointCloud3DSymbol::RenderingStyle renderingStyle() const { return mRenderingStyle; }

//! Returns the byte stride for the geometries used to for the vertex buffer
virtual unsigned int byteStride() = 0;
virtual void fillMaterial( Qt3DRender::QMaterial *material ) = 0;

protected:
QgsPointCloud3DSymbol::RenderingStyle mRenderingStyle = QgsPointCloud3DSymbol::NoRendering;
QgsPointCloudLayer *mLayer = nullptr;
Expand Down Expand Up @@ -127,6 +131,10 @@ class _3D_EXPORT QgsSingleColorPointCloud3DSymbol : public QgsPointCloud3DSymbol
*/
void setSingleColor( QColor color );

unsigned int byteStride() override { return 4 * sizeof( float ); }
void fillMaterial( Qt3DRender::QMaterial *material ) override;


private:
QColor mSingleColor = Qt::blue;
};
Expand Down Expand Up @@ -193,6 +201,9 @@ class _3D_EXPORT QgsColorRampPointCloud3DSymbol : public QgsPointCloud3DSymbol
*/
void setColorRampShaderMinMax( double min, double max );

unsigned int byteStride() override { return 4 * sizeof( float ); }
void fillMaterial( Qt3DRender::QMaterial *material ) override;

private:
QString mRenderingParameter;
QgsColorRampShader mColorRampShader;
Expand All @@ -219,6 +230,9 @@ class _3D_EXPORT QgsRGBPointCloud3DSymbol : public QgsPointCloud3DSymbol

void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const override;
void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;

unsigned int byteStride() override { return 7 * sizeof( float ); }
void fillMaterial( Qt3DRender::QMaterial *material ) override;
};

#endif // QGSPOINTCLOUD3DSYMBOL_H

0 comments on commit 7b9725a

Please sign in to comment.