-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathraytracer.hs
47 lines (36 loc) · 1.53 KB
/
raytracer.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# LANGUAGE RecordWildCards, NamedFieldPuns #-}
-- Needed libraries:
--cabal install bmp
--cabal install parsec-numbers
--cabal install vect
import Codec.BMP
import qualified Data.ByteString as BS
import Raytrace
import Types
import Scene
import SceneParser (loadSceneFile, rawScene)
----------------------------------------------------------------
main = do
--scene@SceneConfig {width, height, output} <- loadSceneFile "./scenes/scene4-emission.test"
--scene@SceneConfig {width, height, output} <- loadSceneFile "./scenes/scene4-diffuse.test"
--scene@SceneConfig {width, height, output} <- loadSceneFile "./scenes/scene4-specular.test"
--scene@SceneConfig {width, height, output} <- loadSceneFile "./scenes/scene5.test"
scene@SceneConfig {width, height, output} <- loadSceneFile "./scenes/scene6.test"
--scene@SceneConfig {width, height, output} <- loadSceneFile "./scenes/scene7.test"
--s <- rawScene "./scenes/scene6.test"
--print scene
print "Ray tracing image..."
let image = raytraceImage scene
let rgba = imageToByteString image
let bmp = packRGBA32ToBMP24 (fromIntegral width) (fromIntegral height) rgba
let filename = (reverse $ drop 4 $ reverse output) ++ ".bmp"
writeBMP filename bmp
print "Done!"
imageToByteString :: [Color] -> BS.ByteString
imageToByteString image = BS.pack imageAsWord8Array
where
imageAsWord8Array = concatMap colorToWord8Array image
colorToWord8Array (Color r g b) = [
fromIntegral (floor ((min 1 r)*255)),
fromIntegral (floor ((min 1 g)*255)),
fromIntegral (floor ((min 1 b)*255)), 255]