aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig
index ee86d4b..c862e1e 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -84,7 +84,19 @@ const Ray = struct {
}
};
+fn hitSphere(center: Point3, radius: f64, r: Ray) bool {
+ const oc = r.origin.sub(center);
+ const a = Vec3.dot(r.dir, r.dir);
+ const b = 2.0 * Vec3.dot(oc, r.dir);
+ const c = Vec3.dot(oc, oc) - radius * radius;
+ const discriminant = b * b - 4.0 * a * c;
+ return discriminant > 0.0;
+}
+
fn rayColor(r: Ray) Color {
+ if (hitSphere(Point3{ .x = 0.0, .y = 0.0, .z = -1.0 }, 0.5, r)) {
+ return Color{ .x = 1.0, .y = 0.0, .z = 0.0 };
+ }
const unit_dir = r.dir.normalized();
const t = 0.5 * (unit_dir.y + 1.0);
return (Color{ .x = 1.0, .y = 1.0, .z = 1.0 }).mul(1.0 - t).add((Color{ .x = 0.5, .y = 0.7, .z = 1.0 }).mul(t));