Polytope MakeGraph(Node node, double xStart, double xEnd, double xStep, double yStart, double yEnd, double yStep) { var result = new Polytope(); int xDelta = 0; int yDelta = 0; for (double i = xStart; i <= xEnd; i += xStep) { xDelta += 1; yDelta = 0; for (double j = yStart; j <= yEnd; j += yStep) { yDelta += 1; result.Add(new Base3D.Point { X = i, Y = j, Z = node.Get(i, j) }); result.AddNormal(new Base3D.Point { X = -node.Dx().Get(i, j), Y = -node.Dy().Get(i, j), Z = 1 }.Normal()); result.AddNormal(new Base3D.Point { X = node.Dx().Get(i, j), Y = node.Dy().Get(i, j), Z = -1 }.Normal()); } } for (int i = 0; i < xDelta - 1; ++i) { for (int j = 0; j < yDelta - 1; ++j) { result.Add(new Polygon(new int[] { j + i * yDelta, j + (i + 1) * yDelta, (j + 1) + (i + 1) * yDelta, (j + 1) + i * yDelta }, new int[] { 2 * (j + i * yDelta), 2 * (j + (i + 1) * yDelta), 2 * ((j + 1) + (i + 1) * yDelta), 2 * ((j + 1) + i * yDelta) } )); result.Add(new Polygon(new int[] { j + i * yDelta, (j + 1) + i * yDelta, (j + 1) + (i + 1) * yDelta, j + (i + 1) * yDelta }, new int[] { 2 * (j + i * yDelta) + 1, 2 * ((j + 1) + i * yDelta) + 1, 2 * ((j + 1) + (i + 1) * yDelta) + 1, 2 * (j + (i + 1) * yDelta) + 1 } )); } } return(result); }
public static Entity Parse(string file) { Directory.SetCurrentDirectory(Path.GetDirectoryName(file)); Polytope current = new Polytope(); Dictionary <string, BaseMaterial> mtl = new Dictionary <string, BaseMaterial>(); foreach (var s in File.ReadAllLines(file)) { if (String.IsNullOrEmpty(s)) { continue; } var lines = Regex.Split(s, @"\s").Where(l => !String.IsNullOrEmpty(l)).ToList(); string flag = lines[0]; if (flag == "v") { current.Add(new Base3D.Point { X = double.Parse(lines[1]), Y = -double.Parse(lines[3]), Z = double.Parse(lines[2]) }); } else if (flag == "vn") { current.AddNormal(new Base3D.Point { X = double.Parse(lines[1]), Y = -double.Parse(lines[3]), Z = double.Parse(lines[2]) }); } else if (flag == "vt") { current.Addtexture(( double.Parse(lines[1]), double.Parse(lines[2]) )); } else if (flag == "f") { var first = lines[1].Split('/'); var vertexes = lines.Skip(1).Select(l => Int32.Parse(l.Split('/')[0]) - 1).ToArray(); var textures = (first.Length > 1 && !String.IsNullOrEmpty(first[1])) ? lines.Skip(1).Select(l => Int32.Parse(l.Split('/')[1]) - 1).ToArray() : null; var normals = (first.Length > 2 && !String.IsNullOrEmpty(first[2])) ? lines.Skip(1).Select(l => Int32.Parse(l.Split('/')[2]) - 1).ToArray() : null; current.Add(new Polygon(vertexes, textures, normals)); } else if (flag == "mtllib") { try { var new_mtl = LoadMtl(Regex.Replace(s, @"\s*mtllib\s*(.*\S)\s*$", "$1")); mtl = new_mtl; } catch (Exception e) { MessageBox.Show($"Не вышло загрузить текстуры: {e.Message}", "Окошко-всплывашка", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else if (flag == "usemtl") { string matName = Regex.Replace(s, @"\s*usemtl\s*(.*\S)\s*$", "$1"); if (mtl.ContainsKey(matName)) { current.Matreial = mtl[matName]; } } } return(current); }