1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#include "colormap.h"
#include <QOpenGLFunctions>
#include <QSGSimpleTextureNode>
class ColormapTexture
: public QSGDynamicTexture
{
public:
ColormapTexture(const std::vector<float> *cmap);
~ColormapTexture();
bool hasAlphaChannel() const { return false; }
bool hasMipmaps() const { return false; }
void bind();
bool updateTexture();
int textureId() const { return m_texture; }
QSize textureSize() const { return m_size; }
private:
QOpenGLFunctions gl;
QSize m_size;
GLuint m_texture;
const std::vector<float> *m_cmap;
};
ColormapTexture::ColormapTexture(const std::vector<float> *cmap)
: gl(QOpenGLContext::currentContext())
, m_size(cmap->size() / 3, 1)
, m_cmap(cmap)
{
// Setup OpenGL texture
gl.glGenTextures(1, &m_texture);
gl.glBindTexture(GL_TEXTURE_2D, m_texture);
gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_size.width(), m_size.height(),
0, GL_RGB, GL_FLOAT, 0);
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
ColormapTexture::~ColormapTexture()
{
gl.glDeleteTextures(1, &m_texture);
}
void ColormapTexture::bind()
{
gl.glBindTexture(GL_TEXTURE_2D, m_texture);
}
bool ColormapTexture::updateTexture()
{
m_size.setWidth(m_cmap->size() / 3);
gl.glBindTexture(GL_TEXTURE_2D, m_texture);
gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_size.width(), m_size.height(),
0, GL_RGB, GL_FLOAT, m_cmap->data());
return true;
}
Colormap::Colormap(QQuickItem *parent)
: QQuickItem(parent)
, m_texture(0)
, m_shouldUpdateTexture(false)
{
setFlag(QQuickItem::ItemHasContents);
}
Colormap::~Colormap()
{
if (m_texture) {
delete m_texture;
}
}
void Colormap::setColorScale(const ColorScale &scale)
{
m_cmap.resize(scale.numColors() * 3);
scale.sample(scale.numColors(), m_cmap.data());
emit colorScaleChanged(scale);
m_shouldUpdateTexture = true;
update();
}
QSGNode *Colormap::newSceneGraph()
{
QSGSimpleTextureNode *node = new QSGSimpleTextureNode;
m_texture = new ColormapTexture(&m_cmap);
node->setTexture(m_texture);
node->setOwnsTexture(false);
const QSize &texSize = m_texture->textureSize();
node->setSourceRect(0, 0, texSize.width(), texSize.height());
return node;
}
QSGNode *Colormap::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
QSGNode *root = oldNode ? oldNode : newSceneGraph();
QSGSimpleTextureNode *node = static_cast<QSGSimpleTextureNode *>(root);
node->setRect(x(), y(), width(), height());
if (m_shouldUpdateTexture) {
m_texture->updateTexture();
m_shouldUpdateTexture = false;
}
return root;
}
|