aboutsummaryrefslogtreecommitdiff
path: root/ray.go
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-06-24 16:01:18 +0100
committerNat Lasseter <user@4574.co.uk>2024-06-24 16:01:18 +0100
commitb7814d6822d03517fc846b95865965618be7d406 (patch)
tree2afc2a5de6d3ca82f465f51686d47dc032d2efb4 /ray.go
Initial Commit, finished Chapter 6
Diffstat (limited to 'ray.go')
-rw-r--r--ray.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/ray.go b/ray.go
new file mode 100644
index 0000000..cd4f6aa
--- /dev/null
+++ b/ray.go
@@ -0,0 +1,26 @@
+package main
+
+import (
+ maths "math"
+)
+
+type Ray struct {
+ Origin Vec3
+ Direction Vec3
+}
+
+func (r Ray) At(t float64) Vec3 {
+ return r.Origin.Add(r.Direction.Mul(Splat(t)));
+}
+
+func (r Ray) Colour(world Hittable) Vec3 {
+ hit := world.Hit(r, Interval{0, maths.Inf(1)})
+ if hit.Valid {
+ return hit.N.Add(Splat(1)).Div(Splat(2));
+ }
+
+ unit_dir := r.Direction.Unit();
+ a := (unit_dir.Y + 1) / 2;
+ return Vec3{0.5, 0.7, 1.0}.Mul(Splat(a)).
+ Add(Splat(1).Mul(Splat(1.0 - a)));
+}