From f8d127d700aac533ddf508739cdbad1dba8a196a Mon Sep 17 00:00:00 2001 From: lisyarus Date: Wed, 17 Feb 2021 14:55:17 +0300 Subject: [PATCH] Support blending textures with a color in painter --- libs/gfx/include/psemek/gfx/painter.hpp | 2 +- libs/gfx/source/painter.cpp | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/libs/gfx/include/psemek/gfx/painter.hpp b/libs/gfx/include/psemek/gfx/painter.hpp index d143e4a0..de697234 100644 --- a/libs/gfx/include/psemek/gfx/painter.hpp +++ b/libs/gfx/include/psemek/gfx/painter.hpp @@ -61,7 +61,7 @@ namespace psemek::gfx geom::vector text_size(std::string_view str, font f = font::font_9x12, float scale = 1.f); void text(geom::point const & p, std::string_view str, text_options const & opts); - void texture(texture_2d const & texture, geom::box const & box); + void texture(texture_2d const & texture, geom::box const & box, color const & c = {0, 0, 0, 0}); // 3D void axes(geom::point const & p, float length, float width); diff --git a/libs/gfx/source/painter.cpp b/libs/gfx/source/painter.cpp index 16b9b819..43973172 100644 --- a/libs/gfx/source/painter.cpp +++ b/libs/gfx/source/painter.cpp @@ -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 vertices; std::vector indices; texture_2d const * texture; + color_rgba c; }; std::vector textures; @@ -281,10 +285,11 @@ namespace psemek::gfx text3d(geom::point{p[0], p[1], 0.f}, str, opts, geom::matrix::identity()); } - void painter::texture(gfx::texture_2d const & texture, geom::box const & box) + void painter::texture(gfx::texture_2d const & texture, geom::box 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();