public void CalculateNormals() { if (indices == null) return; normals = new Vector[vertices.Length]; Vector[] temps = new Vector[indices.Length]; for (int ii = 0; ii < indices.Length; ii++) { Triangle tr = indices[ii]; Vector v1 = vertices[tr.vertex1] - vertices[tr.vertex2]; Vector v2 = vertices[tr.vertex2] - vertices[tr.vertex3]; temps[ii] = v1.CrossProduct(v2); //Console.Write ("I"); } for (int ii = 0; ii < vertices.Length; ii++) { Vector v = new Vector(); int shared = 0; for (int jj = 0; jj < indices.Length; jj++) { Triangle tr = indices[jj]; if (tr.vertex1 == ii || tr.vertex2 == ii || tr.vertex3 == ii) { v += temps[jj]; shared++; } } normals[ii] = (v / shared).Normalize(); } //Console.WriteLine ( "Normals Calculated!" ); normalized = true; }
public double DotProduct(Vector v) { return X * v.X + Y * v.Y + Z * v.Z; }
public Vector CrossProduct(Vector v) { return new Vector(Y * v.Z - Z * v.Y, Z * v.X - X * v.Z, X * v.Y - Y * v.X); }