summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2017-11-12 21:21:50 -0500
committerDavid Thompson <dthompson2@worcester.edu>2017-11-12 21:23:58 -0500
commitd989e2f2de53e73a078d66e8b19e4ed490dea29b (patch)
treea1b98ecfdd4accdad8626d3bbba08e3cded1e935
parenta4670ba826fd6643922072e188ca77f1039769a3 (diff)
math: matrix: Support converting a quaternion into a rotation matrix.
* chickadee/math/matrix.scm (matrix4-rotate!, matrix4-rotate): New procedures.
-rw-r--r--chickadee/math/matrix.scm32
1 files changed, 32 insertions, 0 deletions
diff --git a/chickadee/math/matrix.scm b/chickadee/math/matrix.scm
index c86b20c..1d59674 100644
--- a/chickadee/math/matrix.scm
+++ b/chickadee/math/matrix.scm
@@ -24,6 +24,7 @@
#:use-module (srfi srfi-4)
#:use-module (system foreign)
#:use-module (chickadee math)
+ #:use-module (chickadee math quaternion)
#:use-module (chickadee math vector)
#:export (make-matrix4
make-null-matrix4
@@ -38,6 +39,8 @@
matrix4-translate
matrix4-scale!
matrix4-scale
+ matrix4-rotate!
+ matrix4-rotate
matrix4-rotate-z!
matrix4-rotate-z
transform!))
@@ -317,6 +320,35 @@ clipping plane NEAR and FAR."
(matrix4-scale! matrix s)
matrix))
+(define (matrix4-rotate! matrix q)
+ "Return a new rotation matrix for the quaternion Q."
+ (let ((x (quaternion-x q))
+ (y (quaternion-y q))
+ (z (quaternion-z q))
+ (w (quaternion-w q)))
+ (init-matrix4 matrix
+ (- 1.0 (* 2.0 (* y y)) (* 2.0 (* z z)))
+ (- (* 2.0 x y) (* 2.0 w z))
+ (+ (* 2.0 x z) (* 2.0 w y))
+ 0
+ (+ (* 2.0 x y) (* 2.0 w z))
+ (- 1.0 (* 2.0 (* x x)) (* 2.0 (* z z)))
+ (- (* 2.0 y z) (* 2.0 w x))
+ 0
+ (- (* 2.0 x z) (* 2.0 w y))
+ (+ (* 2.0 y z) (* 2.0 w x))
+ (- 1.0 (* 2.0 (* x x)) (* 2.0 (* y y)))
+ 0.0
+ 0.0
+ 0.0
+ 0.0
+ 1.0)))
+
+(define (matrix4-rotate q)
+ (let ((matrix (make-null-matrix4)))
+ (matrix4-rotate! matrix q)
+ matrix))
+
(define (matrix4-rotate-z! matrix angle)
(init-matrix4 matrix
(cos angle) (- (sin angle)) 0.0 0.0