glm/glm/gtx/rotate_vector.inl

195 lines
4.2 KiB
Plaintext
Raw Normal View History

2010-04-29 10:54:07 +00:00
///////////////////////////////////////////////////////////////////////////////////////////////////
2014-01-04 21:32:28 +00:00
// OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
2010-04-29 10:54:07 +00:00
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2006-11-02
// Updated : 2009-02-19
// Licence : This source is under MIT License
// File : glm/gtx/rotate_vector.inl
///////////////////////////////////////////////////////////////////////////////////////////////////
2011-10-14 13:07:53 +00:00
namespace glm
2011-06-10 15:45:17 +00:00
{
2014-09-07 17:56:24 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec3<T, P> slerp
2014-09-07 17:56:24 +00:00
(
tvec3<T, P> const & x,
tvec3<T, P> const & y,
2014-09-07 17:56:24 +00:00
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;
2014-09-19 13:08:26 +00:00
T t2 = sin(a * Alpha) / SinAlpha;
2014-09-07 17:56:24 +00:00
// interpolate src vectors
return x * t1 + y * t2;
}
2013-04-10 11:46:27 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec2<T, P> rotate
2011-10-14 13:07:53 +00:00
(
tvec2<T, P> const & v,
2011-10-14 13:07:53 +00:00
T const & angle
)
{
tvec2<T, P> Result;
2012-01-25 16:37:09 +00:00
T const Cos(cos(angle));
T const Sin(sin(angle));
2011-10-14 13:07:53 +00:00
Result.x = v.x * Cos - v.y * Sin;
Result.y = v.x * Sin + v.y * Cos;
return Result;
}
2010-04-29 10:54:07 +00:00
2013-04-10 11:46:27 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec3<T, P> rotate
2011-10-14 13:07:53 +00:00
(
tvec3<T, P> const & v,
2013-04-10 11:46:27 +00:00
T const & angle,
tvec3<T, P> const & normal
2011-10-14 13:07:53 +00:00
)
{
return tmat3x3<T, P>(glm::rotate(angle, normal)) * v;
2011-10-14 13:07:53 +00:00
}
/*
2013-04-10 11:46:27 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec3<T, P> rotateGTX(
const tvec3<T, P>& x,
2013-04-10 11:46:27 +00:00
T angle,
const tvec3<T, P>& normal)
2011-10-14 13:07:53 +00:00
{
const T Cos = cos(radians(angle));
const T Sin = sin(radians(angle));
return x * Cos + ((x * normal) * (T(1) - Cos)) * normal + cross(x, normal) * Sin;
}
*/
2013-04-10 11:46:27 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec4<T, P> rotate
2011-10-14 13:07:53 +00:00
(
tvec4<T, P> const & v,
2013-04-10 11:46:27 +00:00
T const & angle,
tvec3<T, P> const & normal
2011-10-14 13:07:53 +00:00
)
{
return rotate(angle, normal) * v;
}
2010-04-29 10:54:07 +00:00
2013-04-10 11:46:27 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec3<T, P> rotateX
2011-10-14 13:07:53 +00:00
(
tvec3<T, P> const & v,
2011-10-14 13:07:53 +00:00
T const & angle
)
{
tvec3<T, P> Result(v);
2012-01-25 16:37:09 +00:00
T const Cos(cos(angle));
T const Sin(sin(angle));
2011-10-14 13:07:53 +00:00
Result.y = v.y * Cos - v.z * Sin;
Result.z = v.y * Sin + v.z * Cos;
return Result;
}
2010-04-29 10:54:07 +00:00
2013-04-10 11:46:27 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec3<T, P> rotateY
2011-10-14 13:07:53 +00:00
(
tvec3<T, P> const & v,
2011-10-14 13:07:53 +00:00
T const & angle
)
{
tvec3<T, P> Result = v;
2012-01-25 16:37:09 +00:00
T const Cos(cos(angle));
T const Sin(sin(angle));
2011-10-14 13:07:53 +00:00
Result.x = v.x * Cos + v.z * Sin;
Result.z = -v.x * Sin + v.z * Cos;
return Result;
}
2010-04-29 10:54:07 +00:00
2013-04-10 11:46:27 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec3<T, P> rotateZ
2011-10-14 13:07:53 +00:00
(
tvec3<T, P> const & v,
2011-10-14 13:07:53 +00:00
T const & angle
)
{
tvec3<T, P> Result = v;
2012-01-25 16:37:09 +00:00
T const Cos(cos(angle));
T const Sin(sin(angle));
2011-10-14 13:07:53 +00:00
Result.x = v.x * Cos - v.y * Sin;
Result.y = v.x * Sin + v.y * Cos;
return Result;
}
2010-04-29 10:54:07 +00:00
2013-04-10 11:46:27 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec4<T, P> rotateX
2011-10-14 13:07:53 +00:00
(
tvec4<T, P> const & v,
2011-10-14 13:07:53 +00:00
T const & angle
)
{
tvec4<T, P> Result = v;
2012-01-25 16:37:09 +00:00
T const Cos(cos(angle));
T const Sin(sin(angle));
2011-10-14 13:07:53 +00:00
Result.y = v.y * Cos - v.z * Sin;
Result.z = v.y * Sin + v.z * Cos;
return Result;
}
2010-04-29 10:54:07 +00:00
2013-04-10 11:46:27 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec4<T, P> rotateY
2011-10-14 13:07:53 +00:00
(
tvec4<T, P> const & v,
2011-10-14 13:07:53 +00:00
T const & angle
)
{
tvec4<T, P> Result = v;
2012-01-25 16:37:09 +00:00
T const Cos(cos(angle));
T const Sin(sin(angle));
2011-10-14 13:07:53 +00:00
Result.x = v.x * Cos + v.z * Sin;
Result.z = -v.x * Sin + v.z * Cos;
return Result;
}
2010-04-29 10:54:07 +00:00
2013-04-10 11:46:27 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec4<T, P> rotateZ
2011-10-14 13:07:53 +00:00
(
tvec4<T, P> const & v,
2011-10-14 13:07:53 +00:00
T const & angle
)
{
tvec4<T, P> Result = v;
2012-01-25 16:37:09 +00:00
T const Cos(cos(angle));
T const Sin(sin(angle));
2011-10-14 13:07:53 +00:00
Result.x = v.x * Cos - v.y * Sin;
Result.y = v.x * Sin + v.y * Cos;
return Result;
}
2010-04-29 10:54:07 +00:00
2013-04-10 11:46:27 +00:00
template <typename T, precision P>
GLM_FUNC_QUALIFIER tmat4x4<T, P> orientation
2011-10-14 13:07:53 +00:00
(
tvec3<T, P> const & Normal,
tvec3<T, P> const & Up
2011-10-14 13:07:53 +00:00
)
{
if(all(equal(Normal, Up)))
return tmat4x4<T, P>(T(1));
2010-04-29 10:54:07 +00:00
tvec3<T, P> RotationAxis = cross(Up, Normal);
T Angle = acos(dot(Normal, Up));
2011-10-14 13:07:53 +00:00
return rotate(Angle, RotationAxis);
}
2010-04-29 10:54:07 +00:00
}//namespace glm