From 71bfdf18c08e04fed139cdfaea4c8e81fecc7a27 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Tue, 4 Jan 2022 02:23:21 -0600 Subject: [PATCH] initial --- .gitignore | 4 ++++ build.zig | 27 +++++++++++++++++++++++++++ src/main.zig | 32 ++++++++++++++++++++++++++++++++ src/point.zig | 15 +++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 .gitignore create mode 100644 build.zig create mode 100644 src/main.zig create mode 100644 src/point.zig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7ffeddf --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +zig-cache +zig-out + +out.* diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..7b00ca5 --- /dev/null +++ b/build.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("zigtracer", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..6531f74 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,32 @@ +const std = @import("std"); + +pub fn main() anyerror!void { + const stdout = std.io.getStdOut().writer(); + const image_width = 256; + const image_height = 256; + + std.log.info("All your codebase are belong to us.", .{}); + + // PPM header + try stdout.print("P3\n{d} {d}\n255\n", .{ image_width, image_height }); + + // this is a REALLY awkward way of writing a for-loop + var j: u32 = 0; + while (j < image_height) : (j += 1) { + var i: u32 = 0; + while (i < image_width) : (i += 1) { + // doing this makes the loop bounds a bit cleaner + const j_ = image_height - 1 - j; + + const r = @intToFloat(f64, i) / (image_width - 1); + const g = @intToFloat(f64, j_) / (image_width - 1); + const b = 0.25; + + const ir = @floatToInt(u32, 255.999 * r); + const ig = @floatToInt(u32, 255.999 * g); + const ib = @floatToInt(u32, 255.999 * b); + + try stdout.print("{d} {d} {d}\n", .{ ir, ig, ib }); + } + } +} diff --git a/src/point.zig b/src/point.zig new file mode 100644 index 0000000..10f8f12 --- /dev/null +++ b/src/point.zig @@ -0,0 +1,15 @@ +const vec3 = struct { + x: f64, + y: f64, + z: f64, + + pub fn plus(self: vec3, other: vec3) vec3 { + return vec3{ + self.x + other.x, + self.y + other.y, + self.z + other.z, + }; + } +}; + +const color = vec3;