diff options
author | Joe Crayne <joe@jerkface.net> | 2019-06-11 02:21:46 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-06-11 02:21:46 -0400 |
commit | d866df206e703854cfa95e6aabe3ed5ed3bfbcaa (patch) | |
tree | 3beb584ce3a21c8df2bb11e6780c8867a1960b82 /src/Graphics/WaveFront | |
parent | 254c7813344075b6a2c915d9724a5e7a18091b87 (diff) |
Fixed Sundqvist v and vt parsing.
Diffstat (limited to 'src/Graphics/WaveFront')
-rw-r--r-- | src/Graphics/WaveFront/Model.hs | 12 | ||||
-rw-r--r-- | src/Graphics/WaveFront/Parse/Common.hs | 19 | ||||
-rw-r--r-- | src/Graphics/WaveFront/Parse/OBJ.hs | 6 | ||||
-rw-r--r-- | src/Graphics/WaveFront/Types.hs | 13 |
4 files changed, 34 insertions, 16 deletions
diff --git a/src/Graphics/WaveFront/Model.hs b/src/Graphics/WaveFront/Model.hs index 96172a8..dfd90ef 100644 --- a/src/Graphics/WaveFront/Model.hs +++ b/src/Graphics/WaveFront/Model.hs | |||
@@ -60,7 +60,7 @@ import Data.Set (Set) | |||
60 | import Data.List (groupBy) | 60 | import Data.List (groupBy) |
61 | import Data.Maybe (listToMaybe, catMaybes) | 61 | import Data.Maybe (listToMaybe, catMaybes) |
62 | 62 | ||
63 | import Linear (V2(..), V3(..)) | 63 | import Linear (V2(..), V3(..), V4(..)) |
64 | 64 | ||
65 | import Control.Lens ((^.), (.~), (%~), (&), _1, _2, _3) | 65 | import Control.Lens ((^.), (.~), (%~), (&), _1, _2, _3) |
66 | 66 | ||
@@ -271,14 +271,16 @@ tessellate = indices %~ triangles | |||
271 | -- - Refactor | 271 | -- - Refactor |
272 | -- - Folding over applicative (fold in parallel) | 272 | -- - Folding over applicative (fold in parallel) |
273 | -- - Make sure the order is right | 273 | -- - Make sure the order is right |
274 | bounds :: (Num f, Ord f, Foldable m, HasVertices (Model f s i m) (m (V3 f))) => Model f s i m -> BoundingBox (V3 f) | 274 | bounds :: (Num f, Ord f, Foldable m, HasVertices (Model f s i m) (m (V4 f))) => Model f s i m -> BoundingBox (V3 f) |
275 | bounds model = fromExtents $ axisBounds (model^.vertices) <$> V3 x y z | 275 | bounds model = fromExtents $ axisBounds (model^.vertices) <$> V3 x y z |
276 | where | 276 | where |
277 | -- TODO | - Factor out 'minmax' | 277 | -- TODO | - Factor out 'minmax' |
278 | minmaxBy :: (Ord o, Num o, Foldable m) => (a -> o) -> m a -> (o, o) | 278 | minmaxBy :: (Ord o, Num o, Foldable m) => (a -> o) -> m a -> (o, o) |
279 | minmaxBy f values = foldr (\val' acc -> let val = f val' in (min val (fst acc), max val (snd acc))) (0, 0) values -- TODO: Factor out | 279 | minmaxBy f values = foldr (\val' acc -> let val = f val' in (min val (fst acc), max val (snd acc))) (0, 0) values -- TODO: Factor out |
280 | 280 | ||
281 | axisBounds vs axis = minmaxBy (^.axis) vs | 281 | axisBounds vs axis = minmaxBy ((^.axis) . proj) vs |
282 | |||
283 | proj (V4 x y z _) = V3 x y z | ||
282 | 284 | ||
283 | -- Orphaned TODOs? | 285 | -- Orphaned TODOs? |
284 | 286 | ||
@@ -316,7 +318,7 @@ diffuseColours faces' = V.concatMap (\f -> V.replicate (V.length $ f^.indices) ( | |||
316 | -- - Add docstrings | 318 | -- - Add docstrings |
317 | 319 | ||
318 | -- | | 320 | -- | |
319 | unindexedVertices :: Model f Text Int Vector -> Maybe (Vector (V3 f)) | 321 | unindexedVertices :: Model f Text Int Vector -> Maybe (Vector (V4 f)) |
320 | unindexedVertices model = sequence $ fromFaceIndices (model^.vertices) (index) (^.ivertex) (model^.faces) | 322 | unindexedVertices model = sequence $ fromFaceIndices (model^.vertices) (index) (^.ivertex) (model^.faces) |
321 | where | 323 | where |
322 | index coords i = coords !? (i-1) | 324 | index coords i = coords !? (i-1) |
@@ -326,7 +328,7 @@ unindexedNormals model = sequence $ fromFaceIndices (model^.normals) (index) (^. | |||
326 | where | 328 | where |
327 | index coords mi = mi >>= \i -> coords !? (i-1) | 329 | index coords mi = mi >>= \i -> coords !? (i-1) |
328 | 330 | ||
329 | unindexedTexcoords :: Model f Text Int Vector -> Maybe (Vector (V2 f)) | 331 | unindexedTexcoords :: Model f Text Int Vector -> Maybe (Vector (V3 f)) |
330 | unindexedTexcoords model = sequence $ fromFaceIndices (model^.texcoords) (index) (^.itexcoord) (model^.faces) | 332 | unindexedTexcoords model = sequence $ fromFaceIndices (model^.texcoords) (index) (^.itexcoord) (model^.faces) |
331 | where | 333 | where |
332 | index coords mi = mi >>= \i -> coords !? (i-1) | 334 | index coords mi = mi >>= \i -> coords !? (i-1) |
diff --git a/src/Graphics/WaveFront/Parse/Common.hs b/src/Graphics/WaveFront/Parse/Common.hs index bfeb2d8..1554d45 100644 --- a/src/Graphics/WaveFront/Parse/Common.hs +++ b/src/Graphics/WaveFront/Parse/Common.hs | |||
@@ -33,10 +33,11 @@ module Graphics.WaveFront.Parse.Common where | |||
33 | -- We'll need these | 33 | -- We'll need these |
34 | -------------------------------------------------------------------------------------------------------------------------------------------- | 34 | -------------------------------------------------------------------------------------------------------------------------------------------- |
35 | import Data.Text (Text, pack) | 35 | import Data.Text (Text, pack) |
36 | import Data.Maybe | ||
36 | import qualified Data.Attoparsec.Text as Atto | 37 | import qualified Data.Attoparsec.Text as Atto |
37 | 38 | ||
38 | import Control.Applicative (pure, liftA2, (<$>), (<*>), (<*), (*>), (<|>)) | 39 | import Control.Applicative (pure, liftA2, (<$>), (<*>), (<*), (*>), (<|>)) |
39 | import Linear (V2(..), V3(..)) | 40 | import Linear (V2(..), V3(..),V4(..)) |
40 | 41 | ||
41 | import Graphics.WaveFront.Types | 42 | import Graphics.WaveFront.Types |
42 | 43 | ||
@@ -151,6 +152,20 @@ point2D :: Fractional f => Atto.Parser (V2 f) | |||
151 | point2D = V2 <$> coord <*> coord | 152 | point2D = V2 <$> coord <*> coord |
152 | 153 | ||
153 | 154 | ||
155 | pointTo3 :: Fractional f => Int -> f -> Atto.Parser (V3 f) | ||
156 | pointTo3 3 def = V3 <$> coord <*> coord <*> coord | ||
157 | pointTo3 2 def = V3 <$> coord <*> coord <*> (fromMaybe def <$> optional coord) | ||
158 | pointTo3 1 def = (\x m -> case m of { Just (y,z) -> V3 x y z; Nothing -> V3 x def def }) | ||
159 | <$> coord <*> optional ( do y <- coord | ||
160 | z <- fromMaybe def <$> optional coord | ||
161 | return (y,z) ) | ||
162 | pointTo3 0 def = fromMaybe (V3 def def def) <$> optional (pointTo3 1 def) | ||
163 | |||
164 | |||
165 | pointTo4 :: Fractional f => Int -> f -> Atto.Parser (V4 f) | ||
166 | pointTo4 4 def = V4 <$> coord <*> coord <*> coord <*> coord | ||
167 | pointTo4 n def = (\(V3 x y z) mw -> V4 x y z $ fromMaybe def mw) <$> pointTo3 n def <*> optional coord | ||
168 | |||
154 | -- | | 169 | -- | |
155 | clamp :: Ord n => n -> n -> n -> Atto.Parser n | 170 | clamp :: Ord n => n -> n -> n -> Atto.Parser n |
156 | clamp lower upper n | 171 | clamp lower upper n |
@@ -163,4 +178,4 @@ clamp lower upper n | |||
163 | -- | | 178 | -- | |
164 | -- TODO | - Clean up and generalise | 179 | -- TODO | - Clean up and generalise |
165 | clamped :: Integral i => i -> i -> Atto.Parser i | 180 | clamped :: Integral i => i -> i -> Atto.Parser i |
166 | clamped lower upper = Atto.decimal >>= clamp lower upper \ No newline at end of file | 181 | clamped lower upper = Atto.decimal >>= clamp lower upper |
diff --git a/src/Graphics/WaveFront/Parse/OBJ.hs b/src/Graphics/WaveFront/Parse/OBJ.hs index 37aa5a0..69cce47 100644 --- a/src/Graphics/WaveFront/Parse/OBJ.hs +++ b/src/Graphics/WaveFront/Parse/OBJ.hs | |||
@@ -139,12 +139,12 @@ normal = OBJNormal <$> point3D | |||
139 | 139 | ||
140 | -- | Two coordinates, separated by whitespace | 140 | -- | Two coordinates, separated by whitespace |
141 | texcoord :: (Fractional f) => Atto.Parser (OBJToken f Text i m) | 141 | texcoord :: (Fractional f) => Atto.Parser (OBJToken f Text i m) |
142 | texcoord = OBJTexCoord <$> point2D | 142 | texcoord = OBJTexCoord <$> pointTo3 1 0.0 |
143 | 143 | ||
144 | 144 | ||
145 | -- | Three coordinates, separated by whitespace | 145 | -- | Three coordinates, separated by whitespace |
146 | vertex :: (Fractional f) => Atto.Parser (OBJToken f s i m) | 146 | vertex :: (Fractional f) => Atto.Parser (OBJToken f s i m) |
147 | vertex = OBJVertex <$> point3D | 147 | vertex = OBJVertex <$> pointTo4 3 1.0 |
148 | 148 | ||
149 | 149 | ||
150 | -- | Object names, separated by whitespace | 150 | -- | Object names, separated by whitespace |
@@ -170,4 +170,4 @@ lib = LibMTL <$> (space *> name) | |||
170 | 170 | ||
171 | -- | An MTL material name | 171 | -- | An MTL material name |
172 | use :: Atto.Parser (OBJToken f Text i m) | 172 | use :: Atto.Parser (OBJToken f Text i m) |
173 | use = UseMTL <$> (space *> name) \ No newline at end of file | 173 | use = UseMTL <$> (space *> name) |
diff --git a/src/Graphics/WaveFront/Types.hs b/src/Graphics/WaveFront/Types.hs index ccd1425..5b093de 100644 --- a/src/Graphics/WaveFront/Types.hs +++ b/src/Graphics/WaveFront/Types.hs | |||
@@ -43,7 +43,7 @@ module Graphics.WaveFront.Types where | |||
43 | import Data.Functor.Classes (Show1) --Eq1, Show1, showsPrec1, eq1) | 43 | import Data.Functor.Classes (Show1) --Eq1, Show1, showsPrec1, eq1) |
44 | import Data.Map as M (Map) | 44 | import Data.Map as M (Map) |
45 | import Data.Set as S (Set) | 45 | import Data.Set as S (Set) |
46 | import Linear (V2(..), V3(..)) | 46 | import Linear (V2(..), V3(..), V4(..)) |
47 | 47 | ||
48 | 48 | ||
49 | 49 | ||
@@ -65,9 +65,9 @@ import Linear (V2(..), V3(..)) | |||
65 | -- | 65 | -- |
66 | -- - Cover the entire spec (http://www.martinreddy.net/gfx/3d/OBJ.spec) | 66 | -- - Cover the entire spec (http://www.martinreddy.net/gfx/3d/OBJ.spec) |
67 | -- (and handle unimplemented attributes gracefully) | 67 | -- (and handle unimplemented attributes gracefully) |
68 | data OBJToken f s i m = OBJVertex (V3 f) | | 68 | data OBJToken f s i m = OBJVertex (V4 f) | |
69 | OBJNormal (V3 f) | | 69 | OBJNormal (V3 f) | |
70 | OBJTexCoord (V2 f) | | 70 | OBJTexCoord (V3 f) | |
71 | OBJFace (m (VertexIndices i)) | -- TODO: Associate material with each face, handle absent indices | 71 | OBJFace (m (VertexIndices i)) | -- TODO: Associate material with each face, handle absent indices |
72 | 72 | ||
73 | Line i i | -- Line (I'm assuming the arguments are indices to the endpoint vertices) | 73 | Line i i | -- Line (I'm assuming the arguments are indices to the endpoint vertices) |
@@ -202,9 +202,9 @@ data Material f s = Material { | |||
202 | -- fTextures :: Set s, | 202 | -- fTextures :: Set s, |
203 | -- data Model f s i m = Model { | 203 | -- data Model f s i m = Model { |
204 | data Model f s i m = Model { | 204 | data Model f s i m = Model { |
205 | fVertices :: m (V3 f), | 205 | fVertices :: m (V4 f), |
206 | fNormals :: m (V3 f), | 206 | fNormals :: m (V3 f), |
207 | fTexcoords :: m (V2 f), | 207 | fTexcoords :: m (V3 f), |
208 | fFaces :: m (Face f s i m), | 208 | fFaces :: m (Face f s i m), |
209 | fMaterials :: MTLTable f s, -- TODO: Type synonym (?) | 209 | fMaterials :: MTLTable f s, -- TODO: Type synonym (?) |
210 | fGroups :: Map (Set s) (i, i), -- TODO: Type synonym | 210 | fGroups :: Map (Set s) (i, i), -- TODO: Type synonym |
@@ -229,6 +229,7 @@ deriving instance (Show1 m, | |||
229 | Show (m f), | 229 | Show (m f), |
230 | Show (m (V2 f)), | 230 | Show (m (V2 f)), |
231 | Show (m (V3 f)), | 231 | Show (m (V3 f)), |
232 | Show (m (V4 f)), | ||
232 | Show (m (Face f s i m)), | 233 | Show (m (Face f s i m)), |
233 | Show (m s), | 234 | Show (m s), |
234 | Show f, | 235 | Show f, |
@@ -251,4 +252,4 @@ deriving instance (Show1 m, | |||
251 | Show (m s), | 252 | Show (m s), |
252 | Show f, | 253 | Show f, |
253 | Show s, | 254 | Show s, |
254 | Show i) => Show (OBJToken f s i m) -- where showsPrec = _ \ No newline at end of file | 255 | Show i) => Show (OBJToken f s i m) -- where showsPrec = _ |