From 523f9439ed0d2e4c2d51edc623b6c4d62885cfdd Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Wed, 12 Jun 2024 23:19:56 +0100 Subject: Finished chapter 9 --- lib/hittable.rb | 4 ++-- lib/ray.rb | 2 +- lib/vec3.rb | 24 ++++++++++++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/hittable.rb b/lib/hittable.rb index 80254d1..c84764f 100644 --- a/lib/hittable.rb +++ b/lib/hittable.rb @@ -8,8 +8,8 @@ class HitRecord attr_accessor :point, :normal, :t def set_face_normal(ray, out_normal) - @front_face = ray.direction.dot(out_normal) < 0 - @normal = @front_face ? out_normal : -out_normal + front_face = ray.direction.dot(out_normal) < 0 + @normal = front_face ? out_normal : -out_normal end end diff --git a/lib/ray.rb b/lib/ray.rb index c1679a5..eb6038c 100644 --- a/lib/ray.rb +++ b/lib/ray.rb @@ -14,7 +14,7 @@ class Ray return Colour.new if depth <= 0 if rec = world.hit(self, Interval.new(0.0001, Float::INFINITY)) - dir = Vec3.random_unit(rec.normal) + dir = rec.normal + Vec3.random_unit return Ray.new(rec.point, dir).colour(world, depth - 1) * 0.5 end diff --git a/lib/vec3.rb b/lib/vec3.rb index e24229a..08cbf69 100644 --- a/lib/vec3.rb +++ b/lib/vec3.rb @@ -87,12 +87,28 @@ class Colour < Vec3 def g; @y; end def b; @z; end + def gamma(gma = 2) + Colour.new( + r > 0 ? r ** (1.0 / gma) : r, + g > 0 ? g ** (1.0 / gma) : g, + b > 0 ? b ** (1.0 / gma) : b + ) + end + + def clamp(interval) + Colour.new( + interval.clamp(r), + interval.clamp(g), + interval.clamp(b) + ) + end + def to_ppm - intensity = Interval.new(0.0, 0.999) + g_space = gamma.clamp(Interval.new(0.0, 0.999)) "%3d %3d %3d" % [ - (intensity.clamp(r) * 256).to_i, - (intensity.clamp(g) * 256).to_i, - (intensity.clamp(b) * 256).to_i + (g_space.r * 256).to_i, + (g_space.g * 256).to_i, + (g_space.b * 256).to_i ] end end -- cgit v1.2.1