get rid of gb_math and create matrices from scratch

This commit is contained in:
Mitchell Hashimoto
2022-08-18 11:33:28 -07:00
parent b562eec83c
commit e5c583ccf5
7 changed files with 37 additions and 2259 deletions

View File

@ -140,8 +140,6 @@ fn addDeps(
b: *std.build.Builder, b: *std.build.Builder,
step: *std.build.LibExeObjStep, step: *std.build.LibExeObjStep,
) !void { ) !void {
step.addIncludeDir("src/");
step.addCSourceFile("src/gb_math.c", &.{});
step.addIncludeDir("vendor/glad/include/"); step.addIncludeDir("vendor/glad/include/");
step.addCSourceFile("vendor/glad/src/gl.c", &.{}); step.addCSourceFile("vendor/glad/src/gl.c", &.{});

View File

@ -10,9 +10,9 @@ const font = @import("font/font.zig");
const terminal = @import("terminal/main.zig"); const terminal = @import("terminal/main.zig");
const Terminal = terminal.Terminal; const Terminal = terminal.Terminal;
const gl = @import("opengl.zig"); const gl = @import("opengl.zig");
const gb = @import("gb_math.zig");
const trace = @import("tracy").trace; const trace = @import("tracy").trace;
const Config = @import("config.zig").Config; const Config = @import("config.zig").Config;
const math = @import("math.zig");
const log = std.log.scoped(.grid); const log = std.log.scoped(.grid);
@ -442,20 +442,20 @@ pub fn updateCells(self: *Grid, term: Terminal) !void {
/// 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 {
// Create a 2D orthographic projection matrix with the full width/height.
var projection: gb.gbMat4 = undefined;
gb.gb_mat4_ortho2d(
&projection,
0,
@intToFloat(f32, dim.width),
@intToFloat(f32, dim.height),
0,
);
// 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",
// 2D orthographic projection with the full w/h
math.ortho2d(
0,
@intToFloat(f32, dim.width),
@intToFloat(f32, dim.height),
0,
),
);
// Recalculate the rows/columns. // Recalculate the rows/columns.
self.size.update(dim, self.cell_size); self.size.update(dim, self.cell_size);

View File

@ -1,6 +0,0 @@
// This file defines the implementation of gb_math and is compiled using clang.
// gb_math.zig then imports gb_math.h without the implementation define set
// so that it can just get the function prototypes.
#define GB_MATH_IMPLEMENTATION
#include "gb_math.h"

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +0,0 @@
pub usingnamespace @cImport({
@cInclude("gb_math.h");
});

24
src/math.zig Normal file
View File

@ -0,0 +1,24 @@
pub const F32x4 = @Vector(4, f32);
/// Matrix type
pub const Mat = [4]F32x4;
/// Identity matrix
pub fn identity() Mat {
return .{
.{ 1.0, 0.0, 0.0, 0.0 },
.{ 0.0, 1.0, 0.0, 0.0 },
.{ 0.0, 0.0, 1.0, 0.0 },
.{ 0.0, 0.0, 0.0, 1.0 },
};
}
pub fn ortho2d(left: f32, right: f32, bottom: f32, top: f32) Mat {
var mat = identity();
mat[0][0] = 2 / (right - left);
mat[1][1] = 2 / (top - bottom);
mat[2][2] = -1.0;
mat[3][0] = -(right + left) / (right - left);
mat[3][1] = -(top + bottom) / (top - bottom);
return mat;
}

View File

@ -4,7 +4,6 @@ const std = @import("std");
const assert = std.debug.assert; const assert = std.debug.assert;
const log = std.log.scoped(.opengl); const log = std.log.scoped(.opengl);
const gb = @import("../gb_math.zig");
const c = @import("c.zig"); const c = @import("c.zig");
const Shader = @import("Shader.zig"); const Shader = @import("Shader.zig");
const errors = @import("errors.zig"); const errors = @import("errors.zig");
@ -87,7 +86,7 @@ pub inline fn setUniform(p: Program, n: [:0]const u8, value: anytype) !void {
@Vector(2, f32) => c.glUniform2f(loc, value[0], value[1]), @Vector(2, f32) => c.glUniform2f(loc, value[0], value[1]),
@Vector(3, f32) => c.glUniform3f(loc, value[0], value[1], value[2]), @Vector(3, f32) => c.glUniform3f(loc, value[0], value[1], value[2]),
@Vector(4, f32) => c.glUniform4f(loc, value[0], value[1], value[2], value[3]), @Vector(4, f32) => c.glUniform4f(loc, value[0], value[1], value[2], value[3]),
gb.gbMat4 => c.glUniformMatrix4fv( [4]@Vector(4, f32) => c.glUniformMatrix4fv(
loc, loc,
1, 1,
c.GL_FALSE, c.GL_FALSE,