QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgscolorramptexture.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscolorramptexture.h
3 -------------------------
4 begin : january 2020
5 copyright : (C) 2020 by Vincent Cloarec
6 email : vcloarec at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgscolorramptexture.h"
19#include "moc_qgscolorramptexture.cpp"
20
22
23// ColorRampTextureGenerator
24
25QgsColorRampTextureGenerator::QgsColorRampTextureGenerator( const QgsColorRampShader &colorRampShader, double verticalScale )
26 : mColorRampShader( colorRampShader )
27 , mVerticalScale( verticalScale )
28{
29}
30
31Qt3DRender::QTextureImageDataPtr QgsColorRampTextureGenerator::operator()()
32{
33 Qt3DRender::QTextureImageDataPtr dataPtr = Qt3DRender::QTextureImageDataPtr::create();
34 dataPtr->setFormat( QOpenGLTexture::RGBA32F );
35 dataPtr->setTarget( QOpenGLTexture::Target1D );
36 dataPtr->setPixelFormat( QOpenGLTexture::RGBA );
37 dataPtr->setPixelType( QOpenGLTexture::Float32 );
38
39 QByteArray data;
40 const QList<QgsColorRampShader::ColorRampItem> colorItemList = mColorRampShader.colorRampItemList();
41 const int size = colorItemList.count();
42
43 dataPtr->setWidth( size );
44 dataPtr->setHeight( 1 );
45 dataPtr->setDepth( 1 );
46 dataPtr->setFaces( 1 );
47 dataPtr->setLayers( 1 );
48 dataPtr->setMipLevels( 1 );
49
50 for ( int i = 0; i < colorItemList.count(); ++i )
51 {
52 float mag = float( colorItemList.at( i ).value * mVerticalScale );
53
54 const QColor color = colorItemList.at( i ).color;
55 if ( color.alphaF() == 0.0f )
56 continue;
57 float rf = float( color.redF() );
58 float gf = float( color.greenF() );
59 float bf = float( color.blueF() );
60
61 data.append( reinterpret_cast<const char *>( &mag ), sizeof( float ) );
62 data.append( reinterpret_cast<const char *>( &rf ), sizeof( float ) );
63 data.append( reinterpret_cast<const char *>( &gf ), sizeof( float ) );
64 data.append( reinterpret_cast<const char *>( &bf ), sizeof( float ) );
65 }
66
67 dataPtr->setData( data, sizeof( float ) ); //size is the size of the type, here float
68
69 return dataPtr;
70}
71
72bool QgsColorRampTextureGenerator::operator==( const Qt3DRender::QTextureImageDataGenerator &other ) const
73{
74 const QgsColorRampTextureGenerator *otherFunctor = functor_cast<QgsColorRampTextureGenerator>( &other );
75 if ( !otherFunctor )
76 return false;
77
78 return mColorRampShader == otherFunctor->mColorRampShader;
79}
80
81// ColorRampTexture
82
83QgsColorRampTexture::QgsColorRampTexture( const QgsColorRampShader &colorRampShader, double verticalScale, Qt3DCore::QNode *parent )
84 : Qt3DRender::QAbstractTextureImage( parent ), mColorRampShader( colorRampShader ), mVerticalScale( verticalScale )
85{
86}
87
88Qt3DRender::QTextureImageDataGeneratorPtr QgsColorRampTexture::dataGenerator() const
89{
90 return Qt3DRender::QTextureImageDataGeneratorPtr( new QgsColorRampTextureGenerator( mColorRampShader, mVerticalScale ) );
91}
92
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.