aboutsummaryrefslogtreecommitdiff
path: root/lib/ray.rb
blob: c1679a54eff8db5e1f635b1ef695eb0592666646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Ray
  def initialize(origin = Point.new, direction = Vec3.new)
    @origin = origin
    @direction = direction
  end

  attr_reader :origin, :direction

  def at(time)
    @origin + (@direction * time)
  end

  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
    a = (unit_dir.y + 1) / 2
    Colour.new(1.0, 1.0, 1.0) * (1-a) + Colour.new(0.5, 0.7, 1.0) * a
  end
end