This commit is contained in:
Michael Zhang 2022-01-04 02:23:21 -06:00
commit 71bfdf18c0
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
4 changed files with 78 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
zig-cache
zig-out
out.*

27
build.zig Normal file
View file

@ -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);
}

32
src/main.zig Normal file
View file

@ -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 });
}
}
}

15
src/point.zig Normal file
View file

@ -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;