internal static void save(Model m, StreamWriter w) { w.WriteLine("Begin Map"); w.WriteLine("Begin Actor Class=LevelInfo Name=LevelInfo2"); w.WriteLine(" TimeSeconds=5.440500"); w.WriteLine(" AIProfile(0)=148"); w.WriteLine(" Level=LevelInfo'MyLevel.LevelInfo2'"); w.WriteLine(" Tag=LevelInfo"); w.WriteLine(" Region=(Zone=LevelInfo'MyLevel.LevelInfo2',iLeaf=-1)"); w.WriteLine(" Name=LevelInfo2"); w.WriteLine("End Actor"); w.WriteLine("Begin Actor Class=Brush Name=Brush2"); w.WriteLine(" MainScale=(SheerAxis=SHEER_ZX)"); w.WriteLine(" PostScale=(SheerAxis=SHEER_ZX)"); w.WriteLine(" Group=Cube"); w.WriteLine(" Level=LevelInfo'MyLevel.LevelInfo2'"); w.WriteLine(" Tag=Brush"); w.WriteLine(" Region=(Zone=LevelInfo'MyLevel.LevelInfo2',iLeaf=-1)"); w.WriteLine(" Begin Brush Name=Brush"); savePolyList(m, w); w.WriteLine(" End Brush"); w.WriteLine(" Brush=Model'MyLevel.Model4'"); w.WriteLine(" Name=Brush3"); w.WriteLine("End Actor"); w.WriteLine("End Map"); }
internal static Model load(StreamReader sr) { //NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat; //nfi.NumberDecimalSeparator = "."; Model m = new Model(); string line; while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); var words = line.Split(' '); switch (words[0]) { case "v": // There are convertions with coordinate systems. // format in obj: v <Y> <Z> <X> m.vertices.Add(new Vertex(Double.Parse(words[3], FileIOHelper.nfi), Double.Parse(words[1], FileIOHelper.nfi), Double.Parse(words[2], FileIOHelper.nfi))); break; case "f": // Note, the -1 is the conversion between indexing between the obj files and the internal structures // obj files start conting with index 1 // csharp arrays start counting with index 0 m.polygons.Add(new Polygon(Int32.Parse(words[1], FileIOHelper.nfi) - 1, Int32.Parse(words[2], FileIOHelper.nfi) - 1, Int32.Parse(words[3], FileIOHelper.nfi) - 1)); break; } } return m; }
internal Model clone() { Model clone = new Model(); clone.vertices.AddRange(vertices); clone.polygons.AddRange(polygons); return clone; }
internal static void Apply(Model m) { // Swap the v2 and v3 arguments in all polygons // This inverts the face of the polygons for (int i = 0; i < m.polygons.Count; i++) { m.polygons[i] = new Polygon(m.polygons[i].v1, m.polygons[i].v3, m.polygons[i].v2); } }
public static void saveModel(Model model, Stream fileStream, string format) { using (var reader = new StreamWriter(fileStream)) { switch (format) { case "obj": ObjFileFormat.save(model, reader); break; case "t3d": T3DFileFormat.save(model, reader); break; } } }
public void Refresh() { _finalModel = RawModel.clone(); if (ShouldSnap) SnapTool.snap(_finalModel, SnapSize); if (ShouldInvert) InvertTool.Apply(_finalModel); if (onModelChange != null) onModelChange(); }
internal static Model load(StreamReader r) { Model m = new Model(); while(!r.EndOfStream) { string line = r.ReadLine(); if (line.Contains("Begin Polygon")){ loadPolygon(m, r); } else if (line.Contains("End Polylist")) { // Quickfix to solve T3D files which contain more than one polygon. break; } } return m; }
internal static string getBoundingBox(Model m) { if (m.vertices.Count == 0) return ""; Vertex min = m.vertices[0]; Vertex max = m.vertices[0]; foreach (var v in m.vertices) { min = getMin(min, v); max = getMax(max, v); } return write("min", min) + write("max", max) + write("size", max - min); }
static int getVertex(Model m, string line) { string[] strArr = line.Trim().Split(' ').Last().Split(','); if (strArr.Length != 3){ throw new Exception("Vertex with an incorrect amount of arguments (3).\n" + line); } Vertex v = new Vertex(getNr(strArr[0]), getNr(strArr[1]), getNr(strArr[2])); int id = m.vertices.IndexOf(v); if (id == -1){ m.vertices.Add(v); return m.vertices.LastIndexOf(v); } return id; }
internal static void save(Model m, StreamWriter w) { w.WriteLine("# OBJ Exported from Titleds Model Converter"); w.WriteLine("usemtl (null)"); w.WriteLine("s 0"); foreach (var v in m.vertices){ // There are convertions with coordinate systems. // format in obj: v <Y> <Z> <X> w.WriteLine("v " + v.y + " " + v.z + " " + v.x); } foreach (var p in m.polygons) { // Note, the +1 is the conversion between indexing between the obj files and the internal structures // obj files start conting with index 1 // csharp arrays start counting with index 0 w.WriteLine("f " + (p.v1 + 1) + " " + (p.v2 + 1) + " " + (p.v3 + 1)); } }
static void loadPolygon(Model m, StreamReader r) { List<int> vertices = new List<int>(); while (!r.EndOfStream) { string line = r.ReadLine(); if (line.Contains("Vertex")) { vertices.Add(getVertex(m, line)); } else if (line.Contains("End Polygon")) { break; } } if (vertices.Count < 3) { throw new Exception("Vertex with less than 3 vertices.\n"); } m.addPolygonByVertices(vertices); }
private static void savePolyList(Model m, StreamWriter w) { w.WriteLine("Begin PolyList"); foreach (var p in m.polygons) { savePolygon(m,p,w); } w.WriteLine("End PolyList"); }
static void savePolygon(Model m, Polygon p, StreamWriter w) { w.WriteLine(" Begin Polygon"); writeVertex("Origin ", m.vertices[p.v1], w); writeVertex("Normal ", p.getNormal(m), w); writeVertex("TextureU", new Vertex(1, 0, 0), w); writeVertex("TextureV", new Vertex(0, 1, 0), w); writeVertex("Vertex ", m.vertices[p.v1], w); writeVertex("Vertex ", m.vertices[p.v2], w); writeVertex("Vertex ", m.vertices[p.v3], w); w.WriteLine(" End Polygon"); }
public Vertex getNormal(Model m) { var v21 = m.vertices[v2] - m.vertices[v1]; var v31 = m.vertices[v3] - m.vertices[v1]; return Vertex.cross(v21, v31).normalize(); }
internal static void snap(Model m, double snap) { for (int i = 0; i < m.vertices.Count; i++) { m.vertices[i] = snapVertex(m.vertices[i], snap); } }
static void scale(Model m, double scale) { for (int i = 0; i < m.vertices.Count; i++) { m.vertices[i] = scaleVertex(m.vertices[i], scale); } }