gdritter repos model-utm / master obj2utm / Main.hs
master

Tree @master (Download .tar.gz)

Main.hs @masterraw · history · blame

{-# LANGUAGE RecordWildCards #-}

module Main where

import           Codec.Wavefront
import qualified Data.ByteString as BS
import           Data.Vector ((!))
import qualified Data.Vector as V
import           System.Environment (getArgs)
import qualified Utmyen.Model.Type as U

main :: IO ()
main = do
  args <- getArgs
  case args of
    [] -> putStrLn "Usage: obj2utm [file]"
    (f:_) -> do
      Right WavefrontOBJ { .. } <- fromFile f
      let mkPoly (FaceIndex { faceLocIndex = i
                            , faceTexCoordIndex = Just j
                            , faceNorIndex = Just k }) =
            U.Point x y z a b c u v
            where Location x y z _ = objLocations ! (i - 1)
                  TexCoord u v _   = objTexCoords ! (j - 1)
                  Normal a b c     = objNormals   ! (k - 1)
      let polys = [ [ mkPoly a, mkPoly b, mkPoly c ]
                  | Element { elValue = Face a b c [] } <- V.toList objFaces
                  ]
          polys' = concat polys
      let model = U.Model { U.modelPoints = V.fromList polys'
                          , U.modelTris   = V.fromList [0..fromIntegral (length polys') - 1]
                          }
      BS.putStr (U.encode model)