aboutsummaryrefslogtreecommitdiff
path: root/colormap.cpp
blob: 0a9b4cc7d080a1ac369be69d5e832d047e9ff7c1 (plain)
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
#include "colormap.h"

#include <algorithm>
#include <glad/gl.h>

Colormap::Colormap()
    : m_width(0)
    , m_height(0)
    , m_shouldUpdateTexture(false)
    , m_orientation(Colormap::Horizontal)
{
    glGenTextures(1, &m_texture);
    glBindTexture(GL_TEXTURE_2D, m_texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}

Colormap::~Colormap()
{
    glDeleteTextures(1, &m_texture);
}

void Colormap::update()
{
    if (m_shouldUpdateTexture) {
        updateTexture();
    }
}

void Colormap::updateTexture()
{
    if (m_cmap.empty()) {
        return;
    }

    switch (m_orientation) {
    case Colormap::Horizontal:
        m_width = m_cmap.size() / 3;
        m_height = 1;
        break;
    case Colormap::Vertical:
        m_width = 1;
        m_height = m_cmap.size() / 3;
        break;
    }

    glBindTexture(GL_TEXTURE_2D, m_texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, m_width, m_height,
            0, GL_RGB, GL_FLOAT, m_cmap.data());
}

static void reverseCMap(std::vector<float> &cmap)
{
    decltype(cmap.size()) i = 0, j = cmap.size() - 3;

    while (i < j) {
        std::swap(cmap[i++], cmap[j++]);
        std::swap(cmap[i++], cmap[j++]);
        std::swap(cmap[i++], cmap[j++]);

        j -= 6;
    }
}

void Colormap::setOrientation(Colormap::Orientation _orientation)
{
    if (m_orientation == _orientation) {
        return;
    }

    if (!m_cmap.empty()) {
        reverseCMap(m_cmap);
    }

    m_orientation = _orientation;
    m_shouldUpdateTexture = true;
    update();
}

void Colormap::setColorScale(std::shared_ptr<const ColorScale> scale)
{
    m_cmap.resize(SAMPLES * 3);
    scale->sample(SAMPLES, m_cmap.data());
    if (m_orientation == Colormap::Vertical) {
        reverseCMap(m_cmap);
    }

    colorScaleChanged(scale);

    m_shouldUpdateTexture = true;
    update();
}