diff options
Diffstat (limited to 'CubeMap.hs')
-rw-r--r-- | CubeMap.hs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/CubeMap.hs b/CubeMap.hs new file mode 100644 index 0000000..337881f --- /dev/null +++ b/CubeMap.hs | |||
@@ -0,0 +1,72 @@ | |||
1 | module CubeMap | ||
2 | ( loadSkyboxes | ||
3 | , Skyboxes(..) | ||
4 | ) where | ||
5 | |||
6 | import LambdaCube.GL as LC | ||
7 | import LambdaCube.GL.Mesh as LC | ||
8 | import LambdaCube.GL.Data (uploadCubeMapToGPU) | ||
9 | |||
10 | import Data.Maybe | ||
11 | import Control.Monad | ||
12 | import Data.List | ||
13 | import System.Directory | ||
14 | import System.FilePath | ||
15 | import Codec.Archive.Zip | ||
16 | import qualified Data.ByteString.Lazy as Lazy | ||
17 | import Codec.Picture as Juicy | ||
18 | |||
19 | image_names_xyz_dir :: [String] | ||
20 | image_names_xyz_dir = | ||
21 | [ "xforward" | ||
22 | , "xreverse" | ||
23 | , "yforward" | ||
24 | , "yreverse" | ||
25 | , "zforward" | ||
26 | , "zreverse" ] | ||
27 | |||
28 | image_names_np_xyz :: [String] | ||
29 | image_names_np_xyz = | ||
30 | [ "negx" | ||
31 | , "negy" | ||
32 | , "negz" | ||
33 | , "posx" | ||
34 | , "posy" | ||
35 | , "posz" ] | ||
36 | |||
37 | |||
38 | filterImageNames :: [String] -> [String] | ||
39 | filterImageNames fns = map snd $ sortOn fst | ||
40 | $ go (zip [0..] image_names_xyz_dir) | ||
41 | (zip [1,3,5,0,2,4] image_names_np_xyz) | ||
42 | $ sort fns | ||
43 | where | ||
44 | go ((n,x):xs) ys (fn:fns) | x `isPrefixOf` fn = (n,fn) : go xs ys fns | ||
45 | go xs ((n,y):ys) (fn:fns) | y `isPrefixOf` fn = (n,fn) : go xs ys fns | ||
46 | go xs ys ( _:fns) = go xs ys fns | ||
47 | go xs ys [] = [] | ||
48 | |||
49 | |||
50 | data Skyboxes = Skyboxes | ||
51 | { skyboxCount :: Int | ||
52 | , skyboxNames :: [String] | ||
53 | , skyboxLoad :: Int -> IO (Either String [DynamicImage]) | ||
54 | } | ||
55 | |||
56 | loadSkyboxes :: IO Skyboxes | ||
57 | loadSkyboxes = do | ||
58 | let dir = "./skyboxes" | ||
59 | zips <- listDirectory dir | ||
60 | let len = length zips | ||
61 | return Skyboxes | ||
62 | { skyboxCount = len | ||
63 | , skyboxNames = zips | ||
64 | , skyboxLoad = \n -> do | ||
65 | let fn = zips !! mod n len | ||
66 | archive <- toArchive <$> Lazy.readFile (dir </> fn) | ||
67 | let es = mapMaybe (`findEntryByPath` archive) $ filterImageNames (filesInArchive archive) | ||
68 | imgs <- forM es $ \entry -> do | ||
69 | return $ Juicy.decodeImage $ Lazy.toStrict $ fromEntry entry | ||
70 | return $ sequence imgs | ||
71 | } | ||
72 | |||