Promoted perspective functions

This commit is contained in:
Christophe Riccio 2010-12-13 12:04:37 +00:00
parent 4425b722c0
commit 455144b990
2 changed files with 89 additions and 2 deletions

View File

@ -89,8 +89,30 @@ namespace glm
T const & zNear, T const & zNear,
T const & zFar); T const & zFar);
//! Builds a perspective projection matrix based on a field of view
//! From GLM_GTC_matrix_transform extension.
template <typename valType>
detail::tmat4x4<valType> perspectiveFov(
valType const & fov,
valType const & width,
valType const & height,
valType const & zNear,
valType const & zFar);
//! Creates a matrix for a symmetric perspective-view frustum with far plane at infinite .
//! From GLM_GTC_matrix_transform extension.
template <typename T>
detail::tmat4x4<T> infinitePerspective(
T fovy, T aspect, T zNear);
//! Creates a matrix for a symmetric perspective-view frustum with far plane at infinite for graphics hardware that doesn't support depth clamping.
//! From GLM_GTC_matrix_transform extension.
template <typename T>
detail::tmat4x4<T> tweakedInfinitePerspective(
T fovy, T aspect, T zNear);
//! Map the specified object coordinates (obj.x, obj.y, obj.z) into window coordinates. //! Map the specified object coordinates (obj.x, obj.y, obj.z) into window coordinates.
//! From GLM_GTC_glu_replacement extension. //! From GLM_GTC_matrix_transform extension.
template <typename T, typename U> template <typename T, typename U>
detail::tvec3<T> project( detail::tvec3<T> project(
detail::tvec3<T> const & obj, detail::tvec3<T> const & obj,
@ -99,7 +121,7 @@ namespace glm
detail::tvec4<U> const & viewport); detail::tvec4<U> const & viewport);
//! Map the specified window coordinates (win.x, win.y, win.z) into object coordinates. //! Map the specified window coordinates (win.x, win.y, win.z) into object coordinates.
//! From GLM_GTC_glu_replacement extension. //! From GLM_GTC_matrix_transform extension.
template <typename T, typename U> template <typename T, typename U>
detail::tvec3<T> unProject( detail::tvec3<T> unProject(
detail::tvec3<T> const & win, detail::tvec3<T> const & win,

View File

@ -220,6 +220,71 @@ namespace matrix_transform
return Result; return Result;
} }
template <typename valType>
inline detail::tmat4x4<valType> perspectiveFov
(
valType const & fov,
valType const & width,
valType const & height,
valType const & zNear,
valType const & zFar
)
{
valType rad = glm::radians(fov);
valType h = glm::cos(valType(0.5) * rad) / glm::sin(valType(0.5) * rad);
valType w = h * height / width;
detail::tmat4x4<valType> Result(valType(0));
Result[0][0] = w;
Result[1][1] = h;
Result[2][2] = (zFar + zNear) / (zFar - zNear);
Result[2][3] = valType(1);
Result[3][2] = -(valType(2) * zFar * zNear) / (zFar - zNear);
return Result;
}
template <typename T>
inline detail::tmat4x4<T> infinitePerspective(
T fovy,
T aspect,
T zNear)
{
T range = tan(radians(fovy / T(2))) * zNear;
T left = -range * aspect;
T right = range * aspect;
T bottom = -range;
T top = range;
detail::tmat4x4<T> Result(T(0));
Result[0][0] = (T(2) * zNear) / (right - left);
Result[1][1] = (T(2) * zNear) / (top - bottom);
Result[2][2] = - T(1);
Result[2][3] = - T(1);
Result[3][2] = - T(2) * zNear;
return Result;
}
template <typename T>
inline detail::tmat4x4<T> tweakedInfinitePerspective(
T fovy,
T aspect,
T zNear)
{
T range = tan(radians(fovy / T(2))) * zNear;
T left = -range * aspect;
T right = range * aspect;
T bottom = -range;
T top = range;
detail::tmat4x4<T> Result(T(0));
Result[0][0] = (T(2) * zNear) / (right - left);
Result[1][1] = (T(2) * zNear) / (top - bottom);
Result[2][2] = T(0.0001) - T(1);
Result[2][3] = T(-1);
Result[3][2] = - (T(0.0001) - T(2)) * zNear;
return Result;
}
template <typename T, typename U> template <typename T, typename U>
inline detail::tvec3<T> project( inline detail::tvec3<T> project(
detail::tvec3<T> const & obj, detail::tvec3<T> const & obj,