From 9ce7437cbd5c8c84f963ff01357c31baef85ca0e Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Tue, 3 Nov 2009 11:45:20 +0000 Subject: [PATCH] Fixed Vector3.Transform signature to return a Vector3, not a Vector4. It was previously impossible to transform a Vector3 structure without intermediate conversions to Vector4. --- Source/OpenTK/Math/Vector3.cs | 30 ++++++++++++++---------------- Source/OpenTK/Math/Vector3d.cs | 31 +++++++++++++++---------------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/Source/OpenTK/Math/Vector3.cs b/Source/OpenTK/Math/Vector3.cs index 0eece2c2..c3c85c09 100644 --- a/Source/OpenTK/Math/Vector3.cs +++ b/Source/OpenTK/Math/Vector3.cs @@ -1076,14 +1076,10 @@ namespace OpenTK /// The vector to transform /// The desired transformation /// The transformed vector - public static Vector4 Transform(Vector3 vec, Matrix4 mat) + public static Vector3 Transform(Vector3 vec, Matrix4 mat) { - Vector4 v4 = new Vector4(vec.X, vec.Y, vec.Z, 1.0f); - Vector4 result; - result.X = Vector4.Dot(v4, mat.Column0); - result.Y = Vector4.Dot(v4, mat.Column1); - result.Z = Vector4.Dot(v4, mat.Column2); - result.W = Vector4.Dot(v4, mat.Column3); + Vector3 result; + Transform(ref vec, ref mat, out result); return result; } @@ -1091,10 +1087,11 @@ namespace OpenTK /// The vector to transform /// The desired transformation /// The transformed vector - public static void Transform(ref Vector3 vec, ref Matrix4 mat, out Vector4 result) + public static void Transform(ref Vector3 vec, ref Matrix4 mat, out Vector3 result) { Vector4 v4 = new Vector4(vec.X, vec.Y, vec.Z, 1.0f); - Vector4.Transform(ref v4, ref mat, out result); + Vector4.Transform(ref v4, ref mat, out v4); + result = v4.Xyz; } /// Transform a Vector3 by the given Matrix, and project the resulting Vector4 back to a Vector3 @@ -1103,8 +1100,9 @@ namespace OpenTK /// The transformed vector public static Vector3 TransformPerspective(Vector3 vec, Matrix4 mat) { - Vector4 h = Transform(vec, mat); - return new Vector3(h.X / h.W, h.Y / h.W, h.Z / h.W); + Vector3 result; + TransformPerspective(ref vec, ref mat, out result); + return result; } /// Transform a Vector3 by the given Matrix, and project the resulting Vector4 back to a Vector3 @@ -1113,11 +1111,11 @@ namespace OpenTK /// The transformed vector public static void TransformPerspective(ref Vector3 vec, ref Matrix4 mat, out Vector3 result) { - Vector4 h; - Vector3.Transform(ref vec, ref mat, out h); - result.X = h.X / h.W; - result.Y = h.Y / h.W; - result.Z = h.Z / h.W; + Vector4 v = new Vector4(vec); + Vector4.Transform(ref v, ref mat, out v); + result.X = v.X / v.W; + result.Y = v.Y / v.W; + result.Z = v.Z / v.W; } #endregion diff --git a/Source/OpenTK/Math/Vector3d.cs b/Source/OpenTK/Math/Vector3d.cs index 2f44bfcf..a5557c32 100644 --- a/Source/OpenTK/Math/Vector3d.cs +++ b/Source/OpenTK/Math/Vector3d.cs @@ -1072,24 +1072,22 @@ namespace OpenTK /// The vector to transform /// The desired transformation /// The transformed vector - public static Vector4d Transform(Vector3d vec, Matrix4d mat) + public static Vector3d Transform(Vector3d vec, Matrix4d mat) { - Vector4d v4 = new Vector4d(vec.X, vec.Y, vec.Z, 1.0f); - return new Vector4d( - Vector4d.Dot(v4, mat.Column0), - Vector4d.Dot(v4, mat.Column1), - Vector4d.Dot(v4, mat.Column2), - Vector4d.Dot(v4, mat.Column3)); + Vector3d result; + Transform(ref vec, ref mat, out result); + return result; } /// Transform a Vector by the given Matrix /// The vector to transform /// The desired transformation /// The transformed vector - public static void Transform(ref Vector3d vec, ref Matrix4d mat, out Vector4d result) + public static void Transform(ref Vector3d vec, ref Matrix4d mat, out Vector3d result) { Vector4d v4 = new Vector4d(vec.X, vec.Y, vec.Z, 1.0f); - Vector4d.Transform(ref v4, ref mat, out result); + Vector4d.Transform(ref v4, ref mat, out v4); + result = v4.Xyz; } /// @@ -1100,8 +1098,9 @@ namespace OpenTK /// The transformed vector public static Vector3d TransformPerspective(Vector3d vec, Matrix4d mat) { - Vector4d h = Transform(vec, mat); - return new Vector3d(h.X / h.W, h.Y / h.W, h.Z / h.W); + Vector3d result; + TransformPerspective(ref vec, ref mat, out result); + return result; } /// Transform a Vector3d by the given Matrix, and project the resulting Vector4d back to a Vector3d @@ -1110,11 +1109,11 @@ namespace OpenTK /// The transformed vector public static void TransformPerspective(ref Vector3d vec, ref Matrix4d mat, out Vector3d result) { - Vector4d h; - Vector3d.Transform(ref vec, ref mat, out h); - result.X = h.X / h.W; - result.Y = h.Y / h.W; - result.Z = h.Z / h.W; + Vector4d v = new Vector4d(vec); + Vector4d.Transform(ref v, ref mat, out v); + result.X = v.X / v.W; + result.Y = v.Y / v.W; + result.Z = v.Z / v.W; } #endregion