Browse Source

Added vec3 slerp #237

Christophe Riccio 11 years ago
parent
commit
0d42f4a320
3 changed files with 36 additions and 0 deletions
  1. 13 0
      glm/gtx/rotate_vector.hpp
  2. 22 0
      glm/gtx/rotate_vector.inl
  3. 1 0
      readme.txt

+ 13 - 0
glm/gtx/rotate_vector.hpp

@@ -51,6 +51,19 @@ namespace glm
 	/// @addtogroup gtx_rotate_vector
 	/// @{
 
+	/// Returns the length of the quaternion.
+	/// 
+	/// @param x A first vector
+	/// @param y A second vector
+	/// @param a Interpolation factor. The interpolation is defined beyond the range [0, 1].
+	/// 
+	/// @see gtc_quaternion
+	template <typename T, precision P>
+	GLM_FUNC_DECL detail::tvec3<T, P> slerp(
+		detail::tvec3<T, P> const & x,
+		detail::tvec3<T, P> const & y,
+		T const & a);
+
 	//! Rotate a two dimensional vector.
 	//! From GLM_GTX_rotate_vector extension.
 	template <typename T, precision P>

+ 22 - 0
glm/gtx/rotate_vector.inl

@@ -9,6 +9,28 @@
 
 namespace glm
 {
+	template <typename T, precision P>
+	GLM_FUNC_QUALIFIER detail::tvec3<T, P> slerp
+	(
+		detail::tvec3<T, P> const & x,
+		detail::tvec3<T, P> const & y,
+		T const & a
+	)
+	{
+		// get cosine of angle between vectors (-1 -> 1)
+		T CosAlpha = dot(x, y);
+		// get angle (0 -> pi)
+		T Alpha = acos(CosAlpha);
+		// get sine of angle between vectors (0 -> 1)
+		T SinAlpha = sin(Alpha);
+		// this breaks down when SinAlpha = 0, i.e. Alpha = 0 or pi
+		T t1 = sin((static_cast<T>(1) - a) * Alpha) / SinAlpha;
+		T t2 = sin(a * Alpha) / sinAlpha;
+
+		// interpolate src vectors
+		return x * t1 + y * t2;
+	}
+
 	template <typename T, precision P>
 	GLM_FUNC_QUALIFIER detail::tvec2<T, P> rotate
 	(

+ 1 - 0
readme.txt

@@ -48,6 +48,7 @@ GLM 0.9.6.0: 2014-XX-XX
 - Added *vec1 support to *vec2 types
 - Limited extended integer type redifinition (#233)
 - Improved linearRand: support precision and integers (#230)
+- Added vec3 slerp (#237)
 
 ================================================================================
 GLM 0.9.5.5: 2014-XX-XX