From d866df206e703854cfa95e6aabe3ed5ed3bfbcaa Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Tue, 11 Jun 2019 02:21:46 -0400 Subject: Fixed Sundqvist v and vt parsing. --- src/Graphics/WaveFront/Parse/Common.hs | 19 +++++++++++++++++-- src/Graphics/WaveFront/Parse/OBJ.hs | 6 +++--- 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'src/Graphics/WaveFront/Parse') 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 -- We'll need these -------------------------------------------------------------------------------------------------------------------------------------------- import Data.Text (Text, pack) +import Data.Maybe import qualified Data.Attoparsec.Text as Atto import Control.Applicative (pure, liftA2, (<$>), (<*>), (<*), (*>), (<|>)) -import Linear (V2(..), V3(..)) +import Linear (V2(..), V3(..),V4(..)) import Graphics.WaveFront.Types @@ -151,6 +152,20 @@ point2D :: Fractional f => Atto.Parser (V2 f) point2D = V2 <$> coord <*> coord +pointTo3 :: Fractional f => Int -> f -> Atto.Parser (V3 f) +pointTo3 3 def = V3 <$> coord <*> coord <*> coord +pointTo3 2 def = V3 <$> coord <*> coord <*> (fromMaybe def <$> optional coord) +pointTo3 1 def = (\x m -> case m of { Just (y,z) -> V3 x y z; Nothing -> V3 x def def }) + <$> coord <*> optional ( do y <- coord + z <- fromMaybe def <$> optional coord + return (y,z) ) +pointTo3 0 def = fromMaybe (V3 def def def) <$> optional (pointTo3 1 def) + + +pointTo4 :: Fractional f => Int -> f -> Atto.Parser (V4 f) +pointTo4 4 def = V4 <$> coord <*> coord <*> coord <*> coord +pointTo4 n def = (\(V3 x y z) mw -> V4 x y z $ fromMaybe def mw) <$> pointTo3 n def <*> optional coord + -- | clamp :: Ord n => n -> n -> n -> Atto.Parser n clamp lower upper n @@ -163,4 +178,4 @@ clamp lower upper n -- | -- TODO | - Clean up and generalise clamped :: Integral i => i -> i -> Atto.Parser i -clamped lower upper = Atto.decimal >>= clamp lower upper \ No newline at end of file +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 -- | Two coordinates, separated by whitespace texcoord :: (Fractional f) => Atto.Parser (OBJToken f Text i m) -texcoord = OBJTexCoord <$> point2D +texcoord = OBJTexCoord <$> pointTo3 1 0.0 -- | Three coordinates, separated by whitespace vertex :: (Fractional f) => Atto.Parser (OBJToken f s i m) -vertex = OBJVertex <$> point3D +vertex = OBJVertex <$> pointTo4 3 1.0 -- | Object names, separated by whitespace @@ -170,4 +170,4 @@ lib = LibMTL <$> (space *> name) -- | An MTL material name use :: Atto.Parser (OBJToken f Text i m) -use = UseMTL <$> (space *> name) \ No newline at end of file +use = UseMTL <$> (space *> name) -- cgit v1.2.3