Attempt color with batch rendering

This commit is contained in:
Dusk 2022-08-03 14:19:46 +02:00
parent 5c7ea5ea37
commit d2fbc27fe5
4 changed files with 48 additions and 42 deletions

View File

@ -65,7 +65,7 @@ pub fn init() !Self {
program.link();
mvp_loc = program.uniformLocation("mvp").?;
mvp_loc = try program.uniformLocation("mvp").?;
color_loc = program.uniformLocation("color").?;
}
program.use();
@ -81,16 +81,24 @@ 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();
const buf = gl.Buffer.gen();
buf.bind(.array_buffer);
gl.vertexAttribPointer(0, 2, .float, false, 0, 0);
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.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
@ -101,20 +109,22 @@ pub fn init() !Self {
}
pub fn render(self: *Self) void {
std.debug.print("Numer of rendered elements: {}\n", .{self.test_counter});
std.debug.print("Number of rendered elements: {}\n", .{self.test_counter});
self.test_counter = 0;
self.buffer.data(f32, &self.vbo, .static_draw);
{
var i: usize = 0;
while (i < self.vbo_index) {
gl.uniform4fv(self.color_loc, &.{self.colors[i]});
std.debug.print("Color: {any}\n", .{self.colors[i]});
gl.drawArrays(.triangles, i, 12);
i += 12;
}
}
// {
// var i: usize = 0;
// while (i < self.vbo_index) {
// gl.uniform4fv(self.color_loc, &.{self.colors[i]});
// //std.debug.print("Color: {any}\n", .{self.colors[i]});
// gl.drawArrays(.triangles, i, 12);
// i += 12;
// }
// }
gl.drawArrays(.triangles, 0, self.vbo_index);
self.vbo_index = 0;
@ -164,31 +174,22 @@ fn renderRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void {
var hf = @intToFloat(f32, h);
const i = self.vbo_index;
self.colors[i + 0] = self.color;
self.colors[i + 1] = self.color;
self.colors[i + 2] = self.color;
self.colors[i + 3] = self.color;
self.colors[i + 4] = self.color;
self.colors[i + 5] = self.color;
self.colors[i + 6] = self.color;
self.colors[i + 7] = self.color;
self.colors[i + 8] = self.color;
self.colors[i + 9] = self.color;
self.colors[i + 10] = self.color;
self.colors[i + 11] = self.color;
self.vbo[i + 0] = xf;
self.vbo[i + 1] = yf; // up-left
self.vbo[i + 2] = xf + wf;
self.vbo[i + 3] = yf; // up-right
self.vbo[i + 4] = xf;
self.vbo[i + 5] = yf + hf; // down-left
self.vbo[i + 6] = xf + wf;
self.vbo[i + 7] = yf + hf; // down-right
self.vbo[i + 8] = xf + wf;
self.vbo[i + 9] = yf; // up-right
self.vbo[i + 10] = xf;
self.vbo[i + 11] = yf + hf; // down-left
self.vbo_index += 12;
const vertex_data = [36]f32{
xf, yf, // up-left
0.5, 0.5, 0.5, 0.5,
xf + wf, yf, // up-right
0.5, 0.5, 0.5, 0.5,
xf, yf + hf,
0.5, 0.5, 0.5, 0.5,
xf + wf, yf + hf, // down-right
0.5, 0.5, 0.5, 0.5,
xf + wf, yf,
0.5, 0.5, 0.5, 0.5,
xf, yf + hf, // down-left
0.5, 0.5, 0.5, 0.5,
};
std.mem.copy(f32, self.vbo[i .. i + 36], &vertex_data);
self.vbo_index += 36;
}
pub const OutputSize = struct { width: c_int, height: c_int };

View File

@ -38,8 +38,8 @@ pub fn main() !void {
const delay = SDL.getTicks64() - start;
std.debug.print("{} ms\n", .{delay});
if (delay < 16) {
SDL.delay(16 - @intCast(u32, delay));
if (delay < 17) {
SDL.delay(17 - @intCast(u32, delay));
}
}
}

View File

@ -1,6 +1,6 @@
#version 330 core
uniform vec4 color;
in vec4 color;
void main(){
gl_FragColor = color;

View File

@ -1,8 +1,13 @@
#version 330 core
layout (location = 0) in vec2 pos;
layout (location = 1) in vec4 v_color;
uniform mat4 mvp;
layout(location = 0) in vec2 pos;
out color;
void main() {
color = v_color; // Taken by fragment.fs
gl_Position = mvp * vec4((pos + vec2(0.5, 0.5)), 0, 1);
}