summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-06-14 21:17:01 -0400
committerJoe Crayne <joe@jerkface.net>2019-06-14 21:17:36 -0400
commitfc20aae36dfa150795967c46048abc8581b37d48 (patch)
treecc758792bd1ef7cedf05ccd7af5e1b7368ca57f1
parentd763106d889c94bfb55096cdd154aeaf7d48f9df (diff)
Switched to homegrown wavefront parser.
-rw-r--r--LoadMesh.hs14
-rw-r--r--lambda-gtk.cabal2
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)
12import qualified Data.Map as Map 12import qualified Data.Map as Map
13import qualified Data.Vector as V 13import qualified Data.Vector as V
14import qualified Data.ByteString as SB 14import qualified Data.ByteString as SB
15import qualified Data.ByteString.Lazy.Char8 as L
15import Data.Text (unpack,Text) 16import Data.Text (unpack,Text)
16import Data.List (groupBy,nub) 17import Data.List (groupBy,nub)
17 18
18import Codec.Picture as Juicy 19import Codec.Picture as Juicy
19import Codec.Wavefront 20import Wavefront
21import Wavefront.Types
20import Data.Aeson 22import Data.Aeson
21 23
22type MeshData = ([(Mesh,Maybe Text)],MtlLib) 24type MeshData = ([(Mesh,Maybe Text)],MtlLib)
23 25
24loadOBJ :: String -> IO (Either String MeshData) 26loadOBJ :: String -> IO (Either String MeshData)
25loadOBJ fname = fromFile fname >>= \case -- load geometry 27loadOBJ 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
32uploadOBJToGPU :: MeshData -> IO ([(GPUMesh, Maybe Text)], MtlLib) 34uploadOBJToGPU :: MeshData -> IO ([(GPUMesh, Maybe Text)], MtlLib)
33uploadOBJToGPU (subModels,mtlLib) = do 35uploadOBJToGPU (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
52objToMesh :: WavefrontOBJ -> [(Mesh,Maybe Text)] 54objToMesh :: WavefrontOBJ -> [(Mesh,Maybe Text)]
53objToMesh WavefrontOBJ{..} = [(toMesh faceGroup, elMtl . head $ faceGroup) | faceGroup <- faces] where 55objToMesh 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