From 735ff622a09a98498fd32ea06ac4c5fd335dfb2a Mon Sep 17 00:00:00 2001 From: Strangedusk Date: Wed, 3 Aug 2022 19:38:44 +0200 Subject: [PATCH] Batch rendering with colors --- src/Renderer.zig | 30 ++++++++++++------------------ src/shaders/vector.vs | 4 ++-- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/Renderer.zig b/src/Renderer.zig index e912ab4..02d3fd0 100644 --- a/src/Renderer.zig +++ b/src/Renderer.zig @@ -8,7 +8,7 @@ const Self = @This(); window: sdl.Window, context: sdl.gl.Context, -color_loc: u32, +//color_loc: u32, buffer: gl.Buffer, color: [4]f32, @@ -44,7 +44,7 @@ pub fn init() !Self { // Shader stuff var mvp_loc: u32 = undefined; - var color_loc: u32 = undefined; + //var color_loc: u32 = undefined; const program = gl.Program.create(); { const vs = gl.Shader.create(.vertex); @@ -65,8 +65,8 @@ pub fn init() !Self { program.link(); - mvp_loc = try program.uniformLocation("mvp").?; - color_loc = program.uniformLocation("color").?; + mvp_loc = program.uniformLocation("mvp").?; + //color_loc = program.uniformLocation("color").?; } program.use(); @@ -81,9 +81,6 @@ pub fn init() !Self { gl.uniformMatrix4fv(mvp_loc, false, &.{ortho.fields}); } - const black = [4]f32{ 0.0, 0.0, 0.0, 1.0 }; - gl.uniform4fv(color_loc, &.{black}); - var vertex_array = gl.VertexArray.gen(); vertex_array.bind(); @@ -93,19 +90,16 @@ pub fn init() !Self { gl.vertexAttribPointer(0, 2, .float, false, @sizeOf(f32) * 6, @sizeOf(f32) * 0); gl.enableVertexAttribArray(0); - gl.vertexAttribPointer(1, 4, .float, false, @sizeOf(f32) * 6, @sizeOf(f32) * 4); + gl.vertexAttribPointer(1, 4, .float, false, @sizeOf(f32) * 6, @sizeOf(f32) * 2); gl.enableVertexAttribArray(1); - // gl.vertexAttribPointer(1, 2, .float, false, 0, 0); - // gl.enableVertexAttribArray(1); - 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, .buffer = buf, .color = .{ 0, 0, 0, 0 } }; } pub fn render(self: *Self) void { @@ -176,17 +170,17 @@ fn renderRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { const i = self.vbo_index; const vertex_data = [36]f32{ xf, yf, // up-left - 0.5, 0.5, 0.5, 0.5, + self.color[0], self.color[1], self.color[2], self.color[3], xf + wf, yf, // up-right - 0.5, 0.5, 0.5, 0.5, + self.color[0], self.color[1], self.color[2], self.color[3], xf, yf + hf, - 0.5, 0.5, 0.5, 0.5, + self.color[0], self.color[1], self.color[2], self.color[3], xf + wf, yf + hf, // down-right - 0.5, 0.5, 0.5, 0.5, + self.color[0], self.color[1], self.color[2], self.color[3], xf + wf, yf, - 0.5, 0.5, 0.5, 0.5, + self.color[0], self.color[1], self.color[2], self.color[3], xf, yf + hf, // down-left - 0.5, 0.5, 0.5, 0.5, + self.color[0], self.color[1], self.color[2], self.color[3], }; std.mem.copy(f32, self.vbo[i .. i + 36], &vertex_data); self.vbo_index += 36; diff --git a/src/shaders/vector.vs b/src/shaders/vector.vs index f996a99..02170fb 100644 --- a/src/shaders/vector.vs +++ b/src/shaders/vector.vs @@ -5,9 +5,9 @@ layout (location = 1) in vec4 v_color; uniform mat4 mvp; -out color; +out vec4 color; void main() { - color = v_color; // Taken by fragment.fs + color = v_color; gl_Position = mvp * vec4((pos + vec2(0.5, 0.5)), 0, 1); }