public static bool NearEqual(XMVector p1, XMVector p2, XMVector epsilon) { XMVector np1 = XMPlane.Normalize(p1); XMVector np2 = XMPlane.Normalize(p2); return(XMVector4.NearEqual(np1, np2, epsilon)); }
public static XMVector FresnelTerm(XMVector cosIncidentAngle, XMVector refractionIndex) { Debug.Assert(!XMVector4.IsInfinite(cosIncidentAngle), "Reviewed"); //// Result = 0.5f * (g - c)^2 / (g + c)^2 * ((c * (g + c) - 1)^2 / (c * (g - c) + 1)^2 + 1) where //// c = CosIncidentAngle //// g = sqrt(c^2 + RefractionIndex^2 - 1) XMVector g = XMVector.MultiplyAdd(refractionIndex, refractionIndex, XMGlobalConstants.NegativeOne); g = XMVector.MultiplyAdd(cosIncidentAngle, cosIncidentAngle, g); g = g.Abs().Sqrt(); XMVector s = XMVector.Add(g, cosIncidentAngle); XMVector d = XMVector.Subtract(g, cosIncidentAngle); XMVector v0 = XMVector.Multiply(d, d); XMVector v1 = XMVector.Multiply(s, s).Reciprocal(); v0 = XMVector.Multiply(XMGlobalConstants.OneHalf, v0); v0 = XMVector.Multiply(v0, v1); XMVector v2 = XMVector.MultiplyAdd(cosIncidentAngle, s, XMGlobalConstants.NegativeOne); XMVector v3 = XMVector.MultiplyAdd(cosIncidentAngle, d, XMGlobalConstants.One); v2 = XMVector.Multiply(v2, v2); v3 = XMVector.Multiply(v3, v3); v3 = v3.Reciprocal(); v2 = XMVector.MultiplyAdd(v2, v3, XMGlobalConstants.One); return(XMVector.Multiply(v0, v2).Saturate()); }
public static XMVector BaryCentricV(XMVector q0, XMVector q1, XMVector q2, XMVector f, XMVector g) { Debug.Assert(f.Y == f.X && f.Z == f.X && f.W == f.X, "Reviewed"); Debug.Assert(g.Y == g.X && g.Z == g.X && g.W == g.X, "Reviewed"); XMVector epsilon = XMVector.FromSplatConstant(1, 16); XMVector s = XMVector.Add(f, g); XMVector result; if (XMVector4.InBounds(s, epsilon)) { result = q0; } else { XMVector q01 = XMQuaternion.SlerpV(q0, q1, s); XMVector q02 = XMQuaternion.SlerpV(q0, q2, s); XMVector gs = s.Reciprocal(); gs = XMVector.Multiply(g, gs); result = XMQuaternion.SlerpV(q01, q02, gs); } return(result); }
public static XMVector ClampLength(XMVector v, float lengthMin, float lengthMax) { XMVector clampMax = XMVector.Replicate(lengthMax); XMVector clampMin = XMVector.Replicate(lengthMin); return(XMVector4.ClampLengthV(v, clampMin, clampMax)); }
public static XMVector RefractV(XMVector incident, XMVector normal, XMVector refractionIndex) { //// Result = RefractionIndex * Incident - Normal * (RefractionIndex * dot(Incident, Normal) + //// sqrt(1 - RefractionIndex * RefractionIndex * (1 - dot(Incident, Normal) * dot(Incident, Normal)))) XMVector zero = XMVector.Zero; XMVector incidentDotNormal = XMVector4.Dot(incident, normal); // R = 1.0f - RefractionIndex * RefractionIndex * (1.0f - IDotN * IDotN) XMVector r = XMVector.NegativeMultiplySubtract(incidentDotNormal, incidentDotNormal, XMGlobalConstants.One); r = XMVector.Multiply(r, refractionIndex); r = XMVector.NegativeMultiplySubtract(r, refractionIndex, XMGlobalConstants.One); if (XMVector4.LessOrEqual(r, zero)) { // Total internal reflection return(zero); } else { // R = RefractionIndex * IDotN + sqrt(R) r = r.Sqrt(); r = XMVector.MultiplyAdd(refractionIndex, incidentDotNormal, r); // Result = RefractionIndex * Incident - Normal * R XMVector result = XMVector.Multiply(refractionIndex, incident); result = XMVector.NegativeMultiplySubtract(normal, r, result); return(result); } }
public static XMVector AngleBetweenNormals(XMVector n1, XMVector n2) { return(XMVector4 .Dot(n1, n2) .Clamp(XMGlobalConstants.NegativeOne, XMGlobalConstants.One) .ACos()); }
public static XMVector DotCoord(XMVector p, XMVector v) { //// Result = P[0] * V[0] + P[1] * V[1] + P[2] * V[2] + P[3] XMVector v3 = XMVector.Select(XMGlobalConstants.One, v, XMGlobalConstants.Select1110); return(XMVector4.Dot(p, v3)); }
public static XMVector Reflect(XMVector incident, XMVector normal) { //// Result = Incident - (2 * dot(Incident, Normal)) * Normal XMVector result = XMVector4.Dot(incident, normal); result = XMVector.Add(result, result); result = XMVector.NegativeMultiplySubtract(result, normal, incident); return(result); }
public static XMVector NormalizeEst(XMVector v) { //// XMVector4NormalizeEst uses a reciprocal estimate and //// returns QNaN on zero and infinite vectors. XMVector result; result = XMVector4.ReciprocalLength(v); result = XMVector.Multiply(v, result); return(result); }
public static XMVector AngleBetweenVectors(XMVector v1, XMVector v2) { XMVector l1 = XMVector4.ReciprocalLength(v1); XMVector l2 = XMVector4.ReciprocalLength(v2); XMVector dot = XMVector4.Dot(v1, v2); l1 = XMVector.Multiply(l1, l2); return(XMVector .Multiply(dot, l1) .Clamp(XMGlobalConstants.NegativeOne, XMGlobalConstants.One) .ACos()); }
public static XMVector Inverse(XMVector q) { XMVector zero = XMVector.Zero; XMVector l = XMVector4.LengthSquare(q); XMVector conjugate = XMQuaternion.Conjugate(q); XMVector control = XMVector.LessOrEqual(l, XMGlobalConstants.Epsilon); XMVector result = XMVector.Divide(conjugate, l); result = XMVector.Select(result, zero, control); return(result); }
public static XMVector Normalize(XMVector v) { XMVector result = XMVector4.Length(v); float length = result.X; // Prevent divide by zero if (length > 0) { length = 1.0f / length; } return(new XMVector( v.X * length, v.Y * length, v.Z * length, v.W * length)); }
public static XMVector ClampLengthV(XMVector v, XMVector lengthMin, XMVector lengthMax) { Debug.Assert(lengthMin.Y == lengthMin.X && lengthMin.Z == lengthMin.X && lengthMin.W == lengthMin.X, "Reviewed"); Debug.Assert(lengthMax.Y == lengthMax.X && lengthMax.Z == lengthMax.X && lengthMax.W == lengthMax.X, "Reviewed"); Debug.Assert(XMVector4.GreaterOrEqual(lengthMin, XMGlobalConstants.Zero), "Reviewed"); Debug.Assert(XMVector4.GreaterOrEqual(lengthMax, XMGlobalConstants.Zero), "Reviewed"); Debug.Assert(XMVector4.GreaterOrEqual(lengthMax, lengthMin), "Reviewed"); XMVector lengthSq = XMVector4.LengthSquare(v); XMVector zero = XMVector.Zero; XMVector reciprocalLength = lengthSq.ReciprocalSqrt(); XMVector infiniteLength = XMVector.EqualInt(lengthSq, XMGlobalConstants.Infinity); XMVector zeroLength = XMVector.Equal(lengthSq, zero); XMVector normal = XMVector.Multiply(v, reciprocalLength); XMVector length = XMVector.Multiply(lengthSq, reciprocalLength); XMVector select = XMVector.EqualInt(infiniteLength, zeroLength); length = XMVector.Select(lengthSq, length, select); normal = XMVector.Select(lengthSq, normal, select); XMVector controlMax = XMVector.Greater(length, lengthMax); XMVector controlMin = XMVector.Less(length, lengthMin); XMVector clampLength = XMVector.Select(length, lengthMax, controlMax); clampLength = XMVector.Select(clampLength, lengthMin, controlMin); XMVector result = XMVector.Multiply(normal, clampLength); // Preserve the original vector (with no precision loss) if the length falls within the given range XMVector control = XMVector.EqualInt(controlMax, controlMin); result = XMVector.Select(result, v, control); return(result); }
public static XMVector Dot(XMVector q1, XMVector q2) { return(XMVector4.Dot(q1, q2)); }
public static bool IsInfinite(XMVector c) { return(XMVector4.IsInfinite(c)); }
public static bool LessOrEqual(XMVector c1, XMVector c2) { return(XMVector4.LessOrEqual(c1, c2)); }
public static bool NotEqual(XMVector c1, XMVector c2) { return(XMVector4.NotEqual(c1, c2)); }
public static XMVector LengthSquare(XMVector v) { return(XMVector4.Dot(v, v)); }
public static bool IsInfinite(XMVector q) { return(XMVector4.IsInfinite(q)); }
public static bool IsInfinite(XMVector p) { return(XMVector4.IsInfinite(p)); }
public static XMVector Dot(XMVector p, XMVector v) { return(XMVector4.Dot(p, v)); }
public static bool GreaterOrEqual(XMVector c1, XMVector c2) { return(XMVector4.GreaterOrEqual(c1, c2)); }
public static bool NotEqual(XMVector p1, XMVector p2) { return(XMVector4.NotEqual(p1, p2)); }
public static bool Less(XMVector c1, XMVector c2) { return(XMVector4.Less(c1, c2)); }
public static XMVector Refract(XMVector incident, XMVector normal, float refractionIndex) { XMVector index = XMVector.Replicate(refractionIndex); return(XMVector4.RefractV(incident, normal, index)); }
public static XMVector ReciprocalLength(XMVector v) { return(XMVector4 .LengthSquare(v) .ReciprocalSqrt()); }
public static bool IsIdentity(XMVector q) { return(XMVector4.Equal(q, XMGlobalConstants.IdentityR3)); }
public static XMVector Length(XMVector v) { return(XMVector4 .LengthSquare(v) .Sqrt()); }
public static bool IsNaN(XMVector p) { return(XMVector4.IsNaN(p)); }
public static bool IsNaN(XMVector c) { return(XMVector4.IsNaN(c)); }