diff options
author | Joe Crayne <joe@jerkface.net> | 2019-06-14 21:17:01 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-06-14 21:17:36 -0400 |
commit | fc20aae36dfa150795967c46048abc8581b37d48 (patch) | |
tree | cc758792bd1ef7cedf05ccd7af5e1b7368ca57f1 | |
parent | d763106d889c94bfb55096cdd154aeaf7d48f9df (diff) |
Switched to homegrown wavefront parser.
-rw-r--r-- | LoadMesh.hs | 14 | ||||
-rw-r--r-- | lambda-gtk.cabal | 2 |
2 files changed, 9 insertions, 7 deletions
diff --git a/LoadMesh.hs b/LoadMesh.hs index 69e66d6..affadba 100644 --- a/LoadMesh.hs +++ b/LoadMesh.hs | |||
@@ -12,23 +12,25 @@ import Data.Map (Map) | |||
12 | import qualified Data.Map as Map | 12 | import qualified Data.Map as Map |
13 | import qualified Data.Vector as V | 13 | import qualified Data.Vector as V |
14 | import qualified Data.ByteString as SB | 14 | import qualified Data.ByteString as SB |
15 | import qualified Data.ByteString.Lazy.Char8 as L | ||
15 | import Data.Text (unpack,Text) | 16 | import Data.Text (unpack,Text) |
16 | import Data.List (groupBy,nub) | 17 | import Data.List (groupBy,nub) |
17 | 18 | ||
18 | import Codec.Picture as Juicy | 19 | import Codec.Picture as Juicy |
19 | import Codec.Wavefront | 20 | import Wavefront |
21 | import Wavefront.Types | ||
20 | import Data.Aeson | 22 | import Data.Aeson |
21 | 23 | ||
22 | type MeshData = ([(Mesh,Maybe Text)],MtlLib) | 24 | type MeshData = ([(Mesh,Maybe Text)],MtlLib) |
23 | 25 | ||
24 | loadOBJ :: String -> IO (Either String MeshData) | 26 | loadOBJ :: String -> IO (Either String MeshData) |
25 | loadOBJ fname = fromFile fname >>= \case -- load geometry | 27 | loadOBJ fname = L.readFile fname >>= \bs -> do |
26 | Left err -> putStrLn err >> return (Left err) | 28 | let obj@OBJ{..} = parse bs |
27 | Right obj@WavefrontOBJ{..} -> do | ||
28 | -- load materials | 29 | -- load materials |
29 | mtlLib <- mconcat . V.toList <$> mapM (readMtl . unpack) objMtlLibs | 30 | mtlLib <- mconcat . V.toList <$> mapM (readMtl . unpack) objMtlLibs |
30 | return $ Right (objToMesh obj,mtlLib) | 31 | return $ Right (objToMesh obj,mtlLib) |
31 | 32 | ||
33 | |||
32 | uploadOBJToGPU :: MeshData -> IO ([(GPUMesh, Maybe Text)], MtlLib) | 34 | uploadOBJToGPU :: MeshData -> IO ([(GPUMesh, Maybe Text)], MtlLib) |
33 | uploadOBJToGPU (subModels,mtlLib) = do | 35 | uploadOBJToGPU (subModels,mtlLib) = do |
34 | gpuSubModels <- forM subModels $ \(mesh,mat) -> LambdaCubeGL.uploadMeshToGPU mesh >>= \a -> return (a,mat) | 36 | gpuSubModels <- forM subModels $ \(mesh,mat) -> LambdaCubeGL.uploadMeshToGPU mesh >>= \a -> return (a,mat) |
@@ -50,7 +52,7 @@ uploadMtlLib mtlLib = do | |||
50 | return $ (\a -> (a, maybe whiteTex (fromMaybe checkerTex . flip Map.lookup textureLib) . mtl_map_Kd $ a)) <$> mtlLib | 52 | return $ (\a -> (a, maybe whiteTex (fromMaybe checkerTex . flip Map.lookup textureLib) . mtl_map_Kd $ a)) <$> mtlLib |
51 | 53 | ||
52 | objToMesh :: WavefrontOBJ -> [(Mesh,Maybe Text)] | 54 | objToMesh :: WavefrontOBJ -> [(Mesh,Maybe Text)] |
53 | objToMesh WavefrontOBJ{..} = [(toMesh faceGroup, elMtl . head $ faceGroup) | faceGroup <- faces] where | 55 | objToMesh OBJ{..} = [(toMesh faceGroup, elMtl . head $ faceGroup) | faceGroup <- faces] where |
54 | faces = groupBy (\a b -> elMtl a == elMtl b) (V.toList objFaces) | 56 | faces = groupBy (\a b -> elMtl a == elMtl b) (V.toList objFaces) |
55 | toMesh l = Mesh | 57 | toMesh l = Mesh |
56 | { mAttributes = Map.fromList | 58 | { mAttributes = Map.fromList |
@@ -66,7 +68,7 @@ objToMesh WavefrontOBJ{..} = [(toMesh faceGroup, elMtl . head $ faceGroup) | fac | |||
66 | defaultPosition = Location 0 0 0 0 | 68 | defaultPosition = Location 0 0 0 0 |
67 | defaultNormal = Normal 0 0 0 | 69 | defaultNormal = Normal 0 0 0 |
68 | defaultTexCoord = TexCoord 0 0 0 | 70 | defaultTexCoord = TexCoord 0 0 0 |
69 | v !- i = v V.!? (i-1) | 71 | v !- i = v V.!? i |
70 | toVertex FaceIndex{..} = ( let Location x y z w = fromMaybe defaultPosition (objLocations !- faceLocIndex) in V4 x y z w | 72 | toVertex FaceIndex{..} = ( let Location x y z w = fromMaybe defaultPosition (objLocations !- faceLocIndex) in V4 x y z w |
71 | , let Normal x y z = fromMaybe defaultNormal ((objNormals !-) =<< faceNorIndex) in V3 x y z | 73 | , let Normal x y z = fromMaybe defaultNormal ((objNormals !-) =<< faceNorIndex) in V3 x y z |
72 | , let TexCoord x y z = fromMaybe defaultTexCoord ((objTexCoords !-) =<< faceTexCoordIndex) in V3 x y z | 74 | , let TexCoord x y z = fromMaybe defaultTexCoord ((objTexCoords !-) =<< faceTexCoordIndex) in V3 x y z |
diff --git a/lambda-gtk.cabal b/lambda-gtk.cabal index 86269e5..75bcdfc 100644 --- a/lambda-gtk.cabal +++ b/lambda-gtk.cabal | |||
@@ -61,7 +61,7 @@ executable meshsketch | |||
61 | -- writer monad | 61 | -- writer monad |
62 | mtl, | 62 | mtl, |
63 | -- rendering | 63 | -- rendering |
64 | lambdacube-ir, lambdacube-gl >=0.5.3, OpenGL, wavefront, | 64 | lambdacube-ir, lambdacube-gl >=0.5.3, OpenGL, wavefront-obj, |
65 | -- GUI | 65 | -- GUI |
66 | gi-gdk , gi-glib , gi-gobject , gi-gtk , haskell-gi-base | 66 | gi-gdk , gi-glib , gi-gobject , gi-gtk , haskell-gi-base |
67 | 67 | ||