summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2013-12-02 21:14:57 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2013-12-02 21:14:57 +0100
commita84d8f644c6642393866ea205cecd4ed76908026 (patch)
tree18553bfdeb6fddf8a7ce8a95576800b38e02142a
parent91c09578afeabb8f0405f4dc4321290735030c6b (diff)
downloadhaskell-a84d8f644c6642393866ea205cecd4ed76908026.tar.gz
haskell-a84d8f644c6642393866ea205cecd4ed76908026.tar.bz2
haskell-a84d8f644c6642393866ea205cecd4ed76908026.zip
all messed up
-rw-r--r--raytracer.hs46
1 files changed, 26 insertions, 20 deletions
diff --git a/raytracer.hs b/raytracer.hs
index c73bf13..883331f 100644
--- a/raytracer.hs
+++ b/raytracer.hs
@@ -14,7 +14,8 @@ data Sphere = Sphere Coord Double Color deriving (Show, Eq)
degrees = pi / 180
-eye = (0, 0, 20)
+eye :: Coord
+eye = (0, -20, 20)
x_of (x, _, _) = x
y_of (_, y, _) = y
@@ -43,16 +44,16 @@ writenum num = trace ("Rendering " ++ show (filename num))
main = mapM writenum spherepos
-alpha1 = 360 * degrees
-alpha2 = 0 * degrees
+alpha1 = 0 * degrees
+alpha2 = 360 * degrees
beta1 = 20 * degrees
-beta2 = -90 * degrees
+beta2 = -100 * degrees
floorscale = 4
-w = 800
-h = 300
+w = 500
+h = 200
oversampling = 1 -- each pixel is 2x2 rays
@@ -117,18 +118,18 @@ 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) + (cos beta)) / (sin beta),
- -z * (sin alpha) / (sin beta),
+intersect_point_floor (x, y, z) (alpha, beta) =
+ ( (-z * ((cos alpha) + (cos beta)) / (sin beta) + x,
+ -z * (sin alpha) / (sin beta) + y,
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)
+ | x > 0 && y > 0 = (attn, 0, 0) -- red
+ | x <= 0 && y > 0 = (0, attn, 0) -- green
+ | x > 0 && y <= 0 = (attn, attn, 0) -- yellow
+ | otherwise = (0, 0, attn) -- blue
checkerboard_pattern :: Double -> Double -> Int -> Color
checkerboard_pattern x y attn
@@ -137,11 +138,12 @@ checkerboard_pattern x y 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))
+-- | beta >= 0 = ((0, 0, 0), 0, black)
+ | x > (-0.5) && x < 0.5 = ((x, y, z), t, (0, attn, attn)) -- x near 0 : cyan
+ | y > (-0.5) && y < 0.5 = ((x, y, z), t, (attn, attn, 0)) -- y near 0 : yellow
+ | beta >= 0 = ((x, y, z), -t, checkerboard_pattern x y 128)
| otherwise = ((x, y, z), t, checkerboard_pattern x y attn)
- where attn = max 0 (round (255 - 8*(sqrt t)))
+ where attn = max 0 (round (255 - 8*(sqrt $ abs t)))
((x, y, z), t) = intersect_point_floor source (alpha, beta)
-- blue is beautiful, but a green tint is nice too
@@ -173,10 +175,14 @@ nearest_obj source scoord spheres
-- First iteration
pixel_color :: Coord -> [Sphere] -> ScreenCoord -> Color
-pixel_color source spheres scoord
+pixel_color source spheres scoord = floorcolor
+ where ((x,y,z), floordist, floorcolor) = intersect_floor source scoord
+ (alpha, beta) = scoord
+
+pixel_colorold source spheres scoord
| nearest_object_dist > 0 = objcolor
- | beta == 0 = (0, 255, 0)
- | otherwise = skycolor source scoord
+ | beta == 0 = (0, 255, 0)
+ | otherwise = skycolor source scoord
where (_, beta) = scoord
(nearest_object_dist, objcolor) = nearest_obj source scoord spheres