Vital
Loading...
Searching...
No Matches
open_gl_image.h
Go to the documentation of this file.
1#pragma once
2
3#include "JuceHeader.h"
4#include "open_gl_component.h"
5
6#include <mutex>
7
17public:
22
28 virtual ~OpenGlImage();
29
34 void init(OpenGlWrapper& open_gl);
35
43 void drawImage(OpenGlWrapper& open_gl);
44
49 void destroy(OpenGlWrapper& open_gl);
50
56 void lock() { mutex_.lock(); }
57
61 void unlock() { mutex_.unlock(); }
62
69 void setOwnImage(Image& image) {
70 mutex_.lock();
71 owned_image_ = std::make_unique<Image>(image);
72 setImage(owned_image_.get());
73 mutex_.unlock();
74 }
75
82 void setImage(Image* image) {
83 image_ = image;
84 image_width_ = image->getWidth();
85 image_height_ = image->getHeight();
86 }
87
92 void setColor(Colour color) { color_ = color; }
93
100 inline void setPosition(float x, float y, int index) {
101 position_vertices_[index] = x;
102 position_vertices_[index + 1] = y;
103 dirty_ = true;
104 }
105
111 inline void setTopLeft(float x, float y) { setPosition(x, y, 0); }
112
116 inline void setBottomLeft(float x, float y) { setPosition(x, y, 4); }
117
121 inline void setBottomRight(float x, float y) { setPosition(x, y, 8); }
122
126 inline void setTopRight(float x, float y) { setPosition(x, y, 12); }
127
132 int getImageWidth() { return image_width_; }
133
138 int getImageHeight() { return image_height_; }
139
144 void setAdditive(bool additive) { additive_ = additive; }
145
150 void setUseAlpha(bool use_alpha) { use_alpha_ = use_alpha; }
151
156 void setScissor(bool scissor) { scissor_ = scissor; }
157
158private:
159 std::mutex mutex_;
160 bool dirty_;
161
162 Image* image_;
163 int image_width_;
164 int image_height_;
165 std::unique_ptr<Image> owned_image_;
166 Colour color_;
167
168 OpenGLTexture texture_;
169 bool additive_;
170 bool use_alpha_;
171 bool scissor_;
172
173 OpenGLShaderProgram* image_shader_;
174 std::unique_ptr<OpenGLShaderProgram::Uniform> image_color_;
175 std::unique_ptr<OpenGLShaderProgram::Attribute> image_position_;
176 std::unique_ptr<OpenGLShaderProgram::Attribute> texture_coordinates_;
177
178 std::unique_ptr<float[]> position_vertices_;
179 std::unique_ptr<int[]> position_triangles_;
180 GLuint vertex_buffer_;
181 GLuint triangle_buffer_;
182
183 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(OpenGlImage)
184};
A utility class for rendering a single image using OpenGL.
Definition open_gl_image.h:16
virtual ~OpenGlImage()
Destructor.
Definition open_gl_image.cpp:31
void setPosition(float x, float y, int index)
Sets a specific vertex position by index.
Definition open_gl_image.h:100
void unlock()
Unlocks the mutex previously locked with lock().
Definition open_gl_image.h:61
void setOwnImage(Image &image)
Sets the image from an owned copy.
Definition open_gl_image.h:69
void setAdditive(bool additive)
Enables or disables additive blending mode.
Definition open_gl_image.h:144
void init(OpenGlWrapper &open_gl)
Initializes the OpenGL buffers and shader attributes needed for rendering the image.
Definition open_gl_image.cpp:33
OpenGlImage()
Constructs an OpenGlImage with default settings.
Definition open_gl_image.cpp:12
void setBottomLeft(float x, float y)
Sets the bottom-left corner position of the image quad.
Definition open_gl_image.h:116
int getImageWidth()
Gets the width of the currently set image.
Definition open_gl_image.h:132
void drawImage(OpenGlWrapper &open_gl)
Draws the image to the current OpenGL context.
Definition open_gl_image.cpp:59
void setImage(Image *image)
Sets the image to render without taking ownership.
Definition open_gl_image.h:82
void setScissor(bool scissor)
Enables or disables scissor test when drawing the image.
Definition open_gl_image.h:156
int getImageHeight()
Gets the height of the currently set image.
Definition open_gl_image.h:138
void setUseAlpha(bool use_alpha)
Enables or disables alpha blending.
Definition open_gl_image.h:150
void setBottomRight(float x, float y)
Sets the bottom-right corner position of the image quad.
Definition open_gl_image.h:121
void setTopLeft(float x, float y)
Sets the top-left corner position of the image quad.
Definition open_gl_image.h:111
void destroy(OpenGlWrapper &open_gl)
Releases any OpenGL resources allocated by this object.
Definition open_gl_image.cpp:128
void setTopRight(float x, float y)
Sets the top-right corner position of the image quad.
Definition open_gl_image.h:126
void setColor(Colour color)
Sets the color tint applied to the image.
Definition open_gl_image.h:92
void lock()
Locks the internal mutex for thread-safe operations.
Definition open_gl_image.h:56
A helper struct containing references to OpenGL context, shaders, and display scale.
Definition shaders.h:174