Vital
Loading...
Searching...
No Matches
overlay.h
Go to the documentation of this file.
1#pragma once
2
3#include "JuceHeader.h"
4#include "synth_section.h"
5
6#include <set>
7
17public:
19 static constexpr int kNumVertices = 4;
21 static constexpr int kNumFloatsPerVertex = 2;
25 static constexpr int kIndices = 6;
26
31 shader_ = nullptr;
32 additive_blending_ = false;
33
34 color_ = Colours::black;
35 // Default coordinates form a full-screen quad
36 data_[0] = -1.0f; data_[1] = -1.0f;
37 data_[2] = -1.0f; data_[3] = 1.0f;
38 data_[4] = 1.0f; data_[5] = -1.0f;
39 data_[6] = 1.0f; data_[7] = 1.0f;
40
41 indices_[0] = 0; indices_[1] = 1; indices_[2] = 2;
42 indices_[3] = 1; indices_[4] = 2; indices_[5] = 3;
43
44 setInterceptsMouseClicks(false, false);
45 }
46
51
56 virtual void init(OpenGlWrapper& open_gl) override {
57 open_gl.context.extensions.glGenBuffers(1, &data_buffer_);
58 open_gl.context.extensions.glBindBuffer(GL_ARRAY_BUFFER, data_buffer_);
59
60 GLsizeiptr vert_size = static_cast<GLsizeiptr>(kTotalFloats * sizeof(float));
61 open_gl.context.extensions.glBufferData(GL_ARRAY_BUFFER, vert_size, data_, GL_STATIC_DRAW);
62
63 open_gl.context.extensions.glGenBuffers(1, &indices_buffer_);
64 open_gl.context.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices_buffer_);
65
66 GLsizeiptr bar_size = static_cast<GLsizeiptr>(kIndices * sizeof(int));
67 open_gl.context.extensions.glBufferData(GL_ELEMENT_ARRAY_BUFFER, bar_size, indices_, GL_STATIC_DRAW);
68
70 shader_->use();
71 color_uniform_ = getUniform(open_gl, *shader_, "color");
72 position_ = getAttribute(open_gl, *shader_, "position");
73 }
74
80 virtual void render(OpenGlWrapper& open_gl, bool animate) override {
81 drawOverlay(open_gl);
82 }
83
88 void paintBackground(Graphics& g) override { }
89
94 virtual void destroy(OpenGlWrapper& open_gl) override {
95 shader_ = nullptr;
96 position_ = nullptr;
97 color_uniform_ = nullptr;
98 open_gl.context.extensions.glDeleteBuffers(1, &data_buffer_);
99 open_gl.context.extensions.glDeleteBuffers(1, &indices_buffer_);
100
101 data_buffer_ = 0;
102 indices_buffer_ = 0;
103 }
104
109 void setColor(const Colour& color) { color_ = color; }
110
115 force_inline void setAdditiveBlending(bool additive_blending) { additive_blending_ = additive_blending; }
116
117protected:
122 void drawOverlay(OpenGlWrapper& open_gl) {
123 if (!setViewPort(open_gl))
124 return;
125
126 if (shader_ == nullptr)
127 init(open_gl);
128
129 glEnable(GL_BLEND);
130 glEnable(GL_SCISSOR_TEST);
132 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
133 else
134 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
135
136 shader_->use();
137 color_uniform_->set(color_.getFloatRed(), color_.getFloatGreen(),
138 color_.getFloatBlue(), color_.getFloatAlpha());
139
140 open_gl.context.extensions.glBindBuffer(GL_ARRAY_BUFFER, data_buffer_);
141 open_gl.context.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices_buffer_);
142
143 open_gl.context.extensions.glVertexAttribPointer(position_->attributeID, kNumFloatsPerVertex, GL_FLOAT,
144 GL_FALSE, kNumFloatsPerVertex * sizeof(float), nullptr);
145 open_gl.context.extensions.glEnableVertexAttribArray(position_->attributeID);
146
147 glDrawElements(GL_TRIANGLES, kIndices, GL_UNSIGNED_INT, nullptr);
148
149 open_gl.context.extensions.glDisableVertexAttribArray(position_->attributeID);
150 open_gl.context.extensions.glBindBuffer(GL_ARRAY_BUFFER, 0);
151 open_gl.context.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
152
153 glDisable(GL_BLEND);
154 glDisable(GL_SCISSOR_TEST);
155 }
156
157 OpenGLShaderProgram* shader_;
158 std::unique_ptr<OpenGLShaderProgram::Uniform> color_uniform_;
159 std::unique_ptr<OpenGLShaderProgram::Attribute> position_;
160
161 Colour color_;
163
168
169 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(OverlayBackgroundRenderer)
170};
171
180class Overlay : public SynthSection {
181public:
186 class Listener {
187 public:
189 virtual ~Listener() { }
190
195 virtual void overlayShown(Overlay* component) = 0;
196
201 virtual void overlayHidden(Overlay* component) = 0;
202
203 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Listener)
204 };
205
214
218 virtual ~Overlay() { }
219
224 void setVisible(bool should_be_visible) override {
225 for (Listener* listener : listeners_) {
226 if (should_be_visible)
227 listener->overlayShown(this);
228 else
229 listener->overlayHidden(this);
230 }
231 Component::setVisible(should_be_visible);
232 }
233
237 virtual void resized() override {
238 background_.setColor(findColour(Skin::kOverlayScreen, true));
239 background_.setBounds(getLocalBounds());
240 }
241
246 virtual void paintBackground(Graphics& g) override { paintOpenGlChildrenBackgrounds(g); }
247
252 void addOverlayListener(Listener* listener) { listeners_.insert(listener); }
253
258 void removeOverlayListener(Listener* listener) { listeners_.erase(listener); }
259
264 void setSizeRatio(float ratio) override { size_ratio_ = ratio; }
265
266protected:
268 std::set<Listener*> listeners_;
270
271 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Overlay)
272};
A base component class that integrates JUCE's Component with OpenGL rendering.
Definition open_gl_component.h:20
static bool setViewPort(Component *component, Rectangle< int > bounds, OpenGlWrapper &open_gl)
Sets the OpenGL viewport to match a specified rectangle within a component.
Definition open_gl_component.cpp:42
static std::unique_ptr< OpenGLShaderProgram::Attribute > getAttribute(const OpenGlWrapper &open_gl, const OpenGLShaderProgram &program, const char *name)
Retrieves an attribute from the shader program if it exists.
Definition open_gl_component.h:79
static std::unique_ptr< OpenGLShaderProgram::Uniform > getUniform(const OpenGlWrapper &open_gl, const OpenGLShaderProgram &program, const char *name)
Retrieves a uniform from the shader program if it exists.
Definition open_gl_component.h:64
Interface for receiving notifications about overlay visibility changes.
Definition overlay.h:186
virtual void overlayHidden(Overlay *component)=0
Called when the overlay is hidden.
virtual ~Listener()
Definition overlay.h:189
Listener()
Definition overlay.h:188
virtual void overlayShown(Overlay *component)=0
Called when the overlay is shown.
A component that renders a full-screen overlay background using OpenGL.
Definition overlay.h:16
OpenGLShaderProgram * shader_
Shader program used for overlay drawing.
Definition overlay.h:157
virtual void init(OpenGlWrapper &open_gl) override
Initializes the OpenGL shader and buffers.
Definition overlay.h:56
std::unique_ptr< OpenGLShaderProgram::Attribute > position_
Attribute for vertex positions.
Definition overlay.h:159
void setColor(const Colour &color)
Sets the overlay color.
Definition overlay.h:109
virtual void destroy(OpenGlWrapper &open_gl) override
Destroys OpenGL resources allocated for this overlay.
Definition overlay.h:94
int indices_[kIndices]
Index data for the overlay quad.
Definition overlay.h:165
OverlayBackgroundRenderer()
Constructs an OverlayBackgroundRenderer with default settings.
Definition overlay.h:30
GLuint indices_buffer_
OpenGL buffer for index data.
Definition overlay.h:167
static constexpr int kNumFloatsPerVertex
Floats per vertex (x and y).
Definition overlay.h:21
virtual void render(OpenGlWrapper &open_gl, bool animate) override
Renders the overlay using OpenGL.
Definition overlay.h:80
virtual ~OverlayBackgroundRenderer()
Destructor.
Definition overlay.h:50
float data_[kTotalFloats]
Vertex data for the overlay quad.
Definition overlay.h:164
std::unique_ptr< OpenGLShaderProgram::Uniform > color_uniform_
Uniform for overlay color.
Definition overlay.h:158
Colour color_
The overlay color.
Definition overlay.h:161
bool additive_blending_
Whether additive blending is enabled.
Definition overlay.h:162
force_inline void setAdditiveBlending(bool additive_blending)
Enables or disables additive blending.
Definition overlay.h:115
static constexpr int kTotalFloats
Total floats for the quad (4 vertices * 2 floats).
Definition overlay.h:23
static constexpr int kIndices
Number of indices (2 triangles * 3 indices).
Definition overlay.h:25
void drawOverlay(OpenGlWrapper &open_gl)
Draws the overlay quad.
Definition overlay.h:122
GLuint data_buffer_
OpenGL buffer for vertex data.
Definition overlay.h:166
static constexpr int kNumVertices
Number of vertices for the overlay quad.
Definition overlay.h:19
void paintBackground(Graphics &g) override
This overlay does not paint a background using JUCE's Graphics.
Definition overlay.h:88
A SynthSection that displays an overlay with a background and optional listeners.
Definition overlay.h:180
Overlay(String name)
Constructs an Overlay with a given name.
Definition overlay.h:210
virtual void paintBackground(Graphics &g) override
Paints the background using OpenGL-rendered children.
Definition overlay.h:246
void addOverlayListener(Listener *listener)
Adds a listener to be notified of overlay visibility changes.
Definition overlay.h:252
void removeOverlayListener(Listener *listener)
Removes a previously added overlay listener.
Definition overlay.h:258
std::set< Listener * > listeners_
Registered overlay listeners.
Definition overlay.h:268
virtual void resized() override
Called when the overlay is resized. Updates background color and size.
Definition overlay.h:237
virtual ~Overlay()
Destructor.
Definition overlay.h:218
void setSizeRatio(float ratio) override
Sets the size ratio for the overlay, used in some layouts.
Definition overlay.h:264
void setVisible(bool should_be_visible) override
Sets the visibility of the overlay and notifies listeners.
Definition overlay.h:224
float size_ratio_
A scaling factor for the overlay size.
Definition overlay.h:267
OverlayBackgroundRenderer background_
Renders the overlay background.
Definition overlay.h:269
@ kPassthroughVertex
Definition shaders.h:29
@ kColorFragment
Definition shaders.h:63
OpenGLShaderProgram * getShaderProgram(VertexShader vertex_shader, FragmentShader fragment_shader, const GLchar **varyings=nullptr)
Retrieves or creates an OpenGLShaderProgram from a given vertex and fragment shader pair.
Definition shaders.cpp:953
@ kOverlayScreen
Definition skin.h:140
@ kOverlay
Definition skin.h:34
Base class for all synthesizer sections, providing UI layout, painting, and interaction logic.
Definition synth_section.h:193
void paintOpenGlChildrenBackgrounds(Graphics &g)
Paints the backgrounds for all OpenGL child components.
Definition synth_section.cpp:267
void addOpenGlComponent(OpenGlComponent *open_gl_component, bool to_beginning=false)
Definition synth_section.cpp:489
void setSkinOverride(Skin::SectionOverride skin_override)
Definition synth_section.h:303
#define force_inline
Definition common.h:23
A helper struct containing references to OpenGL context, shaders, and display scale.
Definition shaders.h:174
OpenGLContext & context
The OpenGLContext for current rendering.
Definition shaders.h:181
Shaders * shaders
Pointer to the Shaders instance providing compiled shaders.
Definition shaders.h:182