aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-08-19 22:21:12 +0900
committernsfisis <nsfisis@gmail.com>2023-08-19 22:21:30 +0900
commitf5356b5023d80e3e5b58bb00f3b23ddcb8554199 (patch)
tree6332d1ce695f3bdf81fd4c5723b323056d57505d
parentb8d5f7cc926c19f87884f8506a925b86c53c99c1 (diff)
downloadRayTracingInOneWeekend.zig-f5356b5023d80e3e5b58bb00f3b23ddcb8554199.tar.gz
RayTracingInOneWeekend.zig-f5356b5023d80e3e5b58bb00f3b23ddcb8554199.tar.zst
RayTracingInOneWeekend.zig-f5356b5023d80e3e5b58bb00f3b23ddcb8554199.zip
feat: change output format to PNG
-rw-r--r--.gitignore2
-rw-r--r--Makefile4
-rw-r--r--src/main.zig38
3 files changed, 21 insertions, 23 deletions
diff --git a/.gitignore b/.gitignore
index 919b114..1f79df4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,3 @@
/zig-cache
/zig-out
-/out.ppm
+/out.png
diff --git a/Makefile b/Makefile
index 9006a27..3eb994f 100644
--- a/Makefile
+++ b/Makefile
@@ -2,8 +2,8 @@ BUILDFLAGS := -Doptimize=ReleaseFast
.PHONY: all
all: fmt
- @rm -f out.ppm
- @zig build ${BUILDFLAGS} run > out.ppm
+ @rm -f out.png
+ @zig build ${BUILDFLAGS} run
.PHONY: build
build: fmt
diff --git a/src/main.zig b/src/main.zig
index fa1f917..75f3161 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -3,6 +3,10 @@ const debug = std.debug;
const math = std.math;
const ArrayList = std.ArrayList;
+const zigimg = @import("zigimg");
+const Image = zigimg.Image;
+const PixelFormat = zigimg.PixelFormat;
+
const Rc = @import("rc.zig").Rc;
const rtw = @import("rtw.zig");
@@ -136,15 +140,6 @@ 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 / @as(f64, @floatFromInt(samples_per_pixel));
- try out.print("{} {} {}\n", .{
- @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))),
- });
-}
-
fn generateTwoSpheres(rng: Random, allocator: anytype) !Hittable {
_ = rng;
var hittable_objects = ArrayList(Hittable).init(allocator);
@@ -390,16 +385,13 @@ pub fn main() !void {
);
// Render
- const stdout_file = std.io.getStdOut().writer();
- var bw = std.io.bufferedWriter(stdout_file);
- const stdout = bw.writer();
+ var image = try Image.create(allocator, image_width, image_height, PixelFormat.rgb24);
+ defer image.deinit();
- try stdout.print("P3\n{} {}\n255\n", .{ image_width, image_height });
-
- var j: i32 = @as(i32, @intCast(image_height)) - 1;
- while (j >= 0) : (j -= 1) {
- std.debug.print("\rScanlines remaining: {} ", .{j});
- var i: i32 = 0;
+ var j: u32 = 0;
+ while (j < image_height) : (j += 1) {
+ std.debug.print("\rScanlines remaining: {} ", .{image_height - j - 1});
+ var i: u32 = 0;
while (i < image_width) : (i += 1) {
var s: u32 = 0;
var pixelColor = rgb(0.0, 0.0, 0.0);
@@ -409,9 +401,15 @@ pub fn main() !void {
const r = camera.getRay(rng, u, v);
pixelColor = pixelColor.add(rayColor(r, background, world, rng, max_depth));
}
- try writeColor(stdout, pixelColor, samples_per_pixel);
+ const scale = 1.0 / @as(f64, @floatFromInt(samples_per_pixel));
+ image.pixels.rgb24[i + (image_height - j - 1) * image_width] = .{
+ .r = @intFromFloat(256.0 * math.clamp(@sqrt(pixelColor.x * scale), 0.0, 0.999)),
+ .g = @intFromFloat(256.0 * math.clamp(@sqrt(pixelColor.y * scale), 0.0, 0.999)),
+ .b = @intFromFloat(256.0 * math.clamp(@sqrt(pixelColor.z * scale), 0.0, 0.999)),
+ };
}
}
- try bw.flush();
+ // Output
+ try image.writeToFilePath("out.png", .{ .png = .{} });
}