Attempt color with batch rendering
This commit is contained in:
parent
5c7ea5ea37
commit
d2fbc27fe5
|
@ -65,7 +65,7 @@ pub fn init() !Self {
|
||||||
|
|
||||||
program.link();
|
program.link();
|
||||||
|
|
||||||
mvp_loc = program.uniformLocation("mvp").?;
|
mvp_loc = try program.uniformLocation("mvp").?;
|
||||||
color_loc = program.uniformLocation("color").?;
|
color_loc = program.uniformLocation("color").?;
|
||||||
}
|
}
|
||||||
program.use();
|
program.use();
|
||||||
|
@ -81,16 +81,24 @@ 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();
|
||||||
|
|
||||||
const buf = gl.Buffer.gen();
|
const buf = gl.Buffer.gen();
|
||||||
buf.bind(.array_buffer);
|
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.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);
|
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
|
||||||
|
@ -101,20 +109,22 @@ pub fn init() !Self {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(self: *Self) void {
|
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.test_counter = 0;
|
||||||
|
|
||||||
self.buffer.data(f32, &self.vbo, .static_draw);
|
self.buffer.data(f32, &self.vbo, .static_draw);
|
||||||
|
|
||||||
{
|
// {
|
||||||
var i: usize = 0;
|
// var i: usize = 0;
|
||||||
while (i < self.vbo_index) {
|
// while (i < self.vbo_index) {
|
||||||
gl.uniform4fv(self.color_loc, &.{self.colors[i]});
|
// gl.uniform4fv(self.color_loc, &.{self.colors[i]});
|
||||||
std.debug.print("Color: {any}\n", .{self.colors[i]});
|
// //std.debug.print("Color: {any}\n", .{self.colors[i]});
|
||||||
gl.drawArrays(.triangles, i, 12);
|
// gl.drawArrays(.triangles, i, 12);
|
||||||
i += 12;
|
// i += 12;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
gl.drawArrays(.triangles, 0, self.vbo_index);
|
||||||
|
|
||||||
self.vbo_index = 0;
|
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);
|
var hf = @intToFloat(f32, h);
|
||||||
|
|
||||||
const i = self.vbo_index;
|
const i = self.vbo_index;
|
||||||
self.colors[i + 0] = self.color;
|
const vertex_data = [36]f32{
|
||||||
self.colors[i + 1] = self.color;
|
xf, yf, // up-left
|
||||||
self.colors[i + 2] = self.color;
|
0.5, 0.5, 0.5, 0.5,
|
||||||
self.colors[i + 3] = self.color;
|
xf + wf, yf, // up-right
|
||||||
self.colors[i + 4] = self.color;
|
0.5, 0.5, 0.5, 0.5,
|
||||||
self.colors[i + 5] = self.color;
|
xf, yf + hf,
|
||||||
self.colors[i + 6] = self.color;
|
0.5, 0.5, 0.5, 0.5,
|
||||||
self.colors[i + 7] = self.color;
|
xf + wf, yf + hf, // down-right
|
||||||
self.colors[i + 8] = self.color;
|
0.5, 0.5, 0.5, 0.5,
|
||||||
self.colors[i + 9] = self.color;
|
xf + wf, yf,
|
||||||
self.colors[i + 10] = self.color;
|
0.5, 0.5, 0.5, 0.5,
|
||||||
self.colors[i + 11] = self.color;
|
xf, yf + hf, // down-left
|
||||||
self.vbo[i + 0] = xf;
|
0.5, 0.5, 0.5, 0.5,
|
||||||
self.vbo[i + 1] = yf; // up-left
|
};
|
||||||
self.vbo[i + 2] = xf + wf;
|
std.mem.copy(f32, self.vbo[i .. i + 36], &vertex_data);
|
||||||
self.vbo[i + 3] = yf; // up-right
|
self.vbo_index += 36;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const OutputSize = struct { width: c_int, height: c_int };
|
pub const OutputSize = struct { width: c_int, height: c_int };
|
||||||
|
|
|
@ -38,8 +38,8 @@ pub fn main() !void {
|
||||||
const delay = SDL.getTicks64() - start;
|
const delay = SDL.getTicks64() - start;
|
||||||
|
|
||||||
std.debug.print("{} ms\n", .{delay});
|
std.debug.print("{} ms\n", .{delay});
|
||||||
if (delay < 16) {
|
if (delay < 17) {
|
||||||
SDL.delay(16 - @intCast(u32, delay));
|
SDL.delay(17 - @intCast(u32, delay));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
uniform vec4 color;
|
in vec4 color;
|
||||||
|
|
||||||
void main(){
|
void main(){
|
||||||
gl_FragColor = color;
|
gl_FragColor = color;
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
|
layout (location = 0) in vec2 pos;
|
||||||
|
layout (location = 1) in vec4 v_color;
|
||||||
|
|
||||||
uniform mat4 mvp;
|
uniform mat4 mvp;
|
||||||
layout(location = 0) in vec2 pos;
|
|
||||||
|
out color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
color = v_color; // Taken by fragment.fs
|
||||||
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