summaryrefslogtreecommitdiff
path: root/ddl/out/haskell/LambdaCube/Mesh.hs
blob: 8d6db7aa1cfc201f1650d5241513e6253bd202f5 (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
-- generated file, do not modify!
-- 2016-11-11T11:17:03.517567000000Z

{-# LANGUAGE OverloadedStrings, RecordWildCards #-}
module LambdaCube.Mesh where

import Data.Int
import Data.Word
import Data.Map
import Data.Vector (Vector(..))
import LambdaCube.Linear

import Data.Text
import Data.Aeson hiding (Value,Bool)
import Data.Aeson.Types hiding (Value,Bool)
import Control.Monad


data MeshAttribute
  = A_Float (Vector Float)
  | A_V2F (Vector V2F)
  | A_V3F (Vector V3F)
  | A_V4F (Vector V4F)
  | A_M22F (Vector M22F)
  | A_M33F (Vector M33F)
  | A_M44F (Vector M44F)
  | A_Int (Vector Int32)
  | A_Word (Vector Word32)
  deriving (Show, Eq, Ord)

data MeshPrimitive
  = P_Points
  | P_TriangleStrip
  | P_Triangles
  | P_TriangleStripI (Vector Int32)
  | P_TrianglesI (Vector Int32)
  deriving (Show, Eq, Ord)

data Mesh
  = Mesh
  { mAttributes :: Map String MeshAttribute
  , mPrimitive :: MeshPrimitive
  }

  deriving (Show, Eq, Ord)


instance ToJSON MeshAttribute where
  toJSON v = case v of
    A_Float arg0 -> object [ "tag" .= ("A_Float" :: Text), "arg0" .= arg0]
    A_V2F arg0 -> object [ "tag" .= ("A_V2F" :: Text), "arg0" .= arg0]
    A_V3F arg0 -> object [ "tag" .= ("A_V3F" :: Text), "arg0" .= arg0]
    A_V4F arg0 -> object [ "tag" .= ("A_V4F" :: Text), "arg0" .= arg0]
    A_M22F arg0 -> object [ "tag" .= ("A_M22F" :: Text), "arg0" .= arg0]
    A_M33F arg0 -> object [ "tag" .= ("A_M33F" :: Text), "arg0" .= arg0]
    A_M44F arg0 -> object [ "tag" .= ("A_M44F" :: Text), "arg0" .= arg0]
    A_Int arg0 -> object [ "tag" .= ("A_Int" :: Text), "arg0" .= arg0]
    A_Word arg0 -> object [ "tag" .= ("A_Word" :: Text), "arg0" .= arg0]

instance FromJSON MeshAttribute where
  parseJSON (Object obj) = do
    tag <- obj .: "tag"
    case tag :: Text of
      "A_Float" -> A_Float <$> obj .: "arg0"
      "A_V2F" -> A_V2F <$> obj .: "arg0"
      "A_V3F" -> A_V3F <$> obj .: "arg0"
      "A_V4F" -> A_V4F <$> obj .: "arg0"
      "A_M22F" -> A_M22F <$> obj .: "arg0"
      "A_M33F" -> A_M33F <$> obj .: "arg0"
      "A_M44F" -> A_M44F <$> obj .: "arg0"
      "A_Int" -> A_Int <$> obj .: "arg0"
      "A_Word" -> A_Word <$> obj .: "arg0"
  parseJSON _ = mzero

instance ToJSON MeshPrimitive where
  toJSON v = case v of
    P_Points -> object [ "tag" .= ("P_Points" :: Text)]
    P_TriangleStrip -> object [ "tag" .= ("P_TriangleStrip" :: Text)]
    P_Triangles -> object [ "tag" .= ("P_Triangles" :: Text)]
    P_TriangleStripI arg0 -> object [ "tag" .= ("P_TriangleStripI" :: Text), "arg0" .= arg0]
    P_TrianglesI arg0 -> object [ "tag" .= ("P_TrianglesI" :: Text), "arg0" .= arg0]

instance FromJSON MeshPrimitive where
  parseJSON (Object obj) = do
    tag <- obj .: "tag"
    case tag :: Text of
      "P_Points" -> pure P_Points
      "P_TriangleStrip" -> pure P_TriangleStrip
      "P_Triangles" -> pure P_Triangles
      "P_TriangleStripI" -> P_TriangleStripI <$> obj .: "arg0"
      "P_TrianglesI" -> P_TrianglesI <$> obj .: "arg0"
  parseJSON _ = mzero

instance ToJSON Mesh where
  toJSON v = case v of
    Mesh{..} -> object
      [ "tag" .= ("Mesh" :: Text)
      , "mAttributes" .= mAttributes
      , "mPrimitive" .= mPrimitive
      ]

instance FromJSON Mesh where
  parseJSON (Object obj) = do
    tag <- obj .: "tag"
    case tag :: Text of
      "Mesh" -> do
        mAttributes <- obj .: "mAttributes"
        mPrimitive <- obj .: "mPrimitive"
        pure $ Mesh
          { mAttributes = mAttributes
          , mPrimitive = mPrimitive
          } 
  parseJSON _ = mzero