From d989e2f2de53e73a078d66e8b19e4ed490dea29b Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sun, 12 Nov 2017 21:21:50 -0500 Subject: math: matrix: Support converting a quaternion into a rotation matrix. * chickadee/math/matrix.scm (matrix4-rotate!, matrix4-rotate): New procedures. --- chickadee/math/matrix.scm | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) 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 -- cgit v1.2.3