render a rainbow grid

This commit is contained in:
Mitchell Hashimoto
2022-04-14 17:44:40 -07:00
parent 544286509f
commit ce70efd771
3 changed files with 47 additions and 10 deletions

View File

@ -12,6 +12,13 @@ flat out vec4 bg_color;
uniform vec2 cell_dims; uniform vec2 cell_dims;
uniform mat4 projection; uniform mat4 projection;
vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
void main() { void main() {
// Top-left cell coordinates converted to world space // Top-left cell coordinates converted to world space
vec2 cell_pos = cell_dims * grid_coord; vec2 cell_pos = cell_dims * grid_coord;
@ -27,5 +34,5 @@ void main() {
cell_pos = cell_pos + cell_dims * position; cell_pos = cell_pos + cell_dims * position;
gl_Position = projection * vec4(cell_pos, 1.0, 1.0); gl_Position = projection * vec4(cell_pos, 1.0, 1.0);
bg_color = bg_color_in; bg_color = vec4(hsv2rgb(bg_color_in.rgb), 1.0);
} }

View File

@ -58,7 +58,7 @@ pub fn init(alloc: Allocator) !App {
var texter = try TextRenderer.init(alloc); var texter = try TextRenderer.init(alloc);
errdefer texter.deinit(alloc); errdefer texter.deinit(alloc);
const grid = try Grid.init(alloc); var grid = try Grid.init(alloc);
try grid.setScreenSize(.{ .width = 3000, .height = 1666 }); try grid.setScreenSize(.{ .width = 3000, .height = 1666 });
window.setSizeCallback((struct { window.setSizeCallback((struct {

View File

@ -36,9 +36,14 @@ const ScreenDim = struct {
height: i32, height: i32,
}; };
alloc: std.mem.Allocator,
/// Current cell dimensions for this grid. /// Current cell dimensions for this grid.
cell_dims: CellDim, cell_dims: CellDim,
columns: u32 = 0,
rows: u32 = 0,
/// Shader program for cell rendering. /// Shader program for cell rendering.
program: gl.Program, program: gl.Program,
@ -93,6 +98,7 @@ pub fn init(alloc: Allocator) !Grid {
try program.setUniform("cell_dims", @Vector(2, f32){ cell_width, cell_height }); try program.setUniform("cell_dims", @Vector(2, f32){ cell_width, cell_height });
return Grid{ return Grid{
.alloc = alloc,
.cell_dims = .{ .width = cell_width, .height = cell_height }, .cell_dims = .{ .width = cell_width, .height = cell_height },
.program = program, .program = program,
}; };
@ -100,7 +106,7 @@ pub fn init(alloc: Allocator) !Grid {
/// 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: ScreenDim) !void { pub fn setScreenSize(self: *Grid, dim: ScreenDim) !void {
// Create a 2D orthographic projection matrix with the full width/height. // Create a 2D orthographic projection matrix with the full width/height.
var projection: gb.gbMat4 = undefined; var projection: gb.gbMat4 = undefined;
gb.gb_mat4_ortho2d( gb.gb_mat4_ortho2d(
@ -111,12 +117,18 @@ pub fn setScreenSize(self: Grid, dim: ScreenDim) !void {
0, 0,
); );
self.columns = @floatToInt(u32, @intToFloat(f32, dim.width) / self.cell_dims.width);
self.rows = @floatToInt(u32, @intToFloat(f32, dim.height) / self.cell_dims.width);
// Update the projection uniform within our shader // Update the projection uniform within our shader
const bind = try self.program.use(); const bind = try self.program.use();
defer bind.unbind(); defer bind.unbind();
try self.program.setUniform("projection", projection); try self.program.setUniform("projection", projection);
log.debug("screen size w={d} h={d}", .{ dim.width, dim.height }); log.debug("screen size w={d} h={d} cols={d} rows={d}", .{
dim.width, dim.height,
self.columns, self.rows,
});
} }
pub fn render(self: Grid) !void { pub fn render(self: Grid) !void {
@ -138,16 +150,34 @@ pub fn render(self: Grid) !void {
1, 2, 3, 1, 2, 3,
}, .StaticDraw); }, .StaticDraw);
// Build our data
var vertices: std.ArrayListUnmanaged([6]f32) = .{};
try vertices.ensureUnusedCapacity(self.alloc, self.columns * self.rows);
defer vertices.deinit(self.alloc);
var row: u32 = 0;
while (row < self.rows) : (row += 1) {
var col: u32 = 0;
while (col < self.columns) : (col += 1) {
const rowf = @intToFloat(f32, row);
const colf = @intToFloat(f32, col);
const hue = ((colf * @intToFloat(f32, self.rows)) + rowf) / @intToFloat(f32, self.columns * self.rows);
vertices.appendAssumeCapacity([6]f32{
colf,
rowf,
hue,
0.7,
0.8,
1.0,
});
}
}
// Vertex buffer (VBO) // Vertex buffer (VBO)
const vbo = try gl.Buffer.create(); const vbo = try gl.Buffer.create();
defer vbo.destroy(); defer vbo.destroy();
var binding = try vbo.bind(.ArrayBuffer); var binding = try vbo.bind(.ArrayBuffer);
defer binding.unbind(); defer binding.unbind();
try binding.setData([_][6]f32{ try binding.setData(vertices.items, .StaticDraw);
.{ 0, 0, 1, 0, 0, 1 },
.{ 1, 0, 0, 1, 0, 1 },
.{ 2, 0, 0, 0, 1, 1 },
}, .StaticDraw);
try binding.attribute(0, 2, [6]f32, 0); try binding.attribute(0, 2, [6]f32, 0);
try binding.attribute(1, 4, [6]f32, 2); try binding.attribute(1, 4, [6]f32, 2);
try binding.attributeDivisor(0, 1); try binding.attributeDivisor(0, 1);
@ -157,7 +187,7 @@ pub fn render(self: Grid) !void {
gl.c.GL_TRIANGLES, gl.c.GL_TRIANGLES,
6, 6,
gl.c.GL_UNSIGNED_INT, gl.c.GL_UNSIGNED_INT,
3, vertices.items.len,
); );
try gl.VertexArray.unbind(); try gl.VertexArray.unbind();
} }