Advertisement
Guest User

Untitled

a guest
Apr 15th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.95 KB | None | 0 0
  1. module OBJLoader =
  2.  
  3.     type Vertex = {
  4.         x: float
  5.         y: float
  6.         z: float
  7.     }
  8.  
  9.     type TexCoord = {
  10.         u: float
  11.         v: float
  12.     }
  13.  
  14.     type FaceDef = {
  15.         v: int option
  16.         n: int option
  17.         t: int option
  18.     }
  19.  
  20.     let space = pchar ' '
  21.  
  22.     let load name =
  23.  
  24.         let vertices = new List<Vertex>()
  25.         let pvertex = pchar 'v' >>. spaces >>. tuple3 (pfloat .>> space) (pfloat .>> space) pfloat .>> newline
  26.                       |>> fun (x, y, z) -> vertices.Add({ x=x; y=y; z=z })
  27.  
  28.         let normals = new List<Vertex>()
  29.         let pnormal = pstring "vn" >>. spaces >>. tuple3 (pfloat .>> space) (pfloat .>> space) pfloat .>> newline
  30.                       |>> fun (x, y, z) -> normals.Add({ x=x; y=y; z=z })
  31.  
  32.         let texcoords = new List<TexCoord>()
  33.         let ptex = pstring "vt" >>. spaces >>. tuple2 (pfloat .>> space) pfloat .>> newline
  34.                    |>> fun (u, v) -> texcoords.Add({ u=u; v=v })
  35.  
  36.         let pfacevert = sepBy (opt pint32) (pchar '/')
  37.                         |>> function
  38.                             | [v; t; n] -> { v=v; n=n; t=t }
  39.                             | _ -> { v=None; n=None; t=None }
  40.  
  41.         let faces = new List<FaceDef>()
  42.         let pface = pchar 'f' >>. spaces >>. sepBy pfacevert space .>> newline
  43.                     |>> faces.AddRange
  44.  
  45.         // These are basically comments...
  46.         let mtllib = (choice [pstring "mtllib"; pstring "usemtl"]) >>. skipRestOfLine true
  47.         let pgroup = pchar 'g' >>. skipRestOfLine true
  48.  
  49.         let comment = pchar '#' >>. skipRestOfLine true
  50.  
  51.         let toplevel = many (choice [pface; ptex; pnormal; pvertex; pgroup; mtllib; comment])
  52.  
  53.         let foo = System.DateTime.Now
  54.         match runParserOnFile toplevel () name System.Text.Encoding.ASCII with
  55.             | Success(v,_,_) ->
  56.                 let diff = System.DateTime.Now - foo
  57.                 ()
  58.             | Failure(str,_,_) -> ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement