aboutsummaryrefslogtreecommitdiff
path: root/ray.go
blob: cd4f6aaab8edf19ae482f416937a6f6add02931a (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
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(Splat(t)));
}

func (r Ray) Colour(world Hittable) Vec3 {
  hit := world.Hit(r, Interval{0, maths.Inf(1)})
  if hit.Valid {
    return hit.N.Add(Splat(1)).Div(Splat(2));
  }

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