From f52d92b94148a0fd86ca4b44287543480dedb19f Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sun, 13 Aug 2017 13:55:21 -0400 Subject: math: matrix: Add perspective-projection function. * chickadee/math/matrix.scm (perspective-projection): New procedure. --- chickadee/math/matrix.scm | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/chickadee/math/matrix.scm b/chickadee/math/matrix.scm index 2e94233..cb9f1e1 100644 --- a/chickadee/math/matrix.scm +++ b/chickadee/math/matrix.scm @@ -23,6 +23,7 @@ #:use-module (srfi srfi-9 gnu) #:use-module (srfi srfi-4) #:use-module (system foreign) + #:use-module (chickadee math) #:use-module (chickadee math vector) #:export (make-matrix4 make-null-matrix4 @@ -32,6 +33,7 @@ matrix4-identity! make-identity-matrix4 orthographic-projection + perspective-projection matrix4-translate! matrix4-translate matrix4-scale! @@ -282,6 +284,16 @@ FAR." (- (/ (+ far near) (- far near))) 1.0)) +(define (perspective-projection field-of-vision aspect-ratio near far) + "Return a new matrix4 that represents a perspective projection with +a FIELD-OF-VISION in degrees, the desired ASPECT-RATIO, and the depth +clipping plane NEAR and FAR." + (let ((f (cotan (/ field-of-vision 2)))) + (make-matrix4 (/ f aspect-ratio) 0 0 0 + 0 f 0 0 + 0 0 (/ (+ far near) (- near far)) -1 + 0 0 (/ (* 2 far near) (- near far)) 0))) + (define (matrix4-translate! matrix v) (init-matrix4 matrix 1.0 0.0 0.0 0.0 -- cgit v1.2.3