summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2022-12-24 18:31:35 +0100
committerTom Barrett <tom@tombarrett.xyz>2022-12-24 18:31:35 +0100
commit39b57fa0729d16873193947b12e59a7da52695fa (patch)
tree2c10fa22ab5f27e4db80732aa55166e42ffc7de6 /src/main.zig
parent8ab21b7ec6bd83fe12b48534e4f670500e8d5e2c (diff)
more ziglike
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig37
1 files changed, 20 insertions, 17 deletions
diff --git a/src/main.zig b/src/main.zig
index e2b63d2..5f6ea29 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -2,28 +2,18 @@ const std = @import("std");
const warn = @import("std").debug.print;
const vec = @import("vec.zig");
-pub fn ray_color(r: ray) vec.vec {
- if (hit_sphere(vec.init(0, 0, -1), 0.5, r)) |t| {
- const n = vec.unit_vector(r.origin + vec.scalar(t) * r.direction - vec.init(0, 0, -1));
- return vec.scalar(0.5) * vec.init(n[0] + 1, n[1] + 1, n[2] + 1);
- }
- const direction = vec.unit_vector(r.direction);
- const t = 0.5 * (direction[1] + 1.0);
- return vec.scalar(1.0 - t) * vec.init(1.0, 1.0, 1.0) + vec.scalar(t) * vec.init(0.5, 0.7, 1.0);
-}
-
-pub fn write_color(stdout: anytype, v: vec.vec) !void {
+pub fn writeColor(stdout: anytype, v: vec.Vec) !void {
const r = @floatToInt(usize, v[0] * 255.999);
const g = @floatToInt(usize, v[1] * 255.999);
const b = @floatToInt(usize, v[2] * 255.999);
try stdout.print("{} {} {}\n", .{ r, g, b });
}
-pub fn hit_sphere(center: vec.vec, radius: f32, r: ray) ?f32 {
+pub fn hitSphere(center: vec.Vec, radius: f32, r: Ray) ?f32 {
const oc = r.origin - center;
- const a = vec.length_squared(r.direction);
+ const a = vec.lengthSquared(r.direction);
const half_b = vec.dot(oc, r.direction);
- const c = vec.length_squared(oc) - radius * radius;
+ const c = vec.lengthSquared(oc) - radius * radius;
const discriminant = half_b * half_b - a * c;
if (discriminant < 0) {
return null;
@@ -32,7 +22,20 @@ pub fn hit_sphere(center: vec.vec, radius: f32, r: ray) ?f32 {
}
}
-const ray = struct { origin: vec.vec, direction: vec.vec };
+const Ray = struct {
+ origin: vec.Vec,
+ direction: vec.Vec,
+
+ pub fn color(self: Ray) vec.Vec {
+ if (hitSphere(vec.init(0, 0, -1), 0.5, self)) |t| {
+ const n = vec.unitVec(self.origin + vec.scalar(t) * self.direction - vec.init(0, 0, -1));
+ return vec.scalar(0.5) * vec.init(n[0] + 1, n[1] + 1, n[2] + 1);
+ }
+ const direction = vec.unitVec(self.direction);
+ const t = 0.5 * (direction[1] + 1.0);
+ return vec.scalar(1.0 - t) * vec.init(1.0, 1.0, 1.0) + vec.scalar(t) * vec.init(0.5, 0.7, 1.0);
+ }
+};
pub fn main() !void {
const aspect_ratio: f32 = 16.0 / 9.0;
@@ -59,9 +62,9 @@ pub fn main() !void {
while (i < width) : (i += 1) {
var u = vec.scalar(@intToFloat(f32, i) / @intToFloat(f32, width - 1));
var v = vec.scalar(@intToFloat(f32, j) / @intToFloat(f32, height - 1));
- var color = ray_color(ray{ .origin = origin, .direction = lower_left_corner + u * horizontal + v * vertical - origin });
+ var r = Ray{ .origin = origin, .direction = lower_left_corner + u * horizontal + v * vertical - origin };
- try write_color(stdout, color);
+ try writeColor(stdout, r.color());
}
}
try bw.flush();