this repo has no description
0
fork

Configure Feed

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

depth-of-field

Altagos 8ddfed3c a5c4d11c

+60 -10
+49 -10
src/camera.zig
··· 17 17 aspect_ratio: f32, 18 18 samples_per_pixel: usize, 19 19 max_depth: usize, 20 + 20 21 vfov: f32 = 90, 21 22 look_from: zm.Vec = zm.f32x4s(0), 22 23 look_at: zm.Vec = zm.f32x4(0, 0, -1, 0), 23 24 vup: zm.Vec = zm.f32x4(0, 1, 0, 0), 25 + 26 + defocus_angle: f32 = 0, 27 + focus_dist: f32 = 10, 24 28 }; 25 29 26 30 image_height: usize, ··· 35 39 look_at: zm.Vec, 36 40 vup: zm.Vec, 37 41 38 - focal_lenght: f32, 42 + defocus_angle: f32, 43 + focus_dist: f32, 44 + 45 + // focal_lenght: f32, 39 46 viewport_height: f32, 40 47 viewport_width: f32, 41 48 center: zm.Vec, ··· 47 54 u: zm.Vec, 48 55 v: zm.Vec, 49 56 w: zm.Vec, 57 + defocus_disk_u: zm.Vec, 58 + defocus_disk_v: zm.Vec, 50 59 51 60 viewport_upper_left: zm.Vec, 52 61 pixel00_loc: zm.Vec, ··· 65 74 const vup = opts.vup; 66 75 const center = look_from; 67 76 68 - const focal_lenght: f32 = zm.length3(look_from - look_at)[0]; 77 + const defocus_angle = opts.defocus_angle; 78 + const focus_dist = opts.focus_dist; 79 + 80 + // const focal_lenght: f32 = zm.length3(look_from - look_at)[0]; 69 81 const theta = util.degreesToRadians(opts.vfov); 70 82 const h = @tan(theta / 2); 71 - const viewport_height: f32 = 2 * h * focal_lenght; 83 + const viewport_height: f32 = 2 * h * focus_dist; 72 84 const viewport_width = viewport_height * (@as(f32, @floatFromInt(image_width)) / @as(f32, @floatFromInt(image_height))); 73 85 74 86 const w = zm.normalize3(look_from - look_at); ··· 84 96 const pixel_delta_v = viewport_v / zm.f32x4s(@as(f32, @floatFromInt(image_height))); 85 97 86 98 // Calculate the location of the upper left pixel. 87 - const viewport_upper_left = center - zm.f32x4s(focal_lenght) * w - viewport_u / zm.f32x4s(2.0) - viewport_v / zm.f32x4s(2.0); 99 + const viewport_upper_left = center - zm.f32x4s(focus_dist) * w - viewport_u / zm.f32x4s(2.0) - viewport_v / zm.f32x4s(2.0); 88 100 const pixel00_loc = viewport_upper_left + zm.f32x4s(0.5) * (pixel_delta_u + pixel_delta_v); 89 101 90 - log.debug("image_width: {}, image_height: {}, aspect_ratio: {d:.2}, focal_lenght: {d:.1}", .{ image_width, image_height, aspect_ratio, focal_lenght }); 102 + // Calculate the camera defocus disk basis vectors. 103 + const defocus_radius = focus_dist * @tan(util.degreesToRadians(defocus_angle / 2)); 104 + const defocus_disk_u = u * zm.f32x4s(defocus_radius); 105 + const defocus_disk_v = v * zm.f32x4s(defocus_radius); 106 + 107 + // log.debug("image_width: {}, image_height: {}, aspect_ratio: {d:.2}, focal_lenght: {d:.1}", .{ 108 + // image_width, 109 + // image_height, 110 + // aspect_ratio, 111 + // focal_lenght, 112 + // }); 91 113 92 114 return Camera{ 93 115 .image_width = image_width, ··· 102 124 .look_at = look_at, 103 125 .vup = vup, 104 126 105 - .focal_lenght = focal_lenght, 127 + .defocus_angle = opts.defocus_angle, 128 + .focus_dist = opts.focus_dist, 129 + 130 + // .focal_lenght = opts.focal_lenght, 106 131 .viewport_height = viewport_height, 107 132 .viewport_width = viewport_width, 108 133 .center = center, ··· 114 139 .u = u, 115 140 .v = v, 116 141 .w = w, 142 + .defocus_disk_u = defocus_disk_u, 143 + .defocus_disk_v = defocus_disk_v, 117 144 118 145 .viewport_upper_left = viewport_upper_left, 119 146 .pixel00_loc = pixel00_loc, ··· 127 154 } 128 155 129 156 pub fn getRay(self: *Camera, i: usize, j: usize) Ray { 130 - const pixel_center = self.pixel00_loc + (zm.f32x4s(@as(f32, @floatFromInt(i))) * self.pixel_delta_u) + (zm.f32x4s(@as(f32, @floatFromInt(j))) * self.pixel_delta_v); 131 - const pixel_sample = pixel_center + self.pixelSamplesSq(); 157 + const offset = sampleSquare(); 158 + const pixel_sample = self.pixel00_loc + 159 + (zm.f32x4s(@as(f32, @floatFromInt(i)) + offset[0]) * self.pixel_delta_u) + 160 + (zm.f32x4s(@as(f32, @floatFromInt(j)) + offset[1]) * self.pixel_delta_v); 161 + 162 + const ray_orig = if (self.defocus_angle <= 0) self.center else self.defocusDiskSample(); 163 + const ray_direction = pixel_sample - ray_orig; 164 + return Ray.init(ray_orig, ray_direction); 165 + } 132 166 133 - const ray_direction = pixel_sample - self.center; 134 - return Ray.init(self.center, ray_direction); 167 + fn sampleSquare() zm.Vec { 168 + return zm.f32x4(util.randomF32() - 0.5, util.randomF32() - 0.5, 0, 0); 169 + } 170 + 171 + fn defocusDiskSample(self: *Camera) zm.Vec { 172 + const p = util.randomInUnitDisk(); 173 + return self.center + (zm.f32x4s(p[0]) * self.defocus_disk_u) + (zm.f32x4s(p[1]) * self.defocus_disk_v); 135 174 } 136 175 137 176 pub fn setPixel(self: *Camera, x: usize, y: usize, c: color.Rgba32) !void {
+4
src/main.zig
··· 48 48 .image_width = 400, 49 49 .samples_per_pixel = 100, 50 50 .max_depth = 50, 51 + 51 52 .vfov = 20, 52 53 .look_from = zm.f32x4(-2, 2, 1, 0), 53 54 .look_at = zm.f32x4(0, 0, -1, 0), 55 + 56 + .defocus_angle = 10.0, 57 + .focus_dist = 3.4, 54 58 }); 55 59 defer raytracer.deinit(); 56 60
+7
src/util.zig
··· 41 41 return zm.f32x4(randomF32M(min, max), randomF32M(min, max), randomF32M(min, max), randomF32M(min, max)); 42 42 } 43 43 44 + pub inline fn randomInUnitDisk() zm.Vec { 45 + while (true) { 46 + const p = zm.f32x4(randomF32M(-1, 1), randomF32M(-1, 1), 0, 0); 47 + if (zm.lengthSq3(p)[0] < 1.0) return p; 48 + } 49 + } 50 + 44 51 pub inline fn randomInUnitSphere() zm.Vec { 45 52 while (true) { 46 53 const p = randomVec3M(-1.0, 1.0);