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 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 ); }
public double DotProduct( Vector v ) { return X*v.X + Y*v.Y + Z*v.Z; }
Vector[] ReadVertices(ThreeDSChunk chunk) { ushort numVerts = reader.ReadUInt16(); chunk.BytesRead += 2; Console.WriteLine(" Vertices: {0}", numVerts); Vector[] verts = new Vector[numVerts]; for (int ii = 0; ii < verts.Length; ii++) { float f1 = reader.ReadSingle(); float f2 = reader.ReadSingle(); float f3 = reader.ReadSingle(); verts[ii] = new Vector(f1, f3, -f2); //Console.WriteLine ( verts [ii] ); } //Console.WriteLine ( "{0} {1}", verts.Length * ( 3 * 4 ), chunk.Length - chunk.BytesRead ); chunk.BytesRead += verts.Length * (3 * 4); //chunk.BytesRead = (int) chunk.Length; //SkipChunk ( chunk ); return verts; }