diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index c0514ef30..4a71c7b41 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -1410,6 +1410,9 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void { // Draw our terminal cells try self.drawCellProgram(&gl_state); + // Draw our custom shaders + try self.drawCustomPrograms(&gl_state); + // Swap our window buffers switch (apprt.runtime) { apprt.glfw => surface.window.swapBuffers(), @@ -1418,6 +1421,20 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void { } } +fn drawCustomPrograms( + self: *OpenGL, + gl_state: *const GLState, +) !void { + _ = self; + + for (gl_state.custom_programs) |program| { + // Bind our cell program state, buffers + const bind = try program.bind(); + defer bind.unbind(); + try gl.drawElementsInstanced(gl.c.GL_TRIANGLES, 6, gl.c.GL_UNSIGNED_BYTE, 1); + } +} + /// Runs the cell program (shaders) to draw the terminal grid. fn drawCellProgram( self: *OpenGL, diff --git a/src/renderer/opengl/CustomProgram.zig b/src/renderer/opengl/CustomProgram.zig index 72e1f006b..e783463bf 100644 --- a/src/renderer/opengl/CustomProgram.zig +++ b/src/renderer/opengl/CustomProgram.zig @@ -84,3 +84,32 @@ pub fn deinit(self: CustomProgram) void { self.vao.destroy(); self.program.destroy(); } + +pub fn bind(self: CustomProgram) !Binding { + const program = try self.program.use(); + errdefer program.unbind(); + + const vao = try self.vao.bind(); + errdefer vao.unbind(); + + const ebo = try self.ebo.bind(.element_array); + errdefer ebo.unbind(); + + return .{ + .program = program, + .vao = vao, + .ebo = ebo, + }; +} + +pub const Binding = struct { + program: gl.Program.Binding, + vao: gl.VertexArray.Binding, + ebo: gl.Buffer.Binding, + + pub fn unbind(self: Binding) void { + self.ebo.unbind(); + self.vao.unbind(); + self.program.unbind(); + } +}; diff --git a/src/renderer/shaders/custom.v.glsl b/src/renderer/shaders/custom.v.glsl index fc6366845..653e1800e 100644 --- a/src/renderer/shaders/custom.v.glsl +++ b/src/renderer/shaders/custom.v.glsl @@ -2,7 +2,7 @@ void main(){ vec2 position; - position.x = (gl_VertexID == 0 || gl_VertexID == 1) ? 1. : 0.; - position.y = (gl_VertexID == 0 || gl_VertexID == 3) ? 0. : 1.; + position.x = (gl_VertexID == 0 || gl_VertexID == 1) ? -1. : 1.; + position.y = (gl_VertexID == 0 || gl_VertexID == 3) ? 1. : -1.; gl_Position = vec4(position.xy, 0.0f, 1.0f); } diff --git a/src/renderer/shaders/temp.f.glsl b/src/renderer/shaders/temp.f.glsl index cbc9de339..632a2f73b 100644 --- a/src/renderer/shaders/temp.f.glsl +++ b/src/renderer/shaders/temp.f.glsl @@ -3,6 +3,10 @@ layout(location = 0) out vec4 out_FragColor; void main() { - // read - out_FragColor = vec4(1.0, 0.0, 0.0, 1.0); + // red + //out_FragColor = vec4(1.0, 0.0, 0.0, 1.0); + + // maze + vec4 I = gl_FragCoord; + out_FragColor = vec4(3)*modf(I*.1,I)[int(length(I)*1e4)&1]; }