aboutsummaryrefslogtreecommitdiff
path: root/material.go
diff options
context:
space:
mode:
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};
+}