aboutsummaryrefslogtreecommitdiff
path: root/voronoisplat.h
blob: 59132185e296d80b3c28735c0cefc82fb6765310 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef VORONOISPLAT_H
#define VORONOISPLAT_H

#include <QQuickFramebufferObject>
#include <QOpenGLFunctions>
#include <QOpenGLBuffer>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <armadillo>

#include "colorscale.h"

class VoronoiSplat; // defined after this class

class VoronoiSplatRenderer
    : public QQuickFramebufferObject::Renderer
{
public:
    // 'size' must be square (and power of 2)
    VoronoiSplatRenderer(const QSize &size);
    ~VoronoiSplatRenderer();

    void synchronize(QQuickFramebufferObject *item);

    // '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);

    QOpenGLFramebufferObject *createFramebufferObject(const QSize &size);
    void render();

private:
    void setupShaders();
    void setupVAOs();
    void setupTextures();
    void copyPoints(const arma::mat &points);
    void computeDT();

    VoronoiSplat *m_item;
    QOpenGLFunctions gl;
    QOpenGLShaderProgram *m_program1, *m_program2;
    GLuint m_VBOs[3];
    GLuint m_textures[2], m_colorMapTex;
    QOpenGLVertexArrayObject m_sitesVAO, m_2ndPassVAO;

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

class VoronoiSplat
    : public QQuickFramebufferObject
{
    Q_OBJECT
public:
    VoronoiSplat(QQuickItem *parent = 0);
    Renderer *createRenderer() const;

    const arma::mat &points() const { return m_points; }
    const arma::vec &values() const { return m_values; }
    const ColorScale *colorScale() const { return m_colorScale; }

    bool colorScaleUpdated() const { return m_colorScaleUpdated; }
    bool pointsUpdated() const { return m_pointsUpdated; }
    bool valuesUpdated() const { return m_valuesUpdated; }

public slots:
    void setColorScale(const ColorScale *scale)
    {
        m_colorScale = scale;
        m_colorScaleUpdated = true;
        update();
    }

    void setPoints(const arma::mat &points)
    {
        m_points = points;
        m_pointsUpdated = true;
        update();
    }

    void setValues(const arma::vec &values)
    {
        m_values = values;
        m_valuesUpdated = true;
        update();
    }

    void setUpdated(bool updated)
    {
        m_colorScaleUpdated = updated;
        m_pointsUpdated     = updated;
        m_valuesUpdated     = updated;
    }

private:
    bool m_colorScaleUpdated, m_pointsUpdated, m_valuesUpdated;
    const ColorScale *m_colorScale;
    arma::mat m_points;
    arma::vec m_values;
};

#endif // VORONOISPLAT_H