diff options
| author | nsfisis <nsfisis@gmail.com> | 2023-07-17 14:28:37 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2023-07-17 15:37:42 +0900 |
| commit | 29b20f94dead6b80942f7a915ca1b3a3d715229f (patch) | |
| tree | 5c47770dff61837bde6a417075f95c55bc07e0b3 | |
| parent | 0b728dd0c87e6a371aa60bec90318b38b5e2026a (diff) | |
| download | RayTracingInOneWeekend.zig-29b20f94dead6b80942f7a915ca1b3a3d715229f.tar.gz RayTracingInOneWeekend.zig-29b20f94dead6b80942f7a915ca1b3a3d715229f.tar.zst RayTracingInOneWeekend.zig-29b20f94dead6b80942f7a915ca1b3a3d715229f.zip | |
8.0
| -rw-r--r-- | src/main.zig | 10 | ||||
| -rw-r--r-- | src/rtw/hittable.zig | 52 |
2 files changed, 59 insertions, 3 deletions
diff --git a/src/main.zig b/src/main.zig index 17480c6..ee899b7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,9 +28,6 @@ const SolidTexture = rtw.texture.SolidTexture; const CheckerTexture = rtw.texture.CheckerTexture; const Hittable = rtw.hittable.Hittable; -const Sphere = rtw.hittable.Sphere; -const MovingSphere = rtw.hittable.MovingSphere; -const HittableList = rtw.hittable.HittableList; const BvhNode = rtw.hittable.BvhNode; const Aabb = rtw.aabb.Aabb; @@ -288,6 +285,8 @@ fn generateCornellBox(allocator: anytype) !Hittable { var white = try allocator.create(Material); var white2 = try allocator.create(Material); var white3 = try allocator.create(Material); + var white4 = try allocator.create(Material); + var white5 = try allocator.create(Material); var green = try allocator.create(Material); var light = try allocator.create(Material); @@ -295,6 +294,8 @@ fn generateCornellBox(allocator: anytype) !Hittable { white.* = .{ .diffuse = .{ .albedo = Texture.makeSolid(rgb(0.73, 0.73, 0.73)) } }; white2.* = .{ .diffuse = .{ .albedo = Texture.makeSolid(rgb(0.73, 0.73, 0.73)) } }; white3.* = .{ .diffuse = .{ .albedo = Texture.makeSolid(rgb(0.73, 0.73, 0.73)) } }; + white4.* = .{ .diffuse = .{ .albedo = Texture.makeSolid(rgb(0.73, 0.73, 0.73)) } }; + white5.* = .{ .diffuse = .{ .albedo = Texture.makeSolid(rgb(0.73, 0.73, 0.73)) } }; green.* = .{ .diffuse = .{ .albedo = Texture.makeSolid(rgb(0.12, 0.45, 0.15)) } }; light.* = .{ .diffuse_light = .{ .emit = Texture.makeSolid(rgb(15, 15, 15)) } }; @@ -305,6 +306,9 @@ fn generateCornellBox(allocator: anytype) !Hittable { try hittable_objects.append(.{ .xzRect = .{ .x0 = 0, .x1 = 555, .z0 = 0, .z1 = 555, .k = 555, .material = white2 } }); try hittable_objects.append(.{ .xyRect = .{ .x0 = 0, .x1 = 555, .y0 = 0, .y1 = 555, .k = 555, .material = white3 } }); + try hittable_objects.append(try Hittable.makeBox(.{ .x = 130, .y = 0, .z = 65 }, .{ .x = 295, .y = 165, .z = 230 }, white4, allocator)); + try hittable_objects.append(try Hittable.makeBox(.{ .x = 265, .y = 0, .z = 295 }, .{ .x = 430, .y = 330, .z = 460 }, white5, allocator)); + return .{ .list = .{ .objects = hittable_objects } }; } diff --git a/src/rtw/hittable.zig b/src/rtw/hittable.zig index 0283aac..a583dd8 100644 --- a/src/rtw/hittable.zig +++ b/src/rtw/hittable.zig @@ -36,6 +36,7 @@ const HittableTag = enum { xyRect, xzRect, yzRect, + box, }; pub const Hittable = union(HittableTag) { @@ -46,6 +47,11 @@ pub const Hittable = union(HittableTag) { xyRect: XyRect, xzRect: XzRect, yzRect: YzRect, + box: Box, + + pub fn makeBox(p0: Point3, p1: Point3, material: *const Material, allocator: anytype) !Hittable { + return .{ .box = try Box.init(p0, p1, material, allocator) }; + } pub fn hit(h: Hittable, r: Ray, t_min: f64, t_max: f64, record: *HitRecord) bool { return switch (h) { @@ -56,6 +62,7 @@ pub const Hittable = union(HittableTag) { HittableTag.xyRect => |rect| rect.hit(r, t_min, t_max, record), HittableTag.xzRect => |rect| rect.hit(r, t_min, t_max, record), HittableTag.yzRect => |rect| rect.hit(r, t_min, t_max, record), + HittableTag.box => |box| box.hit(r, t_min, t_max, record), }; } @@ -68,6 +75,7 @@ pub const Hittable = union(HittableTag) { HittableTag.xyRect => |rect| rect.boudingBox(time0, time1, output_box), HittableTag.xzRect => |rect| rect.boudingBox(time0, time1, output_box), HittableTag.yzRect => |rect| rect.boudingBox(time0, time1, output_box), + HittableTag.box => |box| box.boudingBox(time0, time1, output_box), }; } @@ -80,6 +88,7 @@ pub const Hittable = union(HittableTag) { HittableTag.xyRect => |rect| rect.deinit(allocator), HittableTag.xzRect => |rect| rect.deinit(allocator), HittableTag.yzRect => |rect| rect.deinit(allocator), + HittableTag.box => |box| box.deinit(allocator), }; } }; @@ -510,3 +519,46 @@ const YzRect = struct { _ = allocator; } }; + +const Box = struct { + min: Point3, + max: Point3, + sides: HittableList, + + pub fn init(p0: Point3, p1: Point3, material: *const Material, allocator: anytype) !Box { + var sides = ArrayList(Hittable).init(allocator); + + try sides.append(.{ .xyRect = .{ .x0 = p0.x, .x1 = p1.x, .y0 = p0.y, .y1 = p1.y, .k = p1.z, .material = material } }); + try sides.append(.{ .xyRect = .{ .x0 = p0.x, .x1 = p1.x, .y0 = p0.y, .y1 = p1.y, .k = p0.z, .material = material } }); + try sides.append(.{ .xzRect = .{ .x0 = p0.x, .x1 = p1.x, .z0 = p0.z, .z1 = p1.z, .k = p1.y, .material = material } }); + try sides.append(.{ .xzRect = .{ .x0 = p0.x, .x1 = p1.x, .z0 = p0.z, .z1 = p1.z, .k = p0.y, .material = material } }); + try sides.append(.{ .yzRect = .{ .y0 = p0.y, .y1 = p1.y, .z0 = p0.z, .z1 = p1.z, .k = p1.x, .material = material } }); + try sides.append(.{ .yzRect = .{ .y0 = p0.y, .y1 = p1.y, .z0 = p0.z, .z1 = p1.z, .k = p0.x, .material = material } }); + + return .{ + .min = p0, + .max = p1, + .sides = .{ + .objects = sides, + }, + }; + } + + pub fn hit(box: Box, r: Ray, t_min: f64, t_max: f64, record: *HitRecord) bool { + return box.sides.hit(r, t_min, t_max, record); + } + + pub fn boudingBox(box: Box, time0: f64, time1: f64, output_box: *Aabb) bool { + _ = time0; + _ = time1; + output_box.* = .{ + .min = box.min, + .max = box.max, + }; + return true; + } + + pub fn deinit(box: *const Box, allocator: anytype) void { + box.sides.deinit(allocator); + } +}; |
