This commit is contained in:
2025-11-12 01:56:08 +01:00
parent c11a384b27
commit 2e8cfd36fd
6 changed files with 597 additions and 9 deletions

107
src/math/Vector4.zig Normal file
View File

@@ -0,0 +1,107 @@
const Vector3 = @import("Vector3.zig").Vector3;
pub const Vector4 = extern struct {
vector: Vector,
pub const Vector = @Vector(4, f32);
pub const zero = Vector4.init(0, 0, 0, 0);
pub const one = Vector4.init(1, 1, 1, 1);
pub const unit_x = Vector4.init(1, 0, 0, 0);
pub const unit_y = Vector4.init(0, 1, 0, 0);
pub const unit_z = Vector4.init(0, 0, 1, 0);
pub const unit_w = Vector4.init(0, 0, 0, 1);
pub inline fn init(x: f32, y: f32, z: f32, w: f32) Vector4 {
return .{ .vector = .{ x, y, z, w } };
}
pub inline fn getX(self: Vector4) f32 {
return self.vector[0];
}
pub inline fn getY(self: Vector4) f32 {
return self.vector[1];
}
pub inline fn getZ(self: Vector4) f32 {
return self.vector[2];
}
pub inline fn getW(self: Vector4) f32 {
return self.vector[3];
}
pub inline fn setX(self: Vector4, x: f32) Vector4 {
const x_vector: Vector = @splat(x);
return .{ .vector = @shuffle(Vector, self, x_vector, .{ ~@as(i32, 0), 1, 2, 3 }) };
}
pub inline fn setY(self: Vector4, y: f32) Vector4 {
const y_vector: Vector = @splat(y);
return .{ .vector = @shuffle(Vector, self, y_vector, .{ 0, ~@as(i32, 1), 2, 3 }) };
}
pub inline fn setZ(self: Vector4, z: f32) Vector4 {
const z_vector: Vector = @splat(z);
return .{ .vector = @shuffle(Vector, self, z_vector, .{ 0, 1, ~@as(i32, 2), 3 }) };
}
pub inline fn setW(self: Vector4, w: f32) Vector4 {
const w_vector: Vector = @splat(w);
return .{ .vector = @shuffle(Vector, self, w_vector, .{ 0, 1, 2, ~@as(i32, 3) }) };
}
pub inline fn add(self: Vector4, other: Vector4) Vector4 {
return .{ .vector = self.vector + other.vector };
}
pub inline fn sub(self: Vector4, other: Vector4) Vector4 {
return .{ .vector = self.vector - other.vector };
}
pub inline fn mul(self: Vector4, other: Vector4) Vector4 {
return .{ .vector = self.vector * other.vector };
}
pub inline fn div(self: Vector4, other: Vector4) Vector4 {
return .{ .vector = self.vector / other.vector };
}
pub inline fn mulScalar(self: Vector4, scalar: f32) Vector4 {
const scalar_vector: Vector = @splat(scalar);
return .{ .vector = self.vector * scalar_vector };
}
pub inline fn divScalar(self: Vector4, scalar: f32) Vector4 {
const scalar_vector: Vector = @splat(scalar);
return .{ .vector = self.vector / scalar_vector };
}
pub inline fn negate(self: Vector4) Vector4 {
return .{ .vector = -self.vector };
}
pub inline fn lerp(self: Vector4, other: Vector4, t: f32) Vector4 {
const s = 1.0 - t;
const t_vector: Vector = @splat(t);
const s_vector: Vector = @splat(s);
return .{ .vector = self * t_vector + other * s_vector };
}
pub inline fn asVector3(self: Vector4) Vector3 {
return .{ .vector = @shuffle(Vector3.Vector, self.vector, undefined, .{ 0, 1, 2 }) };
}
pub inline fn swizzle3(self: Vector4, comptime mask: @Vector(3, i32)) Vector3 {
return .{ .vector = @shuffle(Vector3.Vector, self.vector, undefined, mask) };
}
pub inline fn swizzle4(self: Vector4, comptime mask: @Vector(4, i32)) Vector4 {
return .{ .vector = @shuffle(Vector, self.vector, undefined, mask) };
}
pub inline fn asArray(self: Vector4) [4]f32 {
return self.vector;
}
};