Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module OBJLoader =
- type Vertex = {
- x: float
- y: float
- z: float
- }
- type TexCoord = {
- u: float
- v: float
- }
- type FaceDef = {
- v: int option
- n: int option
- t: int option
- }
- let space = pchar ' '
- let load name =
- let vertices = new List<Vertex>()
- let pvertex = pchar 'v' >>. spaces >>. tuple3 (pfloat .>> space) (pfloat .>> space) pfloat .>> newline
- |>> fun (x, y, z) -> vertices.Add({ x=x; y=y; z=z })
- let normals = new List<Vertex>()
- let pnormal = pstring "vn" >>. spaces >>. tuple3 (pfloat .>> space) (pfloat .>> space) pfloat .>> newline
- |>> fun (x, y, z) -> normals.Add({ x=x; y=y; z=z })
- let texcoords = new List<TexCoord>()
- let ptex = pstring "vt" >>. spaces >>. tuple2 (pfloat .>> space) pfloat .>> newline
- |>> fun (u, v) -> texcoords.Add({ u=u; v=v })
- let pfacevert = sepBy (opt pint32) (pchar '/')
- |>> function
- | [v; t; n] -> { v=v; n=n; t=t }
- | _ -> { v=None; n=None; t=None }
- let faces = new List<FaceDef>()
- let pface = pchar 'f' >>. spaces >>. sepBy pfacevert space .>> newline
- |>> faces.AddRange
- // These are basically comments...
- let mtllib = (choice [pstring "mtllib"; pstring "usemtl"]) >>. skipRestOfLine true
- let pgroup = pchar 'g' >>. skipRestOfLine true
- let comment = pchar '#' >>. skipRestOfLine true
- let toplevel = many (choice [pface; ptex; pnormal; pvertex; pgroup; mtllib; comment])
- let foo = System.DateTime.Now
- match runParserOnFile toplevel () name System.Text.Encoding.ASCII with
- | Success(v,_,_) ->
- let diff = System.DateTime.Now - foo
- ()
- | Failure(str,_,_) -> ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement