diff options
author | Joe Crayne <joe@jerkface.net> | 2019-06-19 18:05:17 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-06-19 18:05:17 -0400 |
commit | 018f8847fe987abe3f036ff8739468f164429e7d (patch) | |
tree | 3a186f2169a12fa752b6ce257b2e8f9bd72029bc | |
parent | 797f800ab38505f0866a59f0e4ea38ce90d85c32 (diff) |
Provided data structure for early capture of group-masks.
-rw-r--r-- | LoadMesh.hs | 34 |
1 files 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 | |||
27 | import Data.Aeson | 27 | import Data.Aeson |
28 | import Mask | 28 | import Mask |
29 | 29 | ||
30 | type MeshData = ( [(Mesh,Maybe Text)] -- List of uniform-material meshes (and the name of the material). | 30 | data MaterialMesh m = MaterialMesh |
31 | { materialMesh :: m | ||
32 | , materialName :: Maybe Text | ||
33 | , materialMasks :: Map Text Mask | ||
34 | } | ||
35 | |||
36 | type MeshData = ( [MaterialMesh Mesh] -- List of uniform-material meshes (and the name of the material). | ||
31 | , ( MtlLib -- Material definitions. | 37 | , ( MtlLib -- Material definitions. |
32 | , FilePath ) -- Path to wavefront obj file. | 38 | , FilePath ) -- Path to wavefront obj file. |
33 | ) | 39 | ) |
@@ -108,11 +114,13 @@ transformMesh t m = m | |||
108 | { mAttributes = Map.adjust (tranformAttribute t) "position" (mAttributes m) | 114 | { mAttributes = Map.adjust (tranformAttribute t) "position" (mAttributes m) |
109 | } | 115 | } |
110 | 116 | ||
111 | uploadOBJToGPU :: Maybe BoundingBox -> MeshData -> IO ([(GPUMesh, Maybe Text)],Matrix Float) | 117 | uploadOBJToGPU :: Maybe BoundingBox -> MeshData -> IO ([MaterialMesh GPUMesh],Matrix Float) |
112 | uploadOBJToGPU scalebb (subModels,(mtlLib,objpath)) = do | 118 | uploadOBJToGPU scalebb (subModels,(mtlLib,objpath)) = do |
113 | let meshbb = foldMap (attribBoundingBox . mAttributes . fst) subModels :: BoundingBox | 119 | let meshbb = foldMap (attribBoundingBox . mAttributes . materialMesh) subModels :: BoundingBox |
114 | m = maybe (ident 4) (scaleWithin meshbb) scalebb | 120 | m = maybe (ident 4) (scaleWithin meshbb) scalebb |
115 | gpuSubModels <- forM subModels $ \(mesh,mat) -> LambdaCubeGL.uploadMeshToGPU (transformMesh m mesh) >>= \a -> return (a,mat) | 121 | gpuSubModels <- forM subModels $ \matmesh -> do |
122 | a <- LambdaCubeGL.uploadMeshToGPU (transformMesh m (materialMesh matmesh)) | ||
123 | return matmesh { materialMesh = a } | ||
116 | return (gpuSubModels,m) | 124 | return (gpuSubModels,m) |
117 | 125 | ||
118 | uploadMtlLib :: (MtlLib,FilePath) -> IO (Map Text (ObjMaterial,TextureData)) | 126 | uploadMtlLib :: (MtlLib,FilePath) -> IO (Map Text (ObjMaterial,TextureData)) |
@@ -130,8 +138,12 @@ uploadMtlLib (mtlLib,objpath) = do | |||
130 | -- pair textures and materials | 138 | -- pair textures and materials |
131 | return $ (\a -> (a, maybe whiteTex (fromMaybe checkerTex . flip Map.lookup textureLib) . mtl_map_Kd $ a)) <$> mtlLib | 139 | return $ (\a -> (a, maybe whiteTex (fromMaybe checkerTex . flip Map.lookup textureLib) . mtl_map_Kd $ a)) <$> mtlLib |
132 | 140 | ||
133 | objToMesh :: WavefrontOBJ -> [(Mesh,Maybe Text)] | 141 | objToMesh :: WavefrontOBJ -> [MaterialMesh Mesh] |
134 | objToMesh OBJ{..} = [(toMesh faceGroup, elMtl . head $ faceGroup) | faceGroup <- faces] where | 142 | objToMesh OBJ{..} = [ MaterialMesh (toMesh faceGroup) |
143 | (elMtl . head $ faceGroup) | ||
144 | Map.empty {- TODO -} | ||
145 | | faceGroup <- faces ] | ||
146 | where | ||
135 | faces = groupBy (\a b -> elMtl a == elMtl b) (V.toList objFaces) | 147 | faces = groupBy (\a b -> elMtl a == elMtl b) (V.toList objFaces) |
136 | toMesh l = Mesh | 148 | toMesh l = Mesh |
137 | { mAttributes = Map.fromList | 149 | { mAttributes = Map.fromList |
@@ -166,14 +178,14 @@ objSpan obj = case Map.elems (objAttributes obj) of | |||
166 | 178 | ||
167 | 179 | ||
168 | 180 | ||
169 | addOBJToObjectArray :: GLStorage -> String -> [(GPUMesh, Maybe Text)] -> Map Text (ObjMaterial,TextureData) | 181 | addOBJToObjectArray :: GLStorage -> String -> [MaterialMesh GPUMesh] -> Map Text (ObjMaterial,TextureData) |
170 | -> IO [MaskableObject] | 182 | -> IO [MaskableObject] |
171 | addOBJToObjectArray storage slotName objMesh mtlLib = forM objMesh $ \(mesh,mat) -> do | 183 | addOBJToObjectArray storage slotName objMesh mtlLib = forM objMesh $ \matmesh -> do |
172 | obj <- LambdaCubeGL.addMeshToObjectArray storage slotName ["diffuseTexture","diffuseColor"] mesh | 184 | obj <- LambdaCubeGL.addMeshToObjectArray storage slotName ["diffuseTexture","diffuseColor"] (materialMesh matmesh) |
173 | -- diffuseTexture and diffuseColor values can change on each model | 185 | -- diffuseTexture and diffuseColor values can change on each model |
174 | case mat >>= flip Map.lookup mtlLib of | 186 | case (materialName matmesh) >>= flip Map.lookup mtlLib of |
175 | Nothing -> return () | 187 | Nothing -> return () |
176 | Just (ObjMaterial{..},t) -> LC.updateObjectUniforms obj $ do | 188 | Just (ObjMaterial{..},t) -> LC.updateObjectUniforms obj $ do |
177 | "diffuseTexture" @= return t -- set model's diffuse texture | 189 | "diffuseTexture" @= return t -- set model's diffuse texture |
178 | "diffuseColor" @= let (r,g,b) = mtl_Kd in return (V4 r g b mtl_Tr) | 190 | "diffuseColor" @= let (r,g,b) = mtl_Kd in return (V4 r g b mtl_Tr) |
179 | return $ MaskableObject obj $ maybe Map.empty (`Map.singleton` objSpan obj) mat | 191 | return $ MaskableObject obj $ maybe Map.empty (`Map.singleton` objSpan obj) (materialName matmesh) |