aboutsummaryrefslogtreecommitdiff
path: root/lib/material.rb
blob: c935147797fbbb1c41f19b61ffea321fa82fde7f (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
29
30
31
32
33
34
35
36
37
38
class Material
  def initialize(r, g, b)
    @albedo = Colour.new(r, g, b)
  end

  def attenuation
    @albedo
  end

  def scatter(ray, record)
    nil
  end
end

class Lambertian < Material
  def scatter(ray, record)
    scat = record.normal + Vec3.random_unit
    scat = rec.normal if scat.near_zero?
    Ray.new(record.point, scat)
  end
end

class Metal < Material
  def initialize(r, g, b, fuzz)
    @fuzz = fuzz < 1 ? fuzz : 1
    super(r, g, b)
  end

  def scatter(ray, record)
    refl = ray.direction.reflect(record.normal)
    refl = refl.unit + (Vec3.random_unit * @fuzz)
    if refl.dot(record.normal) > 0
      Ray.new(record.point, refl)
    else
      nil
    end
  end
end