aboutsummaryrefslogtreecommitdiff
path: root/material.go
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-06-24 22:12:44 +0100
committerNat Lasseter <user@4574.co.uk>2024-06-24 22:12:44 +0100
commitd248e30d365d5e401d5cd372aa415fb2f9a39326 (patch)
tree58c5602ec20ad39ff34640503c7a4d35c4e563cb /material.go
parentbd3bbdd239fd91056442384bfe3a86b2b8c0e68e (diff)
Chapter 10
Diffstat (limited to 'material.go')
-rw-r--r--material.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/material.go b/material.go
new file mode 100644
index 0000000..66ea256
--- /dev/null
+++ b/material.go
@@ -0,0 +1,44 @@
+package main
+
+//Material
+
+type Material interface {
+ Attenuation() Vec3
+ Scatter(r Ray, hit HitRecord) Ray
+}
+
+//Lambertian
+
+type Lambertian struct {
+ Albedo Vec3
+}
+
+func (l Lambertian) Attenuation() Vec3 {
+ return l.Albedo;
+}
+
+func (l Lambertian) Scatter(r Ray, hit HitRecord) Ray {
+ scat_dir := hit.N.Add(RandomVec3OnUnitSphere());
+ if scat_dir.NearZero() {
+ scat_dir = hit.N;
+ }
+ return Ray{hit.P, scat_dir, true};
+}
+
+//Metal
+
+type Metal struct {
+ Albedo Vec3
+ Fuzz float64
+}
+
+func (m Metal) Attenuation() Vec3 {
+ return m.Albedo;
+}
+
+func (m Metal) Scatter(r Ray, hit HitRecord) Ray {
+ refl := r.Direction.Reflect(hit.N).Unit().
+ Add(SplatVec3(m.Fuzz).Mul(RandomVec3OnUnitSphere()));
+ valid := refl.Dot(hit.N) > 0;
+ return Ray{hit.P, refl, valid};
+}