aboutsummaryrefslogtreecommitdiff
path: root/ray.go
blob: 2d7f29c6c76dd69281b7128a5aa306d40f325958 (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
package main

import (
  maths "math"
)

type Ray struct {
  Origin Vec3
  Direction Vec3
  Valid bool
}

func (r Ray) At(t float64) Vec3 {
  return r.Origin.Add(r.Direction.Mul(SplatVec3(t)));
}

func (r Ray) Colour(world Hittable, md uint) Vec3 {
  if md <= 0 {
    return SplatVec3(0);
  }

  hit := world.Hit(r, Interval{0.001, maths.Inf(1)})
  if hit.Valid {
    scat := hit.Mat.Scatter(r, hit);
    if scat.Valid {
      return scat.Colour(world, md - 1).Mul(hit.Mat.Attenuation());
    }
    return SplatVec3(0);
  }

  unit_dir := r.Direction.Unit();
  a := (unit_dir.Y + 1) / 2;
  return Vec3{0.5, 0.7, 1.0}.Mul(SplatVec3(a)).
         Add(SplatVec3(1).Mul(SplatVec3(1.0 - a)));
}