grid keeps track of cells

This commit is contained in:
Mitchell Hashimoto
2022-04-15 09:18:57 -07:00
parent 4d9b67fa65
commit 8ea0299375
2 changed files with 36 additions and 23 deletions

View File

@ -20,6 +20,9 @@ size: GridSize,
/// Current cell dimensions for this grid. /// Current cell dimensions for this grid.
cell_size: CellSize, cell_size: CellSize,
/// The current set of cells to render.
cells: std.ArrayListUnmanaged(GPUCell),
/// Shader program for cell rendering. /// Shader program for cell rendering.
program: gl.Program, program: gl.Program,
vao: gl.VertexArray, vao: gl.VertexArray,
@ -100,7 +103,7 @@ pub fn init(alloc: Allocator) !Grid {
errdefer ebo.destroy(); errdefer ebo.destroy();
var ebobind = try ebo.bind(.ElementArrayBuffer); var ebobind = try ebo.bind(.ElementArrayBuffer);
defer ebobind.unbind(); defer ebobind.unbind();
try ebobind.setData([6]u32{ try ebobind.setData([6]u8{
0, 1, 3, // Top-left triangle 0, 1, 3, // Top-left triangle
1, 2, 3, // Bottom-right triangle 1, 2, 3, // Bottom-right triangle
}, .StaticDraw); }, .StaticDraw);
@ -123,6 +126,7 @@ pub fn init(alloc: Allocator) !Grid {
return Grid{ return Grid{
.alloc = alloc, .alloc = alloc,
.cells = .{},
.cell_size = .{ .width = cell_width, .height = cell_height }, .cell_size = .{ .width = cell_width, .height = cell_height },
.size = .{ .rows = 0, .columns = 0 }, .size = .{ .rows = 0, .columns = 0 },
.program = program, .program = program,
@ -137,9 +141,31 @@ pub fn deinit(self: *Grid) void {
self.ebo.destroy(); self.ebo.destroy();
self.vao.destroy(); self.vao.destroy();
self.program.destroy(); self.program.destroy();
self.cells.deinit(self.alloc);
self.* = undefined; self.* = undefined;
} }
/// TODO: remove, this is for testing
pub fn demoCells(self: *Grid) !void {
self.cells.clearRetainingCapacity();
try self.cells.ensureUnusedCapacity(self.alloc, self.size.columns * self.size.rows);
var row: u32 = 0;
while (row < self.size.rows) : (row += 1) {
var col: u32 = 0;
while (col < self.size.columns) : (col += 1) {
self.cells.appendAssumeCapacity(.{
.grid_col = @intCast(u16, col),
.grid_row = @intCast(u16, row),
.bg_r = 200,
.bg_g = 100,
.bg_b = 150,
.bg_a = 255,
});
}
}
}
/// Set the screen size for rendering. This will update the projection /// Set the screen size for rendering. This will update the projection
/// used for the shader so that the scaling of the grid is correct. /// used for the shader so that the scaling of the grid is correct.
pub fn setScreenSize(self: *Grid, dim: ScreenSize) !void { pub fn setScreenSize(self: *Grid, dim: ScreenSize) !void {
@ -165,6 +191,9 @@ pub fn setScreenSize(self: *Grid, dim: ScreenSize) !void {
} }
pub fn render(self: Grid) !void { pub fn render(self: Grid) !void {
// If we have no cells to render, then we render nothing.
if (self.cells.items.len == 0) return;
const pbind = try self.program.use(); const pbind = try self.program.use();
defer pbind.unbind(); defer pbind.unbind();
@ -172,25 +201,6 @@ pub fn render(self: Grid) !void {
try self.vao.bind(); try self.vao.bind();
defer gl.VertexArray.unbind() catch null; defer gl.VertexArray.unbind() catch null;
// Build our data
var vertices: std.ArrayListUnmanaged(GPUCell) = .{};
try vertices.ensureUnusedCapacity(self.alloc, self.size.columns * self.size.rows);
defer vertices.deinit(self.alloc);
var row: u32 = 0;
while (row < self.size.rows) : (row += 1) {
var col: u32 = 0;
while (col < self.size.columns) : (col += 1) {
vertices.appendAssumeCapacity(.{
.grid_col = @intCast(u16, col),
.grid_row = @intCast(u16, row),
.bg_r = 200,
.bg_g = 100,
.bg_b = 150,
.bg_a = 255,
});
}
}
// Bind EBO // Bind EBO
var ebobind = try self.ebo.bind(.ElementArrayBuffer); var ebobind = try self.ebo.bind(.ElementArrayBuffer);
defer ebobind.unbind(); defer ebobind.unbind();
@ -198,13 +208,13 @@ pub fn render(self: Grid) !void {
// Bind VBO and set data // Bind VBO and set data
var binding = try self.vbo.bind(.ArrayBuffer); var binding = try self.vbo.bind(.ArrayBuffer);
defer binding.unbind(); defer binding.unbind();
try binding.setData(vertices.items, .StaticDraw); try binding.setData(self.cells.items, .StaticDraw);
try gl.drawElementsInstanced( try gl.drawElementsInstanced(
gl.c.GL_TRIANGLES, gl.c.GL_TRIANGLES,
6, 6,
gl.c.GL_UNSIGNED_INT, gl.c.GL_UNSIGNED_BYTE,
vertices.items.len, self.cells.items.len,
); );
} }

View File

@ -111,6 +111,9 @@ fn sizeCallback(window: glfw.Window, width: i32, height: i32) void {
.height = @intCast(u32, height), .height = @intCast(u32, height),
}) catch |err| log.err("error updating grid screen size err={}", .{err}); }) catch |err| log.err("error updating grid screen size err={}", .{err});
// TODO: temp
win.grid.demoCells() catch unreachable;
// Update our viewport for this context to be the entire window // Update our viewport for this context to be the entire window
gl.viewport(0, 0, width, height) catch |err| gl.viewport(0, 0, width, height) catch |err|
log.err("error updating OpenGL viewport err={}", .{err}); log.err("error updating OpenGL viewport err={}", .{err});