aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig92
1 files changed, 70 insertions, 22 deletions
diff --git a/src/main.zig b/src/main.zig
index ce31e75..6d119af 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -4,6 +4,7 @@ const c = @import("c.zig");
const debug_gl = @import("debug_gl.zig");
const cfg = @import("config.zig");
const out = @import("output.zig");
+const gl = @import("gl.zig");
var window: *c.GLFWwindow = undefined;
@@ -22,60 +23,107 @@ pub fn main() !void {
}
defer c.glfwTerminate();
- c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 3);
+ c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 4);
c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MINOR, 2);
c.glfwWindowHint(c.GLFW_OPENGL_FORWARD_COMPAT, c.GL_TRUE);
c.glfwWindowHint(c.GLFW_OPENGL_DEBUG_CONTEXT, debug_gl.is_on);
c.glfwWindowHint(c.GLFW_OPENGL_PROFILE, c.GLFW_OPENGL_CORE_PROFILE);
c.glfwWindowHint(c.GLFW_DEPTH_BITS, 0);
- c.glfwWindowHint(c.GLFW_STENCIL_BITS, 8);
+ c.glfwWindowHint(c.GLFW_STENCIL_BITS, 0);
c.glfwWindowHint(c.GLFW_VISIBLE, c.GLFW_FALSE);
window = c.glfwCreateWindow(config.width, config.height, "glsl-view", null, null) orelse {
panic("unable to create window\n", .{});
};
defer c.glfwDestroyWindow(window);
-
+
c.glfwMakeContextCurrent(window);
c.glfwSwapInterval(1);
- c.glClearColor(0.0, 0.0, 0.0, 1.0);
-
- // c.glEnable(c.GL_BLEND);
- // c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA);
- // c.glPixelStorei(c.GL_UNPACK_ALIGNMENT, 1);
-
- c.glViewport(0, 0, config.width, config.height);
-
- debug_gl.assertNoError();
-
- var outputs = try config.arena.allocator.alloc(*out.Output, config.outputs.len);
+ var constants = try gl.Constants.create(window);
+ defer constants.destroy();
+
+ var outputs = try std.ArrayList(*out.Output).initCapacity(&config.arena.allocator, config.outputs.len);
+ defer outputs.deinit();
for (config.outputs) |output_config, i| {
- outputs[i] = out.Output.create(&config.arena.allocator, output_config, window);
+ try outputs.append(out.Output.create(&config.arena.allocator, output_config, &constants));
}
- defer for (outputs) |output| {
+ defer for (outputs.toSlice()) |output| {
output.destroy(output);
};
+ c.glfwMakeContextCurrent(window);
+ c.glClearColor(0.0, 0.0, 0.0, 1.0);
+
+ debug_gl.assertNoError();
+
const start_time = c.glfwGetTime();
var prev_time = start_time;
+ constants.normalizedQuad.bind(0);
+
+ debug_gl.assertNoError();
+
+ var redShader = try gl.ShaderProgram.create(
+ \\#version 330 core
+ \\
+ \\layout(location = 0) in vec2 position;
+ \\out vec2 uv;
+ \\
+ \\void main() {
+ \\ uv = position / 2.0 + 0.5;
+ \\ gl_Position = vec4(position, 0, 1);
+ \\}
+ ,
+ \\#version 330 core
+ \\
+ \\in vec2 uv;
+ \\out vec4 color;
+ \\
+ \\uniform float offset;
+ \\
+ \\void main() {
+ \\ color = vec4(mod(uv + offset, vec2(1)), 0, 1);
+ \\}
+ ,
+ null
+ );
+ defer redShader.destroy();
+
+ var fbo = try gl.FramebufferObject.create(config.width, config.height);
+ defer fbo.destroy();
+
+ var offset: f32 = 0;
+
while (c.glfwWindowShouldClose(window) == c.GL_FALSE) {
- c.glClear(c.GL_COLOR_BUFFER_BIT | c.GL_DEPTH_BUFFER_BIT | c.GL_STENCIL_BUFFER_BIT);
+ c.glfwMakeContextCurrent(window);
+ c.glClear(c.GL_COLOR_BUFFER_BIT);
const now_time = c.glfwGetTime();
const elapsed = now_time - prev_time;
prev_time = now_time;
+
+ fbo.bind();
+ c.glClear(c.GL_COLOR_BUFFER_BIT);
+
+ redShader.bind();
+ offset += @floatCast(f32, elapsed / 4);
+ redShader.setUniformFloat(redShader.uniformLocation("offset"), offset);
+ constants.normalizedQuad.draw();
+ fbo.unbind();
- for (outputs) |output| {
- const close = output.update(output);
+ for (outputs.toSlice()) |output, i| {
+ const close = output.update(output, fbo.texture_id);
if (close) {
- c.glfwSetWindowShouldClose(window, c.GL_TRUE);
+ const removed = outputs.swapRemove(i);
+ removed.destroy(removed);
+ break;
}
}
- c.glfwSwapBuffers(window);
-
+ if (outputs.len == 0)
+ break;
+
c.glfwPollEvents();
}