summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2013-12-01 17:52:38 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2013-12-01 17:52:38 +0100
commit91c09578afeabb8f0405f4dc4321290735030c6b (patch)
tree3d7f172fd8b7bd3e84ea6a7db0d741ec0d88b7af
parent1dbaf7266b0a30096adf5ef58f82dbf156f96d23 (diff)
downloadhaskell-91c09578afeabb8f0405f4dc4321290735030c6b.tar.gz
haskell-91c09578afeabb8f0405f4dc4321290735030c6b.tar.bz2
haskell-91c09578afeabb8f0405f4dc4321290735030c6b.zip
colored floor
-rw-r--r--raytracer.hs29
1 files changed, 21 insertions, 8 deletions
diff --git a/raytracer.hs b/raytracer.hs
index ed0726d..c73bf13 100644
--- a/raytracer.hs
+++ b/raytracer.hs
@@ -14,7 +14,7 @@ data Sphere = Sphere Coord Double Color deriving (Show, Eq)
degrees = pi / 180
-eye = (0, 0, 40)
+eye = (0, 0, 20)
x_of (x, _, _) = x
y_of (_, y, _) = y
@@ -29,13 +29,16 @@ sphere2 = Sphere (80, 0, 5) 20 (255,60,0)
filename num = "foo/foo" ++ show num ++ ".ppm"
-spherepos = take 80 [0,1..]
+spherepos = take 1 [0,20..] -- take 80 [0,1..]
-spheres num = [Sphere (num, 2+(num/2), 5) 20 (255,60,0), sphere1]
+spheres num = [ trace ("Sphere at " ++
+ show (round (80 * sin(num * degrees))) ++ "," ++
+ show (round (80 * cos(num * degrees))))
+ Sphere (80 * sin(num * degrees), 80 * cos(num * degrees), 5) 10 (255,60,0), sphere1]
writenum :: Double -> IO ()
writenum num = trace ("Rendering " ++ show (filename num))
- writeFile (filename num) (image $ spheres num)
+ writeFile (filename num) (image $ spheres num)
main = mapM writenum spherepos
@@ -115,19 +118,29 @@ intersect_sphere source (alpha, beta) (Sphere centre radius color)
intersect_point_floor :: Coord -> ScreenCoord -> (Coord, Double)
intersect_point_floor (_, _, z) (alpha, beta) =
- ( (-z * (cos alpha) / (sin beta) - 2 * (cos beta) / (sin beta),
+ ( (-z * ((cos alpha) + (cos beta)) / (sin beta),
-z * (sin alpha) / (sin beta),
0),
-z / (sin beta) )
+direction_color :: Double -> Double -> Int -> Color
+direction_color x y attn
+ | x > 0 && y > 0 = (attn, 0, 0)
+ | x <= 0 && y > 0 = (0, attn, 0)
+ | x > 0 && y <= 0 = (attn, attn, 0)
+ | otherwise = (0, 0, attn)
+
+checkerboard_pattern :: Double -> Double -> Int -> Color
+checkerboard_pattern x y attn
+ | (round (x/floorscale) `mod` 2) == (round (y/floorscale) `mod` 2) = direction_color x y attn
+ | otherwise = (attn, attn, attn)
+
intersect_floor :: Coord -> ScreenCoord -> (Coord, Double, Color)
intersect_floor source (alpha, beta)
| beta >= 0 = ((0, 0, 0), 0, black)
| x > (-0.5) && x < 0.5 = ((x, y, z), t, (0, attn, attn))
| y > (-0.5) && y < 0.5 = ((x, y, z), t, (attn, attn, 0))
- | (round (x/floorscale) `mod` 2) == (round (y/floorscale) `mod` 2) =
- ((x, y, z), t, (attn, 0, 0))
- | otherwise = ((x, y, z), t, (attn, attn, attn))
+ | otherwise = ((x, y, z), t, checkerboard_pattern x y attn)
where attn = max 0 (round (255 - 8*(sqrt t)))
((x, y, z), t) = intersect_point_floor source (alpha, beta)