diff options
author | Joe Crayne <joe@jerkface.net> | 2019-07-28 21:47:10 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-07-28 21:50:14 -0400 |
commit | b672af0e4b12f8f22f3e11fe987c24d09ff0999d (patch) | |
tree | 903dc4a16fcaee3b4dfedf4944028ade2f3b965b | |
parent | 5aba33b6f801910a8f5e1587c13678e237e31782 (diff) |
Better handle missing material lib.
-rw-r--r-- | LoadMesh.hs | 17 | ||||
-rw-r--r-- | MtlParser.hs | 1 |
2 files changed, 15 insertions, 3 deletions
diff --git a/LoadMesh.hs b/LoadMesh.hs index cd7a8ad..702f2c9 100644 --- a/LoadMesh.hs +++ b/LoadMesh.hs | |||
@@ -12,6 +12,7 @@ import MtlParser | |||
12 | 12 | ||
13 | import Control.Arrow | 13 | import Control.Arrow |
14 | import Control.Monad | 14 | import Control.Monad |
15 | import Data.Functor | ||
15 | import Data.Int | 16 | import Data.Int |
16 | import Data.List as List | 17 | import Data.List as List |
17 | import Data.Maybe | 18 | import Data.Maybe |
@@ -60,7 +61,9 @@ loadOBJ :: String -> IO (Either String MeshData) | |||
60 | loadOBJ fname = L.readFile fname >>= \bs -> do | 61 | loadOBJ fname = L.readFile fname >>= \bs -> do |
61 | let obj@OBJ{..} = Wavefront.parse bs | 62 | let obj@OBJ{..} = Wavefront.parse bs |
62 | -- load materials | 63 | -- load materials |
63 | mtlLib <- mapM (readMtlWithFallback . relativeFrom fname . unpack) objMtlLibs | 64 | mtlLib <- if V.null objMtlLibs |
65 | then return $ V.singleton (Map.singleton "" $ newMaterial "") | ||
66 | else mapM (readMtlWithFallback . relativeFrom fname . unpack) objMtlLibs | ||
64 | return $ Right MeshData | 67 | return $ Right MeshData |
65 | { matMeshes = objToMesh obj | 68 | { matMeshes = objToMesh obj |
66 | , matLib = (mtlLib,fname) | 69 | , matLib = (mtlLib,fname) |
@@ -175,7 +178,10 @@ uploadMtlLib (mtlLib,objpath) = do | |||
175 | Right img -> LC.uploadTexture2DToGPU img | 178 | Right img -> LC.uploadTexture2DToGPU img |
176 | whiteTex <- LC.uploadTexture2DToGPU whiteImage | 179 | whiteTex <- LC.uploadTexture2DToGPU whiteImage |
177 | -- pair textures and materials | 180 | -- pair textures and materials |
178 | return $ fmap (\a -> (a, maybe whiteTex (fromMaybe checkerTex . flip Map.lookup textureLib) . mtl_map_Kd $ a)) <$> mtlLib | 181 | -- type MtlLib = Map Text ObjMaterial |
182 | let withTextureData mat = (,) mat $ maybe whiteTex (fromMaybe checkerTex . flip Map.lookup textureLib) | ||
183 | $ mtl_map_Kd mat | ||
184 | return $ fmap withTextureData <$> mtlLib | ||
179 | 185 | ||
180 | vecLocation :: Location -> StorableV.Vector Float | 186 | vecLocation :: Location -> StorableV.Vector Float |
181 | vecLocation (Location x y z w) = StorableV.fromList [x,y,z,w] | 187 | vecLocation (Location x y z w) = StorableV.fromList [x,y,z,w] |
@@ -259,13 +265,18 @@ addOBJToObjectArray :: GLStorage -> String | |||
259 | addOBJToObjectArray storage slotName objMesh mtlLib = forM objMesh $ \matmesh -> do | 265 | addOBJToObjectArray storage slotName objMesh mtlLib = forM objMesh $ \matmesh -> do |
260 | obj <- case materialName matmesh >>= searchMaterial mtlLib of | 266 | obj <- case materialName matmesh >>= searchMaterial mtlLib of |
261 | Nothing -> do | 267 | Nothing -> do |
262 | let slotnm = slotName ++ "0" | 268 | let slotnm = slotName ++ "1" |
263 | obj <- LambdaCubeGL.addMeshToObjectArray storage | 269 | obj <- LambdaCubeGL.addMeshToObjectArray storage |
264 | slotnm | 270 | slotnm |
265 | [ "diffuseTexture" | 271 | [ "diffuseTexture" |
266 | , "diffuseColor" | 272 | , "diffuseColor" |
267 | , "specularReflectivity"] | 273 | , "specularReflectivity"] |
268 | (materialMesh matmesh) | 274 | (materialMesh matmesh) |
275 | let (white,whiteTex) = (mtlLib V.! 0) Map.! "" | ||
276 | LC.updateObjectUniforms obj $ do | ||
277 | "diffuseTexture" @= return whiteTex -- set model's diffuse texture | ||
278 | "diffuseColor" @= let (r,g,b) = mtl_Kd white in return $ V4 r g b (mtl_Tr white) | ||
279 | "specularReflectivity" @= let (r,g,b) = mtl_Ks white in return $ V4 r g b (mtl_Ns white) | ||
269 | return obj | 280 | return obj |
270 | Just (ObjMaterial{..},t) -> do | 281 | Just (ObjMaterial{..},t) -> do |
271 | let slotnm = slotName ++ show (if 0 <= mtl_illum && mtl_illum <= 2 then mtl_illum else 2) | 282 | let slotnm = slotName ++ show (if 0 <= mtl_illum && mtl_illum <= 2 then mtl_illum else 2) |
diff --git a/MtlParser.hs b/MtlParser.hs index ed952d0..a59c0eb 100644 --- a/MtlParser.hs +++ b/MtlParser.hs | |||
@@ -4,6 +4,7 @@ module MtlParser | |||
4 | , parseMtl | 4 | , parseMtl |
5 | , readMtl | 5 | , readMtl |
6 | , readMtlWithFallback | 6 | , readMtlWithFallback |
7 | , newMaterial | ||
7 | ) where | 8 | ) where |
8 | 9 | ||
9 | import Data.Map (Map) | 10 | import Data.Map (Map) |