-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTransform.zig
54 lines (44 loc) · 2.12 KB
/
Transform.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const zmath = @import("zmath");
const Ray = @import("Ray.zig").Ray;
const HitRecord = @import("HitRecord.zig").HitRecord;
pub const Transform = struct {
const Self = @This();
mat: zmath.Mat = zmath.identity(),
inv_mat: zmath.Mat = zmath.identity(),
inv_trans_mat: zmath.Mat = zmath.identity(),
pub fn translate(self: *Self, x: f32, y: f32, z: f32) void {
self.mat = zmath.mul(self.mat, zmath.translation(-x, -y, -z));
self.update_matrices();
}
pub fn rotate(self: *Self, pitch: f32, yaw: f32, roll: f32) void {
self.mat = zmath.mul(self.mat, zmath.matFromRollPitchYaw(-pitch, -yaw, -roll));
self.update_matrices();
}
pub fn scale(self: *Self, x: f32, y: f32, z: f32) void {
self.mat = zmath.mul(self.mat, zmath.scaling(x, y, z));
self.update_matrices();
}
fn update_matrices(self: *Self) void {
self.inv_mat = zmath.inverse(self.mat);
self.inv_trans_mat = zmath.transpose(zmath.inverse(self.mat));
}
pub inline fn ray_global_to_object(self: *const Self, ray: *const Ray) Ray {
const transformed_origin = zmath.mul(zmath.f32x4(ray.origin[0], ray.origin[1], ray.origin[2], 1), self.mat); // It's a little strange that we need to set 1 for w
const transformed_direction = zmath.mul(zmath.f32x4(ray.direction[0], ray.direction[1], ray.direction[2], 0), self.inv_trans_mat); // Same
return Ray{
.origin = transformed_origin,
.direction = transformed_direction,
};
}
pub inline fn hitRecord_object_to_global(self: *const Self, ray: HitRecord) HitRecord {
const transformed_intersection_point = zmath.mul(zmath.f32x4(ray.intersection_point[0], ray.intersection_point[1], ray.intersection_point[2], 1), self.inv_mat);
const transformed_normal = zmath.mul(zmath.f32x4(ray.normal[0], ray.normal[1], ray.normal[2], 0), self.inv_mat);
return HitRecord{
.hit = ray.hit,
.intersection_point = transformed_intersection_point,
.normal = transformed_normal,
.t = ray.t,
.material = ray.material,
};
}
};