public ObjLoader(string filename, ShaderProgram program) { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; this.defaultProgram = program; System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew(); ObjMaterial defaultMaterial = new ObjMaterial(program); using (StreamReader stream = new StreamReader(filename)) { List<string> lines = new List<string>(); int vertexOffset = 1, vertexCount = 0; int uvOffset = 1, uvCount = 0; // read the entire file while (!stream.EndOfStream) { string line = stream.ReadLine(); if (line.Trim().Length == 0) continue; if ((line[0] == 'o' || line[0] == 'g') && lines.Count != 0) { ObjObject newObject = new ObjObject(lines, materials, vertexOffset, uvOffset); if (vertexCount != 0) objects.Add(newObject); if (newObject.Material == null) newObject.Material = defaultMaterial; lines.Clear(); vertexOffset += vertexCount; uvOffset += uvCount; vertexCount = 0; uvCount = 0; } if (line[0] != '#') lines.Add(line); if (line[0] == 'v') { if (line[1] == ' ') vertexCount++; else uvCount++; } // check if a material file is being used if (line[0] == 'm' && line[1] == 't') LoadMaterials(CreateFixedPath(filename, line.Split(' ')[1])); } // make sure we grab any remaining objects that occured before the EOF if (lines != null) objects.Add(new ObjObject(lines, materials, vertexOffset, uvOffset)); } watch.Stop(); Console.WriteLine("Took {0}ms", watch.ElapsedMilliseconds); }
public ObjLoader(string filename, ShaderProgram program) { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; this.defaultProgram = program; System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew(); ObjMaterial defaultMaterial = new ObjMaterial(program); using (StreamReader stream = new StreamReader(filename)) { List <string> lines = new List <string>(); int vertexOffset = 1, vertexCount = 0; int uvOffset = 1, uvCount = 0; // read the entire file while (!stream.EndOfStream) { string line = stream.ReadLine(); if (line.Trim().Length == 0) { continue; } if ((line[0] == 'o' || line[0] == 'g') && lines.Count != 0) { ObjObject newObject = new ObjObject(lines, materials, vertexOffset, uvOffset); if (vertexCount != 0) { objects.Add(newObject); } if (newObject.Material == null) { newObject.Material = defaultMaterial; } lines.Clear(); vertexOffset += vertexCount; uvOffset += uvCount; vertexCount = 0; uvCount = 0; } if (line[0] != '#') { lines.Add(line); } if (line[0] == 'v') { if (line[1] == ' ') { vertexCount++; } else { uvCount++; } } // check if a material file is being used if (line[0] == 'm' && line[1] == 't') { LoadMaterials(CreateFixedPath(filename, line.Split(' ')[1])); } } // make sure we grab any remaining objects that occured before the EOF if (lines != null) { objects.Add(new ObjObject(lines, materials, vertexOffset, uvOffset)); } } watch.Stop(); Console.WriteLine("Took {0}ms", watch.ElapsedMilliseconds); }