aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main.zig
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2022-11-27 12:23:06 +0900
committernsfisis <nsfisis@gmail.com>2022-11-27 12:23:06 +0900
commit04d3a2d673593c5a36f40894fb29d090a7d7a197 (patch)
tree8e9e6617d0db16ccff98b31b7e0e041f3663438d /src/main.zig
parent88e3d30bb594158e85a5b403b7a59bbf9406a15d (diff)
downloadRayTracingInOneWeekend.zig-04d3a2d673593c5a36f40894fb29d090a7d7a197.tar.gz
RayTracingInOneWeekend.zig-04d3a2d673593c5a36f40894fb29d090a7d7a197.tar.zst
RayTracingInOneWeekend.zig-04d3a2d673593c5a36f40894fb29d090a7d7a197.zip
6.1
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig24
1 files changed, 18 insertions, 6 deletions
diff --git a/src/main.zig b/src/main.zig
index c862e1e..a60526a 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -84,22 +84,34 @@ const Ray = struct {
}
};
-fn hitSphere(center: Point3, radius: f64, r: Ray) bool {
+fn hitSphere(center: Point3, radius: f64, r: Ray) f64 {
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;
+ if (discriminant < 0.0) {
+ // r does not intersect the sphere.
+ return -1.0;
+ } else {
+ // root of the equation.
+ return (-b - @sqrt(discriminant)) / (2.0 * a);
+ }
}
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 sphereOrigin = Point3{ .x = 0.0, .y = 0.0, .z = -1.0 };
+ const sphereRadius = 0.5;
+
+ const t = hitSphere(sphereOrigin, sphereRadius, r);
+ if (t > 0.0) {
+ // r hints the sphere.
+ const sphereNormal = r.at(t).sub(sphereOrigin).normalized();
+ return (Color{ .x = sphereNormal.x + 1.0, .y = sphereNormal.y + 1.0, .z = sphereNormal.z + 1.0 }).mul(0.5);
}
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));
+ const s = 0.5 * (unit_dir.y + 1.0);
+ return (Color{ .x = 1.0, .y = 1.0, .z = 1.0 }).mul(1.0 - s).add((Color{ .x = 0.5, .y = 0.7, .z = 1.0 }).mul(s));
}
fn writeColor(out: anytype, c: Color) !void {