public static void SaveAsObj(List <SurfacePoint> points, string filename, string textureFilename) { var boundingBox = new BoundingBox(); boundingBox.Set(points); var mesh = GetMesh(points, boundingBox); using (StreamWriter writer = new StreamWriter(File.Open(filename + MTL, FileMode.Create))) { writer.WriteLine("newmtl volume"); writer.WriteLine("Ns 10.0000"); writer.WriteLine("Ni 1.5000"); writer.WriteLine("d 1.0000"); writer.WriteLine("Tr 0.0000"); writer.WriteLine("Tf 1.0000 1.0000 1.0000"); writer.WriteLine("illum 2"); writer.WriteLine("Ka 0.0000 0.0000 0.0000"); writer.WriteLine("Kd 0.0 0.0 0.0"); writer.WriteLine("Ks 0.0000 0.0000 0.0000"); writer.WriteLine("Ke 0.0000 0.0000 0.0000"); writer.WriteLine("map_Ka {0}", textureFilename + PNG); writer.WriteLine("map_Kd {0}", textureFilename + PNG); } using (StreamWriter writer = new StreamWriter(File.Open(filename + OBJ, FileMode.Create))) { writer.WriteLine("o volume"); writer.WriteLine("mtllib superficie.mtl"); writer.WriteLine("# vertices (x,y,z)"); foreach (var t in mesh) { writer.WriteLine("v {0} {1} {2}", t.a.x, t.a.y, t.a.z); writer.WriteLine("v {0} {1} {2}", t.b.x, t.b.y, t.b.z); writer.WriteLine("v {0} {1} {2}", t.c.x, t.c.y, t.c.z); } writer.WriteLine(); writer.WriteLine("# normals (i,j,k)"); foreach (var t in mesh) { writer.WriteLine("vn {0} {1} {2}", t.a.i, t.a.j, t.a.k); writer.WriteLine("vn {0} {1} {2}", t.b.i, t.b.j, t.b.k); writer.WriteLine("vn {0} {1} {2}", t.c.i, t.c.j, t.c.k); } writer.WriteLine(); writer.WriteLine("# texture coordinates (u,v)"); foreach (var t in mesh) { writer.WriteLine("vt {0} {1}", t.a.u, t.a.v); writer.WriteLine("vt {0} {1}", t.b.u, t.b.v); writer.WriteLine("vt {0} {1}", t.c.u, t.c.v); } writer.WriteLine(); writer.WriteLine("# faces"); writer.WriteLine("g volume"); writer.WriteLine("usemtl volume"); int index = 1; // indices starts with 1 foreach (var t in mesh) { writer.WriteLine("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}", index, index + 1, index + 2); index += 3; } } }
private static void WriteTextureCoordinates(StreamWriter writer, List <SurfacePoint> points, BoundingBox boundingBox) { writer.WriteLine("# texture coordinates (u,v)"); foreach (var point in points) { writer.WriteLine("vt {0} {1}", boundingBox.NormalizeX(point.GridX), boundingBox.NormalizeY(point.GridY)); } writer.WriteLine(); }