this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Ray Tracing in One Weekend

Altagos 42cb610f 8ddfed3c

+62 -24
results/ray-tracing-in-one-weekend.png

This is a binary file and will not be displayed.

+55 -19
src/main.zig
··· 1 1 const std = @import("std"); 2 2 3 - const a = @import("a"); 3 + const aa = @import("a"); 4 4 const spall = @import("spall"); 5 5 const zm = @import("zmath"); 6 6 ··· 12 12 13 13 pub const std_options = .{ 14 14 .log_level = .debug, 15 - .logFn = a.log.logFn, 15 + .logFn = aa.log.logFn, 16 16 }; 17 17 18 18 pub fn main() !void { ··· 27 27 defer spall.deinit_thread(); 28 28 29 29 // Setting up the world 30 - var material_ground = Material.lambertian(zm.f32x4(0.8, 0.8, 0.0, 1.0)); 31 - var material_center = Material.lambertian(zm.f32x4(0.1, 0.2, 0.5, 1.0)); 32 - var material_left = Material.dielectric(1.5); 33 - var material_bubble = Material.dielectric(1.0 / 1.5); 34 - var material_right = Material.metal(zm.f32x4(0.8, 0.6, 0.2, 1.0), 1.0); 30 + var material_ground = Material.lambertian(zm.f32x4(0.5, 0.5, 0.5, 1.0)); 35 31 36 32 var world = HittableList.init(allocator); 37 - try world.add(Hittable.sphere(Sphere{ .center = zm.f32x4(0, -100.5, -1, 0), .radius = 100, .mat = &material_ground })); 38 - try world.add(Hittable.sphere(Sphere{ .center = zm.f32x4(0, 0, -1.2, 0), .radius = 0.5, .mat = &material_center })); 39 - try world.add(Hittable.sphere(Sphere{ .center = zm.f32x4(-1, 0, -1, 0), .radius = 0.5, .mat = &material_left })); 40 - try world.add(Hittable.sphere(Sphere{ .center = zm.f32x4(-1, 0, -1, 0), .radius = 0.4, .mat = &material_bubble })); 41 - try world.add(Hittable.sphere(Sphere{ .center = zm.f32x4(1, 0, -1, 0), .radius = 0.5, .mat = &material_right })); 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 })); 42 78 43 79 const s = spall.trace(@src(), "Raytracer", .{}); 44 80 45 81 // Raytracing part 46 82 var raytracer = try rayray.Raytracer.init(allocator, world, .{ 47 83 .aspect_ratio = 16.0 / 9.0, 48 - .image_width = 400, 49 - .samples_per_pixel = 100, 84 + .image_width = 1200, 85 + .samples_per_pixel = 500, 50 86 .max_depth = 50, 51 87 52 88 .vfov = 20, 53 - .look_from = zm.f32x4(-2, 2, 1, 0), 54 - .look_at = zm.f32x4(0, 0, -1, 0), 89 + .look_from = zm.f32x4(13, 2, 3, 0), 90 + .look_at = zm.f32x4(0, 0, 0, 0), 55 91 56 - .defocus_angle = 10.0, 57 - .focus_dist = 3.4, 92 + .defocus_angle = 0.6, 93 + .focus_dist = 10, 58 94 }); 59 95 defer raytracer.deinit(); 60 96 ··· 73 109 defer s_saving.end(); 74 110 75 111 try img.writeToFilePath("./out/out.png", .{ .png = .{} }); 76 - std.log.info("Image saved to: ./out/out.ong", .{}); 112 + std.log.info("Image saved to: ./out/out.png", .{}); 77 113 }
+2 -2
src/material.zig
··· 51 51 fuzz: f32, 52 52 53 53 pub fn scatter(self: *Metal, r: *Ray, rec: *hittable.HitRecord, attenuation: *zm.Vec) ?Ray { 54 - const reflected = util.reflect(zm.normalize3(r.dir), rec.normal); 55 - const scattered = Ray.init(rec.p, reflected + zm.f32x4s(self.fuzz) * util.randomUnitVec()); 54 + const reflected = util.reflect(r.dir, rec.normal); 55 + const scattered = Ray.init(rec.p, zm.normalize3(reflected) + zm.f32x4s(self.fuzz) * util.randomUnitVec()); 56 56 attenuation.* = self.albedo; 57 57 return if (zm.dot3(scattered.dir, rec.normal)[0] > 0) scattered else null; 58 58 }
+2
src/rayray.zig
··· 10 10 pub const hittable = @import("hittable.zig"); 11 11 pub const material = @import("material.zig"); 12 12 pub const tracer = @import("tracer.zig"); 13 + pub const util = @import("util.zig"); 13 14 14 15 const log = std.log.scoped(.rayray); 15 16 ··· 120 121 if (task_done and !t.marked_as_done) { 121 122 t.marked_as_done = true; 122 123 node.completeOne(); 124 + try self.camera.image.writeToFilePath("./out/out.png", .{ .png = .{} }); 123 125 } else if (!task_done) { 124 126 done = false; 125 127 }
+3 -3
src/util.zig
··· 18 18 } 19 19 20 20 pub inline fn randomVec2() zm.Vec { 21 - return zm.f32x4(randomF32, randomF32, 0, 0); 21 + return zm.f32x4(randomF32(), randomF32(), 0, 0); 22 22 } 23 23 24 24 pub inline fn randomVec3() zm.Vec { 25 - return zm.f32x4(randomF32, randomF32, randomF32, 0); 25 + return zm.f32x4(randomF32(), randomF32(), randomF32(), 0); 26 26 } 27 27 28 28 pub inline fn randomVec() zm.Vec { 29 - return zm.f32x4(randomF32, randomF32, randomF32, randomF32); 29 + return zm.f32x4(randomF32(), randomF32(), randomF32(), randomF32()); 30 30 } 31 31 32 32 pub inline fn randomVec2M(min: f32, max: f32) zm.Vec {