gg: draw_arc()

pull/3581/head
Leah Lundqvist 2020-01-27 20:42:32 +01:00 committed by Alexander Medvednikov
parent cce0b2425e
commit 8986633624
1 changed files with 27 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import (
gx gx
os os
glfw glfw
math
) )
pub struct Vec2 { 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) 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) { pub fn (c &GG) draw_gray_line(x, y, x2, y2 f32) {
c.draw_line(x, y, x2, y2, gx.Gray) c.draw_line(x, y, x2, y2, gx.Gray)