File: LoudspeakerVisualizer.h

package info (click to toggle)
iem-plugin-suite 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,080 kB
  • sloc: cpp: 58,973; python: 269; sh: 79; makefile: 41
file content (614 lines) | stat: -rw-r--r-- 24,118 bytes parent folder | download | duplicates (2)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
 ==============================================================================
 This file is part of the IEM plug-in suite.
 Authors: Daniel Rudrich, Franz Zotter
 Copyright (c) 2017 - Institute of Electronic Music and Acoustics (IEM)
 https://iem.at

 The IEM plug-in suite is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 The IEM plug-in suite is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this software.  If not, see <https://www.gnu.org/licenses/>.
 ==============================================================================
 */

#pragma once

#include "../JuceLibraryCode/JuceHeader.h"

//==============================================================================
/*
*/
class LoudspeakerVisualizer : public juce::Component, public juce::OpenGLRenderer
{
    struct positionAndColour
    {
        float position[3];
        float colourId;
    };

public:
    LoudspeakerVisualizer (std::vector<R3>& pts,
                           std::vector<Tri>& tris,
                           std::vector<juce::Vector3D<float>>& norms,
                           juce::BigInteger& imagFlags) :
        extPoints (pts), extTriangles (tris), extNormals (norms), imaginaryFlags (imagFlags)
    {
        juce::OpenGLPixelFormat pf;
        pf.multisamplingLevel = 4;
        openGLContext.setPixelFormat (pf);
        openGLContext.setMultisamplingEnabled (true);
        openGLContext.setComponentPaintingEnabled (true);
        openGLContext.setContinuousRepainting (false);
        openGLContext.setRenderer (this);
        openGLContext.attachTo (*this);
    }

    ~LoudspeakerVisualizer() override
    {
        openGLContext.detach();
        openGLContext.setRenderer (nullptr);
    }
    void setActiveSpeakerIndex (int newIdx)
    {
        activePoint = newIdx;
        updateVerticesAndIndices();
    }

    void initialise()
    {
        using namespace juce;
        using namespace juce::gl;

        const float alphaVal = 0.8f;
        PixelARGB colormapData[8];

        colormapData[0] = juce::Colours::limegreen.getPixelARGB(); // selected colour
        colormapData[1] = juce::Colours::orange.getPixelARGB(); // imaginary colour
        colormapData[2] = juce::Colours::cornflowerblue.getPixelARGB(); // regular colour
        colormapData[3] =
            juce::Colours::cornflowerblue.withMultipliedAlpha (alphaVal).getPixelARGB();
        colormapData[4] = juce::Colours::limegreen.withMultipliedAlpha (alphaVal).getPixelARGB();
        colormapData[5] =
            juce::Colours::cornflowerblue.withMultipliedAlpha (alphaVal).getPixelARGB();
        colormapData[6] = juce::Colours::orange.withMultipliedAlpha (alphaVal).getPixelARGB();
        colormapData[7] = juce::Colours::red.withMultipliedAlpha (alphaVal).getPixelARGB();

        texture.loadARGB (colormapData, 8, 1);

        openGLContext.extensions.glActiveTexture (GL_TEXTURE0);
        glEnable (GL_TEXTURE_2D);

        texture.bind();
        glTexParameteri (GL_TEXTURE_2D,
                         GL_TEXTURE_MIN_FILTER,
                         GL_LINEAR); // linear interpolation when too small
        glTexParameteri (GL_TEXTURE_2D,
                         GL_TEXTURE_MAG_FILTER,
                         GL_LINEAR); // linear interpolation when too bi
    }

    void newOpenGLContextCreated() override
    {
        createShaders();
        initialise();
        updateVerticesAndIndices();
    }

    void resized() override
    {
        viewHasChanged = true;
        openGLContext.triggerRepaint();
    }

