From 8986633624877fa9a4acc64460d5fba7471c3b7a Mon Sep 17 00:00:00 2001 From: Leah Lundqvist Date: Mon, 27 Jan 2020 20:42:32 +0100 Subject: [PATCH] gg: draw_arc() --- vlib/gg/gg.v | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index a0867f3405..191cf19f9c 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -11,6 +11,7 @@ import ( gx os glfw + math ) pub struct Vec2 { @@ -414,6 +415,32 @@ pub fn (ctx &GG) draw_line(x, y, x2, y2 f32, color gx.Color) { gl.draw_arrays(C.GL_LINES, 0, 2) } +pub fn (ctx &GG) draw_arc(x, y, r, start_angle, end_angle f32, segments int, color gx.Color) { + ctx.shader.set_int('has_texture', 0) + C.glDeleteBuffers(1, &ctx.vao) + C.glDeleteBuffers(1, &ctx.vbo) + ctx.shader.use() + ctx.shader.set_color('color', color) + start_rads := start_angle * 0.0174533 // deg -> rad approx + end_rads := end_angle * 0.0174533 + rad_increment := end_rads / segments + mut vertices := []f32 + mut i := 0 + for i < segments { + theta := f32(i) * rad_increment - start_rads + vertices << [x + f32(math.cos(theta)) * r, y + f32(math.sin(theta)) * r] + i++ + } + // Add the last vertex at the final arc angle. + vertices << [x + f32(math.cos(end_rads)) * r, y + f32(math.sin(end_rads)) * r] + gl.bind_vao(ctx.vao) + gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW) + gl.vertex_attrib_pointer(0, 2, C.GL_FLOAT, false, 2, 0) + gl.enable_vertex_attrib_array(0) + gl.bind_vao(ctx.vao) + gl.draw_arrays(C.GL_LINE_STRIP, 0, segments + 1) +} + /* pub fn (c &GG) draw_gray_line(x, y, x2, y2 f32) { c.draw_line(x, y, x2, y2, gx.Gray)