Batch rendering with colors

This commit is contained in:
Dusk 2022-08-03 19:38:44 +02:00
parent d2fbc27fe5
commit 735ff622a0
2 changed files with 14 additions and 20 deletions

View File

@ -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;

View File

@ -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);
}