From 9e67a8260fee8f8a423a3131bcf13db3b9de6922 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Thu, 27 Jun 2024 15:43:38 +0100 Subject: maths.Pow turned out to be seriously terrible --- .gitignore | 4 +++- hittable.go | 4 ++-- material.go | 9 ++++++--- rtiaw.go | 9 +++++++++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index fddca93..5abb052 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.swp +*.prof rtiaw -out.ppm +*.ppm +*.png diff --git a/hittable.go b/hittable.go index ac5e996..73e2e3c 100644 --- a/hittable.go +++ b/hittable.go @@ -42,8 +42,8 @@ func (s Sphere) Hit(r Ray, ray_t Interval) (rec HitRecord) { oc := s.Origin.Sub(r.Origin); a := r.Direction.MagSqr(); h := r.Direction.Dot(oc); - c := oc.MagSqr() - maths.Pow(s.Radius, 2); - disc := maths.Pow(h, 2) - a*c; + c := oc.MagSqr() - s.Radius * s.Radius; + disc := h * h - a * c; if disc < 0 { return; diff --git a/material.go b/material.go index 62a0467..6b3a528 100644 --- a/material.go +++ b/material.go @@ -65,7 +65,7 @@ func (d Dielectric) Scatter(r Ray, hit HitRecord) Ray { unit_dir := r.Direction.Unit(); cos_theta := maths.Min(unit_dir.Neg().Dot(hit.N), 1.0); - sin_theta := maths.Sqrt(1.0 - maths.Pow(cos_theta, 2)); + sin_theta := maths.Sqrt(1.0 - cos_theta * cos_theta); cannot_refract := ri * sin_theta > 1.0; @@ -80,7 +80,10 @@ func (d Dielectric) Scatter(r Ray, hit HitRecord) Ray { } func schlick_reflect(cos_theta float64, ri float64) bool { - r0 := maths.Pow((1 - ri) / (1 + ri), 2); - approx := r0 + (1 - r0) * maths.Pow(1 - cos_theta, 5); + r0 := (1 - ri) / (1 + ri); + r0 = r0 * r0; + ct5 := 1 - cos_theta; + ct5 = ct5 * ct5 * ct5 * ct5 * ct5; + approx := r0 + (1 - r0) * ct5; return approx > Interval{0, 1}.Sample(); } diff --git a/rtiaw.go b/rtiaw.go index 392db71..2daa826 100644 --- a/rtiaw.go +++ b/rtiaw.go @@ -1,6 +1,15 @@ package main +import ( + "os" + "runtime/pprof" +) + func main() { + f, _ := os.Create("rtiaw.prof"); + pprof.StartCPUProfile(f); + defer pprof.StopCPUProfile(); + var world Hittables; mat_ground := Lambertian{Vec3{0.5, 0.5, 0.5}}; -- cgit v1.2.1