diff options
Diffstat (limited to 'raytracer.hs')
-rw-r--r-- | raytracer.hs | 33 |
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)) |