8 new_background_ =
false;
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
23 memcpy(vertices_, vertices, 16 *
sizeof(
float));
25 static const int triangles[] = {
31 open_gl.
context.extensions.glGenBuffers(1, &vertex_buffer_);
32 open_gl.
context.extensions.glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
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);
38 open_gl.
context.extensions.glGenBuffers(1, &triangle_buffer_);
39 open_gl.
context.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangle_buffer_);
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);
53 if (background_.getWidth())
54 background_.release();
56 image_shader_ =
nullptr;
58 texture_coordinates_ =
nullptr;
59 texture_uniform_ =
nullptr;
61 open_gl.
context.extensions.glDeleteBuffers(1, &vertex_buffer_);
62 open_gl.
context.extensions.glDeleteBuffers(1, &triangle_buffer_);
66 open_gl_context.extensions.glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
67 open_gl_context.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangle_buffer_);
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);
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);
86 if (position_ !=
nullptr)
87 open_gl_context.extensions.glDisableVertexAttribArray(position_->attributeID);
89 if (texture_coordinates_ !=
nullptr)
90 open_gl_context.extensions.glDisableVertexAttribArray(texture_coordinates_->attributeID);
95 if ((new_background_ || background_.getWidth() == 0) && background_image_.getWidth() > 0) {
97 new_background_ =
false;
98 background_.loadImage(background_image_);
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;
106 vertices_[8] = vertices_[12] = width_end;
107 vertices_[5] = vertices_[9] = height_end;
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);
115 glDisable(GL_SCISSOR_TEST);
117 image_shader_->use();
119 open_gl.
context.extensions.glActiveTexture(GL_TEXTURE0);
121 if (texture_uniform_ !=
nullptr && background_.getWidth())
122 texture_uniform_->set(0);
125 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT,
nullptr);
127 background_.unbind();
129 open_gl.
context.extensions.glBindBuffer(GL_ARRAY_BUFFER, 0);
130 open_gl.
context.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
136 background_image_ = background;
137 new_background_ =
true;
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