diff options
| author | Alexis Brodeur <brodeuralexis@gmail.com> | 2020-02-03 19:24:21 +0000 |
|---|---|---|
| committer | Alexis Brodeur <brodeuralexis@gmail.com> | 2020-02-03 19:24:21 +0000 |
| commit | 950ba8520cdfdf3e3f000ba6006b249dd9fa0251 (patch) | |
| tree | be7f9c0acfbb226ef1484a23bb229da171f7399e /src | |
| download | glm-zig-950ba8520cdfdf3e3f000ba6006b249dd9fa0251.tar.gz glm-zig-950ba8520cdfdf3e3f000ba6006b249dd9fa0251.zip | |
Initial commit
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.zig | 124 | ||||
| -rw-r--r-- | src/matrix.zig | 135 | ||||
| -rw-r--r-- | src/vector.zig | 304 |
3 files changed, 563 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..53f0b10 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,124 @@ +const std = @import("std"); +const testing = std.testing; +const math = std.math; + +const vector = @import("./vector.zig"); +const matrix = @import("./matrix.zig"); + +pub const Matrix = matrix.Matrix; +pub const Vector = vector.Vector; + +pub const Vec2 = Vector(2); +pub const Vec3 = Vector(3); +pub const Vec4 = Vector(4); + +pub const Mat2 = Matrix(2); +pub const Mat3 = Matrix(3); +pub const Mat4 = Matrix(4); + +pub fn translation(v: Vec3) Mat4 { + return .{ + .values = [4][4]f32 { + .{ 1, 0, 0, 0 }, + .{ 0, 1, 0, 0 }, + .{ 0, 0, 1, 0 }, + .{ v.values[0], v.values[1], v.values[2], 1 } + } + }; +} + +pub fn rotation(angle: f32, axis: Vec3) Mat4 { + const unit = axis.normalize(); + + const x = unit.values[0]; + const y = unit.values[1]; + const z = unit.values[2]; + + const a = math.cos(angle) + x * x * (1 - math.cos(angle)); + const b = y * x * (1 - math.cos(angle)) + z * math.sin(angle); + const c = z * x * (1 - math.cos(angle)) - y * math.sin(angle); + + const d = x * y * (1 - math.cos(angle)) - z * math.sin(angle); + const e = math.cos(angle) + y * y * (1 - math.cos(angle)); + const f = z * y * (1 - math.cos(angle)) + x * math.sin(angle); + + const h = x * z * (1 - math.cos(angle)) + y * math.sin(angle); + const i = y * z * (1 - math.cos(angle)) - x * math.sin(angle); + const j = math.cos(angle) + z * z * (1 - math.cos(angle)); + + return .{ + .values = [4][4]f32{ + .{ a, b, c, 0 }, + .{ d, e, f, 0 }, + .{ h, i, j, 0 }, + .{ 0, 0, 0, 1 }, + }, + }; +} + +pub fn scale(v: Vec3) Mat4 { + return .{ + .values = [4][4]f32 { + .{ v.values[0], 0, 0, 0 }, + .{ 0, v.values[1], 0, 0 }, + .{ 0, 0, v.values[2], 0 }, + .{ 0, 0, 0, 1 }, + } + }; +} + +pub fn lookAt(eye: Vec3, center: Vec3, up: Vec3) Mat4 { + const f = center.sub(eye).normalize(); + const s = f.cross(up).normalize(); + const u = s.cross(f); + + return Mat4{ + .values = [4][4]f32{ + .{ s.values[0], u.values[0], -f.values[0], 0.0 }, + .{ s.values[1], u.values[1], -f.values[1], 0.0 }, + .{ s.values[2], u.values[2], -f.values[2], 0.0 }, + .{ -s.dot(eye), -u.dot(eye), f.dot(eye), 1.0 }, + }, + }; +} + +pub fn perspective(fovY: f32, aspectRatio: f32, zNear: f32, zFar: f32) Mat4 { + const f = math.tan(fovY / 2.0); + + const a = 1.0 / (aspectRatio * f); + const b = 1.0 / f; + const c = -(zFar + zNear) / (zFar - zNear); + const d = -(2.0 * zFar * zNear) / (zFar - zNear); + + return .{ + .values = [4][4]f32{ + .{ a, 0.0, 0.0, 0.0 }, + .{ 0.0, b, 0.0, 0.0 }, + .{ 0.0, 0.0, c, -1.0 }, + .{ 0.0, 0.0, d, 0.0 }, + }, + }; +} + +pub fn orthogonal(left: f32, right: f32, bottom: f32, top: f32, zNear: f32, zFar: f32) Mat4 { + const a = 2.0 / (right - left); + const b = 2.0 / (top - bottom); + const c = -2.0 / (zFar - zNear); + const d = -(right + left) / (right - left); + const e = -(top + bottom) / (top - bottom); + const f = -(zFar + zNear) / (zFar - zNear); + + return .{ + .values = [4][4]f32{ + .{ a, 0.0, 0.0, 0.0 }, + .{ 0.0, b, 0.0, 0.0 }, + .{ 0.0, 0.0, c, -1.0 }, + .{ d, e, f, 0.0 }, + }, + }; +} + +test "glm" { + _ = @import("./vector.zig"); + _ = @import("./matrix.zig"); +} diff --git a/src/matrix.zig b/src/matrix.zig new file mode 100644 index 0000000..e107069 --- /dev/null +++ b/src/matrix.zig @@ -0,0 +1,135 @@ +const std = @import("std"); +const math = std.math; +const testing = std.testing; + +pub fn Matrix(comptime N: usize) type { + return packed struct { + const Self = @This(); + pub const Scalar = f32; + + values: [N][N]Scalar, + + /// Initialies a matrix. + pub fn init(values: [N][N]Scalar) Self { + return .{ .values = values }; + } + + /// Creates a matrix filled with the given value. + pub fn filled(n: Scalar) Self { + var result: Self = undefined; + + comptime var i = 0; + inline while (i < N) : (i += 1) { + comptime var j = 0; + inline while (j < N) : (j += 1) { + result.values[i][j] = n; + } + } + + return result; + } + + /// A zero initialized matrix. + pub const ZERO: Self = comptime Self.filled(0); + + /// An identity initialized matrix. + pub const IDENTITY: Self = comptime brk: { + var result: Self = undefined; + + comptime var i = 0; + while (i < N) : (i += 1) { + comptime var j = 0; + while (j < N) : (j += 1) { + result.values[i][j] = brk: { + if (i == j) { + break :brk 1; + } else { + break :brk 0; + } + }; + } + } + + break :brk result; + }; + + /// Transposes this matrix + pub fn transpose(self: Self) Self { + var result: Self = undefined; + + comptime var i = 0; + inline while (i < N) : (i += 1) { + comptime var j = 0; + inline while (j < N) : (j += 1) { + result.values[j][i] = self.values[i][j]; + } + } + + return result; + } + + /// Multiplies 2 matrices together. + pub fn mul(self: Self, other: Self) Self { + var result: Self = undefined; + + const a = self.transpose(); + const b = other; + + comptime var i = 0; + inline while (i < N) : (i += 1) { + comptime var j = 0; + inline while (j < N) : (j += 1) { + const row: @Vector(N, Scalar) = a.values[j]; + const column: @Vector(N, Scalar) = b.values[i]; + const products: [N]Scalar = row * column; + + var sum = @floatCast(f32, 0); + comptime var k = 0; + inline while (k < N) : (k += 1) { + sum += products[k]; + } + + result.values[i][j] = sum; + } + } + + return result; + } + }; +} + +fn expectMatrixEqual(comptime N: usize, expected: [N][N]f32, actual: Matrix(N)) void { + comptime var i = 0; + inline while (i < N) : (i += 1) { + comptime var j = 0; + inline while (j < N) : (j += 1) { + testing.expectEqual(expected[i][j], actual.values[i][j]); + } + } +} + +test "matrix initialization" { + const A = Matrix(2).init(.{ .{ 1, 2 }, .{ 3, 4 } }); + expectMatrixEqual(2, [2][2]f32{ .{ 1, 2, }, .{ 3, 4 } }, A); +} + +test "matrix zero" { + expectMatrixEqual(3, Matrix(3).filled(0).values, Matrix(3).ZERO); +} + +test "matrix identity" { + expectMatrixEqual(3, [3][3]f32{ .{ 1, 0, 0 }, .{ 0, 1 , 0 }, .{ 0, 0, 1 } }, Matrix(3).IDENTITY); +} + +test "matrix transpose" { + const actual = Matrix(3).init(.{ .{ 1, 2, 3 }, .{ 4, 5, 6 }, .{ 7, 8, 9 } }); + const expected = [3][3]f32{ .{ 1, 4, 7 }, .{ 2, 5, 8 }, .{ 3, 6, 9 } }; + expectMatrixEqual(3, expected, actual.transpose()); +} + +test "matrix multiplication" { + const A = Matrix(2).init(.{ .{ 1, 2 }, .{ 3, 4 } }); + const B = Matrix(2).init(.{ .{ 6, 7 }, .{ 8, 9 } }); + const expected = [2][2]f32{ .{ 27, 40 }, .{ 35, 52 } }; + expectMatrixEqual(2, expected, A.mul(B)); +} diff --git a/src/vector.zig b/src/vector.zig new file mode 100644 index 0000000..2453992 --- /dev/null +++ b/src/vector.zig @@ -0,0 +1,304 @@ +const std = @import("std"); +const math = std.math; + +/// The type of a vector. +pub fn Vector(comptime N: usize) type { + return packed struct { + const Self = @This(); + /// The scalar type manager by this vector. + pub const Scalar = f32; + + values: [N]Scalar, + + /// Initializes a vector from its scalar values. + pub fn init(values: [N]Scalar) Self { + return .{ .values = values }; + } + + /// Creates a vector filled with the given scalar value. + pub fn filled(n: Scalar) Self { + var values: [N]Scalar = undefined; + comptime var i = 0; + inline while (i < N) : (i += 1) { + values[i] = n; + } + return .{ .values = values }; + } + + /// Creates a vector filled with zeroes. + pub fn zeroes() Self { + return comptime Self.filled(0); + } + + /// Sums all scalars in this vector. + pub fn sum(self: Self) Scalar { + var total: Scalar = 0; + + comptime var i = 0; + inline while (i < N) : (i += 1) { + total += self.values[i]; + } + + return total; + } + + /// Adds 2 vector together. + pub fn add(self: Self, other: Self) Self { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = other.values; + + return .{ .values = left + right }; + } + + /// Adds another vector to this vector. + pub fn addAssign(self: *Self, other: Self) void { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = other.values; + + self.values = left + right; + } + + /// Subtracts 2 vector together. + pub fn sub(self: Self, other: Self) Self { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = other.values; + + return .{ .values = left - right }; + } + + /// Adds another vector to this vector. + pub fn subAssign(self: *Self, other: Self) void { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = other.values; + + self.values = left - right; + } + + /// Multiplies 2 vectors together. + pub fn mul(self: Self, other: Self) Self { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = other.values; + + return .{ .values = left * right }; + } + + /// Multiplies a vector and a scalar together. + pub fn mulScalar(self: Self, n: Scalar) Self { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = Self.filled(n).values; + + return .{ .values = left * right }; + } + + /// Multiplies this vector with another vector. + pub fn mulAssign(self: *Self, other: Self) void { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = other.values; + + self.values = left * right; + } + + /// Multiplies this vector with a scalar. + pub fn mulAssignScalar(self: *Self, n: Scalar) void { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = Self.filled(n).values; + + self.values = left * right; + } + + /// Multiplies 2 vectors together. + pub fn div(self: Self, other: Self) Self { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = other.values; + + return .{ .values = left / right }; + } + + /// Multiplies a vector and a scalar together. + pub fn divScalar(self: Self, n: Scalar) Self { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = Self.filled(n).values; + + return .{ .values = left / right }; + } + + /// Multiplies this vector with another vector. + pub fn divAssign(self: *Self, other: Self) void { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = other.values; + + self.values = left / right; + } + + /// Multiplies this vector with a scalar. + pub fn divAssignScalar(self: *Self, n: Scalar) void { + const left: @Vector(N, Scalar) = self.values; + const right: @Vector(N, Scalar) = Self.filled(n).values; + + self.values = left / right; + } + + /// Calculates the dot product of 2 vectors. + pub fn dot(self: Self, other: Self) Scalar { + return self.mul(other).sum(); + } + + /// Calculates the norm squared of this vector. + pub fn normSquared(self: Self) Scalar { + return self.dot(self); + } + + /// Calculates the norm of this vector. + pub fn norm(self: Self) Scalar { + return math.sqrt(self.normSquared()); + } + + /// Returns a normalized version of this vector. + pub fn normalize(self: Self) Self { + const values: @Vector(N, Scalar) = self.values; + const norms: @Vector(N, Scalar) = Self.filled(self.norm()).values; + + return .{ .values = values / norms }; + } + + /// Make this vector normalized. + pub fn normalizeAssign(self: *Self) void { + const values: @Vector(N, Scalar) = self.values; + const norms: @Vector(N, Scalar) = Self.filled(self.norm()).values; + + self.values = values / norms; + } + + /// Calculates the cross product of 2 vectors. + pub fn cross(self: Self, other: Self) Self { + if (N != 3) { + @compileError("A cross product can only be calculated for a 3D vector."); + } + + const values = [3]Scalar{ + self.values[1] * other.values[2] - self.values[2] * other.values[1], + self.values[2] * other.values[0] - self.values[0] * other.values[2], + self.values[0] * other.values[1] - self.values[1] * other.values[0], + }; + + return Self{ .values = values }; + } + }; +} + +fn expectVectorEqual(comptime N: usize, expected: Vector(N), actual: Vector(N)) void { + comptime var i = 0; + inline while (i < N) : (i += 1) { + std.testing.expectEqual(expected.values[i], actual.values[i]); + } +} + +test "vector initialization" { + const actual = Vector(2).init(.{ 1, 2 }); + std.testing.expectEqual(@floatCast(f32, 1), actual.values[0]); + std.testing.expectEqual(@floatCast(f32, 2), actual.values[1]); +} + +test "vector scalar initialization" { + const v = Vector(3).filled(3); + expectVectorEqual(3, Vector(3).init(.{ 3, 3, 3 }), v); +} + +test "vector zero initialization" { + const v = Vector(3).zeroes(); + expectVectorEqual(3, Vector(3).init(.{ 0, 0, 0 }), v); +} + +test "vector summation" { + const v = Vector(3).init(.{ 1, 2, 3 }); + std.testing.expectEqual(@floatCast(f32, 6), v.sum()); +} + +test "vectors addition" { + const v1 = Vector(3).init(.{ 3, 4, 5 }); + const v2 = Vector(3).init(.{ 6, 7, 8 }); + expectVectorEqual(3, Vector(3).init(.{ 9, 11, 13 }), v1.add(v2)); + + var v3 = Vector(3).init(.{ 1, 2, 3 }); + v3.addAssign(Vector(3).init(.{ 3, 2, 1 })); + expectVectorEqual(3, Vector(3).init(.{ 4, 4, 4 }), v3); +} + +test "vectors subtraction" { + const v1 = Vector(3).init(.{ 3, 4, 5 }); + const v2 = Vector(3).init(.{ 6, 7, 8 }); + expectVectorEqual(3, Vector(3).init(.{ -3, -3, -3 }), v1.sub(v2)); + + var v3 = Vector(3).init(.{ 1, 2, 3 }); + v3.subAssign(Vector(3).init(.{ 3, 2, 1 })); + expectVectorEqual(3, Vector(3).init(.{ -2, 0, 2 }), v3); +} + +test "vector multiplication" { + const v1 = Vector(3).init(.{ 3, 4, 5 }); + const v2 = Vector(3).init(.{ 6, 7, 8 }); + expectVectorEqual(3, Vector(3).init(.{ 18, 28, 40 }), v1.mul(v2)); + + var v3 = Vector(3).init(.{ 1, 2, 3 }); + v3.mulAssign(Vector(3).init(.{ 3, 2, 1 })); + expectVectorEqual(3, Vector(3).init(.{ 3, 4, 3 }), v3); +} + +test "vector scalar multiplication" { + const v1 = Vector(3).init(.{ 3, 4, 5 }); + expectVectorEqual(3, Vector(3).init(.{ 6, 8, 10 }), v1.mulScalar(2)); + + var v3 = Vector(3).init(.{ 1, 2, 3 }); + v3.mulAssignScalar(3); + expectVectorEqual(3, Vector(3).init(.{ 3, 6, 9 }), v3); +} + +test "vector division" { + const v1 = Vector(3).init(.{ 10, 20, 30 }); + const v2 = Vector(3).init(.{ 5, 2, 6 }); + expectVectorEqual(3, Vector(3).init(.{ 2, 10, 5 }), v1.div(v2)); + + var v3 = Vector(3).init(.{ 3, 2, 6 }); + v3.divAssign(Vector(3).init(.{ 1, 2, 3 })); + expectVectorEqual(3, Vector(3).init(.{ 3, 1, 2 }), v3); +} + +test "vector scalar division" { + const v1 = Vector(3).init(.{ 12, 24, 36 }); + expectVectorEqual(3, Vector(3).init(.{ 1, 2, 3 }), v1.divScalar(12)); + + var v3 = Vector(3).init(.{ 1, 2, 3 }); + v3.divAssignScalar(1); + expectVectorEqual(3, Vector(3).init(.{ 1, 2, 3 }), v3); +} + +test "vector dot product" { + const v1 = Vector(3).init(.{ 1, 2, 3 }); + const v2 = Vector(3).init(.{ 4, 5, 6 }); + std.testing.expectEqual(@floatCast(f32, 32), v1.dot(v2)); +} + +test "vector norm squared" { + const v1 = Vector(2).init(.{ 3, 4 }); + std.testing.expectEqual(@floatCast(f32, 25), v1.normSquared()); +} + +test "vector norm" { + const v1 = Vector(2).init(.{ 3, 4 }); + std.testing.expectEqual(@floatCast(f32, 5), v1.norm()); +} + +test "vector normalization" { + const v1 = Vector(3).init(.{ 30, 10, 20 }); + expectVectorEqual(3, Vector(3).init(.{0.80178372573729, 0.26726124191243, 0.53452248382486}), v1.normalize()); + + var v2 = Vector(3).init(.{ 60, 20, 40 }); + v2.normalizeAssign(); + expectVectorEqual(3, Vector(3).init(.{0.80178372573729, 0.26726124191243, 0.53452248382486}), v2); +} + +test "vector cross product" { + const v1 = Vector(3).init(.{ 4, 5, 6 }); + const v2 = Vector(3).init(.{ 7, 8, 9 }); + expectVectorEqual(3, Vector(3).init(.{ -3, 6, -3 }), v1.cross(v2)); +} |
