diff options
author | Lorenzo Torres <lorenzotorres@outlook.it> | 2025-03-12 19:56:19 +0100 |
---|---|---|
committer | Lorenzo Torres <lorenzotorres@outlook.it> | 2025-03-12 19:56:19 +0100 |
commit | 4f45899a3c28440724ffcaa3a604ad48f18a25c8 (patch) | |
tree | cecd79da4e3e3cb17ffd86ec1a58d6e77a44cb83 /src/math.zig | |
parent | fc39b277155044366f1647b238a415fab4028d83 (diff) |
base code
Diffstat (limited to 'src/math.zig')
-rw-r--r-- | src/math.zig | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/math.zig b/src/math.zig new file mode 100644 index 0000000..a2cbcd2 --- /dev/null +++ b/src/math.zig @@ -0,0 +1,64 @@ +const std = @import("std"); +pub const tan = std.math.tan; +pub const cos = std.math.cos; +pub const sin = std.math.sin; +pub const rad = std.math.degreesToRadians; + +pub const Matrix = struct { + rows: [4]@Vector(4, f32), + + pub fn lookAt(eye: @Vector(3, f32), target: @Vector(3, f32), arbitrary_up: @Vector(3, f32)) Matrix { + const forward = normalize(eye - target); + const right = normalize(cross(arbitrary_up, forward)); + const up = cross(forward, right); + + const view = [_]@Vector(4, f32){ + @Vector(4, f32){ right[0], right[1], right[2], 0.0 }, + @Vector(4, f32){ up[0], up[1], up[2], 0.0 }, + @Vector(4, f32){ forward[0], forward[1], forward[2], 0.0 }, + @Vector(4, f32){ 0.0, 0.0, 1.0, eye[2] }, + }; + + return Matrix{ + .rows = view, + }; + } + + pub fn perspective(fov: f32, aspect: f32, near: f32, far: f32) Matrix { + const projection = [_]@Vector(4, f32){ + @Vector(4, f32){ 1.0 / (aspect * tan(fov / 2.0)), 0.0, 0.0, 0.0 }, + @Vector(4, f32){ 0.0, 1.0 / tan(fov / 2.0), 0.0, 0.0 }, + @Vector(4, f32){ 0.0, 0.0, -((far + near) / (far - near)), -((2 * far * near) / (far - near)) }, + @Vector(4, f32){ 0.0, 0.0, -1.0, 1.0 }, + }; + + return Matrix{ + .rows = projection, + }; + } + + pub fn identity() Matrix { + const view = [_]@Vector(4, f32){ + @Vector(4, f32){ 1.0, 0.0, 0.0, 0.0 }, + @Vector(4, f32){ 0.0, 1.0, 0.0, 0.0 }, + @Vector(4, f32){ 0.0, 0.0, 1.0, 0.0 }, + @Vector(4, f32){ 0.0, 0.0, 0.0, 1.0 }, + }; + + return Matrix{ + .rows = view, + }; + } +}; + +pub fn dot(a: @Vector(3, f32), b: @Vector(3, f32)) f32 { + return @reduce(.Add, a * b); +} + +pub fn cross(a: @Vector(3, f32), b: @Vector(3, f32)) @Vector(3, f32) { + return @Vector(3, f32){ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] }; +} + +pub fn normalize(a: @Vector(3, f32)) @Vector(3, f32) { + return a / @as(@Vector(3, f32), @splat(@sqrt(dot(a, a)))); +} |