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