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 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;
|
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 detail::tvec2<T, P> rotate
|
2011-10-14 13:07:53 +00:00
|
|
|
(
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::tvec2<T, P> const & v,
|
2011-10-14 13:07:53 +00:00
|
|
|
T const & angle
|
|
|
|
)
|
|
|
|
{
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::tvec2<T, P> Result;
|
2012-01-25 16:37:09 +00:00
|
|
|
T const Cos(cos(angle));
|
|
|
|
T const Sin(sin(angle));
|
2013-12-26 18:15:48 +00:00
|
|
|
|
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 detail::tvec3<T, P> rotate
|
2011-10-14 13:07:53 +00:00
|
|
|
(
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::tvec3<T, P> const & v,
|
|
|
|
T const & angle,
|
|
|
|
detail::tvec3<T, P> const & normal
|
2011-10-14 13:07:53 +00:00
|
|
|
)
|
|
|
|
{
|
2013-04-10 11:46:27 +00:00
|
|
|
return detail::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 detail::tvec3<T, P> rotateGTX(
|
|
|
|
const detail::tvec3<T, P>& x,
|
|
|
|
T angle,
|
|
|
|
const detail::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 detail::tvec4<T, P> rotate
|
2011-10-14 13:07:53 +00:00
|
|
|
(
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::tvec4<T, P> const & v,
|
|
|
|
T const & angle,
|
|
|
|
detail::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 detail::tvec3<T, P> rotateX
|
2011-10-14 13:07:53 +00:00
|
|
|
(
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::tvec3<T, P> const & v,
|
2011-10-14 13:07:53 +00:00
|
|
|
T const & angle
|
|
|
|
)
|
|
|
|
{
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::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 detail::tvec3<T, P> rotateY
|
2011-10-14 13:07:53 +00:00
|
|
|
(
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::tvec3<T, P> const & v,
|
2011-10-14 13:07:53 +00:00
|
|
|
T const & angle
|
|
|
|
)
|
|
|
|
{
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::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 detail::tvec3<T, P> rotateZ
|
2011-10-14 13:07:53 +00:00
|
|
|
(
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::tvec3<T, P> const & v,
|
2011-10-14 13:07:53 +00:00
|
|
|
T const & angle
|
|
|
|
)
|
|
|
|
{
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::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 detail::tvec4<T, P> rotateX
|
2011-10-14 13:07:53 +00:00
|
|
|
(
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::tvec4<T, P> const & v,
|
2011-10-14 13:07:53 +00:00
|
|
|
T const & angle
|
|
|
|
)
|
|
|
|
{
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::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 detail::tvec4<T, P> rotateY
|
2011-10-14 13:07:53 +00:00
|
|
|
(
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::tvec4<T, P> const & v,
|
2011-10-14 13:07:53 +00:00
|
|
|
T const & angle
|
|
|
|
)
|
|
|
|
{
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::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 detail::tvec4<T, P> rotateZ
|
2011-10-14 13:07:53 +00:00
|
|
|
(
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::tvec4<T, P> const & v,
|
2011-10-14 13:07:53 +00:00
|
|
|
T const & angle
|
|
|
|
)
|
|
|
|
{
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::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 detail::tmat4x4<T, P> orientation
|
2011-10-14 13:07:53 +00:00
|
|
|
(
|
2013-04-10 11:46:27 +00:00
|
|
|
detail::tvec3<T, P> const & Normal,
|
|
|
|
detail::tvec3<T, P> const & Up
|
2011-10-14 13:07:53 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if(all(equal(Normal, Up)))
|
2013-04-10 11:46:27 +00:00
|
|
|
return detail::tmat4x4<T, P>(T(1));
|
2010-04-29 10:54:07 +00:00
|
|
|
|
2013-09-15 01:57:12 +00:00
|
|
|
detail::tvec3<T, P> RotationAxis = cross(Up, Normal);
|
2013-12-26 18:15:48 +00:00
|
|
|
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
|