diff --git a/Userland/Libraries/LibAccelGfx/Painter.cpp b/Userland/Libraries/LibAccelGfx/Painter.cpp index 7e344c5d35f..1522f04abb9 100644 --- a/Userland/Libraries/LibAccelGfx/Painter.cpp +++ b/Userland/Libraries/LibAccelGfx/Painter.cpp @@ -150,6 +150,42 @@ void Painter::fill_rect(Gfx::FloatRect rect, Gfx::Color color) glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } +void Painter::draw_line(Gfx::IntPoint a, Gfx::IntPoint b, float thickness, Gfx::Color color) +{ + draw_line(a.to_type(), b.to_type(), thickness, color); +} + +void Painter::draw_line(Gfx::FloatPoint a, Gfx::FloatPoint b, float thickness, Color color) +{ + auto midpoint = (a + b) / 2.0f; + auto length = a.distance_from(b); + auto angle = AK::atan2(b.y() - a.y(), b.x() - a.x()); + auto offset = Gfx::FloatPoint { + (length / 2) * AK::cos(angle) - (thickness / 2) * AK::sin(angle), + (length / 2) * AK::sin(angle) + (thickness / 2) * AK::cos(angle), + }; + auto rect = Gfx::FloatRect(midpoint - offset, { length, thickness }); + + auto vertices = rect_to_vertices(to_clip_space(transform().map(rect))); + + auto [red, green, blue, alpha] = gfx_color_to_opengl_color(color); + + m_rectangle_program.use(); + + GLuint position_attribute = m_rectangle_program.get_attribute_location("aVertexPosition"); + GLuint color_uniform = m_rectangle_program.get_uniform_location("uColor"); + + glUniform4f(color_uniform, red, green, blue, alpha); + + glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), vertices.data()); + glEnableVertexAttribArray(position_attribute); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); +} + void Painter::draw_scaled_bitmap(Gfx::IntRect const& dest_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, ScalingMode scaling_mode) { draw_scaled_bitmap(dest_rect.to_type(), bitmap, src_rect.to_type(), scaling_mode); diff --git a/Userland/Libraries/LibAccelGfx/Painter.h b/Userland/Libraries/LibAccelGfx/Painter.h index f1ecdf2f64f..1869daec429 100644 --- a/Userland/Libraries/LibAccelGfx/Painter.h +++ b/Userland/Libraries/LibAccelGfx/Painter.h @@ -46,6 +46,9 @@ public: Bilinear, }; + void draw_line(Gfx::IntPoint a, Gfx::IntPoint b, float thickness, Gfx::Color color); + void draw_line(Gfx::FloatPoint a, Gfx::FloatPoint b, float thickness, Gfx::Color color); + void draw_scaled_bitmap(Gfx::FloatRect const& dst_rect, Gfx::Bitmap const&, Gfx::FloatRect const& src_rect, ScalingMode = ScalingMode::NearestNeighbor); void draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const&, Gfx::IntRect const& src_rect, ScalingMode = ScalingMode::NearestNeighbor); diff --git a/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.cpp b/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.cpp index 528a7c2db03..0be0e0565ad 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.cpp +++ b/Userland/Libraries/LibWeb/Painting/PaintingCommandExecutorGPU.cpp @@ -166,9 +166,10 @@ CommandResult PaintingCommandExecutorGPU::fill_ellipse(Gfx::IntRect const&, Colo return CommandResult::Continue; } -CommandResult PaintingCommandExecutorGPU::draw_line(Color const&, Gfx::IntPoint const&, Gfx::IntPoint const&, int, Gfx::Painter::LineStyle, Color const&) +CommandResult PaintingCommandExecutorGPU::draw_line(Color const& color, Gfx::IntPoint const& a, Gfx::IntPoint const& b, int thickness, Gfx::Painter::LineStyle, Color const&) { - // FIXME + // FIXME: Pass line style and alternate color once AccelGfx::Painter supports it + painter().draw_line(a, b, thickness, color); return CommandResult::Continue; }