summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-06-11 02:51:29 -0400
committerJoe Crayne <joe@jerkface.net>2019-06-11 02:51:29 -0400
commit6fbc401f5cbb89e9c8b6be48f746e911848aed36 (patch)
tree9fea587478800f4ee4d34247950b5cac78c8616b
parent42eef741086804aa106870f5a375c65ac5d83ab2 (diff)
Sabadie parser: support vp token.
-rw-r--r--src/Codec/Wavefront/Lexer.hs6
-rw-r--r--src/Codec/Wavefront/TexCoord.hs17
-rw-r--r--src/Codec/Wavefront/Token.hs15
-rw-r--r--test/TestSabadie.hs2
4 files changed, 38 insertions, 2 deletions
diff --git a/src/Codec/Wavefront/Lexer.hs b/src/Codec/Wavefront/Lexer.hs
index efb4b97..712cabf 100644
--- a/src/Codec/Wavefront/Lexer.hs
+++ b/src/Codec/Wavefront/Lexer.hs
@@ -32,6 +32,8 @@ data Ctxt = Ctxt {
32 ctxtLocations :: DList Location 32 ctxtLocations :: DList Location
33 -- |Texture coordinates. 33 -- |Texture coordinates.
34 , ctxtTexCoords :: DList TexCoord 34 , ctxtTexCoords :: DList TexCoord
35 -- |Texture coordinates.
36 , ctxtParamCoords :: DList ParamCoord
35 -- |Normals. 37 -- |Normals.
36 , ctxtNormals :: DList Normal 38 , ctxtNormals :: DList Normal
37 -- |Points. 39 -- |Points.
@@ -58,6 +60,7 @@ emptyCtxt :: Ctxt
58emptyCtxt = Ctxt { 60emptyCtxt = Ctxt {
59 ctxtLocations = empty 61 ctxtLocations = empty
60 , ctxtTexCoords = empty 62 , ctxtTexCoords = empty
63 , ctxtParamCoords = empty
61 , ctxtNormals = empty 64 , ctxtNormals = empty
62 , ctxtPoints = empty 65 , ctxtPoints = empty
63 , ctxtLines = empty 66 , ctxtLines = empty
@@ -83,6 +86,9 @@ lexer stream = execState (traverse_ consume stream) emptyCtxt
83 TknVT vt -> do 86 TknVT vt -> do
84 texCoords <- gets ctxtTexCoords 87 texCoords <- gets ctxtTexCoords
85 modify $ \ctxt -> ctxt { ctxtTexCoords = texCoords `snoc` vt } 88 modify $ \ctxt -> ctxt { ctxtTexCoords = texCoords `snoc` vt }
89 TknVP vp -> do
90 pCoords <- gets ctxtParamCoords
91 modify $ \ctxt -> ctxt { ctxtParamCoords = pCoords `snoc` vp }
86 TknP p -> do 92 TknP p -> do
87 (pts,element) <- prepareElement ctxtPoints 93 (pts,element) <- prepareElement ctxtPoints
88 modify $ \ctxt -> ctxt { ctxtPoints = pts `append` fmap element (fromList p) } 94 modify $ \ctxt -> ctxt { ctxtPoints = pts `append` fmap element (fromList p) }
diff --git a/src/Codec/Wavefront/TexCoord.hs b/src/Codec/Wavefront/TexCoord.hs
index 02e5c5a..0eb09f4 100644
--- a/src/Codec/Wavefront/TexCoord.hs
+++ b/src/Codec/Wavefront/TexCoord.hs
@@ -18,10 +18,25 @@ module Codec.Wavefront.TexCoord where
18-- let TexCoord r s t = TexCoord 0.1 0.2 0.3 18-- let TexCoord r s t = TexCoord 0.1 0.2 0.3
19-- @ 19-- @
20-- 20--
21-- That type is strcit and unboxed. 21-- That type is strict and unboxed.
22data TexCoord = TexCoord { 22data TexCoord = TexCoord {
23 texcoordR :: {-# UNPACK #-} !Float 23 texcoordR :: {-# UNPACK #-} !Float
24 , texcoordS :: {-# UNPACK #-} !Float 24 , texcoordS :: {-# UNPACK #-} !Float
25 , texcoordT :: {-# UNPACK #-} !Float 25 , texcoordT :: {-# UNPACK #-} !Float
26 } deriving (Eq,Show) 26 } deriving (Eq,Show)
27 27
28
29-- |A parameter-space coordinate is a 3D-floating vector. You can access to its components by pattern
30-- matching on them:
31--
32-- @
33-- let ParamCoord r s t = ParamCoord 0.1 0.2 0.3
34-- @
35--
36-- That type is strict and unboxed.
37data ParamCoord = ParamCoord {
38 paramcoordR :: {-# UNPACK #-} !Float
39 , paramcoordS :: {-# UNPACK #-} !Float
40 , paramcoordT :: {-# UNPACK #-} !Float
41 } deriving (Eq,Show)
42
diff --git a/src/Codec/Wavefront/Token.hs b/src/Codec/Wavefront/Token.hs
index 31e62e7..71691a8 100644
--- a/src/Codec/Wavefront/Token.hs
+++ b/src/Codec/Wavefront/Token.hs
@@ -34,6 +34,7 @@ data Token
34 = TknV Location 34 = TknV Location
35 | TknVN Normal 35 | TknVN Normal
36 | TknVT TexCoord 36 | TknVT TexCoord
37 | TknVP ParamCoord
37 | TknP [Point] 38 | TknP [Point]
38 | TknL [Line] 39 | TknL [Line]
39 | TknF Face 40 | TknF Face
@@ -55,6 +56,7 @@ tokenize = fmap cleanupTokens . analyseResult False . parse (untilEnd tokenizer)
55 fmap (Just . TknV) location 56 fmap (Just . TknV) location
56 , fmap (Just . TknVN) normal 57 , fmap (Just . TknVN) normal
57 , fmap (Just . TknVT) texCoord 58 , fmap (Just . TknVT) texCoord
59 , fmap (Just . TknVP) paramCoord
58 , fmap (Just . TknP) points 60 , fmap (Just . TknP) points
59 , fmap (Just . TknL) lines 61 , fmap (Just . TknL) lines
60 , fmap (Just . TknF) face 62 , fmap (Just . TknF) face
@@ -116,6 +118,19 @@ texCoord = skipSpace *> string "vt " *> skipHSpace *> parseUVW <* eol
116 _ -> fail "wrong number of u, v and w arguments for texture coordinates" 118 _ -> fail "wrong number of u, v and w arguments for texture coordinates"
117 119
118---------------------------------------------------------------------------------------------------- 120----------------------------------------------------------------------------------------------------
121-- Parameter-space coordinates -----------------------------------------------------------------------------
122
123paramCoord :: Parser ParamCoord
124paramCoord = skipSpace *> string "vp " *> skipHSpace *> parseUVW <* eol
125 where
126 parseUVW = do
127 uvw <- float `sepBy1` skipHSpace
128 case uvw of
129 [u,v] -> pure (ParamCoord u v 0)
130 [u,v,w] -> pure (ParamCoord u v w)
131 _ -> fail "wrong number of u, v and w arguments for parameter-space coordinates"
132
133----------------------------------------------------------------------------------------------------
119-- Points ------------------------------------------------------------------------------------------ 134-- Points ------------------------------------------------------------------------------------------
120 135
121points :: Parser [Point] 136points :: Parser [Point]
diff --git a/test/TestSabadie.hs b/test/TestSabadie.hs
index a01693b..de13678 100644
--- a/test/TestSabadie.hs
+++ b/test/TestSabadie.hs
@@ -16,4 +16,4 @@ main = do
16 r1 <- fromFile (ddir ++ "/01.obj") 16 r1 <- fromFile (ddir ++ "/01.obj")
17 print r1 17 print r1
18 -- Failed: ` vp ` [...]: Failed reading: empty 18 -- Failed: ` vp ` [...]: Failed reading: empty
19 -- Reason: vp token and blank lines 19 -- Reason: blank lines