Skip to content

Commit

Permalink
Implement color ramp shader stuff
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 c491b9d commit 7e25284
Show file tree
Hide file tree
Showing 15 changed files with 953 additions and 132 deletions.
88 changes: 88 additions & 0 deletions python/3d/auto_generated/symbols/qgspointcloud3dsymbol.sip.in
Expand Up @@ -26,6 +26,21 @@ class QgsPointCloud3DSymbol : QgsAbstract3DSymbol
#include "qgspointcloud3dsymbol.h"
%End
public:

enum RenderingStyle
{
//! Render the mesh with a single color
SingleColor,
//! Render the mesh with a color ramp
ColorRamp,
};

enum RenderingParameter
{
Height,
ClassID
};

QgsPointCloud3DSymbol();
%Docstring
Constructor for QgsPointCloud3DSymbol
Expand Down Expand Up @@ -68,6 +83,79 @@ Returns the point size of the point cloud
Sets the point size

.. seealso:: :py:func:`pointSize`
%End

QgsPointCloud3DSymbol::RenderingStyle renderingStyle() const;
%Docstring
Returns the rendering style

.. seealso:: :py:func:`setRenderingStyle`
%End

void setRenderingStyle( QgsPointCloud3DSymbol::RenderingStyle style );
%Docstring
Sets the rendering style

.. seealso:: :py:func:`renderingStyle`
%End

QgsPointCloud3DSymbol::RenderingParameter renderingParameter() const;
%Docstring
Returns the parameter used to select the color of the point cloud when using color ramp coloring

.. seealso:: :py:func:`setRenderingParameter`
%End

void setRenderingParameter( QgsPointCloud3DSymbol::RenderingParameter parameter );
%Docstring
Sets the parameter used to select the color of the point cloud when using color ramp coloring

.. seealso:: :py:func:`renderingParameter`
%End

QColor singleColor() const;
%Docstring
Returns the color used by the renderer when using SingleColor rendering mode

.. seealso:: :py:func:`setSingleColor`
%End

void setSingleColor( QColor color );
%Docstring
Sets the color used by the renderer when using SingleColor rendering mode

.. seealso:: :py:func:`singleColor`
%End

QgsColorRampShader colorRampShader() const;
%Docstring
Returns the color ramp shader used to render the color
%End

void setColorRampShader( const QgsColorRampShader &colorRampShader );
%Docstring
Sets the color ramp shader used to render the color
%End

double colorRampShaderMin() const;
%Docstring
Returns the minimum value used when classifying colors in the color ramp shader

.. seealso:: :py:func:`setColorRampShaderMinMax`
%End

double colorRampShaderMax() const;
%Docstring
Returns the maximum value used when classifying colors in the color ramp shader

.. seealso:: :py:func:`setColorRampShaderMinMax`
%End

void setColorRampShaderMinMax( double min, double max );
%Docstring
Sets the minimum and maximum values used when classifying colors in the color ramp shader

