gg: filled arcs and circles

pull/3610/head
Leah Lundqvist 2020-01-30 01:26:45 +01:00 committed by GitHub
parent ed55778d2a
commit 6c5879add9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 1 deletions

View File

@ -219,7 +219,15 @@ pub fn (ctx &GG) draw_rect(x, y, w, h f32, c gx.Color) {
ctx.draw_rect2(x, y, w, h, c)
}
pub fn (ctx &GG) draw_circle(x, y, radius int, c gx.Color) {
// Useful for debugging meshes.
pub fn set_mode_wireframe() {
C.glPolygonMode(C.GL_FRONT_AND_BACK, C.GL_LINE)
}
pub fn set_mode_point() {
C.glPolygonMode(C.GL_FRONT_AND_BACK, C.GL_POINT)
}
pub fn set_mode_fill() {
C.glPolygonMode(C.GL_FRONT_AND_BACK, C.GL_FILL)
}
/*
@ -441,6 +449,38 @@ pub fn (ctx &GG) draw_arc(x, y, r, start_angle, end_angle f32, segments int, col
gl.draw_arrays(C.GL_LINE_STRIP, 0, segments + 1)
}
pub fn (ctx &GG) draw_filled_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 := 1
vertices << [x, y]
vertices << [x + f32(math.cos(start_angle)) * r, y + f32(math.sin(start_angle)) * r]
for i < segments {
theta := f32(i) * rad_increment
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_TRIANGLE_FAN, 0, segments + 2)
}
pub fn (ctx &GG) draw_circle(x, y, r f32, color gx.Color) {
ctx.draw_filled_arc(x, y, r, 0, 360, 32 + int(r / 2), color)
}
/*
pub fn (c &GG) draw_gray_line(x, y, x2, y2 f32) {
c.draw_line(x, y, x2, y2, gx.Gray)