aboutsummaryrefslogtreecommitdiff
path: root/vec3.go
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-06-24 17:26:09 +0100
committerNat Lasseter <user@4574.co.uk>2024-06-24 17:26:09 +0100
commitbd3bbdd239fd91056442384bfe3a86b2b8c0e68e (patch)
treece12c3a6289820092bb9b1f346ee16361b805e4c /vec3.go
parent2f025ebfc6c9e3146ef1c356f99045edc203c9ee (diff)
chapter 9
Diffstat (limited to 'vec3.go')
-rw-r--r--vec3.go29
1 files changed, 25 insertions, 4 deletions
diff --git a/vec3.go b/vec3.go
index 4212b46..bc16a07 100644
--- a/vec3.go
+++ b/vec3.go
@@ -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;
+}