aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-06-12 22:51:25 +0100
committerNat Lasseter <user@4574.co.uk>2024-06-12 22:51:25 +0100
commit1bc4829392b3c91737fbe4ddccd81098ac9d8a12 (patch)
treec2339a79b1fdaf82955a5e38ed35a7f0a9481f21 /lib
parent51416f5dfac39b6ede8340b57b9bed4eb3edc646 (diff)
chap 9.3
Diffstat (limited to 'lib')
-rw-r--r--lib/camera.rb5
-rw-r--r--lib/ray.rb9
-rw-r--r--lib/vec3.rb19
3 files changed, 28 insertions, 5 deletions
diff --git a/lib/camera.rb b/lib/camera.rb
index f8721bb..6a3b190 100644
--- a/lib/camera.rb
+++ b/lib/camera.rb
@@ -1,8 +1,9 @@
class Camera
- def initialize(width = 100, aspect = 1.0, aliasing = 10)
+ def initialize(width = 100, aspect = 1.0, aliasing = 10, depth = 10)
@width = width
@aspect = aspect
@aliasing = aliasing
+ @depth = depth
# Height
@height = (@width / @aspect).to_i
@@ -40,7 +41,7 @@ class Camera
@width.times do |col|
p_colour = Colour.new
@aliasing.times do
- p_colour += get_ray(row, col).colour(world)
+ p_colour += get_ray(row, col).colour(world, @depth)
end
puts (p_colour / @aliasing).to_ppm
end
diff --git a/lib/ray.rb b/lib/ray.rb
index 47f2b26..c1679a5 100644
--- a/lib/ray.rb
+++ b/lib/ray.rb
@@ -10,9 +10,12 @@ class Ray
@origin + (@direction * time)
end
- def colour(world)
- if rec = world.hit(self, Interval.new(0, Float::INFINITY))
- return (Colour.new(1,1,1) + rec.normal) * 0.5
+ def colour(world, depth)
+ return Colour.new if depth <= 0
+
+ if rec = world.hit(self, Interval.new(0.0001, Float::INFINITY))
+ dir = Vec3.random_unit(rec.normal)
+ return Ray.new(rec.point, dir).colour(world, depth - 1) * 0.5
end
unit_dir = direction.unit
diff --git a/lib/vec3.rb b/lib/vec3.rb
index b9a5a73..e24229a 100644
--- a/lib/vec3.rb
+++ b/lib/vec3.rb
@@ -56,9 +56,28 @@ class Vec3
self / mag
end
+ def in_unit_sphere?
+ mag_sqr < 1
+ end
+
def to_s
"{#{@x}, #{@y}, #{@z}}"
end
+
+ def self.random(min = 0, max = 1)
+ interval = Interval.new(min.to_f, max.to_f)
+ Vec3.new(interval.sample, interval.sample, interval.sample)
+ end
+
+ def self.random_unit(normal = nil)
+ p = nil
+ while p.nil?
+ c = Vec3.random
+ p = c.unit if c.in_unit_sphere?
+ end
+
+ normal.nil? || p.dot(normal) > 0 ? p : -p
+ end
end
class Point < Vec3; end