From ec8102db3ad6bd57f3ee1ae701ad306a960f5b38 Mon Sep 17 00:00:00 2001 From: Altagos Date: Fri, 1 Mar 2024 11:14:39 +0100 Subject: [PATCH] cleanup --- build.zig | 22 +++++----------------- src/hittable.zig | 2 ++ src/interval.zig | 10 ++++++++++ src/main.zig | 6 +++++- src/rayray.zig | 1 - src/renderer.zig | 20 +++++++------------- 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/build.zig b/build.zig index 657e216..8cadec5 100644 --- a/build.zig +++ b/build.zig @@ -28,14 +28,6 @@ pub fn build(b: *std.Build) void { exe.root_module.addImport("spall", spall.module("spall")); exe.root_module.addImport("rayray", rayray); - const alib = b.dependency("a", .{ - .target = target, - .optimize = optimize, - .log_ignore_default = true, - }); - - exe.root_module.addImport("a", alib.module("a")); - b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); @@ -47,18 +39,14 @@ pub fn build(b: *std.Build) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - - const lib_unit_tests = b.addTest(.{ - .root_source_file = .{ .path = "src/root.zig" }, - .target = target, - .optimize = optimize, - }); - const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&run_lib_unit_tests.step); } fn addDeps(b: *std.Build, module: *std.Build.Module) void { + const alib = b.dependency("a", .{ + .log_ignore_default = true, + }); + module.addImport("a", alib.module("a")); + const zmath = b.dependency("zmath", .{ .enable_cross_platform_determinism = true, }); diff --git a/src/hittable.zig b/src/hittable.zig index cedb0d2..38620cc 100644 --- a/src/hittable.zig +++ b/src/hittable.zig @@ -4,6 +4,8 @@ const zm = @import("zmath"); const IntervalF32 = @import("interval.zig").IntervalF32; const Ray = @import("ray.zig"); + +// Hittable Objects pub const Sphere = @import("hittable/sphere.zig"); pub const HitRecord = struct { diff --git a/src/interval.zig b/src/interval.zig index 4a1e061..9d39605 100644 --- a/src/interval.zig +++ b/src/interval.zig @@ -54,6 +54,16 @@ pub fn Interval(comptime T: type) type { return self.current; } else return null; } + + pub fn nextInc(self: *Iterator) ?T { + self.current += 1; + return if (self.current <= self.interval.max) self.current else null; + } + + pub fn nextExc(self: *Iterator) ?T { + self.current += 1; + return if (self.current < self.interval.max) self.current else null; + } }; min: T, diff --git a/src/main.zig b/src/main.zig index 7a2fbae..9e1630d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -25,12 +25,14 @@ pub fn main() !void { spall.init_thread(); defer spall.deinit_thread(); + // Setting up the world var world = HittableList.init(allocator); try world.add(Hittable.initSphere(Sphere{ .center = zm.f32x4(0, 0, -1, 0), .radius = 0.5 })); try world.add(Hittable.initSphere(Sphere{ .center = zm.f32x4(0, -100.5, -1, 0), .radius = 100 })); - const s = spall.trace(@src(), "Main", .{}); + const s = spall.trace(@src(), "Raytracer", .{}); + // Raytracing part var raytracer = try rayray.Raytracer.init(allocator, world); defer raytracer.deinit(); @@ -39,8 +41,10 @@ pub fn main() !void { s.end(); + // Saving to file const s_saving = spall.trace(@src(), "Write Image", .{}); defer s_saving.end(); + try img.writeToFilePath("./out/out.png", .{ .png = .{} }); std.log.info("Image saved to: out/out.ong", .{}); } diff --git a/src/rayray.zig b/src/rayray.zig index 3093db9..4c9b1d2 100644 --- a/src/rayray.zig +++ b/src/rayray.zig @@ -3,7 +3,6 @@ const std = @import("std"); const spall = @import("spall"); const zigimg = @import("zigimg"); const color = zigimg.color; -const zm = @import("zmath"); pub const Camera = @import("camera.zig"); pub const hittable = @import("hittable.zig"); diff --git a/src/renderer.zig b/src/renderer.zig index aa5b1b1..e094513 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -2,15 +2,15 @@ const std = @import("std"); const spall = @import("spall"); const zigimg = @import("zigimg"); -const color = zigimg.color; const zm = @import("zmath"); pub const Camera = @import("camera.zig"); +pub const hittable = @import("hittable.zig"); +pub const Ray = @import("ray.zig"); + pub const interval = @import("interval.zig"); pub const IntervalUsize = interval.IntervalUsize; pub const IntervalF32 = interval.IntervalF32; -pub const hittable = @import("hittable.zig"); -pub const Ray = @import("ray.zig"); const log = std.log.scoped(.renderer); @@ -31,17 +31,11 @@ pub fn rayColor(r: *Ray, world: *hittable.HittableList) zm.Vec { pub fn run(ctx: Context, height: IntervalUsize, width: IntervalUsize) void { var height_iter = height.iter(); - height_iter.upper_boundry = .inclusive; - - while (height_iter.next()) |j| { + while (height_iter.nextInc()) |j| { if (j >= ctx.cam.image_height) break; var width_iter = width.iter(); - height_iter.upper_boundry = .inclusive; - - while (width_iter.next()) |i| inner: { - if (i >= ctx.cam.image_width) break :inner; - + while (width_iter.nextExc()) |i| { const pixel_center = ctx.cam.pixel00_loc + (zm.f32x4s(@as(f32, @floatFromInt(i))) * ctx.cam.pixel_delta_u) + (zm.f32x4s(@as(f32, @floatFromInt(j))) * ctx.cam.pixel_delta_v); const ray_direction = pixel_center - ctx.cam.camera_center; var ray = Ray.init(ctx.cam.camera_center, ray_direction); @@ -69,11 +63,11 @@ pub fn renderThread(ctx: Context, done: *std.atomic.Value(bool), row: usize, row done.store(true, .Release); } -fn vecToRgba(v: zm.Vec) color.Rgba32 { +fn vecToRgba(v: zm.Vec) zigimg.color.Rgba32 { const r: u8 = @intFromFloat(255.999 * v[0]); const g: u8 = @intFromFloat(255.999 * v[1]); const b: u8 = @intFromFloat(255.999 * v[2]); const a: u8 = @intFromFloat(255.999 * v[3]); - return color.Rgba32.initRgba(r, g, b, a); + return zigimg.color.Rgba32.initRgba(r, g, b, a); } -- 2.51.2