Vital
Loading...
Searching...
No Matches
open_gl_background.cpp
Go to the documentation of this file.
2
3#include "open_gl_component.h"
4#include "common.h"
5#include "shaders.h"
6
7OpenGlBackground::OpenGlBackground() : image_shader_(nullptr), vertices_() {
8 new_background_ = false;
9 vertex_buffer_ = 0;
10 triangle_buffer_ = 0;
11}
12
14
16 static const float vertices[] = {
17 -1.0f, 1.0f, 0.0f, 1.0f,
18 -1.0f, -1.0f, 0.0f, 0.0f,
19 1.0f, -1.0f, 1.0f, 0.0f,
20 1.0f, 1.0f, 1.0f, 1.0f
21 };
22
23 memcpy(vertices_, vertices, 16 * sizeof(float));
24
25 static const int triangles[] = {
26 0, 1, 2,
27 2, 3, 0
28 };
29
30 // Create and bind vertex buffer
31 open_gl.context.extensions.glGenBuffers(1, &vertex_buffer_);
32 open_gl.context.extensions.glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
33
34 GLsizeiptr vert_size = static_cast<GLsizeiptr>(static_cast<size_t>(sizeof(vertices)));
35 open_gl.context.extensions.glBufferData(GL_ARRAY_BUFFER, vert_size, vertices_, GL_STATIC_DRAW);
36
37 // Create and bind element buffer
38 open_gl.context.extensions.glGenBuffers(1, &triangle_buffer_);
39 open_gl.context.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangle_buffer_);
40
41 GLsizeiptr tri_size = static_cast<GLsizeiptr>(static_cast<size_t>(sizeof(triangles)));
42 open_gl.context.extensions.glBufferData(GL_ELEMENT_ARRAY_BUFFER, tri_size, triangles, GL_STATIC_DRAW);
43
44 // Retrieve shader and uniforms
46 image_shader_->use();
47 position_ = OpenGlComponent::getAttribute(open_gl, *image_shader_, "position");
48 texture_coordinates_ = OpenGlComponent::getAttribute(open_gl, *image_shader_, "tex_coord_in");
49 texture_uniform_ = OpenGlComponent::getUniform(open_gl, *image_shader_, "image");
50}
51
53 if (background_.getWidth())
54 background_.release();
55
56 image_shader_ = nullptr;
57 position_ = nullptr;
58 texture_coordinates_ = nullptr;
59 texture_uniform_ = nullptr;
60
61 open_gl.context.extensions.glDeleteBuffers(1, &vertex_buffer_);
62 open_gl.context.extensions.glDeleteBuffers(1, &triangle_buffer_);
63}
64
65void OpenGlBackground::bind(OpenGLContext& open_gl_context) {
66 open_gl_context.extensions.glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
67 open_gl_context.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangle_buffer_);
68 background_.bind();
69}
70
71void OpenGlBackground::enableAttributes(OpenGLContext& open_gl_context) {
72 if (position_ != nullptr) {
73 open_gl_context.extensions.glVertexAttribPointer(position_->attributeID, 2, GL_FLOAT,
74 GL_FALSE, 4 * sizeof(float), nullptr);
75 open_gl_context.extensions.glEnableVertexAttribArray(position_->attributeID);
76 }
77 if (texture_coordinates_ != nullptr) {
78 open_gl_context.extensions.glVertexAttribPointer(texture_coordinates_->attributeID, 2, GL_FLOAT,
79 GL_FALSE, 4 * sizeof(float),
80 (GLvoid*)(2 * sizeof(float)));
81 open_gl_context.extensions.glEnableVertexAttribArray(texture_coordinates_->attributeID);
82 }
83}
84
85void OpenGlBackground::disableAttributes(OpenGLContext& open_gl_context) {
86 if (position_ != nullptr)
87 open_gl_context.extensions.glDisableVertexAttribArray(position_->attributeID);
88
89 if (texture_coordinates_ != nullptr)
90 open_gl_context.extensions.glDisableVertexAttribArray(texture_coordinates_->attributeID);
91}
92
94 mutex_.lock();
95 if ((new_background_ || background_.getWidth() == 0) && background_image_.getWidth() > 0) {
96 // Load the new background image if needed
97 new_background_ = false;
98 background_.loadImage(background_image_);
99
100 float width_ratio = (1.0f * background_.getWidth()) / background_image_.getWidth();
101 float height_ratio = (1.0f * background_.getHeight()) / background_image_.getHeight();
102 float width_end = 2.0f * width_ratio - 1.0f;
103 float height_end = 1.0f - 2.0f * height_ratio;
104
105 // Update vertex array with the adjusted texture coordinates
106 vertices_[8] = vertices_[12] = width_end;
107 vertices_[5] = vertices_[9] = height_end;
108
109 open_gl.context.extensions.glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
110 GLsizeiptr vert_size = static_cast<GLsizeiptr>(static_cast<size_t>(16 * sizeof(float)));
111 open_gl.context.extensions.glBufferData(GL_ARRAY_BUFFER, vert_size, vertices_, GL_STATIC_DRAW);
112 }
113
114 glDisable(GL_BLEND);
115 glDisable(GL_SCISSOR_TEST);
116
117 image_shader_->use();
118 bind(open_gl.context);
119 open_gl.context.extensions.glActiveTexture(GL_TEXTURE0);
120
121 if (texture_uniform_ != nullptr && background_.getWidth())
122 texture_uniform_->set(0);
123
124 enableAttributes(open_gl.context);
125 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
126 disableAttributes(open_gl.context);
127 background_.unbind();
128
129 open_gl.context.extensions.glBindBuffer(GL_ARRAY_BUFFER, 0);
130 open_gl.context.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
131
132 mutex_.unlock();
133}
134
136 background_image_ = background;
137 new_background_ = true;
138}
void updateBackgroundImage(Image background)
Updates the background image to a new one.
Definition open_gl_background.cpp:135
virtual void destroy(OpenGlWrapper &open_gl)
Cleans up OpenGL resources when the background is no longer needed.
Definition open_gl_background.cpp:52
OpenGlBackground()
Constructs an OpenGlBackground with no initial image.
Definition open_gl_background.cpp:7
void disableAttributes(OpenGLContext &open_gl_context)
Disables vertex attribute arrays for position and texture coordinates.
Definition open_gl_background.cpp:85
virtual ~OpenGlBackground()
Destructor. Frees OpenGL resources if allocated.
Definition open_gl_background.cpp:13
void enableAttributes(OpenGLContext &open_gl_context)
Enables vertex attribute arrays for position and texture coordinates.
Definition open_gl_background.cpp:71
virtual void init(OpenGlWrapper &open_gl)
Initializes OpenGL buffers and shader resources.
Definition open_gl_background.cpp:15
void bind(OpenGLContext &open_gl_context)
Binds the vertex and element arrays, and the background texture.
Definition open_gl_background.cpp:65
virtual void render(OpenGlWrapper &open_gl)
Renders the background image.
Definition open_gl_background.cpp:93
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
@ kImageVertex
Definition shaders.h:28
@ kImageFragment
Definition shaders.h:59
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
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