public static bool Load(ObjMesh mesh, string fileName) { try { using (StreamReader streamReader = new StreamReader(fileName)) { ReadFile(mesh, streamReader); streamReader.Close(); return(true); } } catch { return(false); } }
static void ReadFile(ObjMesh mesh, TextReader textReader) { vertices = new List <Vector3>(); normals = new List <Vector3>(); texCoords = new List <Vector2>(); objVerticesIndexDictionary = new Dictionary <ObjMesh.ObjVertex, uint> (); objVertices = new List <ObjMesh.ObjVertex>(); objTriangles = new List <ObjMesh.ObjTriangle>(); objQuads = new List <ObjMesh.ObjQuad>(); string line; while ((line = textReader.ReadLine()) != null) { line = line.Trim(splitCharacters); line = line.Replace(" ", " "); string[] parameters = line.Split(splitCharacters); switch (parameters[0]) { case "p": // Point break; case "v": // Vertex float x = float.Parse(parameters[1]); float y = float.Parse(parameters[2]); float z = float.Parse(parameters[3]); vertices.Add(new Vector3(x, y, z)); break; case "vt": // TexCoord float u = float.Parse(parameters[1]); float v = float.Parse(parameters[2]); texCoords.Add(new Vector2(u, v)); break; case "vn": // Normal float nx = float.Parse(parameters[1]); float ny = float.Parse(parameters[2]); float nz = float.Parse(parameters[3]); normals.Add(new Vector3(nx, ny, nz)); break; case "f": switch (parameters.Length) { case 4: ObjMesh.ObjTriangle objTriangle = new ObjMesh.ObjTriangle(); objTriangle.Index0 = ParseFaceParameter(parameters[1]); objTriangle.Index1 = ParseFaceParameter(parameters[2]); objTriangle.Index2 = ParseFaceParameter(parameters[3]); objTriangles.Add(objTriangle); break; case 5: ObjMesh.ObjQuad objQuad = new ObjMesh.ObjQuad(); objQuad.Index0 = ParseFaceParameter(parameters[1]); objQuad.Index1 = ParseFaceParameter(parameters[2]); objQuad.Index2 = ParseFaceParameter(parameters[3]); objQuad.Index3 = ParseFaceParameter(parameters[4]); objQuads.Add(objQuad); break; } break; } } mesh.Vertices = objVertices.ToArray(); mesh.Triangles = objTriangles.ToArray(); mesh.Quads = objQuads.ToArray(); objVerticesIndexDictionary = null; vertices = null; normals = null; texCoords = null; objVertices = null; objTriangles = null; objQuads = null; }