AtlatestRepositorysigil-geometry
sigil-geometry / tree / testtest-camera.sgl
1
;;; Tests for (sigil geometry camera)2
;;;3
;;; Reference matrices were computed in numpy (float64) straight from the4
;;; gluPerspective, glOrtho and gluLookAt definitions, then rounded to 95
;;; places. They are written here column by column.7
(import (sigil test)8
(sigil geometry))10
(define (assert-mat4 expected actual)11
(unless (mat4-approx=? expected actual)12
(assert-equal (mat4->list expected) (mat4->list actual))))14
(define (assert-vec3 expected actual)15
(unless (vec3-approx=? expected actual)16
(assert-equal (vec3->list expected) (vec3->list actual))))18
;; The message of the error `thunk` raises, or #f when it returns.19
(define (error-message thunk)20
(guard (e (#t (error-object-message e)))21
(thunk)22
#f))24
(define fovy-msg "mat4-perspective: fovy must be between 0 and pi radians")25
(define aspect-msg "mat4-perspective: aspect must be positive")26
(define near-far-msg "mat4-perspective: need 0 < near < far")28
(test-group "perspective"29
(test "60 degrees, 16:9, near 0.1, far 100"30
(assert-mat4 (mat4 0.974278579 0 0 031
0 1.732050808 0 032
0 0 -1.002002002 -133
0 0 -0.2002002 0)34
(mat4-perspective (degrees->radians 60) (/ 16.0 9.0) 0.1 100)))36
(test "90 degrees, square, near 1, far 3"37
(assert-mat4 (mat4 1 0 0 0 0 1 0 0 0 0 -2 -1 0 0 -3 0)38
(mat4-perspective (degrees->radians 90) 1 1 3)))40
(test "near and far planes map to NDC z -1 and +1"41
(let ((p (mat4-perspective (degrees->radians 70) 1.5 0.5 40)))42
(assert-true (float-approx=? -1.0 (vec3-z (mat4-transform-point p (vec3 0 0 -0.5)))))43
(assert-true (float-approx=? 1.0 (vec3-z (mat4-transform-point p (vec3 0 0 -40)))))))45
(test "the top edge of the view maps to NDC y = 1"46
;; at distance 1 with fovy 90 degrees the top edge is y = 147
(let ((p (mat4-perspective (degrees->radians 90) 1 0.1 10)))48
(assert-vec3 (vec3 0 1 (vec3-z (mat4-transform-point p (vec3 0 1 -1))))49
(mat4-transform-point p (vec3 0 1 -1)))))51
(test "bad arguments raise the documented error"52
;; the message, not just any error: an incidental division by zero53
;; would also raise54
(assert-equal fovy-msg (error-message (lambda () (mat4-perspective 0 1 0.1 10))))55
(assert-equal fovy-msg (error-message (lambda () (mat4-perspective 4 1 0.1 10))))56
(assert-equal fovy-msg (error-message (lambda () (mat4-perspective 5e-324 1 0.1 10))))57
(assert-equal aspect-msg (error-message (lambda () (mat4-perspective 1 0 0.1 10))))58
(assert-equal aspect-msg (error-message (lambda () (mat4-perspective 1 -1.5 0.1 10))))59
(assert-equal near-far-msg (error-message (lambda () (mat4-perspective 1 1 0 10))))60
(assert-equal near-far-msg (error-message (lambda () (mat4-perspective 1 1 5 5)))))62
(test "! form writes into dst"63
(let ((dst (make-mat4)))64
(assert-true (eq? dst (mat4-perspective! dst 1.0 1.0 1.0 2.0)))65
(assert-mat4 (mat4-perspective 1.0 1.0 1.0 2.0) dst))))67
(test-group "orthographic"68
(test "a 480x270 screen box"69
(assert-mat4 (mat4 0.004166667 0 0 070
0 0.007407407 0 071
0 0 -1 072
-1 -1 0 1)73
(mat4-orthographic 0 480 0 270 -1 1)))75
(test "the unit box only flips z"76
(assert-mat4 (mat4 1 0 0 0 0 1 0 0 0 0 -1 0 0 0 0 1)77
(mat4-orthographic -1 1 -1 1 -1 1)))79
(test "coincident planes raise"80
(assert-equal "mat4-orthographic: opposite planes must differ"81
(error-message (lambda () (mat4-orthographic 1 1 0 1 0 1))))82
(assert-equal "mat4-orthographic: opposite planes must differ"83
(error-message (lambda () (mat4-orthographic 0 1 2 2 0 1))))))85
(test-group "look-at"86
(test "gl-matrix's reference case"87
(assert-mat4 (mat4 1 0 0 0 0 0 1 0 0 -1 0 0 0 0 -2 1)88
(mat4-look-at (vec3 0 2 0) (vec3 0 0.6 0) (vec3 0 0 -1))))90
(test "a camera on +Z looking at the origin is a pure translation"91
(assert-mat4 (mat4-translation (vec3 0 0 -5))92
(mat4-look-at (vec3 0 0 5) (vec3 0 0 0) (vec3 0 1 0))))94
(test "z-up camera"95
(assert-mat4 (mat4 0.8 0 -0.6 0 -0.6 0 -0.8 0 0 1 0 0 0.4 -3 2.2 1)96
(mat4-look-at (vec3 1 2 3) (vec3 4 6 3) (vec3 0 0 1))))98
(test "an oblique camera"99
(assert-mat4 (mat4 0.6 -0.458769875 0.655385536 0100
0 0.819231921 0.573462344 0101
-0.8 -0.344077407 0.491539152 0102
2.2 -0.180231023 -6.717701748 1)103
(mat4-look-at (vec3 3 4 5) (vec3 -1 0.5 2) (vec3 0 1 0))))105
(test "the target lands on -Z at its distance"106
(let* ((eye (vec3 3 4 5)) (target (vec3 -1 0.5 2))107
(v (mat4-look-at eye target (vec3 0 1 0))))108
(assert-vec3 (vec3 0 0 (- (vec3-distance eye target)))109
(mat4-transform-point v target))))111
(test "eye equal to target gives the identity"112
(assert-mat4 (make-mat4) (mat4-look-at (vec3 1 1 1) (vec3 1 1 1) (vec3 0 1 0))))114
(test "looking straight down still gives a rotation"115
(let ((v (mat4-look-at (vec3 0 10 0) (vec3 0 0 0) (vec3 0 1 0))))116
(assert-true (float-approx=? 1.0 (mat4-determinant v)))117
(assert-vec3 (vec3 0 0 -10) (mat4-transform-point v (vec3 0 0 0))))))119
(test-group "a whole camera"120
(test "view-projection puts the target at the centre of the screen"121
(let* ((proj (mat4-perspective (degrees->radians 70) (/ 480.0 270.0) 0.1 100))122
(view (mat4-look-at (vec3 2 1.7 6) (vec3 0 1 0) (vec3 0 1 0)))123
(ndc (mat4-transform-point (mat4-mul proj view) (vec3 0 1 0))))124
(assert-true (float-approx=? 0.0 (vec3-x ndc)))125
(assert-true (float-approx=? 0.0 (vec3-y ndc)))126
(assert-true (< -1.0 (vec3-z ndc) 1.0)))))128
(test-group "look-at with a short up vector"129
(test "a short up is still an up, not a parallel one"130
;; |f x up| is 1e-7 here; judged against |up| it is perpendicular131
(assert-mat4 (mat4-translation (vec3 0 0 -5))132
(mat4-look-at (vec3 0 0 5) (vec3 0 0 0) (vec3 0 1e-7 0)))))