From 64c94fd1b8897ae266b5c5142729f6d25f805daf Mon Sep 17 00:00:00 2001 From: nothke Date: Wed, 12 Jun 2024 01:18:22 +0200 Subject: [PATCH] Circle! (black and 8 sided) --- src/main.zig | 57 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/src/main.zig b/src/main.zig index 79f6e74..20e9807 100644 --- a/src/main.zig +++ b/src/main.zig @@ -24,25 +24,60 @@ export fn init() void { std.log.info("Vertex buffers len: {}", .{state.bind.vertex_buffers.len}); - var triangleVerts = [_]f32{ - // positions colors - 0.0, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, - 0.5, -0.5, 0.5, 0.0, 1.0, 0.0, 1.0, - -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, - 0.7, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, - }; + const vertexCount = 8; + const radius: f32 = 0.4; + + const cX: f32 = 0; + const cY: f32 = 0; + + const attributesCount = 7; + + var triangleVerts = std.mem.zeroes([vertexCount * attributesCount]f32); + + const angleDiff: f32 = 1 / @as(f32, @floatFromInt(vertexCount)); + + for (0..vertexCount) |i| { + const pi: f32 = @floatCast(std.math.pi); + const angle: f32 = @as(f32, @floatFromInt(i)) * angleDiff * pi * 2; + + const x = radius * @cos(angle) + cX; + const y = radius * @sin(angle) + cY; + + const si = i * attributesCount; + triangleVerts[si + 0] = x; + triangleVerts[si + 1] = y; + triangleVerts[si + 2] = 0.5; + } + + // var triangleVerts = [_]f32{ + // // positions colors + // 0.0, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, + // 0.5, -0.5, 0.5, 0.0, 1.0, 0.0, 1.0, + // -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, + // 0.7, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, + // }; state.bind.vertex_buffers[0] = sg.makeBuffer(.{ .data = sg.asRange(&triangleVerts), }); - const indexBuffer = [_]u16{ - 0, 1, 2, - 0, 3, 1, - }; + const trisCount = vertexCount - 2; + var indexBuffer: [trisCount * 3]u16 = undefined; state.indexCount = indexBuffer.len; + // const indexBuffer = [_]u16{ + // 0, 1, 2, + // 0, 2, 3, + // }; + + for (0..trisCount) |tri| { + const triu: u16 = @intCast(tri); + indexBuffer[tri * 3 + 0] = 0; + indexBuffer[tri * 3 + 1] = triu + 1; + indexBuffer[tri * 3 + 2] = triu + 2; + } + state.bind.index_buffer = sg.makeBuffer(.{ .type = .INDEXBUFFER, .data = sg.asRange(&indexBuffer),