From 220cfe21d5da214c0cac1251994491a3fc6d0a27 Mon Sep 17 00:00:00 2001 From: Rostyslav Hnatyshyn Date: Thu, 22 Sep 2022 20:05:45 -0700 Subject: [PATCH] Added samples per pixel --- app/Main.hs | 13 ++++--------- src/Maths.hs | 7 +++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 8f4b2fa..09b1b8f 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -84,26 +84,21 @@ getBestHit' [] hit = hit colorWithNormal :: V3 Double -> V3 Double colorWithNormal v = 0.5 *^ (v ^+^ V3 1.0 1.0 1.0) -rayTrace :: (Shape s) => (Int, Int) -> ImageDimensions -> ViewPortDimensions -> [s] -> IO (V3 Double) +-- rayTrace :: (Shape s) => (Int, Int) -> ImageDimensions -> ViewPortDimensions -> [s] -> IO (V3 Double) rayTrace (x, y) (w, h) (vw, vh) world = do - evalRandIO (getRayColor world 50 $ buildRay (xf * (vw / wf)) (yf * (vh / hf))) + l <- replicateM 25 (evalRandIO (getRayColor world 50 $ buildRay (xf * (vw / wf)) (yf * (vh / hf)))) + return $ foldl (^+^) (V3 0.0 0.0 0.0) l where xf = fromIntegral x yf = fromIntegral y wf = fromIntegral (w - 1) hf = fromIntegral (h - 1) -clamp :: Double -> Double -> Double -> Double -clamp x mi ma - | x < mi = mi - | x > ma = ma - | otherwise = x - colorToString :: Color -> IO () colorToString (x, y, z) = tupToString (xr, yr, zr) where -- scale 1 - scale = 1.0 + scale = 1.0 / 25 xr = truncate ((clamp (sqrt (x * scale)) 0.0 0.999) * 256.0) yr = truncate ((clamp (sqrt (y * scale)) 0.0 0.999) * 256.0) zr = truncate ((clamp (sqrt (z * scale)) 0.0 0.999) * 256.0) diff --git a/src/Maths.hs b/src/Maths.hs index 09061e8..74e8fef 100644 --- a/src/Maths.hs +++ b/src/Maths.hs @@ -7,6 +7,7 @@ module Maths reflect, refract, reflectance, + clamp, ) where @@ -63,3 +64,9 @@ reflectance c r = r00 + (1 - r00) * (1 - c) ^ 5 where r0 = (1 - r) / (1 + r) r00 = r0 * r0 + +clamp :: Double -> Double -> Double -> Double +clamp x mi ma + | x < mi = mi + | x > ma = ma + | otherwise = x