From 1bc4829392b3c91737fbe4ddccd81098ac9d8a12 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Wed, 12 Jun 2024 22:51:25 +0100 Subject: chap 9.3 --- lib/camera.rb | 5 +++-- lib/ray.rb | 9 ++++++--- lib/vec3.rb | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) (limited to 'lib') 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 -- cgit v1.2.1