public static Vector Cross(Vector v1, Vector v2) { Vector result = new Vector(); result.X = (v1.Y * v2.Z) - (v1.Z * v2.Y); result.Y = (v1.Z * v2.X) - (v1.X * v2.Z); result.Z = (v1.X * v2.Y) - (v1.Y * v2.X); return result; }
public static Vector Normalize(Vector v1) { Vector result = new Vector(v1.X, v1.Y, v1.Z, v1.W); float length = result.X * result.X + result.Y * result.Y + result.Z * result.Z; length = (float)Math.Sqrt(length); result.X /= length; result.Y /= length; result.Z /= length; return result; }
public static Mesh Load(string path) { Mesh mesh = new Mesh(); mesh.Name = System.IO.Path.GetFileNameWithoutExtension(path); Vector[] vertices = new Vector[0]; Color[] colors = new Color[0]; uint[] triangles = new uint[0]; uint v = 0; //The Vertices index uint c = 0; //The Colors index uint t = 0; //The Triangles index uint j = 0; //The current index to offset triangles by string[] lines = System.IO.File.ReadAllLines(path); int vertCount = 0; int triCount = 0; foreach (string line in lines) { if (line.StartsWith("v")) { vertCount++; } if (line.StartsWith("t")) { triCount += 3; } } vertices = new Vector[vertCount]; colors = new Color[vertCount]; triangles = new uint[triCount]; for (int i = 0; i < lines.Length; i++) { string line = lines[i]; if (line == "") continue; string[] bits = line.Split(" ".ToCharArray()); if (bits[0] == "v") { vertices[v] = new Vector(float.Parse(bits[1]), float.Parse(bits[2]), float.Parse(bits[3])); v++; } else if (bits[0] == "c") { colors[c] = new Color(byte.Parse(bits[1]), byte.Parse(bits[2]), byte.Parse(bits[3]), ((bits.Length > 4) ? byte.Parse(bits[4]) : (byte)255)); c++; } else if (bits[0] == "t") { triangles[t + 0] = uint.Parse(bits[1]) + j; triangles[t + 1] = uint.Parse(bits[2]) + j; triangles[t + 2] = uint.Parse(bits[3]) + j; t += 3; } else if (bits[0] == "#") { j = v; } } mesh.Vertices = vertices; mesh.Colors = colors; mesh.Triangles = triangles; mesh.CalculateNormals(); return mesh; }