aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYaroslav de la Peña Smirnov <yps@yaroslavps.com>2024-09-22 15:21:51 +0300
committerYaroslav de la Peña Smirnov <yps@yaroslavps.com>2024-09-22 15:21:51 +0300
commit38fdb591d3630aa895dbe6cb42b964ef0f2106df (patch)
tree8101521fa42979aedc81e09752970967ac1aadb9
parent7f2bee0d11ce5f98a450b80d33ef28c6e19b94e9 (diff)
downloadzigway-gl/rgb-square-anim.tar.gz
zigway-gl/rgb-square-anim.zip
gl: animated rgb squaregl/rgb-square-anim
-rw-r--r--src/main.zig48
1 files changed, 38 insertions, 10 deletions
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| {