    void updateVerticesAndIndices()
    {
        vertices.clear();
        indices.clear();
        normals.clear();

        nPoints = (int) extPoints.size();

        for (int i = 0; i < nPoints; ++i)
        {
            float col = extPoints[i].lspNum == activePoint    ? 0.0f
                        : imaginaryFlags[extPoints[i].lspNum] ? 0.2f
                                                              : 0.4f;
            vertices.push_back ({ extPoints[i].x, extPoints[i].z, -extPoints[i].y, col });
            indices.push_back (i);
            normals.push_back (1.0f);
            normals.push_back (1.0f);
            normals.push_back (1.0f);
        }

        //normals.insert(normals.end(), extNormals.begin(), extNormals.end());

        nTriangles = (int) extTriangles.size();
        for (int i = 0; i < nTriangles; ++i)
        {
            const float col = 0.4f + 0.6f * ((float) i / nTriangles); // defining a colour
            juce::Vector3D<float> normal = extNormals[i];
            Tri tr = extTriangles[i];
            juce::Vector3D<float> a { extPoints[tr.a].x, extPoints[tr.a].y, extPoints[tr.a].z };
            juce::Vector3D<float> b { extPoints[tr.b].x, extPoints[tr.b].y, extPoints[tr.b].z };
            juce::Vector3D<float> c { extPoints[tr.c].x, extPoints[tr.c].y, extPoints[tr.c].z };

            // making sure that triangles are facing outward
            if (normal * ((b - a) ^ (c - a))
                < 0.0f) // incorrect but no swap because of inverse y axis swap
            {
                vertices.push_back ({ { a.x, a.z, -a.y }, col });
                vertices.push_back ({ b.x, b.z, -b.y, col });
            }
            else // correct -> swap (inverse y axis)
            {
                vertices.push_back ({ b.x, b.z, -b.y, col });
                vertices.push_back ({ a.x, a.z, -a.y, col });
            }
            vertices.push_back ({ c.x, c.z, -c.y, col });

            indices.push_back (nPoints + i * 3);
            indices.push_back (nPoints + i * 3 + 1);
            indices.push_back (nPoints + i * 3 + 2);

            normals.push_back (normal.x);
            normals.push_back (normal.z);
            normals.push_back (-normal.y);
            normals.push_back (normal.x);
            normals.push_back (normal.z);
            normals.push_back (-normal.y);
            normals.push_back (normal.x);
            normals.push_back (normal.z);
            normals.push_back (-normal.y);
        }

        updatedBuffers = true;

        openGLContext.triggerRepaint();
    }

    void uploadBuffers() // this should only be called by the openGl thread
    {
        using namespace juce;
        using namespace juce::gl;

        openGLContext.extensions.glDeleteBuffers (1, &vertexBuffer);
        openGLContext.extensions.glDeleteBuffers (1, &indexBuffer);
        openGLContext.extensions.glDeleteBuffers (1, &normalsBuffer);

        openGLContext.extensions.glGenBuffers (1, &vertexBuffer);
        openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer);
        openGLContext.extensions.glBufferData (GL_ARRAY_BUFFER,
                                               vertices.size() * sizeof (positionAndColour),
                                               &vertices[0],
                                               GL_STATIC_DRAW);

        openGLContext.extensions.glGenBuffers (1, &indexBuffer);
        openGLContext.extensions.glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
        openGLContext.extensions.glBufferData (GL_ELEMENT_ARRAY_BUFFER,
                                               indices.size() * sizeof (int),
                                               &indices[0],
                                               GL_STATIC_DRAW);