.. seealso:: :py:func:`setColorRampShaderMinMax`
%End

};
Expand Down
2 changes: 2 additions & 0 deletions src/3d/CMakeLists.txt
Expand Up @@ -36,6 +36,7 @@ set(QGIS_3D_SRCS
qgspostprocessingentity.cpp
qgspreviewquad.cpp
qgsshadowsettings.cpp
qgscolorramptexture.cpp

qgspointcloudlayer3drenderer.cpp
qgspointcloudlayerchunkloader_p.cpp
Expand Down Expand Up @@ -176,6 +177,7 @@ set(QGIS_3D_PRIVATE_HDRS
mesh/qgsmesh3dentity_p.h
mesh/qgsmesh3dgeometry_p.h
mesh/qgsmesh3dmaterial_p.h
qgscolorramptexture.h
)

set (QGIS_3D_RCCS shaders.qrc ../../resources/3d/textures/textures.qrc)
Expand Down
113 changes: 1 addition & 112 deletions src/3d/mesh/qgsmesh3dmaterial_p.cpp
Expand Up @@ -33,118 +33,7 @@
#include "qgsmeshlayerutils.h"
#include "qgstriangularmesh.h"

class ColorRampTextureGenerator: public Qt3DRender::QTextureImageDataGenerator
{

public:
ColorRampTextureGenerator( const QgsColorRampShader &colorRampShader, double verticalScale = 1 ):
mColorRampShader( colorRampShader ),
mVerticalScale( verticalScale )
{}

public:
Qt3DRender::QTextureImageDataPtr operator()() override
{
Qt3DRender::QTextureImageDataPtr dataPtr = Qt3DRender::QTextureImageDataPtr::create();
dataPtr->setFormat( QOpenGLTexture::RGBA32F );
dataPtr->setTarget( QOpenGLTexture::Target1D );
dataPtr->setPixelFormat( QOpenGLTexture::RGBA );
dataPtr->setPixelType( QOpenGLTexture::Float32 );

QByteArray data;
QList<QgsColorRampShader::ColorRampItem> colorItemList = mColorRampShader.colorRampItemList();
int size = colorItemList.count() ;

dataPtr->setWidth( size );
dataPtr->setHeight( 1 );
dataPtr->setDepth( 1 );
dataPtr->setFaces( 1 );
dataPtr->setLayers( 1 );
dataPtr->setMipLevels( 1 );

for ( int i = 0; i < colorItemList.count(); ++i )
{
float mag = float( colorItemList.at( i ).value * mVerticalScale );

QColor color = colorItemList.at( i ).color;
float rf = float( color.redF() );
float gf = float( color.greenF() );
float bf = float( color.blueF() );

data.append( reinterpret_cast<const char *>( &mag ), sizeof( float ) );
data.append( reinterpret_cast<const char *>( &rf ), sizeof( float ) );
data.append( reinterpret_cast<const char *>( &gf ), sizeof( float ) );
data.append( reinterpret_cast<const char *>( &bf ), sizeof( float ) );

}

dataPtr->setData( data, sizeof( float ) ); //size is the size of the type, here float

return dataPtr;
}

bool operator ==( const Qt3DRender::QTextureImageDataGenerator &other ) const override
{
const ColorRampTextureGenerator *otherFunctor = functor_cast<ColorRampTextureGenerator>( &other );
if ( !otherFunctor )
return false;

QgsColorRampShader otherColorRampShader = otherFunctor->mColorRampShader;

if ( mColorRampShader.colorRampItemList().count() != otherColorRampShader.colorRampItemList().count() ||
mColorRampShader.classificationMode() != otherColorRampShader.classificationMode() ||
mColorRampShader.colorRampType() != otherColorRampShader.colorRampType() )
{
return false;
}

QList<QgsColorRampShader::ColorRampItem> colorItemList = mColorRampShader.colorRampItemList();
QList<QgsColorRampShader::ColorRampItem> otherColorItemList = otherColorRampShader.colorRampItemList();
for ( int i = 0; i < colorItemList.count(); ++i )
{
const QColor color = colorItemList.at( i ).color;
const QColor otherColor = otherColorItemList.at( i ).color;
double value = colorItemList.at( i ).value;
double otherValue = otherColorItemList.at( i ).value;
if ( color != otherColor ||
( !std::isnan( value ) && !std::isnan( otherValue ) && colorItemList.at( i ).value != otherColorItemList.at( i ).value ) ||
( std::isnan( value ) != std::isnan( otherValue ) ) )
return false;
}

return true;
}

QT3D_FUNCTOR( ColorRampTextureGenerator )

private:
QgsColorRampShader mColorRampShader;
double mVerticalScale = 1;
};


class ColorRampTexture: public Qt3DRender::QAbstractTextureImage
{
public:
ColorRampTexture( const QgsColorRampShader &colorRampShader, double verticalScale = 1, Qt3DCore::QNode *parent = nullptr ):
Qt3DRender::QAbstractTextureImage( parent ),
mColorRampShader( colorRampShader ),
mVerticalScale( verticalScale )
{

}
// QAbstractTextureImage interface
protected:
Qt3DRender::QTextureImageDataGeneratorPtr dataGenerator() const override
{
return Qt3DRender::QTextureImageDataGeneratorPtr( new ColorRampTextureGenerator( mColorRampShader, mVerticalScale ) );
}

private:
QgsColorRampShader mColorRampShader;
double mVerticalScale = 1;
};

#include "qgscolorramptexture.h"

class ArrowsTextureGenerator: public Qt3DRender::QTextureImageDataGenerator
{
Expand Down
113 changes: 113 additions & 0 deletions src/3d/qgscolorramptexture.cpp
@@ -0,0 +1,113 @@
/***************************************************************************
qgscolorramptexture.h
-------------------------
begin : january 2020
copyright : (C) 2020 by Vincent Cloarec
email : vcloarec 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 "qgscolorramptexture.h"

// ColorRampTextureGenerator

ColorRampTextureGenerator::ColorRampTextureGenerator( const QgsColorRampShader &colorRampShader, double verticalScale )
: mColorRampShader( colorRampShader )
, mVerticalScale( verticalScale )
{
}

Qt3DRender::QTextureImageDataPtr ColorRampTextureGenerator::operator()()
{
Qt3DRender::QTextureImageDataPtr dataPtr = Qt3DRender::QTextureImageDataPtr::create();
dataPtr->setFormat( QOpenGLTexture::RGBA32F );
dataPtr->setTarget( QOpenGLTexture::Target1D );
dataPtr->setPixelFormat( QOpenGLTexture::RGBA );
dataPtr->setPixelType( QOpenGLTexture::Float32 );

QByteArray data;
QList<QgsColorRampShader::ColorRampItem> colorItemList = mColorRampShader.colorRampItemList();
int size = colorItemList.count() ;

dataPtr->setWidth( size );
dataPtr->setHeight( 1 );
dataPtr->setDepth( 1 );
dataPtr->setFaces( 1 );
dataPtr->setLayers( 1 );
dataPtr->setMipLevels( 1 );

for ( int i = 0; i < colorItemList.count(); ++i )
{
float mag = float( colorItemList.at( i ).value * mVerticalScale );

QColor color = colorItemList.at( i ).color;
float rf = float( color.redF() );
float gf = float( color.greenF() );
float bf = float( color.blueF() );

data.append( reinterpret_cast<const char *>( &mag ), sizeof( float ) );
data.append( reinterpret_cast<const char *>( &rf ), sizeof( float ) );
data.append( reinterpret_cast<const char *>( &gf ), sizeof( float ) );
data.append( reinterpret_cast<const char *>( &bf ), sizeof( float ) );

}

dataPtr->setData( data, sizeof( float ) ); //size is the size of the type, here float

return dataPtr;
}

bool ColorRampTextureGenerator::operator==( const Qt3DRender::QTextureImageDataGenerator &other ) const
{
const ColorRampTextureGenerator *otherFunctor = functor_cast<ColorRampTextureGenerator>( &other );
if ( !otherFunctor )
return false;

QgsColorRampShader otherColorRampShader = otherFunctor->mColorRampShader;

if ( mColorRampShader.colorRampItemList().count() != otherColorRampShader.colorRampItemList().count() ||
mColorRampShader.classificationMode() != otherColorRampShader.classificationMode() ||
mColorRampShader.colorRampType() != otherColorRampShader.colorRampType() )
{
return false;
}

QList<QgsColorRampShader::ColorRampItem> colorItemList = mColorRampShader.colorRampItemList();
QList<QgsColorRampShader::ColorRampItem> otherColorItemList = otherColorRampShader.colorRampItemList();
for ( int i = 0; i < colorItemList.count(); ++i )
{
const QColor color = colorItemList.at( i ).color;
const QColor otherColor = otherColorItemList.at( i ).color;
double value = colorItemList.at( i ).value;
double otherValue = otherColorItemList.at( i ).value;
if ( color != otherColor ||
( !std::isnan( value ) && !std::isnan( otherValue ) && colorItemList.at( i ).value != otherColorItemList.at( i ).value ) ||
( std::isnan( value ) != std::isnan( otherValue ) ) )
return false;
}

return true;
}

// ColorRampTexture

ColorRampTexture::ColorRampTexture( const QgsColorRampShader &colorRampShader, double verticalScale, Qt3DCore::QNode *parent )
: Qt3DRender::QAbstractTextureImage( parent ),
mColorRampShader( colorRampShader ),
mVerticalScale( verticalScale )
{

}

Qt3DRender::QTextureImageDataGeneratorPtr ColorRampTexture::dataGenerator() const
{
return Qt3DRender::QTextureImageDataGeneratorPtr( new ColorRampTextureGenerator( mColorRampShader, mVerticalScale ) );
}

0 comments on commit 7e25284

Please sign in to comment.