public static List<Mesh> FromObject(string path) { uint lineno = 1; StreamReader sr = new StreamReader(path); PrimitiveType ptype = PrimitiveType.Triangles; List<Mesh> meshes = new List<Mesh>(); List<Vector3> vertices = new List<Vector3>(); List<Vector3> normals = new List<Vector3>(); List<uint> elements = new List<uint>(); int offset = 0, primitiveCnt = 0; Mesh mesh = new Mesh(0, ptype); for (string current_line = sr.ReadLine(); current_line != null; current_line = sr.ReadLine(), lineno++) { string[] split = current_line.Split(null); if (split.Length == 1 && split[0] == "") continue; // get rid of empty strings (caused by repeated delimiters): // v\x20\x20\x200.1234 converts to: {"v", "", "", "0.1234"} int notempty = 0; for (int i = 0; i < split.Length; ++i) if (split[i] != "") ++notempty; string[] tok = split; if (notempty != split.Length) { tok = new string[notempty]; for (int i = 0, j = 0; i < split.Length; ++i) if (split[i] != "") tok[j++] = split[i]; } switch (tok[0]) { case "v": if (tok.Length < 4) throw new Exception("Parsing error at line " + lineno + ": expected at least 4 tokens"); vertices.Add(new Vector3(float.Parse(tok[1], System.Globalization.CultureInfo.InvariantCulture), float.Parse(tok[2], System.Globalization.CultureInfo.InvariantCulture), float.Parse(tok[3], System.Globalization.CultureInfo.InvariantCulture))); normals.Add(new Vector3()); break; case "f": if (tok.Length < 4) throw new Exception("Parsing error at line " + lineno + ": expected at least 4 tokens"); int i1 = ParseIndex(int.Parse(tok[1].Split('/')[0]), vertices.Count); int i2 = ParseIndex(int.Parse(tok[2].Split('/')[0]), vertices.Count); int i3 = ParseIndex(int.Parse(tok[3].Split('/')[0]), vertices.Count); int i4 = tok.Length >= 5 ? int.Parse(tok[4].Split('/')[0]) : 0; AddNormal(vertices, normals, i1, i2, i3); elements.Add((uint)i1); elements.Add((uint)i2); elements.Add((uint)i3); offset += 3; primitiveCnt += 3; if (i4 != 0) { i4 = ParseIndex(i4, vertices.Count); elements.Add((uint)i1); elements.Add((uint)i3); elements.Add((uint)i4); AddNormal(vertices, normals, i1, i3, i4); offset += 3; primitiveCnt += 3; } break; case "o": case "g": if (mesh != null && primitiveCnt != 0) //wrong { mesh.primitiveCount = primitiveCnt; primitiveCnt = 0; meshes.Add(mesh); } mesh = new Mesh(offset, ptype); break; } } if (meshes.Count == 0 && primitiveCnt == 0) return null; if (primitiveCnt != 0) { mesh.primitiveCount = primitiveCnt; meshes.Add(mesh); } // normals Vector4b[] colors = new Vector4b[normals.Count]; for (int i = 0; i < normals.Count; ++i) { Vector3 n = normals[i].Normalized(); calcFullcolor(ref n, ref colors[i]); } // arrange Vector3[] positions = new Vector3[vertices.Count]; for (int i = 0; i < vertices.Count; ++i) positions[i] = vertices[i]; uint[] elems = new uint[elements.Count]; for (int i = 0; i < elements.Count; ++i) elems[i] = elements[i]; int vao = arrangeData(positions, colors, elems); foreach(Mesh m in meshes) m.vao = vao; return meshes; }
public SceneNode(Mesh mesh, int modelMatrixLoc) { this.mesh = mesh; modelLoc = modelMatrixLoc; }
public static Mesh FromZArray(ZArrayDescriptor desc, ColoringMethod coloring) { Tuple<Vector3[], Vector4b[], uint[]> meshData = ParseZArray(desc, coloring); Mesh rv = new Mesh(meshData.Item1, meshData.Item2, meshData.Item3); rv.primitiveCount = meshData.Item3.Length; rv.primitiveType = PrimitiveType.TriangleStrip; return rv; }