summaryrefslogtreecommitdiffstats
path: root/raytracer.hs
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2013-11-23 12:46:38 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2013-11-23 12:46:38 +0100
commit65778a8af5c39932a4fcca89df23ec9d4b200704 (patch)
tree1407da81875c76c67eabf5b19a6b3007d9708afc /raytracer.hs
parent6d592918ef72a956abcbaf2736a3f04f372ee175 (diff)
downloadhaskell-65778a8af5c39932a4fcca89df23ec9d4b200704.tar.gz
haskell-65778a8af5c39932a4fcca89df23ec9d4b200704.tar.bz2
haskell-65778a8af5c39932a4fcca89df23ec9d4b200704.zip
oversampling
Diffstat (limited to 'raytracer.hs')
-rw-r--r--raytracer.hs33
1 files changed, 26 insertions, 7 deletions
diff --git a/raytracer.hs b/raytracer.hs
index f33a87f..b77445b 100644
--- a/raytracer.hs
+++ b/raytracer.hs
@@ -44,7 +44,7 @@ floorcolor alpha beta
where attn = max 0 (round (255 - 8*(sqrt t)))
(x, y, t) = intersect_floor alpha beta
--- blue is beautiful
+-- blue is beautiful, but a green tint is nice too
skycolor :: Double -> Double -> (Int, Int, Int)
skycolor alpha beta = (60,
round ((sqrt (alpha/6)) / (sqrt (90 * degrees)) * 128),
@@ -61,14 +61,33 @@ cartProd xs ys = [(x,y) | x <- xs, y <- ys]
pixel_to_ppm (r,g,b) = show r ++ " " ++ show g ++ " " ++ show b ++ "\n"
---tuplesum x y = (a1 + a2, b1 + b2)
--- where (a1, a2) = x
--- (b1, b2) = y
+tuple2sum x y = (a1 + b1, a2 + b2)
+ where (a1, a2) = x
+ (b1, b2) = y
--- from one pixel alpha beta, get a list of oversampled pixels
---oversample (a,b) = (cartProd ov_alphaoffsets ov_betaoffsets)
+-- from one pixel (alpha, beta), get a list of oversampled pixels
+oversample :: (Double, Double) -> [(Double, Double)]
+oversample (a,b) = map (tuple2sum (a,b)) (cartProd ov_alphaoffsets ov_betaoffsets)
-allpixels = map (uncurry pixel_color) (cartProdTranspose betas alphas)
+tuple3sum x y = (a1 + b1, a2 + b2, a3 + b3)
+ where (a1, a2, a3) = x
+ (b1, b2, b3) = y
+
+coloraverage :: [(Int, Int, Int)] -> (Int, Int, Int)
+coloraverage xs = ( round (fromIntegral s1/l),
+ round (fromIntegral s2/l),
+ round (fromIntegral s3/l) )
+ where (s1, s2, s3) = foldr tuple3sum (0,0,0) xs
+ l = fromIntegral (length xs)
+
+-- calculate color of oversampled pixels
+ov_color :: [(Double, Double)] -> (Int, Int, Int)
+ov_color xs = coloraverage (map (uncurry pixel_color) xs)
+
+-- list of list of (alpha, beta)-tuples
+ov_pixels = map oversample (cartProdTranspose betas alphas)
+
+allpixels = map ov_color ov_pixels
image = imgheader ++ (foldr (++) "" (map pixel_to_ppm allpixels))