aboutsummaryrefslogtreecommitdiff
path: root/voronoisplat.h
blob: 9859e8d862dbde0cc82fa5344b99698b63256f81 (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
#ifndef VORONOISPLAT_H
#define VORONOISPLAT_H

#include <QSGDynamicTexture>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <armadillo>

#include "colorscale.h"

class VoronoiSplatTexture
    : public QSGDynamicTexture
{
public:
    // 'size' must be square (and power of 2)
    VoronoiSplatTexture(const QSize &size);
    virtual ~VoronoiSplatTexture();

    void bind();

    // Call this whenever the texture should be updated (after changing the
    // sites, values or colormap). When true is returned, should probably call
    // resetOpenGLState() on QtQuick window
    bool updateTexture();

    bool hasAlphaChannel() const { return true; }
    bool hasMipmaps() const { return false; }
    int textureId() const { return m_tex; }
    QSize textureSize() const { return m_size; }

    // 'points' should be a 2D points matrix (each point in a row)
    void setSites(const arma::mat &points);

    // Set the value to be colormapped in each site
    void setValues(const arma::vec &values);

    // Set colormap data based on the given color scale;
    void setColormap(const ColorScale *scale);

private:
    void setupShaders();
    void setupVAOs();
    void setupTextures();

    void updateSites();
    void updateValues();
    void updateColormap();
    void computeDT();

    QOpenGLFunctions gl;
    QOpenGLShaderProgram *m_program1, *m_program2;
    GLuint m_FBO;
    GLuint m_VBOs[3];
    GLuint m_textures[2], m_colormapTex, m_tex;
    QOpenGLVertexArrayObject m_sitesVAO, m_2ndPassVAO;

    std::vector<float> m_sites, m_values, m_cmap;
    QSize m_size;

    bool m_sitesChanged, m_valuesChanged, m_colormapChanged;
};

#endif // VORONOISPLAT_H