Vital
Loading...
Searching...
No Matches
open_gl_multi_image.h
Go to the documentation of this file.
1#pragma once
2
3#include "JuceHeader.h"
4#include "open_gl_component.h"
5#include <mutex>
6
17public:
19 static constexpr int kNumVertices = 4;
21 static constexpr int kNumFloatsPerVertex = 4;
25 static constexpr int kNumIndicesPerQuad = 6;
26
31 OpenGlMultiImage(int max_images);
32
36 virtual ~OpenGlMultiImage();
37
42 virtual void init(OpenGlWrapper& open_gl) override;
43
49 virtual void render(OpenGlWrapper& open_gl, bool animate) override;
50
55 virtual void destroy(OpenGlWrapper& open_gl) override;
56
61 void paintBackground(Graphics& g) override { }
62
66 void resized() override {
68 dirty_ = true;
69 }
70
74 void lock() { mutex_.lock(); }
75
79 void unlock() { mutex_.unlock(); }
80
85 void setOwnImage(Image& image) {
86 mutex_.lock();
87 owned_image_ = std::make_unique<Image>(image);
88 setImage(owned_image_.get());
89 mutex_.unlock();
90 }
91
96 void setImage(Image* image) {
97 image_ = image;
98 image_width_ = image->getWidth();
99 image_height_ = image->getHeight();
100 }
101
106 void setNumQuads(int num_quads) { num_quads_ = num_quads; }
107
112 void setColor(Colour color) { color_ = color; }
113
122 inline void setQuad(int i, float x, float y, float w, float h) {
123 int index = kNumFloatsPerQuad * i;
124 data_[index] = x;
125 data_[index + 1] = y;
126 data_[index + 4] = x;
127 data_[index + 5] = y + h;
128 data_[index + 8] = x + w;
129 data_[index + 9] = y + h;
130 data_[index + 12] = x + w;
131 data_[index + 13] = y;
132 dirty_ = true;
133 }
134
139 int getImageWidth() { return image_width_; }
140
145 int getImageHeight() { return image_height_; }
146
151 void setAdditive(bool additive) { additive_blending_ = additive; }
152
153private:
154 std::mutex mutex_;
155 Image* image_;
156 int image_width_;
157 int image_height_;
158 std::unique_ptr<Image> owned_image_;
159 Colour color_;
160 OpenGLTexture texture_;
161
162 int max_quads_;
163 int num_quads_;
164
165 bool dirty_;
166 bool additive_blending_;
167
168 std::unique_ptr<float[]> data_;
169 std::unique_ptr<int[]> indices_;
170
171 OpenGLShaderProgram* image_shader_;
172 std::unique_ptr<OpenGLShaderProgram::Uniform> color_uniform_;
173 std::unique_ptr<OpenGLShaderProgram::Attribute> position_;
174 std::unique_ptr<OpenGLShaderProgram::Attribute> texture_coordinates_;
175
176 GLuint vertex_buffer_;
177 GLuint indices_buffer_;
178
179 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(OpenGlMultiImage)
180};
A base component class that integrates JUCE's Component with OpenGL rendering.
Definition open_gl_component.h:20
virtual void resized() override
Called when the component is resized.
Definition open_gl_component.cpp:121
A component for rendering multiple image quads using OpenGL.
Definition open_gl_multi_image.h:16
void setOwnImage(Image &image)
Sets an owned image by making a copy and uses it for rendering.
Definition open_gl_multi_image.h:85
int getImageWidth()
Gets the width of the current image.
Definition open_gl_multi_image.h:139
virtual void render(OpenGlWrapper &open_gl, bool animate) override
Renders the set of image quads using OpenGL.
Definition open_gl_multi_image.cpp:79
static constexpr int kNumFloatsPerQuad
Number of floats per quad (4 vertices * 4 floats each).
Definition open_gl_multi_image.h:23
static constexpr int kNumIndicesPerQuad
Number of indices per quad (2 triangles * 3 indices each).
Definition open_gl_multi_image.h:25
void setNumQuads(int num_quads)
Sets the number of quads currently drawn.
Definition open_gl_multi_image.h:106
void setQuad(int i, float x, float y, float w, float h)
Sets the position and size of a quad.
Definition open_gl_multi_image.h:122
virtual void init(OpenGlWrapper &open_gl) override
Initializes OpenGL buffers and shader attributes for rendering.
Definition open_gl_multi_image.cpp:44
virtual ~OpenGlMultiImage()
Destructor. Frees any allocated OpenGL resources.
Definition open_gl_multi_image.cpp:42
void setColor(Colour color)
Sets the color tint applied to all image quads.
Definition open_gl_multi_image.h:112
void setAdditive(bool additive)
Enables or disables additive blending for the image quads.
Definition open_gl_multi_image.h:151
void lock()
Locks the internal mutex for thread-safe image updates.
Definition open_gl_multi_image.h:74
virtual void destroy(OpenGlWrapper &open_gl) override
Destroys the OpenGL resources used by this class.
Definition open_gl_multi_image.cpp:64
void unlock()
Unlocks the mutex locked by lock().
Definition open_gl_multi_image.h:79
void resized() override
Called when the component is resized, marks vertex data as dirty to recalculate.
Definition open_gl_multi_image.h:66
static constexpr int kNumVertices
Number of vertices per quad (2 triangles forming a rectangle).
Definition open_gl_multi_image.h:19
OpenGlMultiImage(int max_images)
Constructs an OpenGlMultiImage with a given maximum number of quads.
Definition open_gl_multi_image.cpp:6
static constexpr int kNumFloatsPerVertex
Number of floats per vertex (x, y, u, v).
Definition open_gl_multi_image.h:21
void paintBackground(Graphics &g) override
Override to suppress default background painting.
Definition open_gl_multi_image.h:61
int getImageHeight()
Gets the height of the current image.
Definition open_gl_multi_image.h:145
void setImage(Image *image)
Sets the image to render without ownership.
Definition open_gl_multi_image.h:96
A helper struct containing references to OpenGL context, shaders, and display scale.
Definition shaders.h:174