summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2022-12-21 21:53:24 +0100
committerTom Barrett <tom@tombarrett.xyz>2022-12-21 21:53:24 +0100
commit8ab21b7ec6bd83fe12b48534e4f670500e8d5e2c (patch)
tree3b52e0d8cd5187db969e0948b6e68f56df3677a0 /src
parent545c41f79e37e9b7d4b5bf7ba64aa7efa549aded (diff)
move to file
Diffstat (limited to 'src')
-rw-r--r--src/main.zig58
-rw-r--r--src/vec.zig25
2 files changed, 44 insertions, 39 deletions
diff --git a/src/main.zig b/src/main.zig
index 1a8e14c..e2b63d2 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,40 +1,29 @@
const std = @import("std");
const warn = @import("std").debug.print;
+const vec = @import("vec.zig");
-pub fn unit_vector(v: vec) vec {
- return v / scalar(length(v));
-}
-
-pub fn length_squared(v: vec) f32 {
- return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
-}
-
-pub fn length(v: vec) f32 {
- return @sqrt(length_squared(v));
-}
-
-pub fn ray_color(r: ray) vec {
- if (hit_sphere(vec{ 0, 0, -1 }, 0.5, r)) |t| {
- const n = unit_vector(r.origin + scalar(t) * r.direction - vec{ 0, 0, -1 });
- return scalar(0.5) * vec{ n[0] + 1, n[1] + 1, n[2] + 1 };
+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 = unit_vector(r.direction);
+ const direction = vec.unit_vector(r.direction);
const t = 0.5 * (direction[1] + 1.0);
- return scalar(1.0 - t) * vec{ 1.0, 1.0, 1.0 } + scalar(t) * vec{ 0.5, 0.7, 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) !void {
+pub fn write_color(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, radius: f32, r: ray) ?f32 {
+pub fn hit_sphere(center: vec.vec, radius: f32, r: ray) ?f32 {
const oc = r.origin - center;
- const a = length_squared(r.direction);
- const half_b = dot(oc, r.direction);
- const c = length_squared(oc) - radius * radius;
+ const a = vec.length_squared(r.direction);
+ const half_b = vec.dot(oc, r.direction);
+ const c = vec.length_squared(oc) - radius * radius;
const discriminant = half_b * half_b - a * c;
if (discriminant < 0) {
return null;
@@ -43,16 +32,7 @@ pub fn hit_sphere(center: vec, radius: f32, r: ray) ?f32 {
}
}
-const vec = @Vector(3, f32);
-pub fn scalar(x: f32) vec {
- return @splat(3, x);
-}
-
-pub fn dot(x: vec, y: vec) f32 {
- return x[0] * y[0] + x[1] * y[1] + x[2] * y[2];
-}
-
-const ray = struct { origin: vec, direction: vec };
+const ray = struct { origin: vec.vec, direction: vec.vec };
pub fn main() !void {
const aspect_ratio: f32 = 16.0 / 9.0;
@@ -65,10 +45,10 @@ pub fn main() !void {
const viewport_height = 2.0;
const viewport_width = aspect_ratio * viewport_height;
const focal_length = 1.0;
- const origin = vec{ 0, 0, 0 };
- const horizontal = vec{ viewport_width, 0, 0 };
- const vertical = vec{ 0, viewport_height, 0 };
- const lower_left_corner = origin - (horizontal / scalar(2.0)) - (vertical / scalar(2.0)) - vec{ 0, 0, focal_length };
+ const origin = vec.init(0, 0, 0);
+ const horizontal = vec.init(viewport_width, 0, 0);
+ const vertical = vec.init(0, viewport_height, 0);
+ const lower_left_corner = origin - (horizontal / vec.scalar(2.0)) - (vertical / vec.scalar(2.0)) - vec.init(0, 0, focal_length);
try stdout.print("P3\n{} {}\n255\n", .{ width, height });
@@ -77,8 +57,8 @@ pub fn main() !void {
warn("remaining: {}\n", .{j});
var i: usize = 0;
while (i < width) : (i += 1) {
- var u = scalar(@intToFloat(f32, i) / @intToFloat(f32, width - 1));
- var v = scalar(@intToFloat(f32, j) / @intToFloat(f32, height - 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 });
try write_color(stdout, color);
diff --git a/src/vec.zig b/src/vec.zig
new file mode 100644
index 0000000..cc245a4
--- /dev/null
+++ b/src/vec.zig
@@ -0,0 +1,25 @@
+pub const vec = @Vector(3, f32);
+
+pub fn init(x: f32, y: f32, z: f32) vec {
+ return @Vector(3, f32){ x, y, z };
+}
+
+pub fn scalar(x: f32) vec {
+ return @splat(3, x);
+}
+
+pub fn unit_vector(v: vec) vec {
+ return v / scalar(length(v));
+}
+
+pub fn length_squared(v: vec) f32 {
+ return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
+}
+
+pub fn length(v: vec) f32 {
+ return @sqrt(length_squared(v));
+}
+
+pub fn dot(x: vec, y: vec) f32 {
+ return x[0] * y[0] + x[1] * y[1] + x[2] * y[2];
+}