From 65bf7af24e2bf0f7e5b7b7f5ceee1b64f0e73387 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 2 Dec 2015 10:03:25 +1100 Subject: [PATCH] matrix: perspective construction takes a Z-range --- matrix.cpp | 6 +++--- matrix.hpp | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/matrix.cpp b/matrix.cpp index 5d9b1e62..63167538 100644 --- a/matrix.cpp +++ b/matrix.cpp @@ -307,14 +307,14 @@ matrix::ortho2D (T left, T right, //----------------------------------------------------------------------------- template matrix4 -matrix::perspective (T fov, T aspect, T near, T far) +matrix::perspective (T fov, T aspect, range Z) { T f = std::tan (fov / 2); T tx = 1 / (f * aspect); T ty = 1 / f; - T z1 = (far + near) / (near - far); - T z2 = (2 * far * near) / (near - far); + T z1 = (Z.max + Z.min) / (Z.min - Z.max); + T z2 = (2 * Z.max * Z.min) / (Z.min - Z.max); return { { { tx, 0, 0, 0 }, diff --git a/matrix.hpp b/matrix.hpp index 81489f27..88076155 100644 --- a/matrix.hpp +++ b/matrix.hpp @@ -18,6 +18,7 @@ #define __UTIL_MATRIX_HPP #include "point.hpp" +#include "range.hpp" #include @@ -65,7 +66,7 @@ namespace util { // Perspective matrices static matrix<4,T> ortho (T left, T right, T bottom, T top, T near, T far); static matrix<4,T> ortho2D (T left, T right, T bottom, T top); - static matrix<4,T> perspective (T fov, T aspect, T near, T far); + static matrix<4,T> perspective (T fov, T aspect, range Z); static matrix<4,T> look_at (point<3,T> eye, point<3,T> centre, vector<3,T> up); // Affine matrices