public DAEVector3 Cross(DAEVector3 v) { return(new DAEVector3( Y * v.Z - Z * v.Y, Z * v.X - X * v.Z, X * v.Y - Y * v.X)); }
public DAEVector4(DAEVector3 xyz, double w) { X = xyz.X; Y = xyz.Y; Z = xyz.Z; W = w; }
public static DAEMatrix4 Rotation(DAEVector3 axis, double angle) { if (angle == 0.0f) { return(DAEMatrix4.Identity); } axis = axis.Normalized; double x, y, z, s, c, t; x = axis.X; y = axis.Y; z = axis.Z; s = (double)System.Math.Sin(angle); c = (double)System.Math.Cos(angle); t = 1 - c; return(new DAEMatrix4(c + x * x * t, x * y * t - z * s, x * z * t + y * s, 0, y * x * t + z * s, c + y * y * t, y * z * t - x * s, 0, z * x * t - y * s, z * y * t + x * s, c + z * z * t, 0, 0, 0, 0, 1)); }
public DAEVector3 GetVector3(int index) { DAEVector3 result = new DAEVector3(0, 0, 0); if (Stride > 0) { result.X = Values[index * Stride]; } if (Stride > 1) { result.Y = Values[index * Stride + 1]; } if (Stride > 2) { result.Z = Values[index * Stride + 2]; } return(result); }
internal static void GenerateMeshTangents(DAEVertexDescription[] vertices) { DAEVector3[] v = new DAEVector3[3]; DAEVector3[] n = new DAEVector3[3]; DAEVector3[] t = new DAEVector3[3]; for (int i = 0; i < vertices.Length; i += 3) { for (int j = 0; j < 3; j++) { v[j] = vertices[i + j].Vertex; n[j] = vertices[i + j].Normal; t[j] = vertices[i + j].TexCoord; } DAEVector3[] tangents = GenerateTriangleTangents(v, n, t); for (int j = 0; j < 3; j++) { vertices[i + j].TexTangent = tangents[j]; vertices[i + j].TexBinormal = tangents[j].Cross(n[j]).Normalized; } } }
/// <summary> /// Generates tangents for a triangle. Source: /// http://download.autodesk.com/us/maya/2009help/index.html?url=Appendix_A_Tangent_and_binormal_vectors.htm,topicNumber=d0e216694 /// </summary> /// <param name="v">Vertices of the triangle</param> /// <param name="n">Normals of the triangle</param> /// <param name="t">Texture coords of the triangle</param> /// <returns>the tangents</returns> internal static DAEVector3[] GenerateTriangleTangents(DAEVector3[] v, DAEVector3[] n, DAEVector3[] t) { if (v.Length == 3 && n.Length == 3 && t.Length == 3) { DAEVector3[] finalTangentArray = new DAEVector3[3]; double[,] tangentArray = new double[3, 3]; double[] edge1 = new double[3]; double[] edge2 = new double[3]; DAEVector3 crossP; // ============================================== // x, s, t // S & T vectors get used several times in this vector, // but are only computed once. // ============================================== edge1[0] = v[1].X - v[0].X; edge1[1] = t[1].X - t[0].X; // s-vector - don't need to compute // this multiple times edge1[2] = t[1].Y - t[0].Y; // t-vector edge2[0] = v[2].X - v[0].X; edge2[1] = t[2].X - t[0].X; // another s-vector edge2[2] = t[2].Y - t[0].Y; // another t-vector crossP = new DAEVector3(edge1).Cross(new DAEVector3(edge2)).Normalized; bool degnerateUVTangentPlane = crossP.X == 0.0f; if (degnerateUVTangentPlane) { crossP = new DAEVector3(1, crossP.Y, crossP.Z); } double tanX = -crossP.Y / crossP.X; tangentArray[0, 0] = tanX; tangentArray[1, 0] = tanX; tangentArray[2, 0] = tanX; // -------------------------------------------------------- // y, s, t // -------------------------------------------------------- edge1[0] = v[1].Y - v[0].Y; edge2[0] = v[2].Y - v[0].Y; crossP = new DAEVector3(edge1).Cross(new DAEVector3(edge2)).Normalized; degnerateUVTangentPlane = crossP.X == 0.0f; if (degnerateUVTangentPlane) { crossP = new DAEVector3(1, crossP.Y, crossP.Z); } double tanY = -crossP.Y / crossP.X; tangentArray[0, 1] = tanY; tangentArray[1, 1] = tanY; tangentArray[2, 1] = tanY; // ------------------------------------------------------ // z, s, t // ------------------------------------------------------ edge1[0] = v[1].Z - v[0].Z; edge2[0] = v[2].Z - v[0].Z; crossP = new DAEVector3(edge1).Cross(new DAEVector3(edge2)).Normalized; degnerateUVTangentPlane = crossP.X == 0.0f; if (degnerateUVTangentPlane) { crossP = new DAEVector3(1, crossP.Y, crossP.Z); } double tanZ = -crossP.Y / crossP.X; tangentArray[0, 2] = tanZ; tangentArray[1, 2] = tanZ; tangentArray[2, 2] = tanZ; // Orthnonormalize to normal for (int i = 0; i < 3; i++) { DAEVector3 tangent = new DAEVector3(tangentArray[i, 0], tangentArray[i, 1], tangentArray[i, 2]); finalTangentArray[i] = (tangent - (n[i] * (tangent.Dot(n[i])))).Normalized; } return(finalTangentArray); } return(null); }
public static DAEMatrix4 Translation(DAEVector3 v) { return(Translation(v.X, v.Y, v.Z)); }
private double GetLuminance(DAEVector3 color) { return(color.X * 0.212671 + color.Y * 0.715160 + color.Z * 0.072169); }
public double SmallAngle(DAEVector3 v) { double angle = Angle(v); return((double)System.Math.Min(angle, System.Math.PI - angle)); }
public double Angle(DAEVector3 v) { return((double)System.Math.Acos(System.Math.Max(-1, System.Math.Min(1, this.Dot(v) / (this.Length * v.Length))))); }
public double Dot(DAEVector3 v) { return(X * v.X + Y * v.Y + Z * v.Z); }