summaryrefslogtreecommitdiff
path: root/src/Graphics/WaveFront/Parse/MTL.hs
blob: 060952fa1c5c047231b71137c4a92033bad08608 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
-- |
-- Module      : Graphics.WaveFront.Parse.MTL
-- Description :
-- Copyright   : (c) Jonatan H Sundqvist, October 2 2016
-- License     : MIT
-- Maintainer  : Jonatan H Sundqvist
-- Stability   : experimental|stable
-- Portability : POSIX (not sure)

-- TODO | - 
--        - 

-- SPEC | -
--        -



--------------------------------------------------------------------------------------------------------------------------------------------
-- GHC Extensions
--------------------------------------------------------------------------------------------------------------------------------------------
{-# LANGUAGE UnicodeSyntax     #-}
{-# LANGUAGE TupleSections     #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NamedFieldPuns    #-}



--------------------------------------------------------------------------------------------------------------------------------------------
-- API
--------------------------------------------------------------------------------------------------------------------------------------------
module Graphics.WaveFront.Parse.MTL (
  mtl, row, token,
  ambient, diffuse, specular,
  mapDiffuse, newMaterial
) where



--------------------------------------------------------------------------------------------------------------------------------------------
-- We'll need these
--------------------------------------------------------------------------------------------------------------------------------------------
-- import qualified Data.Map  as M
-- import qualified Data.Set  as S
-- import qualified Data.Vector as V
import Data.Text (Text)

import qualified Data.Attoparsec.Text as Atto

import Control.Applicative ((<$>), (<*), (*>), (<|>))

import Graphics.WaveFront.Parse.Common

import Graphics.WaveFront.Types hiding (ambient, diffuse, specular)



--------------------------------------------------------------------------------------------------------------------------------------------
-- Functions
--------------------------------------------------------------------------------------------------------------------------------------------

-- MTL parsing -----------------------------------------------------------------------------------------------------------------------------

-- | Produces a list of MTL tokens
mtl :: (Fractional f) => Atto.Parser (MTL f Text [])
mtl = Atto.sepBy row lineSeparator


-- | Parses a single MTL row.
row :: (Fractional f) => Atto.Parser (MTLToken f Text)
row = token <* ignore comment

--------------------------------------------------------------------------------------------------------------------------------------------

-- | Parse an MTL token
-- TODO: How to deal with common prefix (Ka, Kd, Ks) (backtrack?)
token :: (Fractional f) => Atto.Parser (MTLToken f Text)
token = (Atto.string "Ka"     *> ambient)     <|>
        (Atto.string "Kd"     *> diffuse)     <|>
        (Atto.string "Ks"     *> specular)    <|>
        (Atto.string "Ns"     *> specExp)     <|>
        (Atto.string "illum"  *> illum)       <|>
        (Atto.string "Ni"     *> refraction)  <|>
        (Atto.string "d"      *> dissolve)    <|> -- TODO: Handle inverse as well (cf. 'Tr' attribute)
        (Atto.string "map_Kd" *> mapDiffuse)  <|>
        (Atto.string "map_Ka" *> mapAmbient)  <|>
        (Atto.string "newmtl" *> newMaterial)

--------------------------------------------------------------------------------------------------------------------------------------------

-- TODO: Expose these parsers for testing purposes (?)

-- TODO | - Change definition of 'colour' and 'Colour' to only allow three channels (alpha is handled by the 'dissolve' attribute)
--        - Change the definition of 'Colour' or use the one defined in the colour package

-- | Three or four channel values (RGB[A])
ambient :: (Fractional f) => Atto.Parser (MTLToken f s)
ambient = Ambient <$> colour


-- | Three or four channel values (RGB[A])
diffuse :: (Fractional f) => Atto.Parser (MTLToken f s)
diffuse = Diffuse <$> colour


-- | Three or four channel values (RGB[A])
specular :: (Fractional f) => Atto.Parser (MTLToken f s)
specular = Specular <$> colour


-- | A rational number, preceded by whitespace (specular exponent)
specExp :: (Fractional f) => Atto.Parser (MTLToken f s)
specExp = space *> (SpecularExponent <$> Atto.rational)


-- | A number between 0 and 10 (inclusive) (illumination model)
illum :: Atto.Parser (MTLToken f s)
illum = space *> (Illum <$> clamped 0 10)


-- | A rational number, preceded by whitespace (refraction index)
refraction :: (Fractional f) => Atto.Parser (MTLToken f s)
refraction = space *> (Refraction <$> Atto.rational)


-- | A rational number, preceded by whitespace (doss)
dissolve :: (Fractional f) => Atto.Parser (MTLToken f s)
dissolve = space *> (Dissolve <$> Atto.rational)


-- | A texture name, preceded by whitespace
mapDiffuse :: Atto.Parser (MTLToken f Text)
mapDiffuse = space *> (MapDiffuse <$> name)


-- | A texture name, preceded by whitespace
mapAmbient :: Atto.Parser (MTLToken f Text)
mapAmbient = space *> (MapAmbient <$> name)


-- | A material name, preceded by whitespace
newMaterial :: Atto.Parser (MTLToken f Text)
newMaterial = space *> (NewMaterial <$> name)