Vital
Loading...
Searching...
No Matches
open_gl_image_component.cpp
Go to the documentation of this file.
2
3OpenGlImageComponent::OpenGlImageComponent(String name) : OpenGlComponent(name), component_(nullptr),
4 active_(true), static_image_(false),
5 paint_entire_component_(true) {
6 image_.setTopLeft(-1.0f, 1.0f);
7 image_.setTopRight(1.0f, 1.0f);
8 image_.setBottomLeft(-1.0f, -1.0f);
9 image_.setBottomRight(1.0f, -1.0f);
10 image_.setColor(Colours::white);
11
12 if (name == "")
13 setInterceptsMouseClicks(false, false);
14}
15
17 if (!active_)
18 return;
19
20 Component* component = component_ ? component_ : this;
21
22 int pixel_scale = Desktop::getInstance().getDisplays().findDisplayForPoint(getScreenPosition()).scale;
23 int width = component->getWidth() * pixel_scale;
24 int height = component->getHeight() * pixel_scale;
25 if (width <= 0 || height <= 0)
26 return;
27
28 bool new_image = draw_image_ == nullptr || draw_image_->getWidth() != width || draw_image_->getHeight() != height;
29 if (!new_image && (static_image_ || !force))
30 return;
31
32 image_.lock();
33
34 if (new_image)
35 draw_image_ = std::make_unique<Image>(Image::ARGB, width, height, false);
36
37 draw_image_->clear(Rectangle<int>(0, 0, width, height));
38 Graphics g(*draw_image_);
39 g.addTransform(AffineTransform::scale(pixel_scale));
40 paintToImage(g);
42
43 // Adjust texture coordinates to accommodate power-of-two sizing if needed.
44 float gl_width = vital::utils::nextPowerOfTwo(width);
45 float gl_height = vital::utils::nextPowerOfTwo(height);
46 float width_ratio = gl_width / width;
47 float height_ratio = gl_height / height;
48
49 float right = -1.0f + 2.0f * width_ratio;
50 float bottom = 1.0f - 2.0f * height_ratio;
51 image_.setTopRight(right, 1.0f);
52 image_.setBottomLeft(-1.0f, bottom);
53 image_.setBottomRight(right, bottom);
54 image_.unlock();
55}
56
58 image_.init(open_gl);
59}
60
61void OpenGlImageComponent::render(OpenGlWrapper& open_gl, bool animate) {
62 Component* component = component_ ? component_ : this;
63 if (!active_ || !setViewPort(component, open_gl) || !component->isVisible())
64 return;
65
66 image_.drawImage(open_gl);
67}
68
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
OpenGlImage image_
The OpenGlImage used to upload and draw the cached image.
Definition open_gl_image_component.h:139
bool static_image_
Whether the image is static or updated on events.
Definition open_gl_image_component.h:136
virtual void init(OpenGlWrapper &open_gl) override
Initializes any OpenGL resources for rendering this component.
Definition open_gl_image_component.cpp:57
std::unique_ptr< Image > draw_image_
The cached image that stores the drawn component.
Definition open_gl_image_component.h:138
virtual void paintToImage(Graphics &g)
Renders the associated component (or itself) into the provided Graphics context.
Definition open_gl_image_component.h:46
virtual void destroy(OpenGlWrapper &open_gl) override
Destroys OpenGL-related resources used by this component.
Definition open_gl_image_component.cpp:69
Component * component_
The component being drawn into the image (if any).
Definition open_gl_image_component.h:134
OpenGlImageComponent(String name="")
Constructs an OpenGlImageComponent.
Definition open_gl_image_component.cpp:3
virtual void redrawImage(bool force)
Redraws the image if necessary, creating or updating the internal Image.
Definition open_gl_image_component.cpp:16
virtual void render(OpenGlWrapper &open_gl, bool animate) override
Renders the image using OpenGL.
Definition open_gl_image_component.cpp:61
bool active_
Whether this component is active and should render.
Definition open_gl_image_component.h:135
void unlock()
Unlocks the mutex previously locked with lock().
Definition open_gl_image.h:61
void init(OpenGlWrapper &open_gl)
Initializes the OpenGL buffers and shader attributes needed for rendering the image.
Definition open_gl_image.cpp:33
void setBottomLeft(float x, float y)
Sets the bottom-left corner position of the image quad.
Definition open_gl_image.h:116
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 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
force_inline int nextPowerOfTwo(mono_float value)
Finds the next power of two greater than or equal to a float value.
Definition utils.h:370
A helper struct containing references to OpenGL context, shaders, and display scale.
Definition shaders.h:174