From bdc73aad4bfd2f8d971da655d155df8ebbf21577 Mon Sep 17 00:00:00 2001 From: Dendy Date: Fri, 29 Jul 2022 10:31:24 +0200 Subject: [PATCH] Renderer general cleanup --- src/Renderer.zig | 103 +++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 61 deletions(-) diff --git a/src/Renderer.zig b/src/Renderer.zig index 4dd756e..d61551d 100644 --- a/src/Renderer.zig +++ b/src/Renderer.zig @@ -15,7 +15,6 @@ color: [4]f32, pub fn init() !Self { try sdl.init(.{ .video = true, .audio = true, .events = true }); - // Set OpenGL version try sdl.gl.setAttribute(.{ .context_major_version = 3 }); try sdl.gl.setAttribute(.{ .context_minor_version = 3 }); try sdl.gl.setAttribute(.{ .multisamplebuffers = true }); @@ -27,43 +26,52 @@ pub fn init() !Self { .{ .centered = {} }, 1280, 720, - .{ .context = .opengl, .vis = .shown } + .{ .context = .opengl, .vis = .shown }, ); const ctx = try sdl.gl.createContext(window); - sdl.gl.setSwapInterval( .immediate ) catch { + sdl.gl.setSwapInterval(.immediate) catch { std.debug.print("WARNING: Unable to configure the swap interval.\n", .{}); }; + // Shader stuff var mvp_loc: u32 = undefined; var color_loc: u32 = undefined; const program = gl.Program.create(); { const vs = gl.Shader.create(.vertex); defer vs.delete(); - vs.source(1, &.{@embedFile("shaders/vector.vs")}); - vs.compile(); const fs = gl.Shader.create(.fragment); defer fs.delete(); + + vs.source(1, &.{@embedFile("shaders/vector.vs")}); fs.source(1, &.{@embedFile("shaders/fragment.fs")}); + + vs.compile(); fs.compile(); + program.attach(vs); defer program.detach(vs); program.attach(fs); defer program.detach(fs); + program.link(); + mvp_loc = program.uniformLocation("mvp").?; color_loc = program.uniformLocation("color").?; } program.use(); - var wsize = window.getSize(); - var xunit = @intToFloat(f32, wsize.width); - var yunit = @intToFloat(f32, wsize.height); + // Get the matrix that converts coordinates to 1:1 on the screen from the OpenGL default + { + const wsize = window.getSize(); + const xunit = @intToFloat(f32, wsize.width); + const yunit = @intToFloat(f32, wsize.height); - const ortho = m.Mat4.createOrthogonal(0.0, xunit, yunit, 0.0, 0.0, 100.0); + const ortho = m.Mat4.createOrthogonal(0.0, xunit, yunit, 0.0, 0.0, 100.0); - gl.uniformMatrix4fv(mvp_loc, false, &.{ ortho.fields }); + gl.uniformMatrix4fv(mvp_loc, false, &.{ortho.fields}); + } var vertex_array = gl.VertexArray.gen(); vertex_array.bind(); @@ -73,21 +81,15 @@ pub fn init() !Self { gl.clearColor(0.91, 0.97, 1.00, 1.00); + // TODO: See if there's a more optimal solution to this, maybe activating only when necessary gl.enable(.blend); gl.blendFunc(.src_alpha, .one_minus_src_alpha); - return Self{ - .window = window, - .context = ctx, - .color_loc = color_loc, - .buffer = buf, - .color = .{0,0,0,0} - }; + return Self{ .window = window, .context = ctx, .color_loc = color_loc, .buffer = buf, .color = .{ 0, 0, 0, 0 } }; } pub fn render(self: Self) void { sdl.gl.swapWindow(self.window); - gl.clear(.{ .color = true }); } @@ -99,65 +101,44 @@ pub fn deinit(self: Self) void { pub fn setColor(self: *Self, r: u8, g: i32, b: i32, a: i32) void { self.*.color = .{ - @intToFloat(f32, r)/255.0, - @intToFloat(f32, g)/255.0, - @intToFloat(f32, b)/255.0, - @intToFloat(f32, a)/255.0, + @intToFloat(f32, r) / 255.0, + @intToFloat(f32, g) / 255.0, + @intToFloat(f32, b) / 255.0, + @intToFloat(f32, a) / 255.0, }; } -pub fn drawRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { +pub fn drawRectangle(self: Self, x: i32, y: i32, w: i32, h: i32) void { + const indices = [_]u8{ 0, 1, 1, 2, 2, 3, 3, 0 }; + self.renderRectangle(x, y, w, h, .lines, &indices); +} + +pub fn fillRectangle(self: Self, x: i32, y: i32, w: i32, h: i32) void { + const indices = [_]u8{ 1, 3, 0, 1, 3, 2 }; + self.renderRectangle(x, y, w, h, .triangles, &indices); +} + +fn renderRectangle(self: Self, x: i32, y: i32, w: i32, h: i32, primitive: gl.PrimitiveType, indices: []const u8) void { + gl.uniform4fv(self.color_loc, &.{self.color}); + var xf = @intToFloat(f32, x); var yf = @intToFloat(f32, y); var wf = @intToFloat(f32, w); var hf = @intToFloat(f32, h); - gl.enableVertexAttribArray(0); gl.vertexAttribPointer(0, 2, .float, false, 0, 0); const vertex_buffer = [_]f32{ - xf, yf , - xf + wf, yf , - xf + wf, yf + hf, - xf, yf + hf, + xf , yf , // up-left + xf + wf, yf , // up-right + xf + wf, yf + hf, // down-right + xf , yf + hf, // down-left }; - const indices = [_]u8{0, 1, 1, 2, 2, 3, 3, 0}; - self.buffer.data(f32, &vertex_buffer, .static_draw); - gl.uniform4fv(self.color_loc, &.{ self.color }); - _ = indices; - - gl.drawElements(.lines, 8, .u8, @ptrToInt(&indices)); - - gl.disableVertexAttribArray(0); -} - -pub fn fillRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { - var xf = @intToFloat(f32, x); - var yf = @intToFloat(f32, y); - var wf = @intToFloat(f32, w); - var hf = @intToFloat(f32, h); - gl.enableVertexAttribArray(0); - gl.vertexAttribPointer(0, 2, .float, false, 0, 0); - - const vertex_buffer = [_]f32{ - xf + wf, yf , - xf, yf + hf, - xf, yf , - xf + wf, yf + hf, - }; - - const indices = [_]u8{0, 1, 2, 0, 1, 3}; - - self.buffer.data(f32, &vertex_buffer, .static_draw); - - gl.uniform4fv(self.color_loc, &.{ self.color }); - - gl.drawElements(.triangles, 6, .u8, @ptrToInt(&indices)); - + gl.drawElements(primitive, indices.len, .u8, @ptrToInt(indices.ptr)); gl.disableVertexAttribArray(0); }