summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2014-05-26 11:38:30 -0400
committerDavid Thompson <dthompson2@worcester.edu>2014-05-26 11:38:30 -0400
commit3544245f263d93e70e645b8eb1941d6272176adc (patch)
tree637be3e56f48dad19f1978c06096f7ece8a0e3d3
parent31b116d92c0f78ad40e844aa2827010afc5a4767 (diff)
Add perspective-projection procedure.
* 2d/transform.scm (perspective-projection): New procedure.
-rw-r--r--2d/transform.scm29
1 files changed, 28 insertions, 1 deletions
diff --git a/2d/transform.scm b/2d/transform.scm
index e1dba7b..4edd080 100644
--- a/2d/transform.scm
+++ b/2d/transform.scm
@@ -25,6 +25,7 @@
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-42)
+ #:use-module (2d math)
#:use-module (2d vector2)
#:export (make-transform
make-transform*
@@ -40,7 +41,8 @@
transform-translate
transform-scale
transform-rotate
- orthographic-projection))
+ orthographic-projection
+ perspective-projection))
(define-record-type <transform>
(%make-transform matrix)
@@ -183,3 +185,28 @@ FAR."
(- (/ (+ top bottom) (- top bottom)))
(- (/ (+ far near) (- far near)))
1))
+
+(define (perspective-projection field-of-vision aspect-ratio near far)
+ "Return a new transform 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 ((size (* near (tan (/ (degrees->radians field-of-vision) 2)))))
+ (let ((left (- size))
+ (right size)
+ (top (/ size aspect-ratio))
+ (bottom (/ (- size) aspect-ratio)))
+ (make-transform (/ (* 2 near) (- right left)) ;; First row
+ 0
+ (/ (+ right left) (- right left))
+ 0
+ ;; Second row
+ 0
+ (/ (* 2 near) (- top bottom))
+ (/ (+ top bottom) (- top bottom))
+ 0
+ ;; Third row
+ 0 0
+ (- (/ (+ far near) (- far near)))
+ (- (/ (* 2 far near) (- far near)))
+ ;; Fourth row
+ 0 0 -1 0))))