Fix color blending (component VAO), add config.hpp

This commit is contained in:
Corentin 2021-07-17 01:39:11 +09:00
commit b530f65f38
8 changed files with 65 additions and 17 deletions

View file

@ -15,10 +15,12 @@ int main()
uui::GlApplication app;
// uui::GlApplication app2; // exception : application already created
auto window = app.create_window(800, 600, "OpenGl!");
// auto comp = window->create_component(0, 100, 0.5f, 50);
// comp->set_background_color(0.8f, 0.1f, 0.5f);
auto comp = window->create_component(0, 100, 0.5f, 50);
comp->set_background_color(0.8f, 0.1f, 0.5f);
comp = window->create_component(0.25f, 50, 0.4f, 150);
comp->set_background_color(0.3f, 0.8f, 0.4f, 0.5f);
auto label = window->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f});
label->set_background_color(0.1f, 0.2f, 0.5f);
label->set_background_color(0.1f, 0.2f, 0.5f, 0.8f);
label = window->create_label(0.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f});
label->set_background_color(0.5f, 0.5f, 0.1f);
label = window->create_label(0.75f, 0.75f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.1f});
@ -32,9 +34,12 @@ int main()
// comp->set_background_color(0.8f, 0.1f, 0.5f);
// comp = window_1->create_component(0.75f, 0.75f, 0.25f, 0.25f);
// comp->set_background_color(0.2f, 0.1f, 0.5f);
// auto window_2 = app.create_window(600, 600, "OpenGL 2!");
// comp = window_2->create_component(0, 0.5f, 100, 0.5f);
// comp->set_background_color(0.2f, 0.5f, 0.3f);
// auto label = window_2->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f});
// label->set_background_color(0.1f, 0.2f, 0.5f);
// app.run();
// }
}

View file

@ -40,7 +40,7 @@ uui::GlComponent::~GlComponent()
deinit();
}
void uui::GlComponent::set_background_color(float r, float g, float b, float a)
void uui::GlComponent::set_background_color(float r, float g, float b, float a /*=0.0f*/)
{
background_color = {r, g, b, a};
glBindVertexArray(gl_vao);
@ -95,12 +95,14 @@ void uui::GlComponent::set_vbo_data()
float x_max = x_min + (2.0f * vert_w);
float y_min = (-2.0f * vert_y) + 1.0f;
float y_max = y_min - (2.0f * vert_h);
std::cout << "Sending vbo data with color: " << background_color[0] << ", " << background_color[1] << ", " << background_color[2] << ", "
<< background_color[3] << std::endl;
// clang-format off
float vertices[] = {
x_max, y_min, 0.0f, background_color[0] , background_color[1], background_color[2], background_color[3], // top right
x_max, y_max, 0.0f, background_color[0] , background_color[1], background_color[2], background_color[3], // bottom right
x_min, y_max, 0.0f, background_color[0] , background_color[1], background_color[2], background_color[3], // bottom left
x_min, y_min, 0.0f, background_color[0] , background_color[1], background_color[2], background_color[3] // top left
x_max, y_min, background_color[0] , background_color[1], background_color[2], background_color[3], // top right
x_max, y_max, background_color[0] , background_color[1], background_color[2], background_color[3], // bottom right
x_min, y_max, background_color[0] , background_color[1], background_color[2], background_color[3], // bottom left
x_min, y_min, background_color[0] , background_color[1], background_color[2], background_color[3] // top left
};
// clang-format on
glBindBuffer(GL_ARRAY_BUFFER, gl_vbo);
@ -108,20 +110,20 @@ void uui::GlComponent::set_vbo_data()
glVertexAttribPointer(
0, // index
3, // size
2, // size
GL_FLOAT, // type
GL_FALSE, // enable normalization,
7 * sizeof(float), // stride
6 * sizeof(float), // stride
(void*)0 // offset
);
glEnableVertexAttribArray(0);
glVertexAttribPointer(
1, // index
3, // size
4, // size
GL_FLOAT, // type
GL_FALSE, // enable normalization,
7 * sizeof(float), // stride
(void*)(3 * sizeof(float)) // offset
6 * sizeof(float), // stride
(void*)(2 * sizeof(float)) // offset
);
glEnableVertexAttribArray(1);
}

View file

@ -6,6 +6,7 @@
#include <algorithm>
#include <iostream>
#include "uui/config.hpp"
#include "uui/opengl/application.hpp"
#include "uui/opengl/gl_utils.hpp"
#include "uui/shader.hpp"
@ -166,7 +167,7 @@ const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string&
glLinkProgram(gl_program);
// Create a texture that will be used to hold all ASCII glyphs
glActiveTexture(GL_TEXTURE0);
glActiveTexture(Config::FONT_TEXTURE_UNIT);
glGenTextures(1, &font.atlas_texture);
glBindTexture(GL_TEXTURE_2D, font.atlas_texture);
@ -249,7 +250,7 @@ void uui::GlFontManager::render_text(
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)(&gl_prev_program));
glUseProgram(gl_program);
glActiveTexture(GL_TEXTURE0);
glActiveTexture(Config::FONT_TEXTURE_UNIT);
auto [color_r, color_g, color_b, color_a] = text_color;
glUniform4f(glGetUniformLocation(gl_program, "textColor"), color_r, color_g, color_b, color_a);
glBindVertexArray(gl_vao);

View file

@ -7,4 +7,5 @@ in vec4 backgroundColor;
void main()
{
FragColor = backgroundColor;
// FragColor = vec4(backgroundColor.xyz * backgroundColor.a, backgroundColor.a); // premultiply
}

View file

@ -120,7 +120,8 @@ void uui::GlWindow::init()
// glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_FUNC_ADD);
if constexpr(uui::GlApplication::debug_mode)
{