Skip to content

Commit

Permalink
refactor the attributes and vertex buffer code
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 7b9725a commit 67a4cc4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
3 changes: 3 additions & 0 deletions python/3d/auto_generated/symbols/qgspointcloud3dsymbol.sip.in
Expand Up @@ -87,6 +87,9 @@ Returns the rendering style used to render the point cloud
Returns the byte stride for the geometries used to for the vertex buffer
%End
virtual void fillMaterial( Qt3DRender::QMaterial *material ) = 0;
%Docstring
Used to fill material object with necessary QParameters (and consequently opengl uniforms)
%End

protected:
float mPointSize = 2.0f;
Expand Down
71 changes: 42 additions & 29 deletions src/3d/qgspointcloudlayerchunkloader_p.cpp
Expand Up @@ -57,6 +57,7 @@ QgsPointCloud3DGeometry::QgsPointCloud3DGeometry( Qt3DCore::QNode *parent, const
#endif
, mByteStride( symbol->byteStride() )
, mRenderingStyle( symbol->renderingStyle() )
, mSymbol( symbol )
{
mPositionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
mPositionAttribute->setBuffer( mVertexBuffer );
Expand All @@ -65,28 +66,37 @@ QgsPointCloud3DGeometry::QgsPointCloud3DGeometry( Qt3DCore::QNode *parent, const
mPositionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
mPositionAttribute->setByteOffset( 0 );
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( mByteStride );

addAttribute( mPositionAttribute );
addAttribute( mParameterAttribute );

if ( mRenderingStyle == QgsPointCloud3DSymbol::RenderingStyle::RGBRendering )
switch ( mRenderingStyle )
{
mColorAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
mColorAttribute->setBuffer( mVertexBuffer );
mColorAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
mColorAttribute->setVertexSize( 3 );
mColorAttribute->setName( QStringLiteral( "vertexColor" ) );
mColorAttribute->setByteOffset( 16 );
mColorAttribute->setByteStride( mByteStride );
addAttribute( mColorAttribute );
case QgsPointCloud3DSymbol::RenderingStyle::NoRendering:
case QgsPointCloud3DSymbol::RenderingStyle::SingleColor:
break;
case QgsPointCloud3DSymbol::RenderingStyle::ColorRamp:
{
mParameterAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
mParameterAttribute->setBuffer( mVertexBuffer );
mParameterAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
mParameterAttribute->setVertexSize( 1 );
mParameterAttribute->setName( "vertexParameter" );
mParameterAttribute->setByteOffset( 12 );
mParameterAttribute->setByteStride( mByteStride );
addAttribute( mParameterAttribute );
break;
}
case QgsPointCloud3DSymbol::RenderingStyle::RGBRendering:
{
mColorAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
mColorAttribute->setBuffer( mVertexBuffer );
mColorAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
mColorAttribute->setVertexSize( 3 );
mColorAttribute->setName( QStringLiteral( "vertexColor" ) );
mColorAttribute->setByteOffset( 12 );
mColorAttribute->setByteStride( mByteStride );
addAttribute( mColorAttribute );
break;
}
}

makeVertexBuffer( data );
Expand All @@ -95,24 +105,27 @@ QgsPointCloud3DGeometry::QgsPointCloud3DGeometry( Qt3DCore::QNode *parent, const
void QgsPointCloud3DGeometry::makeVertexBuffer( const QgsPointCloud3DSymbolHandler::PointData &data )
{
QByteArray vertexBufferData;
if ( mRenderingStyle == QgsPointCloud3DSymbol::RenderingStyle::RGBRendering )
vertexBufferData.resize( data.positions.size() * mByteStride );
else
vertexBufferData.resize( data.positions.size() * mByteStride );
vertexBufferData.resize( data.positions.size() * mByteStride );
float *rawVertexArray = reinterpret_cast<float *>( vertexBufferData.data() );
int idx = 0;
Q_ASSERT( data.positions.count() == data.parameter.count() );
for ( int i = 0; i < data.positions.size(); ++i )
{
rawVertexArray[idx++] = data.positions.at( i ).x();
rawVertexArray[idx++] = data.positions.at( i ).y();
rawVertexArray[idx++] = data.positions.at( i ).z();
rawVertexArray[idx++] = data.parameter.at( i );
if ( mRenderingStyle == QgsPointCloud3DSymbol::RenderingStyle::RGBRendering )
switch ( mRenderingStyle )
{
rawVertexArray[idx++] = data.colors.at( i ).x();
rawVertexArray[idx++] = data.colors.at( i ).y();
rawVertexArray[idx++] = data.colors.at( i ).z();
case QgsPointCloud3DSymbol::NoRendering:
case QgsPointCloud3DSymbol::SingleColor:
break;
case QgsPointCloud3DSymbol::ColorRamp:
rawVertexArray[idx++] = data.parameter.at( i );
break;
case QgsPointCloud3DSymbol::RGBRendering:
rawVertexArray[idx++] = data.colors.at( i ).x();
rawVertexArray[idx++] = data.colors.at( i ).y();
rawVertexArray[idx++] = data.colors.at( i ).z();
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/3d/qgspointcloudlayerchunkloader_p.h
Expand Up @@ -108,6 +108,7 @@ class QgsPointCloud3DGeometry: public Qt3DRender::QGeometry

unsigned int mByteStride = 16;
QgsPointCloud3DSymbol::RenderingStyle mRenderingStyle;
QgsPointCloud3DSymbol *mSymbol = nullptr;
};

/**
Expand Down
5 changes: 3 additions & 2 deletions src/3d/symbols/qgspointcloud3dsymbol.h
Expand Up @@ -91,6 +91,7 @@ class _3D_EXPORT QgsPointCloud3DSymbol : public QgsAbstract3DSymbol

//! Returns the byte stride for the geometries used to for the vertex buffer
virtual unsigned int byteStride() = 0;
//! Used to fill material object with necessary QParameters (and consequently opengl uniforms)
virtual void fillMaterial( Qt3DRender::QMaterial *material ) = 0;

protected:
Expand Down Expand Up @@ -131,7 +132,7 @@ class _3D_EXPORT QgsSingleColorPointCloud3DSymbol : public QgsPointCloud3DSymbol
*/
void setSingleColor( QColor color );

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


Expand Down Expand Up @@ -231,7 +232,7 @@ 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 ); }
unsigned int byteStride() override { return 6 * sizeof( float ); }
void fillMaterial( Qt3DRender::QMaterial *material ) override;
};

Expand Down

0 comments on commit 67a4cc4

Please sign in to comment.