|
| 1 | +#pragma once |
| 2 | + |
| 3 | +//============================================================================== |
| 4 | + |
| 5 | +#include "gui/display/AbstractDisplay.h" |
| 6 | +#include "gui/widget/Label.h" |
| 7 | +// #include "utility/Settings.h" |
| 8 | +#include "utility/Fonts.h" |
| 9 | +#include <JuceHeader.h> |
| 10 | + |
| 11 | +//============================================================================== |
| 12 | + |
| 13 | +namespace dmt { |
| 14 | +namespace gui { |
| 15 | +namespace display { |
| 16 | + |
| 17 | +//============================================================================== |
| 18 | + |
| 19 | +/** |
| 20 | + * @class ImpulseResponseDisplay |
| 21 | + * @brief Specialized display component inheriting from |
| 22 | + * AbstractDisplay. |
| 23 | + */ |
| 24 | +class ImpulseResponseDisplay |
| 25 | + : public dmt::gui::display::AbstractDisplay |
| 26 | + , public juce::OpenGLRenderer |
| 27 | +{ |
| 28 | + using Label = dmt::gui::widget::Label; |
| 29 | + using Settings = dmt::Settings; |
| 30 | + using Fonts = dmt::utility::Fonts; |
| 31 | + using Colour = juce::Colour; |
| 32 | + |
| 33 | +public: |
| 34 | + explicit ImpulseResponseDisplay() |
| 35 | + : AbstractDisplay() |
| 36 | + , label("Impulse Response", fonts.bold, rawFontSize, juce::Colours::white) |
| 37 | + { |
| 38 | + // addAndMakeVisible(label); |
| 39 | + openGLContext.setRenderer(this); |
| 40 | + openGLContext.attachTo(*this); |
| 41 | + openGLContext.setContinuousRepainting(true); |
| 42 | + openGLContext.setComponentPaintingEnabled(true); |
| 43 | + } |
| 44 | + |
| 45 | + ~ImpulseResponseDisplay() override { openGLContext.detach(); } |
| 46 | + |
| 47 | + void newOpenGLContextCreated() override |
| 48 | + { |
| 49 | + shaderProgram.reset(new juce::OpenGLShaderProgram(openGLContext)); |
| 50 | + |
| 51 | + if (!shaderProgram->addVertexShader(vertexShaderSource) || |
| 52 | + !shaderProgram->addFragmentShader(fragmentShaderSource) || |
| 53 | + !shaderProgram->link()) { |
| 54 | + jassertfalse; // shader compile/link failed |
| 55 | + } |
| 56 | + |
| 57 | + positionAttributeID = openGLContext.extensions.glGetAttribLocation( |
| 58 | + shaderProgram->getProgramID(), "aPos"); |
| 59 | + jassert(positionAttributeID >= 0); |
| 60 | + createTriangle(); |
| 61 | + } |
| 62 | + |
| 63 | + void renderOpenGL() override |
| 64 | + { |
| 65 | + juce::OpenGLHelpers::clear(juce::Colours::black); |
| 66 | + |
| 67 | + juce::OpenGLContext::getCurrentContext()->extensions.glUseProgram( |
| 68 | + shaderProgram->getProgramID()); |
| 69 | + |
| 70 | + auto& gl = openGLContext.extensions; |
| 71 | + |
| 72 | + gl.glEnableVertexAttribArray(positionAttributeID); |
| 73 | + |
| 74 | + gl.glBindBuffer(juce::gl::GL_ARRAY_BUFFER, vbo); |
| 75 | + |
| 76 | + gl.glVertexAttribPointer(positionAttributeID, |
| 77 | + 2, |
| 78 | + juce::gl::GL_FLOAT, |
| 79 | + juce::gl::GL_FALSE, |
| 80 | + sizeof(float) * 2, |
| 81 | + nullptr); |
| 82 | + |
| 83 | + juce::gl::glDrawArrays(juce::gl::GL_TRIANGLES, 0, 3); |
| 84 | + |
| 85 | + gl.glDisableVertexAttribArray(positionAttributeID); |
| 86 | + } |
| 87 | + |
| 88 | + void resized() noexcept override { label.setBounds(getLocalBounds()); } |
| 89 | + |
| 90 | + void paint(juce::Graphics& _g) noexcept override |
| 91 | + { |
| 92 | + if (Settings::debugBounds) { |
| 93 | + _g.setColour(juce::Colours::cyan); |
| 94 | + _g.drawRect(getLocalBounds(), 1); |
| 95 | + } |
| 96 | + _g.fillAll(juce::Colours::cyan); |
| 97 | + } |
| 98 | + |
| 99 | + void openGLContextClosing() override |
| 100 | + { |
| 101 | + auto& gl = openGLContext.extensions; |
| 102 | + shaderProgram.reset(); |
| 103 | + gl.glDeleteBuffers(1, &vbo); |
| 104 | + } |
| 105 | + |
| 106 | +private: |
| 107 | + juce::OpenGLContext openGLContext; |
| 108 | + std::unique_ptr<juce::OpenGLShaderProgram> shaderProgram; |
| 109 | + int positionAttributeID = -1; |
| 110 | + GLuint vbo = 0; |
| 111 | + void createTriangle() |
| 112 | + { |
| 113 | + float vertices[] = { -0.5f, -0.5f, 0.5f, -0.5f, 0.0f, 0.5f }; |
| 114 | + auto& gl = openGLContext.extensions; |
| 115 | + gl.glGenBuffers(1, &vbo); |
| 116 | + gl.glBindBuffer(juce::gl::GL_ARRAY_BUFFER, vbo); |
| 117 | + gl.glBufferData(juce::gl::GL_ARRAY_BUFFER, |
| 118 | + sizeof(vertices), |
| 119 | + vertices, |
| 120 | + juce::gl::GL_STATIC_DRAW); |
| 121 | + } |
| 122 | + const char* vertexShaderSource = R"( |
| 123 | + #version 330 core |
| 124 | + layout(location = 0) in vec2 aPos; |
| 125 | +
|
| 126 | + void main() |
| 127 | + { |
| 128 | + gl_Position = vec4(aPos, 0.0, 1.0); |
| 129 | + } |
| 130 | + )"; |
| 131 | + |
| 132 | + const char* fragmentShaderSource = R"( |
| 133 | + #version 330 core |
| 134 | + out vec4 FragColor; |
| 135 | +
|
| 136 | + void main() |
| 137 | + { |
| 138 | + FragColor = vec4(1.0, 0.6, 0.2, 1.0); |
| 139 | + } |
| 140 | + )"; |
| 141 | + |
| 142 | + Label label; |
| 143 | + Fonts fonts; |
| 144 | + float rawFontSize = 24.0f; |
| 145 | + |
| 146 | + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImpulseResponseDisplay) |
| 147 | +}; |
| 148 | +} // namespace display |
| 149 | +} // namespace gui |
| 150 | +} // namespace dmt |
0 commit comments