Merge pull request #659 from CaptainCarrot/QuatLookAt

Added a quaternion-based "Look At" function #659
This commit is contained in:
Christophe
2017-07-24 12:05:18 +02:00
committed by GitHub
3 changed files with 80 additions and 0 deletions

View File

@@ -177,6 +177,34 @@ namespace glm
vec<3, T, P> const & orig,
vec<3, T, P> const & dest);
/// Build a look at quaternion based on the default handedness.
///
/// @param direction Desired direction of the camera.
/// @param up Up vector, how the camera is oriented. Typically (0, 1, 0).
template<typename T, precision P>
GLM_FUNC_DECL tquat<T, P> quatLookAt(
tvec3<T, P> const & direction,
tvec3<T, P> const & up);
/// Build a right-handed look at quaternion.
///
/// @param direction Desired direction of the camera.
/// @param up Up vector, how the camera is oriented. Typically (0, 1, 0).
template<typename T, precision P>
GLM_FUNC_DECL tquat<T, P> quatLookAtRH(
tvec3<T, P> const & direction,
tvec3<T, P> const & up);
/// Build a left-handed look at quaternion.
///
/// @param eye Position of the camera
/// @param direction Desired direction onto which the +z-axis gets mapped
/// @param up Up vector, how the camera is oriented. Typically (0, 1, 0).
template<typename T, precision P>
GLM_FUNC_DECL tquat<T, P> quatLookAtLH(
tvec3<T, P> const & direction,
tvec3<T, P> const & up);
/// Returns the squared length of x.
///
/// @see gtx_quaternion

View File

@@ -208,5 +208,39 @@ namespace glm
rotationAxis.y * invs,
rotationAxis.z * invs);
}
template<typename T, precision P>
GLM_FUNC_QUALIFIER tquat<T, P> quatLookAt(tvec3<T, P> const& direction, tvec3<T, P> const& up)
{
# if GLM_COORDINATE_SYSTEM == GLM_LEFT_HANDED
return quatLookAtLH(direction, up);
# else
return quatLookAtRH(direction, up);
# endif
}
template<typename T, precision P>
GLM_FUNC_QUALIFIER tquat<T, P> quatLookAtRH(tvec3<T, P> const& direction, tvec3<T, P> const& up)
{
tmat3x3<T, P> Result(uninitialize);
Result[2] = -normalize(direction);
Result[0] = normalize(cross(up, Result[2]));
Result[1] = cross(Result[2], Result[0]);
return quat_cast(Result);
}
template<typename T, precision P>
GLM_FUNC_QUALIFIER tquat<T, P> quatLookAtLH(tvec3<T, P> const& direction, tvec3<T, P> const& up)
{
tmat3x3<T, P> Result(uninitialize);
Result[2] = normalize(direction);
Result[0] = normalize(cross(up, Result[2]));
Result[1] = cross(Result[2], Result[0]);
return quat_cast(Result);
}
}//namespace glm