diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.zig | 22 | ||||
| -rw-r--r-- | src/rtw/perlin.zig | 18 | ||||
| -rw-r--r-- | src/rtw/texture.zig | 12 |
3 files changed, 26 insertions, 26 deletions
diff --git a/src/main.zig b/src/main.zig index ad2273b..fa1f917 100644 --- a/src/main.zig +++ b/src/main.zig @@ -137,11 +137,11 @@ fn rayColor(r: Ray, background: Color, world: Hittable, rng: Random, depth: u32) } fn writeColor(out: anytype, c: Color, samples_per_pixel: u32) !void { - const scale = 1.0 / @intToFloat(f64, samples_per_pixel); + const scale = 1.0 / @as(f64, @floatFromInt(samples_per_pixel)); try out.print("{} {} {}\n", .{ - @floatToInt(u8, 256.0 * math.clamp(@sqrt(c.x * scale), 0.0, 0.999)), - @floatToInt(u8, 256.0 * math.clamp(@sqrt(c.y * scale), 0.0, 0.999)), - @floatToInt(u8, 256.0 * math.clamp(@sqrt(c.z * scale), 0.0, 0.999)), + @as(u8, @intFromFloat(256.0 * math.clamp(@sqrt(c.x * scale), 0.0, 0.999))), + @as(u8, @intFromFloat(256.0 * math.clamp(@sqrt(c.y * scale), 0.0, 0.999))), + @as(u8, @intFromFloat(256.0 * math.clamp(@sqrt(c.z * scale), 0.0, 0.999))), }); } @@ -204,9 +204,9 @@ fn generateRandomScene(rng: Random, allocator: anytype) !Hittable { while (b < 3) : (b += 1) { const choose_mat = randomReal01(rng); const center = Point3{ - .x = @intToFloat(f64, a) + 0.9 * randomReal01(rng), + .x = @as(f64, @floatFromInt(a)) + 0.9 * randomReal01(rng), .y = 0.2, - .z = @intToFloat(f64, b) + 0.9 * randomReal01(rng), + .z = @as(f64, @floatFromInt(b)) + 0.9 * randomReal01(rng), }; if (center.sub(.{ .x = 4, .y = 0.2, .z = 0 }).norm() <= 0.9) { @@ -309,7 +309,7 @@ fn generateCornellBox(allocator: anytype) !Hittable { pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); - defer debug.assert(!gpa.deinit()); + defer debug.assert(gpa.deinit() == .ok); var rng_ = std.rand.DefaultPrng.init(42); var rng = rng_.random(); @@ -317,7 +317,7 @@ pub fn main() !void { // Image var aspect_ratio: f64 = 3.0 / 2.0; var image_width: u32 = 600; - var image_height: u32 = @floatToInt(u32, @divTrunc(@intToFloat(f64, image_width), aspect_ratio)); + var image_height: u32 = @as(u32, @intFromFloat(@divTrunc(@as(f64, @floatFromInt(image_width)), aspect_ratio))); const max_depth = 50; var samples_per_pixel: u32 = 50; @@ -396,7 +396,7 @@ pub fn main() !void { try stdout.print("P3\n{} {}\n255\n", .{ image_width, image_height }); - var j: i32 = @intCast(i32, image_height) - 1; + var j: i32 = @as(i32, @intCast(image_height)) - 1; while (j >= 0) : (j -= 1) { std.debug.print("\rScanlines remaining: {} ", .{j}); var i: i32 = 0; @@ -404,8 +404,8 @@ pub fn main() !void { var s: u32 = 0; var pixelColor = rgb(0.0, 0.0, 0.0); while (s < samples_per_pixel) : (s += 1) { - const u = (@intToFloat(f64, i) + randomReal01(rng)) / (@intToFloat(f64, image_width) - 1.0); - const v = (@intToFloat(f64, j) + randomReal01(rng)) / (@intToFloat(f64, image_height) - 1.0); + const u = (@as(f64, @floatFromInt(i)) + randomReal01(rng)) / (@as(f64, @floatFromInt(image_width)) - 1.0); + const v = (@as(f64, @floatFromInt(j)) + randomReal01(rng)) / (@as(f64, @floatFromInt(image_height)) - 1.0); const r = camera.getRay(rng, u, v); pixelColor = pixelColor.add(rayColor(r, background, world, rng, max_depth)); } diff --git a/src/rtw/perlin.zig b/src/rtw/perlin.zig index c60bb34..39173cd 100644 --- a/src/rtw/perlin.zig +++ b/src/rtw/perlin.zig @@ -53,9 +53,9 @@ pub const Perlin = struct { const v_ = v * v * (3 - 2 * v); const w_ = w * w * (3 - 2 * w); - const i = @floatToInt(i32, @floor(p.x)); - const j = @floatToInt(i32, @floor(p.y)); - const k = @floatToInt(i32, @floor(p.z)); + const i = @as(i32, @intFromFloat(@floor(p.x))); + const j = @as(i32, @intFromFloat(@floor(p.y))); + const k = @as(i32, @intFromFloat(@floor(p.z))); var c: [2][2][2]Vec3 = undefined; @@ -65,9 +65,9 @@ pub const Perlin = struct { while (dj < 2) : (dj += 1) { var dk: usize = 0; while (dk < 2) : (dk += 1) { - const ix = @intCast(usize, i + @intCast(i32, di)) & 255; - const iy = @intCast(usize, j + @intCast(i32, dj)) & 255; - const iz = @intCast(usize, k + @intCast(i32, dk)) & 255; + const ix = @as(usize, @intCast(i + @as(i32, @intCast(di)))) & 255; + const iy = @as(usize, @intCast(j + @as(i32, @intCast(dj)))) & 255; + const iz = @as(usize, @intCast(k + @as(i32, @intCast(dk)))) & 255; c[di][dj][dk] = perlin.randomVec.items[ perlin.permX.items[ix] ^ perlin.permY.items[iy] ^ perlin.permZ.items[iz] ]; @@ -109,9 +109,9 @@ pub const Perlin = struct { while (j < 2) : (j += 1) { var k: usize = 0; while (k < 2) : (k += 1) { - const ti = @intToFloat(f64, i); - const tj = @intToFloat(f64, j); - const tk = @intToFloat(f64, k); + const ti = @as(f64, @floatFromInt(i)); + const tj = @as(f64, @floatFromInt(j)); + const tk = @as(f64, @floatFromInt(k)); const weight = Vec3{ .x = u - ti, .y = v - tj, .z = w - tk }; accum += (ti * u + (1.0 - ti) * (1.0 - u)) * diff --git a/src/rtw/texture.zig b/src/rtw/texture.zig index aabc18d..4095a9c 100644 --- a/src/rtw/texture.zig +++ b/src/rtw/texture.zig @@ -130,18 +130,18 @@ pub const ImageTexture = struct { // Clamp input texture coordinates to [0, 1] x [1, 0] const u_ = std.math.clamp(u, 0.0, 1.0); const v_ = 1.0 - std.math.clamp(v, 0.0, 1.0); // Flip v to image coordinates - const i = @floatToInt(usize, u_ * @intToFloat(f64, tx.image.width)); - const j = @floatToInt(usize, v_ * @intToFloat(f64, tx.image.height)); + const i = @as(usize, @intFromFloat(u_ * @as(f64, @floatFromInt(tx.image.width)))); + const j = @as(usize, @intFromFloat(v_ * @as(f64, @floatFromInt(tx.image.height)))); // Clamp integer mapping, since actual coordinates should be less than 1.0 const i_ = @min(i, tx.image.width - 1); const j_ = @min(j, tx.image.width - 1); const color_scale = 1.0 / 255.0; const pixels = tx.image.pixels.asBytes(); const offset = j_ * tx.image.width * 4 + i_ * 4; - const r = @intToFloat(f64, pixels[offset + 0]); - const g = @intToFloat(f64, pixels[offset + 1]); - const b = @intToFloat(f64, pixels[offset + 2]); - const a = @intToFloat(f64, pixels[offset + 3]); + const r = @as(f64, @floatFromInt(pixels[offset + 0])); + const g = @as(f64, @floatFromInt(pixels[offset + 1])); + const b = @as(f64, @floatFromInt(pixels[offset + 2])); + const a = @as(f64, @floatFromInt(pixels[offset + 3])); if (a == 0) { // Ocean return rgb(0, 0, 1.0); |
