diff options
author | Nat Lasseter <user@4574.co.uk> | 2024-06-24 17:26:09 +0100 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2024-06-24 17:26:09 +0100 |
commit | bd3bbdd239fd91056442384bfe3a86b2b8c0e68e (patch) | |
tree | ce12c3a6289820092bb9b1f346ee16361b805e4c /vec3.go | |
parent | 2f025ebfc6c9e3146ef1c356f99045edc203c9ee (diff) |
chapter 9
Diffstat (limited to 'vec3.go')
-rw-r--r-- | vec3.go | 29 |
1 files changed, 25 insertions, 4 deletions
@@ -52,16 +52,37 @@ func (v Vec3) Cross(w Vec3) Vec3 { v.X * w.Y - v.Y * w.X}; } +func linear_to_gamma(comp float64) float64 { + if comp > 0 { + return maths.Sqrt(comp); + } + return 0; +} + func (v Vec3) ToPPM() string { i := Interval{0, 0.999}; - r := uint8(256 * i.Clamp(v.X)); - g := uint8(256 * i.Clamp(v.Y)); - b := uint8(256 * i.Clamp(v.Z)); + r := uint8(256 * i.Clamp(linear_to_gamma(v.X))); + g := uint8(256 * i.Clamp(linear_to_gamma(v.Y))); + b := uint8(256 * i.Clamp(linear_to_gamma(v.Z))); return fmt.Sprintf("%3d %3d %3d", r, g, b); } -func Splat(f float64) Vec3 { +func SplatVec3(f float64) Vec3 { return Vec3{f, f, f}; } + +func RandomVec3OnUnitSphere() (v Vec3) { + i := Interval{-1, 1} + + for true { + v = i.RandomVec3() + if v.MagSqr() < 1 { + v = v.Unit(); + return; + } + } + + return; +} |