From 38fdb591d3630aa895dbe6cb42b964ef0f2106df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Sun, 22 Sep 2024 15:21:51 +0300 Subject: gl: animated rgb square --- src/main.zig | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index 33e8aba..bcd12d6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -34,7 +34,7 @@ const frag_shader_src: [*c]const u8 = \\ \\void main() \\{ - \\ frag_color = vec4(color); + \\ frag_color = color; \\} ; @@ -152,12 +152,10 @@ pub const Window = struct { width: i32 = 480, /// Height in pixels height: i32 = 360, - /// Offset of our checkerboard animation - offset: f32 = 0.0, /// Last frame's timestamp in ms last_frame: u32 = 0, /// Frames per second statistic - current_fps: f32 = 0.0, + // current_fps: f32 = 0.0, /// Should we keep this window open? open: bool = true, @@ -227,7 +225,7 @@ pub const Window = struct { c.GL_ARRAY_BUFFER, @sizeOf(@TypeOf(win.colors)), &win.colors, - c.GL_STATIC_DRAW, + c.GL_DYNAMIC_DRAW, ); c.glVertexAttribPointer(1, 3, c.GL_FLOAT, c.GL_FALSE, 4 * @sizeOf(f32), null); @@ -278,8 +276,39 @@ pub const Window = struct { c.glClearColor(0, 0.05, 0.15, 1); c.glClear(c.GL_COLOR_BUFFER_BIT); + var ts: f32 = @floatFromInt(win.last_frame); + ts /= 1000; + const light_sine = @sin(ts) / 2.0 + 0.5; + const dark_sine = @sin(ts-std.math.pi/2.0) / 2.0 + 0.5; + // red + win.colors[0] = light_sine; + win.colors[1] = dark_sine; + win.colors[2] = dark_sine; + // green + win.colors[4] = dark_sine; + win.colors[5] = light_sine; + win.colors[6] = dark_sine; + // blue + win.colors[8] = dark_sine; + win.colors[9] = dark_sine; + win.colors[10] = light_sine; + // yellow + win.colors[12] = light_sine; + win.colors[13] = light_sine; + win.colors[14] = dark_sine; + c.glUseProgram(win.shader_prog); + c.glBindVertexArray(win.vao); + + c.glBindBuffer(c.GL_ARRAY_BUFFER, win.cbo); + c.glBufferData( + c.GL_ARRAY_BUFFER, + @sizeOf(@TypeOf(win.colors)), + &win.colors, + c.GL_DYNAMIC_DRAW, + ); + c.glDrawElements(c.GL_TRIANGLES, win.indices.len, c.GL_UNSIGNED_INT, null); try egl_display.swapBuffers(win.egl_surface); @@ -349,11 +378,10 @@ pub const Window = struct { }; new_cb.setListener(*Window, frameListener, win); - if (win.last_frame != 0) { - const elapsed: f32 = @floatFromInt(done.callback_data -% win.last_frame); - win.offset -= elapsed / 1000.0 * 32.0; - win.current_fps = 1000.0 / elapsed; - } + // if (win.last_frame != 0) { + // const elapsed: f32 = @floatFromInt(done.callback_data -% win.last_frame); + // win.current_fps = 1000.0 / elapsed; + // } win.last_frame = done.callback_data; win.drawFrame() catch |err| { -- cgit v1.2.3