aboutsummaryrefslogtreecommitdiff
path: root/material.go
blob: 66ea25641d92fb53373970da2c63aa045e3f7d83 (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
39
40
41
42
43
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};
}