aboutsummaryrefslogtreecommitdiff
path: root/lib/ray.rb
blob: 1285158180f8045977754f6fe2e33b3ef851d0e5 (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
26
27
28
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))
      if scat = rec.material.scatter(self, rec)
        return scat.colour(world, depth - 1) * rec.material.attenuation
      else
        return Colour.new(0,0,0)
      end
    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