Skip to content

Commit

Permalink
basic parsing of obj, now for the output
Browse files Browse the repository at this point in the history
  • Loading branch information
bookshelfdave committed Apr 2, 2012
1 parent 27f2daa commit 1e4ac36
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
10 changes: 10 additions & 0 deletions demo.json
@@ -0,0 +1,10 @@
{"objects":
[
{
"name":"object1",
"verts": [ [1.0,2.0,3.0],[2.0,3.0,4.0] ],
"vert_norms": [ [1.0,2.0,3.0],[2.0,3.0,4.0] ],
"faces":[ [1,2,3,4], [4,3,2,1]]
}
]
}
59 changes: 47 additions & 12 deletions obj2json.hs
@@ -1,17 +1,22 @@
module ObjParser where
--module Main (main,getDataLines,processLine) where
-- Currently only supports one object per file.
-- partition lists based on line type = o and then call process data on
-- each segment

import Data.Char
import Data.Maybe
import Control.Monad

-- raw types
data RawType =
O String |
V Float Float Float |
VN Float Float Float |
F [Integer] |
USEMATL String |
V Double Double Double |
VN Double Double Double |
F [Int] |
USEMTL String |
S String |
MTLLIB String
MTLLIB String |
Unknown String
deriving (Show,Read)

firstToken :: String -> Maybe String
Expand All @@ -28,24 +33,54 @@ getDataLines :: String -> [String]
getDataLines contents = filter (not . isCommentLine . firstToken) $ lines contents


_processLine :: String -> String -> RawType
_processLine _ line = read line :: RawType
getStringData :: String -> String
getStringData s = (words s) !! 1

-- abust of pattern matching? you decide :-)
getData :: String -> String -> RawType
getData "V" line = V (nums !! 0) (nums !! 1) (nums !! 2)
where
numStrings = tail $ words line
nums = map (\x -> read x :: Double) numStrings

-- warning: repeated code!
getData "VN" line = VN (nums !! 0) (nums !! 1) (nums !! 2)
where
numStrings = tail $ words line
nums = map (\x -> read x :: Double) numStrings
getData "F" line = F numData
where
strData = words $ tail line
numData = map (\n -> read n :: Int) strData

getData "O" line = O objname
where objname = getStringData line
getData "USEMTL" line = USEMTL matlName
where matlName = getStringData line
getData "S" line = S s
where s = getStringData line

getData "MTLLIB" line = MTLLIB m
where m = getStringData line
getData _ line = Unknown line

processLine :: String -> RawType
processLine l =
_processLine ft l
getData ft l
where
ft = map toUpper $ (fromJust $ firstToken l)

processData :: String -> IO ()
processData contents =
return ()
processData contents =
mapM_ putStrLn mappedStrs
where
ls = getDataLines contents
mapped = map processLine ls
mappedStrs = map show mapped

main :: IO ()
main = do
contents <- readFile "/Users/dparfitt/src/cube.obj"
contents <- readFile "cube.obj"
processData contents
return ()

0 comments on commit 1e4ac36

Please sign in to comment.