From 018f8847fe987abe3f036ff8739468f164429e7d Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Wed, 19 Jun 2019 18:05:17 -0400 Subject: Provided data structure for early capture of group-masks. --- LoadMesh.hs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/LoadMesh.hs b/LoadMesh.hs index 7cc61bc..bfc5070 100644 --- a/LoadMesh.hs +++ b/LoadMesh.hs @@ -27,7 +27,13 @@ import Wavefront.Types import Data.Aeson import Mask -type MeshData = ( [(Mesh,Maybe Text)] -- List of uniform-material meshes (and the name of the material). +data MaterialMesh m = MaterialMesh + { materialMesh :: m + , materialName :: Maybe Text + , materialMasks :: Map Text Mask + } + +type MeshData = ( [MaterialMesh Mesh] -- List of uniform-material meshes (and the name of the material). , ( MtlLib -- Material definitions. , FilePath ) -- Path to wavefront obj file. ) @@ -108,11 +114,13 @@ transformMesh t m = m { mAttributes = Map.adjust (tranformAttribute t) "position" (mAttributes m) } -uploadOBJToGPU :: Maybe BoundingBox -> MeshData -> IO ([(GPUMesh, Maybe Text)],Matrix Float) +uploadOBJToGPU :: Maybe BoundingBox -> MeshData -> IO ([MaterialMesh GPUMesh],Matrix Float) uploadOBJToGPU scalebb (subModels,(mtlLib,objpath)) = do - let meshbb = foldMap (attribBoundingBox . mAttributes . fst) subModels :: BoundingBox + let meshbb = foldMap (attribBoundingBox . mAttributes . materialMesh) subModels :: BoundingBox m = maybe (ident 4) (scaleWithin meshbb) scalebb - gpuSubModels <- forM subModels $ \(mesh,mat) -> LambdaCubeGL.uploadMeshToGPU (transformMesh m mesh) >>= \a -> return (a,mat) + gpuSubModels <- forM subModels $ \matmesh -> do + a <- LambdaCubeGL.uploadMeshToGPU (transformMesh m (materialMesh matmesh)) + return matmesh { materialMesh = a } return (gpuSubModels,m) uploadMtlLib :: (MtlLib,FilePath) -> IO (Map Text (ObjMaterial,TextureData)) @@ -130,8 +138,12 @@ uploadMtlLib (mtlLib,objpath) = do -- pair textures and materials return $ (\a -> (a, maybe whiteTex (fromMaybe checkerTex . flip Map.lookup textureLib) . mtl_map_Kd $ a)) <$> mtlLib -objToMesh :: WavefrontOBJ -> [(Mesh,Maybe Text)] -objToMesh OBJ{..} = [(toMesh faceGroup, elMtl . head $ faceGroup) | faceGroup <- faces] where +objToMesh :: WavefrontOBJ -> [MaterialMesh Mesh] +objToMesh OBJ{..} = [ MaterialMesh (toMesh faceGroup) + (elMtl . head $ faceGroup) + Map.empty {- TODO -} + | faceGroup <- faces ] + where faces = groupBy (\a b -> elMtl a == elMtl b) (V.toList objFaces) toMesh l = Mesh { mAttributes = Map.fromList @@ -166,14 +178,14 @@ objSpan obj = case Map.elems (objAttributes obj) of -addOBJToObjectArray :: GLStorage -> String -> [(GPUMesh, Maybe Text)] -> Map Text (ObjMaterial,TextureData) +addOBJToObjectArray :: GLStorage -> String -> [MaterialMesh GPUMesh] -> Map Text (ObjMaterial,TextureData) -> IO [MaskableObject] -addOBJToObjectArray storage slotName objMesh mtlLib = forM objMesh $ \(mesh,mat) -> do - obj <- LambdaCubeGL.addMeshToObjectArray storage slotName ["diffuseTexture","diffuseColor"] mesh +addOBJToObjectArray storage slotName objMesh mtlLib = forM objMesh $ \matmesh -> do + obj <- LambdaCubeGL.addMeshToObjectArray storage slotName ["diffuseTexture","diffuseColor"] (materialMesh matmesh) -- diffuseTexture and diffuseColor values can change on each model - case mat >>= flip Map.lookup mtlLib of + case (materialName matmesh) >>= flip Map.lookup mtlLib of Nothing -> return () Just (ObjMaterial{..},t) -> LC.updateObjectUniforms obj $ do "diffuseTexture" @= return t -- set model's diffuse texture "diffuseColor" @= let (r,g,b) = mtl_Kd in return (V4 r g b mtl_Tr) - return $ MaskableObject obj $ maybe Map.empty (`Map.singleton` objSpan obj) mat + return $ MaskableObject obj $ maybe Map.empty (`Map.singleton` objSpan obj) (materialName matmesh) -- cgit v1.2.3