From bcadf1d091efe76e7a1b2393f87f7e24128b723b Mon Sep 17 00:00:00 2001 From: yum Date: Mon, 7 Oct 2024 23:33:35 -0700 Subject: Add fog gimmick --- math.cginc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'math.cginc') diff --git a/math.cginc b/math.cginc index c2903c8..46e19e1 100644 --- a/math.cginc +++ b/math.cginc @@ -197,5 +197,29 @@ float median(float3 x) return median(x.x, x.y, x.z); } +// Yoinked from here +// https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection.html +bool solveQuadratic(float a, float b, float c, out float x0, out float x1) +{ + float discriminant = b * b - 4 * a * c; + if (discriminant < 0) { + return false; + } else if (discriminant == 0) { + x0 = -0.5 * b / a; + x1 = x0; + } else { + float q = (b > 0) ? + -0.5 * (b + sqrt(discriminant)) : + -0.5 * (b - sqrt(discriminant)); + x0 = q/a; + x1 = c/q; + } + float tmp_min = min(x0, x1); + float tmp_max = max(x0, x1); + x0 = tmp_min; + x1 = tmp_max; + return true; +} + #endif // __MATH_INC -- cgit v1.2.3