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

import (
  maths "math"
)

type Ray struct {
  Origin Vec3
  Direction Vec3
}

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 {
    dir := hit.N.Add(RandomVec3OnUnitSphere());
    return Ray{hit.P, dir}.Colour(world, md - 1).Div(SplatVec3(10));
  }

  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)));
}