Skip to content

Commit

Permalink
Make little optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Miou-zora committed May 9, 2024
1 parent 15ea32b commit ead3c72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
29 changes: 12 additions & 17 deletions src/Scene.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,23 @@ const rl = @cImport({

fn compute_record_with_transform(obj: anytype, ray: Ray) HitRecord {
if (obj.transform) |transform| {
const record = transform.hitRecord_object_to_global(obj.hits(transform.ray_global_to_object(&ray)));
return .{
.hit = record.hit,
.t = zmath.length3(record.intersection_point - ray.origin)[0],
.intersection_point = record.intersection_point,
.normal = record.normal,
.material = record.material,
};
return transform.hitRecord_object_to_global(obj.hits(transform.ray_global_to_object(&ray)));
} else {
const record = obj.hits(ray);
return .{
.hit = record.hit,
.t = zmath.length3(record.intersection_point - ray.origin)[0],
.intersection_point = record.intersection_point,
.normal = record.normal,
.material = record.material,
};
return obj.hits(ray);
}
}

fn fetch_closest_object_with_transform(obj: anytype, closest_hit: *HitRecord, ray: Ray, t_min: f32, t_max: f32) void {
const record = compute_record_with_transform(obj, ray);
const record_without_t = compute_record_with_transform(obj, ray);
if (!record_without_t.hit)
return;
const record: HitRecord = .{
.hit = record_without_t.hit,
.t = zmath.length3(record_without_t.intersection_point - ray.origin)[0],
.intersection_point = record_without_t.intersection_point,
.normal = record_without_t.normal,
.material = record_without_t.material,
};
if (record.hit and (!closest_hit.hit or record.t < closest_hit.t) and record.t > t_min and record.t < t_max) {
closest_hit.* = record;
}
Expand Down
14 changes: 8 additions & 6 deletions src/Transform.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ pub const Transform = struct {
};
}

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);
pub inline fn hitRecord_object_to_global(self: *const Self, record: HitRecord) HitRecord {
if (!record.hit)
return record;
const transformed_intersection_point = zmath.mul(zmath.f32x4(record.intersection_point[0], record.intersection_point[1], record.intersection_point[2], 1), self.inv_mat);
const transformed_normal = zmath.mul(zmath.f32x4(record.normal[0], record.normal[1], record.normal[2], 0), self.inv_mat);

return HitRecord{
.hit = ray.hit,
.hit = record.hit,
.intersection_point = transformed_intersection_point,
.normal = transformed_normal,
.t = ray.t,
.material = ray.material,
.t = record.t,
.material = record.material,
};
}
};

0 comments on commit ead3c72

Please sign in to comment.