ソースを参照

Ensure quaternion is normalized

Daniele Bartolini 11 年 前
コミット
3714c18341
3 ファイル変更20 行追加28 行削除
  1. 2 4
      engine/core/math/matrix3x3.h
  2. 1 1
      engine/core/math/matrix4x4.h
  3. 17 23
      engine/core/math/quaternion.h

+ 2 - 4
engine/core/math/matrix3x3.h

@@ -29,6 +29,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "math_types.h"
 #include "vector3.h"
 #include "assert.h"
+#include "quaternion.h"
 
 namespace crown
 {
@@ -254,7 +255,6 @@ namespace matrix3x3
 		{
 			case 0:
 			{
-				printf("case=0\n");
 				tmp.w = biggest;
 				tmp.x = (m.y.z - m.z.y) * mult;
 				tmp.y = (m.z.x - m.x.z) * mult;
@@ -263,7 +263,6 @@ namespace matrix3x3
 			}
 			case 1:
 			{
-				printf("case=1\n");
 				tmp.x = biggest;
 				tmp.w = (m.y.z - m.z.y) * mult;
 				tmp.y = (m.x.y + m.y.x) * mult;
@@ -272,7 +271,6 @@ namespace matrix3x3
 			}
 			case 2:
 			{
-				printf("case=2\n");
 				tmp.y = biggest;
 				tmp.w = (m.z.x - m.x.z) * mult;
 				tmp.x = (m.x.y + m.y.x) * mult;
@@ -281,7 +279,6 @@ namespace matrix3x3
 			}
 			case 3:
 			{
-				printf("case=3\n");
 				tmp.z = biggest;
 				tmp.w = (m.x.y - m.y.x) * mult;
 				tmp.x = (m.z.x + m.x.z) * mult;
@@ -295,6 +292,7 @@ namespace matrix3x3
 			}
 		}
 
+		quaternion::normalize(tmp);
 		return tmp;
 	}
 } // namespace matrix3x3

+ 1 - 1
engine/core/math/matrix4x4.h

@@ -426,7 +426,7 @@ namespace matrix4x4
 
 	inline void set_rotation(Matrix4x4& m, const Quaternion& rot)
 	{
-		set_rotation(m, quaternion::to_matrix3x3(rot));
+		set_rotation(m, Matrix3x3(rot));
 	}
 
 	inline void set_rotation(Matrix4x4& m, const Matrix3x3& rot)

+ 17 - 23
engine/core/math/quaternion.h

@@ -29,7 +29,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "types.h"
 #include "vector3.h"
 #include "math_types.h"
-#include "matrix3x3.h"
 
 namespace crown
 {
@@ -56,6 +55,9 @@ namespace quaternion
 	/// Returns the length of @a q.
 	float length(const Quaternion& q);
 
+	/// Normalizes the quaternion @a q and returns the result.
+	Quaternion& normalize(Quaternion& q);
+
 	/// Returns the conjugate of quaternion @a q.
 	Quaternion conjugate(const Quaternion& q);
 
@@ -64,13 +66,6 @@ namespace quaternion
 
 	/// Returns the quaternion @a q raised to the power of @a exp.
 	Quaternion power(const Quaternion& q, float exp);
-
-	/// Returns the Matrix3x3 representation of the quaternion @a q.
-	Matrix3x3 to_matrix3x3(const Quaternion& q);
-
-	/// Returns the Matrix4x4 representation of the quaternion @a q.
-	Matrix4x4 to_matrix4x4(const Quaternion& q);
-
 } // namespace quaternion
 
 inline Quaternion operator-(const Quaternion& q)
@@ -101,6 +96,16 @@ namespace quaternion
 		return math::sqrt(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z);
 	}
 
+	inline Quaternion& normalize(Quaternion& q)
+	{
+		const float inv_len = 1.0f / length(q);
+		q.x *= inv_len;
+		q.y *= inv_len;
+		q.z *= inv_len;
+		q.w *= inv_len;
+		return q;
+	}
+
 	inline Quaternion conjugate(const Quaternion& q)
 	{
 		return Quaternion(-q.x, -q.y, -q.z, q.w);
@@ -128,16 +133,6 @@ namespace quaternion
 
 		return q;
 	}
-
-	inline Matrix3x3 to_matrix3x3(const Quaternion& q)
-	{
-		return Matrix3x3(q);
-	}
-
-	inline Matrix4x4 to_matrix4x4(const Quaternion& q)
-	{
-		return Matrix4x4(q, Vector3(0, 0, 0));
-	}
 } // namespace quaternion
 
 inline Quaternion::Quaternion()
@@ -175,10 +170,10 @@ inline const float& Quaternion::operator[](uint32_t i) const
 
 inline Quaternion& Quaternion::operator*=(const Quaternion& a)
 {
-	const float t_x = w * a.x + x * a.w + z * a.y - y * a.z;
-	const float t_y = w * a.y + y * a.w + x * a.z - z * a.x;
-	const float t_z = w * a.z + z * a.w + y * a.x - x * a.y;
-	const float t_w = w * a.w - x * a.x - y * a.y - z * a.z;
+	const float t_w = w*a.w - x*a.x - y*a.y - z*a.z;
+	const float t_x = w*a.x + x*a.w + y*a.z - z*a.y;
+	const float t_y = w*a.y + y*a.w + z*a.x - x*a.z;
+	const float t_z = w*a.z + z*a.w + x*a.y - y*a.x;
 
 	x = t_x;
 	y = t_y;
@@ -189,4 +184,3 @@ inline Quaternion& Quaternion::operator*=(const Quaternion& a)
 }
 
 } // namespace crown
-