diff --git a/src/hittable/BVH.zig b/src/hittable/BVH.zig index 601dadd..681da10 100644 --- a/src/hittable/BVH.zig +++ b/src/hittable/BVH.zig @@ -34,6 +34,7 @@ const Node = struct { if (object_span == 1) { self.hittable = objects[0]; + self.bbox = AABB.initAB(&self.bbox, &objects[0].boundingBox()); // std.log.info("Node.hittable = .{?}", .{self.hittable}); return; } @@ -67,6 +68,8 @@ const Node = struct { self.left = left; self.right = right; + self.combineBbox(); + // std.log.info("Node created", .{}); } @@ -90,19 +93,27 @@ const Node = struct { return @constCast(&object).hit(r, ray_t); } + var rec: ?HitRecord = null; if (self.left) |left| { - if (left.hit(r, ray_t)) |rec| { - return rec; + if (left.hit(r, ray_t)) |res| { + rec = res; } } if (self.right) |right| { - if (right.hit(r, ray_t)) |rec| { - return rec; + const interval = blk: { + if (rec) |rec_| { + break :blk IntervalF32.init(ray_t.min, rec_.t); + } + break :blk ray_t; + }; + + if (right.hit(r, interval)) |res| { + rec = res; } } - return null; + return rec; } pub fn print(self: *Node, depth: usize, side: u8) void { @@ -120,6 +131,16 @@ const Node = struct { if (self.left) |left| left.print(depth + 1, 1); if (self.right) |right| right.print(depth + 1, 2); } + + fn combineBbox(self: *Node) void { + var left = AABB{}; + var right = AABB{}; + + if (self.left) |l| left = l.bbox; + if (self.right) |r| right = r.bbox; + + self.bbox = AABB.initAB(&left, &right); + } }; allocator: std.mem.Allocator, @@ -133,7 +154,7 @@ pub fn init(allocator: std.mem.Allocator, objects: hittable.HittableList) !BVH { try root.init(allocator, objects.list.items); defer @constCast(&objects).deinit(); - root.print(0, 0); + // root.print(0, 0); return .{ .allocator = allocator, diff --git a/src/main.zig b/src/main.zig index 21c2b7c..a374997 100644 --- a/src/main.zig +++ b/src/main.zig @@ -39,7 +39,7 @@ pub fn main() !void { // Raytracing part var raytracer = try rayray.Raytracer.init(allocator, scence.world, .{ .aspect_ratio = 16.0 / 9.0, - .image_width = 400, + .image_width = 800, .samples_per_pixel = 100, .max_depth = 50, diff --git a/src/scences/in_one_weekend.zig b/src/scences/in_one_weekend.zig index 9b2e206..e42b37f 100644 --- a/src/scences/in_one_weekend.zig +++ b/src/scences/in_one_weekend.zig @@ -56,15 +56,15 @@ pub fn scene(allocator: std.mem.Allocator) !@This() { const material1 = try allocator.create(Material); material1.* = Material.dielectric(1.5); - try world.add(Hittable.sphere("One", Sphere{ .center = zm.f32x4(0, 1, 0, 0), .radius = 1, .mat = material1 })); + try world.add(Hittable.sphere("One: Dielectric", Sphere{ .center = zm.f32x4(0, 1, 0, 0), .radius = 1, .mat = material1 })); const material2 = try allocator.create(Material); material2.* = Material.lambertian(zm.f32x4(0.4, 0.2, 0.1, 1)); - try world.add(Hittable.sphere("Two", Sphere{ .center = zm.f32x4(-4, 1, 0, 0), .radius = 1, .mat = material2 })); + try world.add(Hittable.sphere("Two: Lambertian", Sphere{ .center = zm.f32x4(-4, 1, 0, 0), .radius = 1, .mat = material2 })); const material3 = try allocator.create(Material); material3.* = Material.metal(zm.f32x4(0.7, 0.6, 0.5, 1), 0); - try world.add(Hittable.sphere("Three", Sphere{ .center = zm.f32x4(4, 1, 0, 0), .radius = 1, .mat = material3 })); + try world.add(Hittable.sphere("Three: Metal", Sphere{ .center = zm.f32x4(4, 1, 0, 0), .radius = 1, .mat = material3 })); return .{ .allocator = allocator, .world = world }; }