QGIS API Documentation 3.99.0-Master (752b475928d)
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
20#include "moc_qgscolorramptexture.cpp"
21
23
24// ColorRampTextureGenerator
25
26QgsColorRampTextureGenerator::QgsColorRampTextureGenerator( const QgsColorRampShader &colorRampShader, double verticalScale )
27 : mColorRampShader( colorRampShader )
28 , mVerticalScale( verticalScale )
29{
30}
31
32Qt3DRender::QTextureImageDataPtr QgsColorRampTextureGenerator::operator()()
33{
34 Qt3DRender::QTextureImageDataPtr dataPtr = Qt3DRender::QTextureImageDataPtr::create();
35 dataPtr->setFormat( QOpenGLTexture::RGBA32F );
36 dataPtr->setTarget( QOpenGLTexture::Target1D );
37 dataPtr->setPixelFormat( QOpenGLTexture::RGBA );
38 dataPtr->setPixelType( QOpenGLTexture::Float32 );
39
40 QByteArray data;
41 const QList<QgsColorRampShader::ColorRampItem> colorItemList = mColorRampShader.colorRampItemList();
42 const int size = colorItemList.count();
43
44 dataPtr->setWidth( size );
45 dataPtr->setHeight( 1 );
46 dataPtr->setDepth( 1 );
47 dataPtr->setFaces( 1 );
48 dataPtr->setLayers( 1 );
49 dataPtr->setMipLevels( 1 );
50
51 for ( int i = 0; i < colorItemList.count(); ++i )
52 {
53 float mag = float( colorItemList.at( i ).value * mVerticalScale );
54
55 const QColor color = colorItemList.at( i ).color;
56 if ( color.alphaF() == 0.0f )
57 continue;
58 float rf = float( color.redF() );
59 float gf = float( color.greenF() );
60 float bf = float( color.blueF() );
61
62 data.append( reinterpret_cast<const char *>( &mag ), sizeof( float ) );
63 data.append( reinterpret_cast<const char *>( &rf ), sizeof( float ) );
64 data.append( reinterpret_cast<const char *>( &gf ), sizeof( float ) );
65 data.append( reinterpret_cast<const char *>( &bf ), sizeof( float ) );
66 }
67
68 dataPtr->setData( data, sizeof( float ) ); //size is the size of the type, here float
69
70 return dataPtr;
71}
72
73bool QgsColorRampTextureGenerator::operator==( const Qt3DRender::QTextureImageDataGenerator &other ) const
74{
75 const QgsColorRampTextureGenerator *otherFunctor = dynamic_cast<const QgsColorRampTextureGenerator *>( &other );
76 if ( !otherFunctor )
77 return false;
78
79 return mColorRampShader == otherFunctor->mColorRampShader;
80}
81
82// ColorRampTexture
83
84QgsColorRampTexture::QgsColorRampTexture( const QgsColorRampShader &colorRampShader, double verticalScale, Qt3DCore::QNode *parent )
85 : Qt3DRender::QAbstractTextureImage( parent ), mColorRampShader( colorRampShader ), mVerticalScale( verticalScale )
86{
87}
88
89Qt3DRender::QTextureImageDataGeneratorPtr QgsColorRampTexture::dataGenerator() const
90{
91 return Qt3DRender::QTextureImageDataGeneratorPtr( new QgsColorRampTextureGenerator( mColorRampShader, mVerticalScale ) );
92}
93
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.