        openGLContext.extensions.glGenBuffers (1, &normalsBuffer);
        openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, normalsBuffer);
        openGLContext.extensions.glBufferData (GL_ARRAY_BUFFER,
                                               normals.size() * sizeof (float),
                                               &normals[0],
                                               GL_STATIC_DRAW);
    }

    void renderOpenGL() override
    {
        using namespace juce;
        using namespace juce::gl;

        jassert (juce::OpenGLHelpers::isContextActive());

        juce::OpenGLHelpers::clear (juce::Colour (0xFF2D2D2D));

        const float desktopScale = (float) openGLContext.getRenderingScale();
        glViewport (0,
                    0,
                    juce::roundToInt (desktopScale * getWidth()),
                    juce::roundToInt (desktopScale * getHeight()));

        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnable (GL_DEPTH_TEST);

#ifdef JUCE_OPENGL3
        glEnable (GL_BLEND);
        glBlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
        glBlendEquationSeparate (GL_FUNC_ADD, GL_FUNC_ADD);
#endif

        openGLContext.extensions.glActiveTexture (GL_TEXTURE0);
        glEnable (GL_TEXTURE_2D);
        texture.bind();

        if (updatedBuffers)
        {
            updatedBuffers = false;
            uploadBuffers();
        }

        shader->use();
        GLint programID = shader->getProgramID();

        if (viewHasChanged)
        {
            viewHasChanged = false;
            if (projectionMatrix != nullptr)
                projectionMatrix->setMatrix4 (getProjectionMatrix().mat, 1, false);
            if (viewMatrix != nullptr)
                viewMatrix->setMatrix4 (getViewMatrix().mat, 1, false);
        }

        GLint attributeID;

        // 1st attribute buffer : vertices
        attributeID = openGLContext.extensions.glGetAttribLocation (programID, "position");
        openGLContext.extensions.glEnableVertexAttribArray (attributeID);
        openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer);
        openGLContext.extensions.glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

        openGLContext.extensions.glVertexAttribPointer (
            attributeID, // attribute 0. No particular reason for 0, but must match the layout in the shader.
            3, // size
            GL_FLOAT, // type
            GL_FALSE, // normalized?
            sizeof (positionAndColour), // stride
            (void*) 0 // array buffer offset
        );

        // 2nd attribute buffer : normals
        attributeID = openGLContext.extensions.glGetAttribLocation (programID, "normals");
        openGLContext.extensions.glEnableVertexAttribArray (attributeID);
        openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, normalsBuffer);

        openGLContext.extensions.glVertexAttribPointer (
            attributeID, // attribute 0. No particular reason for 0, but must match the layout in the shader.
            3, // size
            GL_FLOAT, // type
            GL_FALSE, // normalized?
            0, // stride
            (void*) 0 // array buffer offset
        );

        // 3rd attribute buffer : colors
        attributeID = openGLContext.extensions.glGetAttribLocation (programID, "colormapDepthIn");
        openGLContext.extensions.glEnableVertexAttribArray (attributeID);
        openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer);

        openGLContext.extensions.glVertexAttribPointer (
            attributeID, // attribute
            1, // size
            GL_FLOAT, // type
            GL_TRUE, // normalized?
            sizeof (positionAndColour), // stride
            (void*) offsetof (positionAndColour, colourId) // array buffer offset
        );

        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);

        if (blackFlag != nullptr)
            blackFlag->set (0.0f);
        if (drawPointsFlag != nullptr)
            drawPointsFlag->set (0.0f);

        // render with alpha = 0, to prime the depth buffer

        if (alpha != nullptr)
            alpha->set (0.0f);
        glDisable (GL_CULL_FACE);
        glDepthFunc (GL_ALWAYS); // priming depth buffer
        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
        glDrawElements (GL_TRIANGLES, // mode
                        nTriangles * 3, // count
                        GL_UNSIGNED_INT, // type
                        (void*) (nPoints * sizeof (int)) // element array buffer offset
        );

        // setting alpha factor to 1 to actually draw something
        if (alpha != nullptr)
            alpha->set (1.0f);

        // draw points
        if (drawPointsFlag != nullptr)
            drawPointsFlag->set (1.0f);
        glPointSize (8 * desktopScale);
        glDepthFunc (GL_ALWAYS);
        glDrawElements (GL_POINTS, nPoints, GL_UNSIGNED_INT, (void*) 0); // Draw points!
        if (drawPointsFlag != nullptr)
            drawPointsFlag->set (0.0f);
        // draw background first (inward facing triangles in the background -> looking towards us)
        glEnable (GL_CULL_FACE);
        glCullFace (GL_BACK);
        glDepthFunc (GL_ALWAYS);
        glDrawElements (GL_TRIANGLES, // mode
                        nTriangles * 3, // count
                        GL_UNSIGNED_INT, // type
                        (void*) (nPoints * sizeof (int)) // element array buffer offset
        );

        // render black lines (all)
        glLineWidth (2.5 * desktopScale);
        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
        if (blackFlag != nullptr)
            blackFlag->set (1.0f);

        glDrawElements (GL_TRIANGLES, // mode
                        nTriangles * 3, // count
                        GL_UNSIGNED_INT, // type
                        (void*) (nPoints * sizeof (int)) // element array buffer offset
        );

        if (blackFlag != nullptr)
            blackFlag->set (0.0f);
        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);

        // draw foreground triangles
        glEnable (GL_CULL_FACE);
        glCullFace (GL_FRONT);
        glDepthFunc (GL_ALWAYS);
        glDrawElements (GL_TRIANGLES, // mode
                        nTriangles * 3, // count
                        GL_UNSIGNED_INT, // type
                        (void*) (nPoints * sizeof (int)) // element array buffer offset
        );

        // render black lines (foreground)
        glLineWidth (2.5 * desktopScale);
        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
        glDepthFunc (GL_LEQUAL);
        if (blackFlag != nullptr)
            blackFlag->set (1.0f);

        glDrawElements (GL_TRIANGLES, // mode
                        nTriangles * 3, // count
                        GL_UNSIGNED_INT, // type
                        (void*) (nPoints * sizeof (int)) // element array buffer offset
        );

        // draw foreground points
        if (drawPointsFlag != nullptr)
            drawPointsFlag->set (1.0f);
        if (blackFlag != nullptr)
            blackFlag->set (0.0f);
        glPointSize (8 * desktopScale);
        glDepthFunc (GL_LEQUAL);
        glDrawElements (GL_POINTS, nPoints, GL_UNSIGNED_INT, (void*) 0); // Draw points!
        if (drawPointsFlag != nullptr)
            drawPointsFlag->set (0.0f);

        openGLContext.extensions.glDisableVertexAttribArray (
            openGLContext.extensions.glGetAttribLocation (programID, "position"));
        openGLContext.extensions.glDisableVertexAttribArray (
            openGLContext.extensions.glGetAttribLocation (programID, "normals"));
        openGLContext.extensions.glDisableVertexAttribArray (
            openGLContext.extensions.glGetAttribLocation (programID, "colormapDepthIn"));

        openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, 0);
        openGLContext.extensions.glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
    }

    void openGLContextClosing() override
    {
        openGLContext.extensions.glDeleteBuffers (1, &vertexBuffer);
        openGLContext.extensions.glDeleteBuffers (1, &indexBuffer);
        openGLContext.extensions.glDeleteBuffers (1, &normalsBuffer);
        texture.release();
    }

    void mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel) override
    {
        const double delta =
            (std::abs (wheel.deltaX) > std::abs (wheel.deltaY) ? -wheel.deltaX : wheel.deltaY);

        zoom += delta;
        zoom = juce::jmin (zoom, 8.0f);
        zoom = juce::jmax (zoom, 2.5f);
        viewHasChanged = true;
        openGLContext.triggerRepaint();
    }

    void mouseDown (const juce::MouseEvent& e) override
    {
        tiltBeforeDrag = tilt;
        yawBeforeDrag = yaw;
    }

    void mouseDrag (const juce::MouseEvent& e) override
    {
        float deltaY = (float) e.getDistanceFromDragStartY() / 100;
        tilt = tiltBeforeDrag + deltaY;
        tilt = juce::jmin (tilt, (float) juce::MathConstants<float>::pi / 2.0f);
        tilt = juce::jmax (tilt, 0.0f);

        float deltaX = (float) e.getDistanceFromDragStartX() / 100;
        yaw = yawBeforeDrag + deltaX;
        viewHasChanged = true;
        openGLContext.triggerRepaint();
    }

    void createShaders()
    {
        // NOTE: the two lights below are single light sources which produce diffuse light on the body
        // adding speculars would be great
        vertexShader =
            "attribute vec3 position;\n"
            "attribute vec3 normals;\n"
            "attribute float colormapDepthIn;\n"
            "\n"
            "uniform mat4 projectionMatrix;\n"
            "uniform mat4 viewMatrix;\n"
            "uniform float blackFlag;\n"
            "uniform float alpha;\n"
            "uniform float drawPointsFlag;\n"
            "\n"
            "varying float colormapDepthOut;\n"
            "varying float lightIntensity;\n"
            "varying float blackFlagOut;\n"
            "varying float alphaOut;\n"
            "varying float drawPointsFlagOut;\n"
            "void main()\n"
            "{\n"
            "   gl_Position.xyz = 500.0 * position;\n" // scaling leads to a better result
            "   gl_Position.w = 1.0;\n"
            "   gl_Position = projectionMatrix * viewMatrix * gl_Position;\n"
            "   vec4 normal;\n"
            "   normal.xyz = normals;\n"
            "   normal.w = 0.0;\n"
            "   vec4 light = normalize(vec4(-0.8, 0.4, 0.8, 0.0));\n"
            "   float value;\n"
            "   value = dot (light , viewMatrix * normal);\n"
            "   lightIntensity = (value > 0.0) ? value : 0.0;\n"
            "   colormapDepthOut = colormapDepthIn;\n"
            "   blackFlagOut = blackFlag;\n"
            "   alphaOut = alpha;\n"
            "   drawPointsFlagOut = drawPointsFlag;\n"
            "}";

        fragmentShader =
            "varying float colormapDepthOut;\n"
            "varying float lightIntensity;\n"
            "varying float blackFlagOut;\n"
            "varying float alphaOut;\n"
            "varying float drawPointsFlagOut;\n"
            "uniform sampler2D tex0;\n"
            "void main()\n"
            "{\n"
            "      gl_FragColor = texture2D(tex0, vec2(colormapDepthOut, 0));\n"
            "      if (drawPointsFlagOut != 1.0) gl_FragColor.xyz = gl_FragColor.xyz * (0.2/0.9 + lightIntensity * 0.8/0.9);\n"
            "      if (blackFlagOut == 1.0) gl_FragColor = vec4(0, 0, 0, 1);"
            "      gl_FragColor.w = alphaOut * gl_FragColor.w;\n"
            "}";

        std::unique_ptr<juce::OpenGLShaderProgram> newShader (
            new juce::OpenGLShaderProgram (openGLContext));
        juce::String statusText;

        if (newShader->addVertexShader (
                juce::OpenGLHelpers::translateVertexShaderToV3 (vertexShader))
            && newShader->addFragmentShader (
                juce::OpenGLHelpers::translateFragmentShaderToV3 (fragmentShader))
            && newShader->link())
        {
            shader = std::move (newShader);
            shader->use();
            statusText =
                "GLSL: v" + juce::String (juce::OpenGLShaderProgram::getLanguageVersion(), 2);

            projectionMatrix.reset (createUniform (openGLContext, *shader, "projectionMatrix"));
            viewMatrix.reset (createUniform (openGLContext, *shader, "viewMatrix"));
            alpha.reset (createUniform (openGLContext, *shader, "alpha"));
            blackFlag.reset (createUniform (openGLContext, *shader, "blackFlag"));
            drawPointsFlag.reset (createUniform (openGLContext, *shader, "drawPointsFlag"));
        }
        else
        {
            statusText = newShader->getLastError();
        }
    }

    juce::Matrix3D<float> getProjectionMatrix() const
    {
        const float near = 1.0f;
        const float ratio = 1.0f / 3.0f; // similar to focal length (maybe reciprocal)
        const float w = near * ratio;
        const float h = w * getLocalBounds().toFloat().getAspectRatio (false);
        return juce::Matrix3D<float>::fromFrustum (-w, w, -h, h, near, 10000.0f);
    }

    juce::Matrix3D<float>
        createRotationMatrix (juce::Vector3D<float> eulerAngleRadians) const noexcept
    {
        const float cx = std::cos (eulerAngleRadians.x), sx = std::sin (eulerAngleRadians.x),
                    cy = std::cos (eulerAngleRadians.y), sy = std::sin (eulerAngleRadians.y),
                    cz = std::cos (eulerAngleRadians.z), sz = std::sin (eulerAngleRadians.z);

        return juce::Matrix3D<float> ((cy * cz) + (sx * sy * sz),
                                      cx * sz,
                                      (cy * sx * sz) - (cz * sy),
                                      0.0f,
                                      (cz * sx * sy) - (cy * sz),
                                      cx * cz,
                                      (cy * cz * sx) + (sy * sz),
                                      0.0f,
                                      cx * sy,
                                      -sx,
                                      cx * cy,
                                      0.0f,
                                      0.0f,
                                      0.0f,
                                      0.0f,
                                      1.0f);
    }

    juce::Matrix3D<float> getViewMatrix()
    {
        auto translationMatrix = juce::Matrix3D<float>::fromTranslation (
            { 0.0f, 0.0f, -500.0f * zoom }); // move object further away
        juce::Matrix3D<float> tiltMatrix =
            createRotationMatrix (juce::Vector3D<float> (tilt, 0.0f, 0.0f));
        juce::Matrix3D<float> rotationMatrix =
            createRotationMatrix (juce::Vector3D<float> (0.0f, yaw, 0.0f));
        return translationMatrix * tiltMatrix * rotationMatrix;
    }

