this repo has no description

works now

+31 -10
+27 -6
src/hittable/BVH.zig
··· 34 34 35 35 if (object_span == 1) { 36 36 self.hittable = objects[0]; 37 + self.bbox = AABB.initAB(&self.bbox, &objects[0].boundingBox()); 37 38 // std.log.info("Node.hittable = .{?}", .{self.hittable}); 38 39 return; 39 40 } ··· 67 68 self.left = left; 68 69 self.right = right; 69 70 71 + self.combineBbox(); 72 + 70 73 // std.log.info("Node created", .{}); 71 74 } 72 75 ··· 90 93 return @constCast(&object).hit(r, ray_t); 91 94 } 92 95 96 + var rec: ?HitRecord = null; 93 97 if (self.left) |left| { 94 - if (left.hit(r, ray_t)) |rec| { 95 - return rec; 98 + if (left.hit(r, ray_t)) |res| { 99 + rec = res; 96 100 } 97 101 } 98 102 99 103 if (self.right) |right| { 100 - if (right.hit(r, ray_t)) |rec| { 101 - return rec; 104 + const interval = blk: { 105 + if (rec) |rec_| { 106 + break :blk IntervalF32.init(ray_t.min, rec_.t); 107 + } 108 + break :blk ray_t; 109 + }; 110 + 111 + if (right.hit(r, interval)) |res| { 112 + rec = res; 102 113 } 103 114 } 104 115 105 - return null; 116 + return rec; 106 117 } 107 118 108 119 pub fn print(self: *Node, depth: usize, side: u8) void { ··· 120 131 if (self.left) |left| left.print(depth + 1, 1); 121 132 if (self.right) |right| right.print(depth + 1, 2); 122 133 } 134 + 135 + fn combineBbox(self: *Node) void { 136 + var left = AABB{}; 137 + var right = AABB{}; 138 + 139 + if (self.left) |l| left = l.bbox; 140 + if (self.right) |r| right = r.bbox; 141 + 142 + self.bbox = AABB.initAB(&left, &right); 143 + } 123 144 }; 124 145 125 146 allocator: std.mem.Allocator, ··· 133 154 try root.init(allocator, objects.list.items); 134 155 defer @constCast(&objects).deinit(); 135 156 136 - root.print(0, 0); 157 + // root.print(0, 0); 137 158 138 159 return .{ 139 160 .allocator = allocator,
+1 -1
src/main.zig
··· 39 39 // Raytracing part 40 40 var raytracer = try rayray.Raytracer.init(allocator, scence.world, .{ 41 41 .aspect_ratio = 16.0 / 9.0, 42 - .image_width = 400, 42 + .image_width = 800, 43 43 .samples_per_pixel = 100, 44 44 .max_depth = 50, 45 45
+3 -3
src/scences/in_one_weekend.zig
··· 56 56 57 57 const material1 = try allocator.create(Material); 58 58 material1.* = Material.dielectric(1.5); 59 - try world.add(Hittable.sphere("One", Sphere{ .center = zm.f32x4(0, 1, 0, 0), .radius = 1, .mat = material1 })); 59 + try world.add(Hittable.sphere("One: Dielectric", Sphere{ .center = zm.f32x4(0, 1, 0, 0), .radius = 1, .mat = material1 })); 60 60 61 61 const material2 = try allocator.create(Material); 62 62 material2.* = Material.lambertian(zm.f32x4(0.4, 0.2, 0.1, 1)); 63 - try world.add(Hittable.sphere("Two", Sphere{ .center = zm.f32x4(-4, 1, 0, 0), .radius = 1, .mat = material2 })); 63 + try world.add(Hittable.sphere("Two: Lambertian", Sphere{ .center = zm.f32x4(-4, 1, 0, 0), .radius = 1, .mat = material2 })); 64 64 65 65 const material3 = try allocator.create(Material); 66 66 material3.* = Material.metal(zm.f32x4(0.7, 0.6, 0.5, 1), 0); 67 - try world.add(Hittable.sphere("Three", Sphere{ .center = zm.f32x4(4, 1, 0, 0), .radius = 1, .mat = material3 })); 67 + try world.add(Hittable.sphere("Three: Metal", Sphere{ .center = zm.f32x4(4, 1, 0, 0), .radius = 1, .mat = material3 })); 68 68 69 69 return .{ .allocator = allocator, .world = world }; 70 70 }