瀏覽代碼

Promoted perspective functions

Christophe Riccio 15 年之前
父節點
當前提交
455144b990
共有 2 個文件被更改,包括 89 次插入2 次删除
  1. 24 2
      glm/gtc/matrix_transform.hpp
  2. 65 0
      glm/gtc/matrix_transform.inl

+ 24 - 2
glm/gtc/matrix_transform.hpp

@@ -89,8 +89,30 @@ namespace glm
 			T const & zNear, 
 			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.
-		//! From GLM_GTC_glu_replacement extension.
+		//! From GLM_GTC_matrix_transform extension.
 		template <typename T, typename U> 
 		detail::tvec3<T> project(
 			detail::tvec3<T> const & obj, 
@@ -99,7 +121,7 @@ namespace glm
 			detail::tvec4<U> const & viewport);
 
 		//! 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> 
 		detail::tvec3<T> unProject(
 			detail::tvec3<T> const & win, 

+ 65 - 0
glm/gtc/matrix_transform.inl

@@ -220,6 +220,71 @@ namespace matrix_transform
 		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>
 	inline detail::tvec3<T> project(
 		detail::tvec3<T> const & obj,