From c98cce478e902e8569dd59dc1d5748ec85cc6a69 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 13 May 2021 21:01:57 -0400 Subject: math: matrix: Add in-place orthographic/perspective projection procedures. --- chickadee/math/matrix.scm | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/chickadee/math/matrix.scm b/chickadee/math/matrix.scm index f16e583..b79c94d 100644 --- a/chickadee/math/matrix.scm +++ b/chickadee/math/matrix.scm @@ -49,7 +49,9 @@ matrix4-mult! matrix4* matrix4-identity! + orthographic-projection! orthographic-projection + perspective-projection! perspective-projection look-at! look-at @@ -520,11 +522,9 @@ column-major format." (matrix4-identity! matrix) matrix)) -(define (orthographic-projection left right top bottom near far) - "Return a new matrix4 that represents an orthographic projection for -the horizontal clipping plane LEFT and RIGHT, the vertical clipping -plane TOP and BOTTOM, and the depth clipping plane NEAR and FAR." - (make-matrix4 (/ 2 (- right left)) 0.0 0.0 0.0 +(define (orthographic-projection! matrix left right top bottom near far) + (init-matrix4 matrix + (/ 2 (- right left)) 0.0 0.0 0.0 0.0 (/ 2 (- top bottom)) 0.0 0.0 0.0 0.0 (/ 2 (- far near)) 0.0 (- (/ (+ right left) (- right left))) @@ -532,16 +532,30 @@ plane TOP and BOTTOM, and the depth clipping plane NEAR and 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 radians, the desired ASPECT-RATIO, and the depth -clipping plane NEAR and FAR." +(define (orthographic-projection left right top bottom near far) + "Return a new matrix4 that represents an orthographic projection for +the horizontal clipping plane LEFT and RIGHT, the vertical clipping +plane TOP and BOTTOM, and the depth clipping plane NEAR and FAR." + (let ((matrix (make-null-matrix4))) + (orthographic-projection! matrix left right top bottom near far) + matrix)) + +(define (perspective-projection! matrix field-of-vision aspect-ratio near far) (let ((f (cotan (/ field-of-vision 2)))) - (make-matrix4 (/ f aspect-ratio) 0 0 0 + (init-matrix4 matrix + (/ 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 (perspective-projection field-of-vision aspect-ratio near far) + "Return a new matrix4 that represents a perspective projection with +a FIELD-OF-VISION in radians, the desired ASPECT-RATIO, and the depth +clipping plane NEAR and FAR." + (let ((matrix (make-null-matrix4))) + (perspective-projection! matrix field-of-vision aspect-ratio near far) + matrix)) + (define (look-at! matrix eye at up) ;; TODO: Eliminate allocation of vectors. (let* ((zaxis (vec3-normalize (vec3- at eye))) -- cgit v1.2.3