worked a bit further

This commit is contained in:
Nicole Dresselhaus 2014-10-22 17:37:07 +02:00
parent 89a83a1579
commit 551685e131
2 changed files with 34 additions and 18 deletions

View File

@ -1,4 +1,4 @@
-- Initial raytrace.cabal generated by cabal init. For further -- Initial raytrace.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/ -- documentation, see http://haskell.org/cabal/users-guide/
-- The name of the package. -- The name of the package.
@ -63,7 +63,9 @@ executable raytrace
attoparsec >= 0.12, attoparsec >= 0.12,
bytestring >= 0.10, bytestring >= 0.10,
linear >= 1.10, linear >= 1.10,
JuicyPixels >= 3.1 JuicyPixels >= 3.1,
parallel >= 3.2,
vector >= 0.10
-- Directories containing source files. -- Directories containing source files.
-- hs-source-dirs: -- hs-source-dirs:

View File

@ -6,12 +6,16 @@ import Codec.Picture.Png
import Codec.Picture.Types import Codec.Picture.Types
import qualified Data.ByteString as B import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8 import qualified Data.ByteString.Char8 as B8
import Data.Vector hiding ((++),map)
import Data.Word (Word8)
import Data.Attoparsec import Data.Attoparsec
import Scene.Parser import Scene.Parser
import Scene.Types import Scene.Types
import Debug.Trace
findCamera :: [ObjectParser] -> Either String Camera findCamera :: [ObjectParser] -> Either String Camera
findCamera [] = Left "No camera found" findCamera [] = Left "No camera found"
findCamera (a:as) = case a of findCamera (a:as) = case a of
@ -50,8 +54,9 @@ filterObjects (a:as) = case a of
OpP p -> P p:filterObjects as OpP p -> P p:filterObjects as
_ -> filterObjects as _ -> filterObjects as
validateScene :: [ObjectParser] -> Either String Scene validateAndParseScene :: B8.ByteString -> Either String Scene
validateScene obs = do validateAndParseScene f = do
obs <- parseScene f
cam <- findCamera obs cam <- findCamera obs
depth <- findDepth obs depth <- findDepth obs
amb <- findAmbience obs amb <- findAmbience obs
@ -67,21 +72,30 @@ validateScene obs = do
, sceneObjects = objects , sceneObjects = objects
} }
render :: Scene -> Image PixelRGB8 render :: Int -> Int -> Scene -> Int -> PixelRGB8
render s = generateImage pixelRenderer 250 300 render w h s index = trace (show (x,y)) PixelRGB8 255 (fromIntegral x) (fromIntegral y)
where pixelRenderer x y = PixelRGB8 128 128 128 where
y = index `mod` w
x = index `div` w
data Ray = Ray (V3 Float) (V3 Float)
data Collision = Collision (V3 Float) Float Collidable
intersect :: Ray -> Collidable -> Maybe Collision
intersect (Ray ro rd) (S (Sphere sc sr _) = undefined
intersect _ = undefined
main :: IO () main :: IO ()
main = do main = do
f <- B.readFile "scenes/test.sce" f <- B.readFile "scenes/test.sce"
rawScene <- return $ parseScene f case validateAndParseScene f of
case rawScene of Left error -> putStrLn $ "Error: " ++ error
Left error -> putStrLn $ "error Parsing: " ++ error Right s -> do
Right raw -> do let (w,h) = (width . sceneCamera $ s, height . sceneCamera $ s)
scene <- return $ validateScene raw imdata = map (render w h s) [0..w*h-1]
case scene of imvec = fromList imdata
Left error -> putStrLn $ "Error: " ++ error im = generateImage (\x y -> imvec ! (x*w+y)) w h
Right s -> do print s
print s print (w,h)
im <- return $ render s writePng "out.png" im
writePng "out.png" im