htrace/src/Scene/Parser.hs

154 lines
4.6 KiB
Haskell
Raw Normal View History

2014-10-21 22:29:57 +00:00
{-# LANGUAGE OverloadedStrings #-}
2014-10-21 19:40:48 +00:00
module Scene.Parser where
2014-10-21 22:29:57 +00:00
import Control.Applicative
import Data.Attoparsec.ByteString.Char8
2014-10-21 19:40:48 +00:00
import Data.Functor
import Data.ByteString as B
import Data.ByteString.Char8 as B8
2014-10-21 22:29:57 +00:00
import Linear.V3
2014-10-21 19:40:48 +00:00
import Scene.Types
2014-10-21 22:29:57 +00:00
import Debug.Trace
2014-10-21 19:40:48 +00:00
partitionEithers :: [Either a b] -> ([a],[b])
partitionEithers e = go e ([],[])
where
go (Left a:as) (xs,ys) = go as (a:xs,ys)
go (Right b:bs) (xs,ys) = go bs (xs,b:ys)
2014-10-21 22:29:57 +00:00
go [] (xs,ys) = (xs,ys)
preprocess :: ByteString -> ByteString
preprocess = B8.unlines . clean . B8.lines
where
clean :: [ByteString] -> [ByteString]
clean (a:as) = if B8.all isWhitespace a
|| B8.head a == '#'
then clean as else a:clean as
clean [] = []
isWhitespace :: Char -> Bool
isWhitespace ' ' = True
isWhitespace '\t' = True
isWhitespace '\n' = True
isWhitespace '\r' = True
isWhitespace _ = False
2014-10-21 19:40:48 +00:00
2014-10-21 22:29:57 +00:00
parseScene :: ByteString -> Either String [ObjectParser]
parseScene s = parseOnly (many parseObject) (preprocess s)
2014-10-21 19:40:48 +00:00
2014-10-21 22:29:57 +00:00
parseObject :: Parser ObjectParser
2014-10-21 19:40:48 +00:00
parseObject = do
t <- string "camera" <|>
string "depth" <|>
string "background" <|>
string "ambience" <|>
string "light" <|>
string "sphere" <|>
string "plane" <|>
string "mesh"
case t of
"camera" -> parseCamera
2014-10-21 22:29:57 +00:00
"depth" -> do
skipSpace
d <- decimal
endOfLine
return $ OpR d
"background" -> do
c <- parseVector
endOfLine
return $ OpB (Background c)
"ambience" -> do
c <- parseVector
endOfLine
return $ OpA (Ambience c)
"light" -> do
p <- parseVector
skipSpace
c <- parseVector
intensity <- double <|> return 0
i <- return $ if intensity == 0
then Nothing
else Just (fromRational . toRational $ intensity)
endOfLine
return $ OpL (Light p c i)
"sphere" -> parseSphere
"plane" -> parsePlane
_ -> undefined
2014-10-21 19:40:48 +00:00
2014-10-21 22:29:57 +00:00
parseCamera :: Parser ObjectParser
2014-10-21 19:40:48 +00:00
parseCamera = do
2014-10-21 22:29:57 +00:00
eye' <- parseVector
center' <- parseVector
up' <- parseVector
skipSpace
fovy' <- double
skipSpace
width' <- decimal
skipSpace
height' <- decimal
endOfLine
return $ OpC $ Camera
{ eye = eye'
, center = center'
, up = up'
, fovy = (fromRational . toRational) fovy'
, width = width'
, height = height'
}
parsePlane :: Parser ObjectParser
parsePlane = do
c <- parseVector
n <- parseVector
m <- parseMaterial
endOfLine
return $ OpP Plane
{ planeCenter = c
, planeNormal = n
, planeMaterial = m
}
parseSphere :: Parser ObjectParser
parseSphere = do
p <- parseVector
skipSpace
r <- double
m <- parseMaterial
endOfLine
return $ OpS Sphere
{ sphereCenter = p
, sphereRadius = (fromRational . toRational) r
, sphereMaterial = m
}
parseMaterial :: Parser Material
parseMaterial = do
a <- parseVector
d <- parseVector
s <- parseVector
skipSpace
sh <- double
skipSpace
r <- double
return $ Material
{ materialAmbience = a
, materialDiffuse = d
, materialSpec = s
, materialShinyness = (fromRational . toRational) sh
, materialReflection = (fromRational . toRational) r
}
2014-10-21 19:40:48 +00:00
parseVector :: Parser (V3 Float)
parseVector = do
2014-10-21 22:29:57 +00:00
skipSpace
a <- double
skipSpace
b <- double
skipSpace
c <- double
return $ V3 (f a) (f b) (f c)
where
f = fromRational . toRational --convert Double to Float