aboutsummaryrefslogtreecommitdiff
path: root/lib/ray.rb
blob: 47f2b26e9fa4456b5f473476882f7c23fc1b7124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)
    if rec = world.hit(self, Interval.new(0, Float::INFINITY))
      return (Colour.new(1,1,1) + rec.normal) * 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