Batch rendering with colors
This commit is contained in:
parent
d2fbc27fe5
commit
735ff622a0
|
@ -8,7 +8,7 @@ const Self = @This();
|
||||||
|
|
||||||
window: sdl.Window,
|
window: sdl.Window,
|
||||||
context: sdl.gl.Context,
|
context: sdl.gl.Context,
|
||||||
color_loc: u32,
|
//color_loc: u32,
|
||||||
buffer: gl.Buffer,
|
buffer: gl.Buffer,
|
||||||
color: [4]f32,
|
color: [4]f32,
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ pub fn init() !Self {
|
||||||
|
|
||||||
// Shader stuff
|
// Shader stuff
|
||||||
var mvp_loc: u32 = undefined;
|
var mvp_loc: u32 = undefined;
|
||||||
var color_loc: u32 = undefined;
|
//var color_loc: u32 = undefined;
|
||||||
const program = gl.Program.create();
|
const program = gl.Program.create();
|
||||||
{
|
{
|
||||||
const vs = gl.Shader.create(.vertex);
|
const vs = gl.Shader.create(.vertex);
|
||||||
|
@ -65,8 +65,8 @@ pub fn init() !Self {
|
||||||
|
|
||||||
program.link();
|
program.link();
|
||||||
|
|
||||||
mvp_loc = try program.uniformLocation("mvp").?;
|
mvp_loc = program.uniformLocation("mvp").?;
|
||||||
color_loc = program.uniformLocation("color").?;
|
//color_loc = program.uniformLocation("color").?;
|
||||||
}
|
}
|
||||||
program.use();
|
program.use();
|
||||||
|
|
||||||
|
@ -81,9 +81,6 @@ pub fn init() !Self {
|
||||||
gl.uniformMatrix4fv(mvp_loc, false, &.{ortho.fields});
|
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();
|
var vertex_array = gl.VertexArray.gen();
|
||||||
vertex_array.bind();
|
vertex_array.bind();
|
||||||
|
|
||||||
|
@ -93,19 +90,16 @@ pub fn init() !Self {
|
||||||
gl.vertexAttribPointer(0, 2, .float, false, @sizeOf(f32) * 6, @sizeOf(f32) * 0);
|
gl.vertexAttribPointer(0, 2, .float, false, @sizeOf(f32) * 6, @sizeOf(f32) * 0);
|
||||||
gl.enableVertexAttribArray(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.enableVertexAttribArray(1);
|
||||||
|
|
||||||
// gl.vertexAttribPointer(1, 2, .float, false, 0, 0);
|
|
||||||
// gl.enableVertexAttribArray(1);
|
|
||||||
|
|
||||||
gl.clearColor(0.91, 0.97, 1.00, 1.00);
|
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
|
// TODO: See if there's a more optimal solution to this, maybe activating only when necessary
|
||||||
gl.enable(.blend);
|
gl.enable(.blend);
|
||||||
gl.blendFunc(.src_alpha, .one_minus_src_alpha);
|
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 {
|
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 i = self.vbo_index;
|
||||||
const vertex_data = [36]f32{
|
const vertex_data = [36]f32{
|
||||||
xf, yf, // up-left
|
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
|
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,
|
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
|
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,
|
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
|
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);
|
std.mem.copy(f32, self.vbo[i .. i + 36], &vertex_data);
|
||||||
self.vbo_index += 36;
|
self.vbo_index += 36;
|
||||||
|
|
|
@ -5,9 +5,9 @@ layout (location = 1) in vec4 v_color;
|
||||||
|
|
||||||
uniform mat4 mvp;
|
uniform mat4 mvp;
|
||||||
|
|
||||||
out color;
|
out vec4 color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
color = v_color; // Taken by fragment.fs
|
color = v_color;
|
||||||
gl_Position = mvp * vec4((pos + vec2(0.5, 0.5)), 0, 1);
|
gl_Position = mvp * vec4((pos + vec2(0.5, 0.5)), 0, 1);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue