this repo has no description
at e83a90656521d2026ea6d7f95f839dbf220bedbf 113 lines 4.0 kB view raw
1const std = @import("std"); 2 3const aa = @import("a"); 4const spall = @import("spall"); 5const zm = @import("zmath"); 6 7const rayray = @import("rayray"); 8const Hittable = rayray.hittable.Hittable; 9const HittableList = rayray.hittable.HittableList; 10const Material = rayray.material.Material; 11const Sphere = rayray.hittable.Sphere; 12 13pub const std_options = .{ 14 .log_level = .debug, 15 .logFn = aa.log.logFn, 16}; 17 18pub fn main() !void { 19 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); 20 defer arena.deinit(); 21 const allocator = arena.allocator(); 22 23 try spall.init("./out/trace.spall"); 24 defer spall.deinit(); 25 26 spall.init_thread(); 27 defer spall.deinit_thread(); 28 29 // Setting up the world 30 var material_ground = Material.lambertian(zm.f32x4(0.5, 0.5, 0.5, 1.0)); 31 32 var world = HittableList.init(allocator); 33 try world.add(Hittable.sphere(Sphere{ .center = zm.f32x4(0, -1000, 0, 0), .radius = 1000, .mat = &material_ground })); 34 35 var a: isize = -11; 36 while (a < 11) : (a += 1) { 37 var b: isize = -11; 38 while (b < 11) : (b += 1) { 39 const choose_mat = rayray.util.randomF32(); 40 const center = zm.f32x4( 41 @as(f32, @floatFromInt(a)) + 0.9 * rayray.util.randomF32(), 42 0.2, 43 @as(f32, @floatFromInt(b)) + 0.9 * rayray.util.randomF32(), 44 0, 45 ); 46 47 if (zm.length3(center - zm.f32x4(4, 0.2, 0, 0))[0] > 0.9) { 48 const material = try allocator.create(Material); 49 50 if (choose_mat < 0.8) { 51 // diffuse 52 const albedo = rayray.util.randomVec3() * rayray.util.randomVec3() + zm.f32x4(0, 0, 0, 1); 53 material.* = Material.lambertian(albedo); 54 try world.add(Hittable.sphere(Sphere{ .center = center, .radius = 0.2, .mat = material })); 55 } else if (choose_mat < 0.95) { 56 // metal 57 const albedo = rayray.util.randomVec3M(0.5, 1) + zm.f32x4(0, 0, 0, 1); 58 const fuzz = rayray.util.randomF32M(0, 0.5); 59 material.* = Material.metal(albedo, fuzz); 60 try world.add(Hittable.sphere(Sphere{ .center = center, .radius = 0.2, .mat = material })); 61 } else { 62 // glass 63 material.* = Material.dielectric(1.5); 64 try world.add(Hittable.sphere(Sphere{ .center = center, .radius = 0.2, .mat = material })); 65 } 66 } 67 } 68 } 69 70 var material1 = Material.dielectric(1.5); 71 try world.add(Hittable.sphere(Sphere{ .center = zm.f32x4(0, 1, 0, 0), .radius = 1, .mat = &material1 })); 72 73 var material2 = Material.lambertian(zm.f32x4(0.4, 0.2, 0.1, 1)); 74 try world.add(Hittable.sphere(Sphere{ .center = zm.f32x4(-4, 1, 0, 0), .radius = 1, .mat = &material2 })); 75 76 var material3 = Material.metal(zm.f32x4(0.7, 0.6, 0.5, 1), 0); 77 try world.add(Hittable.sphere(Sphere{ .center = zm.f32x4(4, 1, 0, 0), .radius = 1, .mat = &material3 })); 78 79 const s = spall.trace(@src(), "Raytracer", .{}); 80 81 // Raytracing part 82 var raytracer = try rayray.Raytracer.init(allocator, world, .{ 83 .aspect_ratio = 16.0 / 9.0, 84 .image_width = 1200, 85 .samples_per_pixel = 500, 86 .max_depth = 50, 87 88 .vfov = 20, 89 .look_from = zm.f32x4(13, 2, 3, 0), 90 .look_at = zm.f32x4(0, 0, 0, 0), 91 92 .defocus_angle = 0.6, 93 .focus_dist = 10, 94 }); 95 defer raytracer.deinit(); 96 97 var timer = try std.time.Timer.start(); 98 99 const img = try raytracer.render(); 100 101 const rendering_time = timer.lap(); 102 103 std.log.info("Image rendered ({}s)", .{rendering_time / std.time.ns_per_s}); 104 105 s.end(); 106 107 // Saving to file 108 const s_saving = spall.trace(@src(), "Write Image", .{}); 109 defer s_saving.end(); 110 111 try img.writeToFilePath("./out/out.png", .{ .png = .{} }); 112 std.log.info("Image saved to: ./out/out.png", .{}); 113}