From 1101f71d8f1be66f87e341230d50abec47f815bc Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Fri, 18 Sep 2020 13:55:48 +0200 Subject: [PATCH] add red sphere --- in_one_weekend/src/main.zig | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/in_one_weekend/src/main.zig b/in_one_weekend/src/main.zig index cb0d59c..5cfeb4b 100644 --- a/in_one_weekend/src/main.zig +++ b/in_one_weekend/src/main.zig @@ -16,7 +16,19 @@ const SDL_WINDOWPOS_UNDEFINED = @bitCast(c_int, sdl.c.SDL_WINDOWPOS_UNDEFINED_MA const fps = 60; +fn hitSphere(center: Point3, radius: f32, ray: Ray) bool { + const ssr = ray.origin.sub(center); // Sphere-space ray, (A - C) in book + const a = Vec3.dot(ray.direction, ray.direction); + const b = 2.0 * Vec3.dot(ssr, ray.direction); + const c = Vec3.dot(ssr, ssr) - (radius * radius); + const discriminant = b * b - 4 * a * c; + return discriminant > 0; +} + fn rayColor(ray: Ray) Color { + if (hitSphere(Point3{ .x = 0, .y = 0, .z = -1 }, 0.5, ray)) { + return Color{ .x = 1, .y = 0, .z = 0 }; + } const unitDirection = vec3.unitVector(ray.direction); const t = 0.5 * (unitDirection.y + 1.0); const white = Color{ .x = 1.0, .y = 1.0, .z = 1.0 };