Skip to content

Commit

Permalink
Optimize calcul
Browse files Browse the repository at this point in the history
  • Loading branch information
Miou-zora committed May 8, 2024
1 parent 5800379 commit 350591e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
15 changes: 8 additions & 7 deletions src/Cylinder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ pub const Cylinder = struct {
transform: ?Transform = null,

pub fn hits(self: *const Self, ray: Ray) HitRecord {
const rx_minus_cx = ray.origin[0] - self.origin[0];
const ry_minus_cy = ray.origin[1] - self.origin[1];
const a = ray.direction[0] * ray.direction[0] + ray.direction[1] * ray.direction[1];
const b = 2.0 * (ray.direction[0] * rx_minus_cx + ray.direction[1] * ry_minus_cy);
const c = rx_minus_cx * rx_minus_cx + ry_minus_cy * ry_minus_cy - self.radius * self.radius;
const r_minus_c = ray.origin - self.origin;
const a = zmath.dot2(ray.direction, ray.direction)[0] * 2; // Good luck to understand this, I don't
const b = zmath.dot2(ray.direction, r_minus_c)[0] * 4;
const c = zmath.dot2(r_minus_c, r_minus_c)[0] * 2 - self.radius * self.radius;

const delta = b * b - 4.0 * a * c;
if (delta < 0.0 or a == 0.0) {
Expand All @@ -31,9 +30,10 @@ pub const Cylinder = struct {
if (t < 0.0) {
return HitRecord.nil();
} else {
const normal = intersection_point - self.origin;
return HitRecord{
.hit = true,
.normal = zmath.f32x4(intersection_point[0] - self.origin[0], intersection_point[1] - self.origin[1], 0, 0),
.normal = zmath.f32x4(normal[0], normal[1], 0, 0),
.intersection_point = intersection_point,
.t = zmath.length3(intersection_point - ray.origin)[0],
.material = self.material,
Expand All @@ -47,9 +47,10 @@ pub const Cylinder = struct {
}
const t = if (t1 < t2 and t1 > 0) t1 else t2;
const intersection_point = zmath.mulAdd(ray.direction, @as(Vec3, @splat(t)), ray.origin);
const normal = intersection_point - self.origin;
return HitRecord{
.hit = true,
.normal = zmath.f32x4(intersection_point[0] - self.origin[0], intersection_point[1] - self.origin[1], 0, 0),
.normal = zmath.f32x4(normal[0], normal[1], 0, 0),
.intersection_point = intersection_point,
.t = zmath.length3(intersection_point - ray.origin)[0],
.material = self.material,
Expand Down
4 changes: 2 additions & 2 deletions src/Plane.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ pub const Plane = struct {
}

pub fn hits(self: *const Self, ray: Ray) HitRecord {
const denom = @reduce(.Add, self.normal * ray.direction);
const denom: f32 = zmath.dot3(self.normal, ray.direction)[0];

if (denom == 0.0) {
return HitRecord.nil();
}

const t = @reduce(.Add, (self.origin - ray.origin) * self.normal) / denom;
const t: f32 = zmath.dot3(self.origin - ray.origin, self.normal)[0] / denom;

if (t < 0.0) {
return HitRecord.nil();
Expand Down
14 changes: 8 additions & 6 deletions src/Triangle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@ pub const Triangle = struct {
const aSubc = a - c;
const cSubb = c - b;

const u = @reduce(.Add, zmath.cross3(bSuba, hit_point - a) * normal);
const v = @reduce(.Add, zmath.cross3(cSubb, hit_point - b) * normal);
const w = @reduce(.Add, zmath.cross3(aSubc, hit_point - c) * normal);
const u = zmath.dot3(zmath.cross3(bSuba, hit_point - a), normal)[0];
const v = zmath.dot3(zmath.cross3(cSubb, hit_point - b), normal)[0];
const w = zmath.dot3(zmath.cross3(aSubc, hit_point - c), normal)[0];

if (u < 0 or v < 0 or w < 0) {
return HitRecord.nil();
}

const barycentric = zmath.f32x4(u, v, w, 0);
const texCoord1 = zmath.f32x4(self.va.texCoord[0], self.vb.texCoord[0], self.vc.texCoord[0], 0); // Is it efficient to store this in tmp const?
const texCoord2 = zmath.f32x4(self.va.texCoord[1], self.vb.texCoord[1], self.vc.texCoord[1], 0); // same
const posInImage: @Vector(2, usize) = .{
@as(usize, @intFromFloat((u * self.va.texCoord[0] + v * self.vb.texCoord[0] + w * self.vc.texCoord[0]) / (u + v + w) * @as(f32, @floatFromInt(self.text.rlImage.width)))),
@as(usize, @intFromFloat((u * self.va.texCoord[1] + v * self.vb.texCoord[1] + w * self.vc.texCoord[1]) / (u + v + w) * @as(f32, @floatFromInt(self.text.rlImage.height)))),
@as(usize, @intFromFloat(@reduce(.Add, barycentric * texCoord1) / @reduce(.Add, barycentric) * @as(f32, @floatFromInt(self.text.rlImage.width)))),
@as(usize, @intFromFloat(@reduce(.Add, barycentric * texCoord2) / @reduce(.Add, barycentric) * @as(f32, @floatFromInt(self.text.rlImage.height)))),
};
const cInt_to_usize = @as(usize, @intCast(self.text.rlImage.width));
const color: rl.Color = self.text.rlColors[posInImage[1] * cInt_to_usize + posInImage[0]];
Expand Down
6 changes: 0 additions & 6 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ fn get_pixel_color(ray: Ray, scene: *Scene.Scene, height: u32, width: u32, recur
width,
recursion_depth - 1,
);
// return .{
// .r = @as(u8, @intFromFloat(@as(f32, @floatFromInt(color.r)) * (1 - reflective) + @as(f32, @floatFromInt(reflected_color.r)) * reflective)),
// .g = @as(u8, @intFromFloat(@as(f32, @floatFromInt(color.g)) * (1 - reflective) + @as(f32, @floatFromInt(reflected_color.g)) * reflective)),
// .b = @as(u8, @intFromFloat(@as(f32, @floatFromInt(color.b)) * (1 - reflective) + @as(f32, @floatFromInt(reflected_color.b)) * reflective)),
// .a = 255,
// };
return color * @as(Vec3, @splat(1 - reflective)) + reflected_color * @as(Vec3, @splat(reflective));
}

Expand Down

0 comments on commit 350591e

Please sign in to comment.