diff options
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 65 |
1 files changed, 33 insertions, 32 deletions
diff --git a/src/main.zig b/src/main.zig index 19e48b6..060c6ee 100644 --- a/src/main.zig +++ b/src/main.zig @@ -123,7 +123,7 @@ fn rayColor(r: Ray, background: Color, world: Hittable, rng: Random, depth: u32) fn generateTwoSpheres(rng: Random, allocator: anytype) !Hittable { _ = rng; - var hittable_objects = std.ArrayList(Hittable).init(allocator); + var hittable_objects = try std.ArrayList(Hittable).initCapacity(allocator, 2); const checker = try Texture.makeChecker(allocator, rgb(0.2, 0.3, 0.1), rgb(0.9, 0.9, 0.9)); const mat1 = try allocator.create(Material); @@ -132,14 +132,14 @@ fn generateTwoSpheres(rng: Random, allocator: anytype) !Hittable { mat1.* = .{ .diffuse = .{ .albedo = checker } }; mat2.* = .{ .diffuse = .{ .albedo = checker } }; - try hittable_objects.append(makeSphere(.{ .x = 0, .y = -10, .z = 0 }, 10, mat1)); - try hittable_objects.append(makeSphere(.{ .x = 0, .y = 10, .z = 0 }, 10, mat2)); + try hittable_objects.append(allocator, makeSphere(.{ .x = 0, .y = -10, .z = 0 }, 10, mat1)); + try hittable_objects.append(allocator, makeSphere(.{ .x = 0, .y = 10, .z = 0 }, 10, mat2)); return .{ .list = .{ .objects = hittable_objects } }; } fn generateTwoPerlinSpheres(rng: Random, allocator: anytype) !Hittable { - var hittable_objects = std.ArrayList(Hittable).init(allocator); + var hittable_objects = try std.ArrayList(Hittable).initCapacity(allocator, 2); const perlin = try Texture.makeNoise(allocator, 4.0, rng); const mat1 = try allocator.create(Material); @@ -148,14 +148,14 @@ fn generateTwoPerlinSpheres(rng: Random, allocator: anytype) !Hittable { mat1.* = .{ .diffuse = .{ .albedo = perlin } }; mat2.* = .{ .diffuse = .{ .albedo = perlin } }; - try hittable_objects.append(makeSphere(.{ .x = 0, .y = -1000, .z = 0 }, 1000, mat1)); - try hittable_objects.append(makeSphere(.{ .x = 0, .y = 2, .z = 0 }, 2, mat2)); + try hittable_objects.append(allocator, makeSphere(.{ .x = 0, .y = -1000, .z = 0 }, 1000, mat1)); + try hittable_objects.append(allocator, makeSphere(.{ .x = 0, .y = 2, .z = 0 }, 2, mat2)); return .{ .list = .{ .objects = hittable_objects } }; } fn generateRandomScene(rng: Random, allocator: anytype) !Hittable { - var hittable_objects = std.ArrayList(Hittable).init(allocator); + var hittable_objects = try std.ArrayList(Hittable).initCapacity(allocator, 4); const mat_ground = try allocator.create(Material); const mat1 = try allocator.create(Material); @@ -169,10 +169,10 @@ fn generateRandomScene(rng: Random, allocator: anytype) !Hittable { mat2.* = .{ .diffuse = .{ .albedo = .{ .solid = .{ .color = rgb(0.4, 0.2, 0.1) } } } }; mat3.* = .{ .metal = .{ .albedo = rgb(0.7, 0.6, 0.5), .fuzz = 0.0 } }; - try hittable_objects.append(makeSphere(.{ .x = 0, .y = -1000, .z = 0 }, 1000, mat_ground)); - try hittable_objects.append(makeSphere(.{ .x = 0, .y = 1, .z = 0 }, 1.0, mat1)); - try hittable_objects.append(makeSphere(.{ .x = -4, .y = 1, .z = 0 }, 1.0, mat2)); - try hittable_objects.append(makeSphere(.{ .x = 4, .y = 1, .z = 0 }, 1.0, mat3)); + try hittable_objects.append(allocator, makeSphere(.{ .x = 0, .y = -1000, .z = 0 }, 1000, mat_ground)); + try hittable_objects.append(allocator, makeSphere(.{ .x = 0, .y = 1, .z = 0 }, 1.0, mat1)); + try hittable_objects.append(allocator, makeSphere(.{ .x = -4, .y = 1, .z = 0 }, 1.0, mat2)); + try hittable_objects.append(allocator, makeSphere(.{ .x = 4, .y = 1, .z = 0 }, 1.0, mat3)); var a: i32 = -3; while (a < 3) : (a += 1) { @@ -195,7 +195,7 @@ fn generateRandomScene(rng: Random, allocator: anytype) !Hittable { const albedo = Color.random01(rng).mulV(Color.random01(rng)); mat_sphere.* = .{ .diffuse = .{ .albedo = .{ .solid = .{ .color = albedo } } } }; const center1 = center.add(.{ .x = 0, .y = randomReal(rng, 0, 0.5), .z = 0 }); - try hittable_objects.append(.{ .movingSphere = .{ + try hittable_objects.append(allocator, .{ .movingSphere = .{ .center0 = center, .center1 = center1, .time0 = 0, @@ -208,11 +208,11 @@ fn generateRandomScene(rng: Random, allocator: anytype) !Hittable { const albedo = Color.random(rng, 0.5, 1); const fuzz = randomReal(rng, 0, 0.5); mat_sphere.* = .{ .metal = .{ .albedo = albedo, .fuzz = fuzz } }; - try hittable_objects.append(makeSphere(center, 0.2, mat_sphere)); + try hittable_objects.append(allocator, makeSphere(center, 0.2, mat_sphere)); } else { // glass mat_sphere.* = .{ .dielectric = .{ .ir = 1.5 } }; - try hittable_objects.append(makeSphere(center, 0.2, mat_sphere)); + try hittable_objects.append(allocator, makeSphere(center, 0.2, mat_sphere)); } } } @@ -221,20 +221,20 @@ fn generateRandomScene(rng: Random, allocator: anytype) !Hittable { } fn generateEarthScene(allocator: anytype) !Hittable { - var hittable_objects = std.ArrayList(Hittable).init(allocator); + var hittable_objects = try std.ArrayList(Hittable).initCapacity(allocator, 1); const earth_texture = try Texture.makeImage(allocator, "assets/sekaichizu.png"); const earth_surface = try allocator.create(Material); earth_surface.* = .{ .diffuse = .{ .albedo = earth_texture } }; - try hittable_objects.append(makeSphere(.{ .x = 0, .y = 0, .z = 0 }, 2, earth_surface)); + try hittable_objects.append(allocator, makeSphere(.{ .x = 0, .y = 0, .z = 0 }, 2, earth_surface)); return .{ .list = .{ .objects = hittable_objects } }; } fn generateSimpleLightScene(rng: Random, allocator: anytype) !Hittable { - var hittable_objects = std.ArrayList(Hittable).init(allocator); + var hittable_objects = try std.ArrayList(Hittable).initCapacity(allocator, 3); const perlin = try Texture.makeNoise(allocator, 4.0, rng); const mat1 = try allocator.create(Material); @@ -243,21 +243,21 @@ fn generateSimpleLightScene(rng: Random, allocator: anytype) !Hittable { mat1.* = .{ .diffuse = .{ .albedo = perlin } }; mat2.* = .{ .diffuse = .{ .albedo = perlin } }; - try hittable_objects.append(makeSphere(.{ .x = 0, .y = -1000, .z = 0 }, 1000, mat1)); - try hittable_objects.append(makeSphere(.{ .x = 0, .y = 2, .z = 0 }, 2, mat2)); + try hittable_objects.append(allocator, makeSphere(.{ .x = 0, .y = -1000, .z = 0 }, 1000, mat1)); + try hittable_objects.append(allocator, makeSphere(.{ .x = 0, .y = 2, .z = 0 }, 2, mat2)); const light = Texture.makeSolid(rgb(4, 4, 4)); var mat3 = try Rc(Material).init(allocator); mat3.get_mut().* = .{ .diffuse_light = .{ .emit = light } }; - try hittable_objects.append(.{ .xyRect = .{ .x0 = 3.0, .x1 = 5.0, .y0 = 1.0, .y1 = 3.0, .k = -2.0, .material = mat3 } }); + try hittable_objects.append(allocator, .{ .xyRect = .{ .x0 = 3.0, .x1 = 5.0, .y0 = 1.0, .y1 = 3.0, .k = -2.0, .material = mat3 } }); return .{ .list = .{ .objects = hittable_objects } }; } fn generateCornellBox(allocator: anytype) !Hittable { - var hittable_objects = std.ArrayList(Hittable).init(allocator); + var hittable_objects = try std.ArrayList(Hittable).initCapacity(allocator, 8); var red = try Rc(Material).init(allocator); var white = try Rc(Material).init(allocator); @@ -269,12 +269,12 @@ fn generateCornellBox(allocator: anytype) !Hittable { green.get_mut().* = .{ .diffuse = .{ .albedo = Texture.makeSolid(rgb(0.12, 0.45, 0.15)) } }; light.get_mut().* = .{ .diffuse_light = .{ .emit = Texture.makeSolid(rgb(15, 15, 15)) } }; - try hittable_objects.append(.{ .yzRect = .{ .y0 = 0, .y1 = 555, .z0 = 0, .z1 = 555, .k = 555, .material = green } }); - try hittable_objects.append(.{ .yzRect = .{ .y0 = 0, .y1 = 555, .z0 = 0, .z1 = 555, .k = 0, .material = red } }); - try hittable_objects.append(.{ .xzRect = .{ .x0 = 213, .x1 = 343, .z0 = 227, .z1 = 332, .k = 554, .material = light } }); - try hittable_objects.append(.{ .xzRect = .{ .x0 = 0, .x1 = 555, .z0 = 0, .z1 = 555, .k = 0, .material = white } }); - try hittable_objects.append(.{ .xzRect = .{ .x0 = 0, .x1 = 555, .z0 = 0, .z1 = 555, .k = 555, .material = white.clone() } }); - try hittable_objects.append(.{ .xyRect = .{ .x0 = 0, .x1 = 555, .y0 = 0, .y1 = 555, .k = 555, .material = white.clone() } }); + try hittable_objects.append(allocator, .{ .yzRect = .{ .y0 = 0, .y1 = 555, .z0 = 0, .z1 = 555, .k = 555, .material = green } }); + try hittable_objects.append(allocator, .{ .yzRect = .{ .y0 = 0, .y1 = 555, .z0 = 0, .z1 = 555, .k = 0, .material = red } }); + try hittable_objects.append(allocator, .{ .xzRect = .{ .x0 = 213, .x1 = 343, .z0 = 227, .z1 = 332, .k = 554, .material = light } }); + try hittable_objects.append(allocator, .{ .xzRect = .{ .x0 = 0, .x1 = 555, .z0 = 0, .z1 = 555, .k = 0, .material = white } }); + try hittable_objects.append(allocator, .{ .xzRect = .{ .x0 = 0, .x1 = 555, .z0 = 0, .z1 = 555, .k = 555, .material = white.clone() } }); + try hittable_objects.append(allocator, .{ .xyRect = .{ .x0 = 0, .x1 = 555, .y0 = 0, .y1 = 555, .k = 555, .material = white.clone() } }); var box1 = try Rc(Hittable).init(allocator); var box1r = try Rc(Hittable).init(allocator); @@ -283,11 +283,11 @@ fn generateCornellBox(allocator: anytype) !Hittable { box1.get_mut().* = try Hittable.makeBox(.{ .x = 0, .y = 0, .z = 0 }, .{ .x = 165, .y = 330, .z = 165 }, white.clone(), allocator); box1r.get_mut().* = Hittable.makeRotateY(box1, deg2rad(15)); - try hittable_objects.append(Hittable.makeTranslate(box1r, .{ .x = 265, .y = 0, .z = 295 })); + try hittable_objects.append(allocator, Hittable.makeTranslate(box1r, .{ .x = 265, .y = 0, .z = 295 })); box2.get_mut().* = try Hittable.makeBox(.{ .x = 0, .y = 0, .z = 0 }, .{ .x = 165, .y = 165, .z = 165 }, white.clone(), allocator); box2r.get_mut().* = Hittable.makeRotateY(box2, deg2rad(-18)); - try hittable_objects.append(Hittable.makeTranslate(box2r, .{ .x = 130, .y = 0, .z = 65 })); + try hittable_objects.append(allocator, Hittable.makeTranslate(box2r, .{ .x = 130, .y = 0, .z = 65 })); return .{ .list = .{ .objects = hittable_objects } }; } @@ -360,7 +360,7 @@ pub fn main() !void { image_height = 600; samples_per_pixel = 200; } - defer world.deinit(); + defer world.deinit(allocator); // Camera const camera = Camera.init( @@ -377,7 +377,7 @@ pub fn main() !void { // Render var image = try Image.create(allocator, image_width, image_height, PixelFormat.rgb24); - defer image.deinit(); + defer image.deinit(allocator); var j: u32 = 0; while (j < image_height) : (j += 1) { @@ -402,5 +402,6 @@ pub fn main() !void { } // Output - try image.writeToFilePath("out.png", .{ .png = .{} }); + var write_buf: [zigimg.io.DEFAULT_BUFFER_SIZE]u8 = undefined; + try image.writeToFilePath(allocator, "out.png", write_buf[0..], .{ .png = .{} }); } |
