summaryrefslogtreecommitdiffstats
path: root/src/widget/wvumeterglsl.cpp
blob: 9d2c8995969dde0fee02a4c2d9aa5b929cd1e9b4 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "widget/wvumeterglsl.h"

#include "util/assert.h"
#include "util/math.h"
#include "util/texture.h"

WVuMeterGLSL::WVuMeterGLSL(QWidget* pParent)
        : WVuMeterBase(pParent) {
}

WVuMeterGLSL::~WVuMeterGLSL() {
    cleanupGL();
}

void WVuMeterGLSL::draw() {
    if (shouldRender()) {
        makeCurrentIfNeeded();
        paintGL();
        doneCurrent();
    }
}

void WVuMeterGLSL::initializeGL() {
    m_pTextureBack.reset(createTexture(m_pPixmapBack));
    m_pTextureVu.reset(createTexture(m_pPixmapVu));
    m_textureShader.init();
}

void WVuMeterGLSL::paintGL() {
    glClearColor(static_cast<float>(m_qBgColor.redF()),
            static_cast<float>(m_qBgColor.greenF()),
            static_cast<float>(m_qBgColor.blueF()),
            1.f);
    glClear(GL_COLOR_BUFFER_BIT);

    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    QMatrix4x4 matrix;
    matrix.ortho(QRectF(0, 0, width(), height()));

    m_textureShader.bind();

    int matrixLocation = m_textureShader.matrixLocation();
    int samplerLocation = m_textureShader.samplerLocation();
    int positionLocation = m_textureShader.positionLocation();
    int texcoordLocation = m_textureShader.texcoordLocation();

    m_textureShader.setUniformValue(matrixLocation, matrix);

    m_textureShader.setUniformValue(matrixLocation, matrix);

    m_textureShader.enableAttributeArray(positionLocation);
    m_textureShader.enableAttributeArray(texcoordLocation);

    m_textureShader.setUniformValue(samplerLocation, 0);

    if (m_pTextureBack) {
        QRectF sourceRect(0, 0, m_pPixmapBack->width(), m_pPixmapBack->height());
        drawTexture(m_pTextureBack.get(), rect(), sourceRect);
    }

    const double widgetWidth = width();
    const double widgetHeight = height();

    if (m_pTextureVu) {
        const double pixmapWidth = m_pTextureVu->width();
        const double pixmapHeight = m_pTextureVu->height();
        if (m_bHorizontal) {
            const double widgetPosition = math_clamp(widgetWidth * m_dParameter, 0.0, widgetWidth);
            QRectF targetRect(0, 0, widgetPosition, widgetHeight);

            const double pixmapPosition = math_clamp(
                    pixmapWidth * m_dParameter, 0.0, pixmapWidth);
            QRectF sourceRect(0, 0, pixmapPosition, pixmapHeight);
            drawTexture(m_pTextureVu.get(), targetRect, sourceRect);

            if (m_iPeakHoldSize > 0 && m_dPeakParameter > 0.0 &&
                    m_dPeakParameter > m_dParameter) {
                const double widgetPeakPosition = math_clamp(
                        widgetWidth * m_dPeakParameter, 0.0, widgetWidth);
                const double pixmapPeakHoldSize = static_cast<double>(m_iPeakHoldSize);
                const double widgetPeakHoldSize = widgetWidth * pixmapPeakHoldSize / pixmapWidth;

                QRectF targetRect(widgetPeakPosition - widgetPeakHoldSize,
                        0,
                        widgetPeakHoldSize,
                        widgetHeight);

                const double pixmapPeakPosition = math_clamp(
                        pixmapWidth * m_dPeakParameter, 0.0, pixmapWidth);

                QRectF sourceRect =
                        QRectF(pixmapPeakPosition - pixmapPeakHoldSize,
                                0,
                                pixmapPeakHoldSize,
                                pixmapHeight);
                drawTexture(m_pTextureVu.get(), targetRect, sourceRect);
            }
        } else {
            // vertical
            const double widgetPosition =
                    math_clamp(widgetHeight * m_dParameter, 0.0, widgetHeight);
            QRectF targetRect(0, widgetHeight - widgetPosition, widgetWidth, widgetPosition);

            const double pixmapPosition = math_clamp(
                    pixmapHeight * m_dParameter, 0.0, pixmapHeight);
            QRectF sourceRect(0, pixmapHeight - pixmapPosition, pixmapWidth, pixmapPosition);
            drawTexture(m_pTextureVu.get(), targetRect, sourceRect);

            if (m_iPeakHoldSize > 0 && m_dPeakParameter > 0.0 &&
                    m_dPeakParameter > m_dParameter) {
                const double widgetPeakPosition = math_clamp(
                        widgetHeight * m_dPeakParameter, 0.0, widgetHeight);
                const double pixmapPeakHoldSize = static_cast<double>(m_iPeakHoldSize);
                const double widgetPeakHoldSize = widgetHeight * pixmapPeakHoldSize / pixmapHeight;

                QRectF targetRect(0,
                        widgetHeight - widgetPeakPosition,
                        widgetWidth,
                        widgetPeakHoldSize);

                const double pixmapPeakPosition = math_clamp(
                        pixmapHeight * m_dPeakParameter, 0.0, pixmapHeight);

                QRectF sourceRect = QRectF(0,
                        pixmapHeight - pixmapPeakPosition,
                        pixmapWidth,
                        pixmapPeakHoldSize);
                drawTexture(m_pTextureVu.get(), targetRect, sourceRect);
            }
        }
    }

    m_textureShader.disableAttributeArray(positionLocation);
    m_textureShader.disableAttributeArray(texcoordLocation);
    m_textureShader.release();
}

void WVuMeterGLSL::cleanupGL() {
    makeCurrentIfNeeded();
    m_pTextureBack.reset();
    m_pTextureVu.reset();
    doneCurrent();
}

void WVuMeterGLSL::drawTexture(QOpenGLTexture* texture,
        const QRectF& targetRect,
        const QRectF& sourceRect) {
    const float texx1 = static_cast<float>(sourceRect.x() / texture->width());
    const float texy1 = static_cast<float>(sourceRect.y() / texture->height());
    const float texx2 = static_cast<float>(
            (sourceRect.x() + sourceRect.width()) / texture->width());
    const float texy2 = static_cast<float>(
            (sourceRect.y() + sourceRect.height()) / texture->height());

    const float posx1 = static_cast<float>(targetRect.x());
    const float posy1 = static_cast<float>(targetRect.y());
    const float posx2 = static_cast<float>(targetRect.x() + targetRect.width());
    const float posy2 = static_cast<float>(targetRect.y() + targetRect.height());

    const float posarray[] = {posx1, posy1, posx2, posy1, posx1, posy2, posx2, posy2};
    const float texarray[] = {texx1, texy1, texx2, texy1, texx1, texy2, texx2, texy2};

    int positionLocation = m_textureShader.positionLocation();
    int texcoordLocation = m_textureShader.texcoordLocation();

    m_textureShader.setAttributeArray(
            positionLocation, GL_FLOAT, posarray, 2);
    m_textureShader.setAttributeArray(
            texcoordLocation, GL_FLOAT, texarray, 2);

    texture->bind();

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    texture->release();
}