private:
    GLuint vertexBuffer = 0, indexBuffer = 0, normalsBuffer = 0;
    const char* vertexShader;
    const char* fragmentShader;
    std::unique_ptr<juce::OpenGLShaderProgram> shader;
    std::unique_ptr<juce::OpenGLShaderProgram::Uniform> projectionMatrix, viewMatrix, alpha,
        blackFlag, drawPointsFlag;

    static juce::OpenGLShaderProgram::Uniform*
        createUniform (juce::OpenGLContext& openGLContext,
                       juce::OpenGLShaderProgram& shaderProgram,
                       const char* uniformName)
    {
        if (openGLContext.extensions.glGetUniformLocation (shaderProgram.getProgramID(),
                                                           uniformName)
            < 0)
            return nullptr; // Return if error
        return new juce::OpenGLShaderProgram::Uniform (shaderProgram, uniformName);
    }

    bool updatedBuffers;
    std::vector<R3>& extPoints;
    std::vector<Tri>& extTriangles;
    std::vector<juce::Vector3D<float>>& extNormals;
    juce::BigInteger& imaginaryFlags;

    std::vector<positionAndColour> vertices;
    std::vector<int> indices;
    std::vector<float> normals;

    bool viewHasChanged = true;
    int nVertices;
    int nPoints = 0;
    int activePoint = -1;
    int nTriangles;

    float zoom = 5.0f;
    float tilt = 0.0f;
    float tiltBeforeDrag;

    float yaw = 0.0f;
    float yawBeforeDrag;

    juce::OpenGLTexture texture;

    juce::OpenGLContext openGLContext;
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LoudspeakerVisualizer)
};