aboutsummaryrefslogtreecommitdiff
path: root/lib/material.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/material.rb')
-rw-r--r--lib/material.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/material.rb b/lib/material.rb
new file mode 100644
index 0000000..c935147
--- /dev/null
+++ b/lib/material.rb
@@ -0,0 +1,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