From f434c300c730124b5b4032dd6d205a10922f06ef Mon Sep 17 00:00:00 2001 From: Altagos Date: Mon, 22 Jul 2024 11:29:48 +0200 Subject: [PATCH] small changes --- src/BVH.zig | 27 ++++++++++----------------- src/hittable.zig | 4 ++-- src/hittable/sphere.zig | 30 ++++++++++++++++++++---------- src/scences/in_one_weekend.zig | 12 ++++++------ 4 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/BVH.zig b/src/BVH.zig index a3384f0..7ceb201 100644 --- a/src/BVH.zig +++ b/src/BVH.zig @@ -8,6 +8,8 @@ const IntervalF32 = @import("interval.zig").IntervalF32; const Ray = @import("Ray.zig"); const util = @import("util.zig"); +const log = std.log.scoped(.BVH); + pub const BVH = @This(); const Ast = struct { @@ -41,11 +43,11 @@ const Leaf = struct { objects: []Hittable, bbox: AABB, - pub inline fn hit(self: *Leaf, r: *Ray, ray_t: IntervalF32) ?HitRecord { + pub fn hit(self: *Leaf, r: *Ray, ray_t: IntervalF32) ?HitRecord { var rec: ?HitRecord = null; var interval = ray_t; for (self.objects) |obj| { - if (@constCast(&obj).hit(r, interval)) |res| { + if (obj.hit(r, interval)) |res| { interval = IntervalF32.init(ray_t.min, res.t); rec = res; } @@ -123,11 +125,8 @@ const Node = union(enum) { } } - pub inline fn bbox(self: *Node) AABB { - switch (self.*) { - .ast => |*a| return a.bbox, - .leaf => |*l| return l.bbox, - } + pub fn bbox(self: *Node) AABB { + return self.bbox; } pub inline fn hit(self: *Node, r: *Ray, ray_t: IntervalF32) ?HitRecord { @@ -197,13 +196,13 @@ bbox: AABB, pub fn init(allocator: std.mem.Allocator, objects: hittable.HittableList, max_depth: usize) !BVH { defer @constCast(&objects).deinit(); - std.log.info("Creating BVH Tree with {} objects", .{objects.list.items.len}); + log.info("Creating BVH Tree with {} objects", .{objects.list.items.len}); const root = try allocator.create(Node); try root.init(allocator, objects.list.items, max_depth, 0); const bbox = root.recomputeBbox(); - std.log.debug("Reached depth of: {}, max objects: {}", .{ reached_depth, max_objects }); + log.debug("Reached depth of: {}, max objects: {}", .{ reached_depth, max_objects }); // root.print(0, 0); return .{ @@ -218,17 +217,11 @@ pub fn deinit(self: *BVH) void { } pub inline fn hit(self: *BVH, r: *Ray, ray_t: IntervalF32) ?HitRecord { - if (self.bbox.hit(r, ray_t)) { - return self.root.hit(r, ray_t); - } - - return null; + return self.root.hit(r, ray_t); } inline fn boxCompare(a: *Hittable, b: *Hittable, axis_index: i32) bool { - const a_axis_interval = a.boundingBox().axisInterval(axis_index); - const b_axis_interval = b.boundingBox().axisInterval(axis_index); - return a_axis_interval.min < b_axis_interval.min; + return a.boundingBox().axisInterval(axis_index).min < b.boundingBox().axisInterval(axis_index).min; } fn boxXCompare(_: @TypeOf(.{}), a: Hittable, b: Hittable) bool { diff --git a/src/hittable.zig b/src/hittable.zig index cc91d29..aad4551 100644 --- a/src/hittable.zig +++ b/src/hittable.zig @@ -33,7 +33,7 @@ pub const Hittable = union(enum) { pub fn boundingBox(self: *Hittable) AABB { switch (self.*) { - inline else => |*n| return n[0].boundingBox(), + inline else => |*n| return n[0].bbox, } } @@ -43,7 +43,7 @@ pub const Hittable = union(enum) { } } - pub inline fn hit(self: *Hittable, r: *Ray, ray_t: IntervalF32) ?HitRecord { + pub fn hit(self: *const Hittable, r: *Ray, ray_t: IntervalF32) ?HitRecord { switch (self.*) { inline else => |*n| return n[0].hit(r, ray_t), } diff --git a/src/hittable/sphere.zig b/src/hittable/sphere.zig index 0348726..5a74e39 100644 --- a/src/hittable/sphere.zig +++ b/src/hittable/sphere.zig @@ -13,7 +13,17 @@ radius: f32, mat: *Material, is_moving: bool = false, center_vec: zm.Vec = zm.f32x4s(0), -bbox: ?AABB = null, +bbox: AABB, + +pub fn init(center: zm.Vec, radius: f32, mat: *Material) Sphere { + const rvec = zm.f32x4s(radius); + return Sphere{ + .center = center, + .radius = @max(0, radius), + .mat = mat, + .bbox = AABB.initP(center - rvec, center + rvec), + }; +} pub fn initMoving(center1: zm.Vec, center2: zm.Vec, radius: f32, mat: *Material) Sphere { const rvec = zm.f32x4s(radius); @@ -31,16 +41,16 @@ pub fn initMoving(center1: zm.Vec, center2: zm.Vec, radius: f32, mat: *Material) } pub inline fn boundingBox(self: *Sphere) AABB { - if (self.bbox) |bbox| { - return bbox; - } else { - const rvec = zm.f32x4s(self.radius); - self.bbox = AABB.initP(self.center - rvec, self.center + rvec); - return self.bbox.?; - } + // if (self.bbox) |bbox| { + return self.bbox; + // } else { + // const rvec = zm.f32x4s(self.radius); + // self.bbox = AABB.initP(self.center - rvec, self.center + rvec); + // return self.bbox.?; + // } } -pub inline fn hit(self: *Sphere, r: *Ray, ray_t: IntervalF32) ?HitRecord { +pub fn hit(self: *const Sphere, r: *Ray, ray_t: IntervalF32) ?HitRecord { const center = blk: { if (self.is_moving) { break :blk self.sphereCenter(r.tm); @@ -77,6 +87,6 @@ pub inline fn hit(self: *Sphere, r: *Ray, ray_t: IntervalF32) ?HitRecord { return rec; } -pub inline fn sphereCenter(self: *Sphere, time: f32) zm.Vec { +pub inline fn sphereCenter(self: *const Sphere, time: f32) zm.Vec { return self.center + zm.f32x4s(time) * self.center_vec; } diff --git a/src/scences/in_one_weekend.zig b/src/scences/in_one_weekend.zig index 18c6929..b025c44 100644 --- a/src/scences/in_one_weekend.zig +++ b/src/scences/in_one_weekend.zig @@ -15,7 +15,7 @@ pub fn scene(allocator: std.mem.Allocator) !@This() { const material_ground = try allocator.create(Material); material_ground.* = Material.lambertian(zm.f32x4(0.5, 0.5, 0.5, 1.0)); - try world.add(Hittable.sphere("Ground", Sphere{ .center = zm.f32x4(0, -1000, 0, 0), .radius = 1000, .mat = material_ground })); + try world.add(Hittable.sphere("Ground", Sphere.init(zm.f32x4(0, -1000, 0, 0), 1000, material_ground))); const a_max = 50; const b_max = 50; @@ -47,11 +47,11 @@ pub fn scene(allocator: std.mem.Allocator) !@This() { const albedo = rayray.util.randomVec3M(0.5, 1) + zm.f32x4(0, 0, 0, 1); const fuzz = rayray.util.randomF32M(0, 0.5); material.* = Material.metal(albedo, fuzz); - try world.add(Hittable.sphere("Metal", Sphere{ .center = center, .radius = 0.2, .mat = material })); + try world.add(Hittable.sphere("Metal", Sphere.init(center, 0.2, material))); } else { // glass material.* = Material.dielectric(1.5); - try world.add(Hittable.sphere("Dielectric", Sphere{ .center = center, .radius = 0.2, .mat = material })); + try world.add(Hittable.sphere("Dielectric", Sphere.init(center, 0.2, material))); } } } @@ -63,13 +63,13 @@ pub fn scene(allocator: std.mem.Allocator) !@This() { const material2 = try allocator.create(Material); material2.* = Material.lambertian(zm.f32x4(0.4, 0.2, 0.1, 1)); - try world.add(Hittable.sphere("Two: Lambertian", Sphere{ .center = zm.f32x4(-4, 1, 0, 0), .radius = 1, .mat = material1 })); + try world.add(Hittable.sphere("Two: Lambertian", Sphere.init(zm.f32x4(-4, 1, 0, 0), 1, material1))); 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: Metal", Sphere{ .center = zm.f32x4(4, 1, 0, 0), .radius = 1, .mat = material3 })); + try world.add(Hittable.sphere("Three: Metal", Sphere.init(zm.f32x4(4, 1, 0, 0), 1, material3))); - try world.add(Hittable.sphere("One: Dielectric", Sphere{ .center = zm.f32x4(0, 1, 0, 0), .radius = 1, .mat = material2 })); + try world.add(Hittable.sphere("One: Dielectric", Sphere.init(zm.f32x4(0, 1, 0, 0), 1, material2))); return .{ .allocator = allocator, .world = world }; } -- 2.51.2