Support blending textures with a color in painter

This commit is contained in:
Nikita Lisitsa 2021-02-17 14:55:17 +03:00
parent 74ba55257f
commit f8d127d700
2 changed files with 9 additions and 3 deletions

View file

@ -61,7 +61,7 @@ namespace psemek::gfx
geom::vector<float, 2> text_size(std::string_view str, font f = font::font_9x12, float scale = 1.f);
void text(geom::point<float, 2> const & p, std::string_view str, text_options const & opts);
void texture(texture_2d const & texture, geom::box<float, 2> const & box);
void texture(texture_2d const & texture, geom::box<float, 2> const & box, color const & c = {0, 0, 0, 0});
// 3D
void axes(geom::point<float, 3> const & p, float length, float width);

View file

@ -95,6 +95,7 @@ static const char texture_fragment_source[] =
R"(#version 330
uniform sampler2D u_texture;
uniform vec4 u_color;
in vec2 texcoord;
@ -102,7 +103,9 @@ out vec4 out_color;
void main()
{
out_color = texture(u_texture, texcoord);
vec4 color = texture(u_texture, texcoord);
color.rgb = mix(color.rgb, u_color.rgb, u_color.a);
out_color = color;
}
)";
@ -148,6 +151,7 @@ namespace psemek::gfx
std::vector<texture_vertex> vertices;
std::vector<std::uint32_t> indices;
texture_2d const * texture;
color_rgba c;
};
std::vector<texture_render_data> textures;
@ -281,10 +285,11 @@ namespace psemek::gfx
text3d(geom::point{p[0], p[1], 0.f}, str, opts, geom::matrix<float, 3, 3>::identity());
}
void painter::texture(gfx::texture_2d const & texture, geom::box<float, 2> const & box)
void painter::texture(gfx::texture_2d const & texture, geom::box<float, 2> const & box, color const & c)
{
impl::texture_render_data data;
data.texture = &texture;
data.c = c;
data.indices = {0, 1, 2, 2, 1, 3};
@ -563,6 +568,7 @@ namespace psemek::gfx
impl().texture_program["u_texture"] = 0;
for (auto const & data : impl().textures)
{
impl().texture_program["u_color"] = to_colorf(data.c);
impl().texture_mesh.load(data.vertices, data.indices, gl::TRIANGLES, gl::STREAM_DRAW);
data.texture->bind();
impl().texture_mesh.draw();