public void AddLoop(int vertexIndex, int normalsIndex, int uvIndex) { var loop = new FaceLoop() { VertexIndex = vertexIndex, NormalIndex = normalsIndex }; loop.UvIndices.Add(uvIndex); FaceLoops.Add(loop); }
public void CalculateNormals() { Normals.Clear(); for (int i = 0; i < Vertices.Count; i++) { Normals.Add(Vector3.Zero); } foreach (var submesh in Submeshes) { var loops = submesh.FaceLoops; for (int i = 0; i < loops.Count; i += 3) { FaceLoop loop0 = loops[i]; FaceLoop loop1 = loops[i + 1]; FaceLoop loop2 = loops[i + 2]; //normal calculation Vector3 p1 = Vertices[loop0.VertexIndex]; Vector3 p2 = Vertices[loop1.VertexIndex]; Vector3 p3 = Vertices[loop2.VertexIndex]; Vector3 U = (p2 - p1); Vector3 V = (p3 - p1); Vector3 normal = Vector3.Zero; normal.X = (U.Y * V.Z) - (U.Z * V.Y); normal.Y = (U.Z * V.X) - (U.X * V.Z); normal.Z = (U.X * V.Y) - (U.Y * V.X); normal = Vector3.Normalize(normal); Normals[loop0.VertexIndex] += normal; Normals[loop1.VertexIndex] += normal; Normals[loop2.VertexIndex] += normal; //set loop normal loop0.NormalIndex = loop0.VertexIndex; loop1.NormalIndex = loop1.VertexIndex; loop2.NormalIndex = loop2.VertexIndex; } } for (int i = 0; i < Normals.Count; i++) { Normals[i] = Vector3.Normalize(Normals[i]); } }