summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2021-05-13 21:01:57 -0400
committerDavid Thompson <dthompson2@worcester.edu>2021-05-13 21:01:57 -0400
commitc98cce478e902e8569dd59dc1d5748ec85cc6a69 (patch)
tree96e5f4cca899289e768ffab0ff4e909bb629a60b
parentdc7520bff1c5544811d9f22b45920eb5fe02fac5 (diff)
math: matrix: Add in-place orthographic/perspective projection procedures.
-rw-r--r--chickadee/math/matrix.scm34
1 files 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)))