diff options
-rw-r--r-- | chickadee/audio/openal.scm | 3 | ||||
-rw-r--r-- | chickadee/graphics/shader.scm | 11 | ||||
-rw-r--r-- | chickadee/math/vector.scm | 122 |
3 files changed, 35 insertions, 101 deletions
diff --git a/chickadee/audio/openal.scm b/chickadee/audio/openal.scm index 9cded01..54a8b59 100644 --- a/chickadee/audio/openal.scm +++ b/chickadee/audio/openal.scm @@ -21,6 +21,7 @@ (define-module (chickadee audio openal) #:use-module (chickadee config) + #:use-module (chickadee data bytestruct) #:use-module (chickadee math vector) #:use-module (ice-9 format) #:use-module (ice-9 match) @@ -745,7 +746,7 @@ otherwise." ((or 'position 'velocity 'direction) (al-source-fv (source-id source) param - ((@@ (chickadee math vector) vec3->pointer) value))) + (bytestruct->pointer <vec3> value))) (_ (al-source-f (source-id source) param value))))) diff --git a/chickadee/graphics/shader.scm b/chickadee/graphics/shader.scm index 94c6bfc..1acd3ac 100644 --- a/chickadee/graphics/shader.scm +++ b/chickadee/graphics/shader.scm @@ -26,6 +26,7 @@ #:use-module (srfi srfi-9) #:use-module (srfi srfi-9 gnu) #:use-module (gl) + #:use-module (chickadee data bytestruct) #:use-module (chickadee math matrix) #:use-module (chickadee math vector) #:use-module (chickadee math rect) @@ -176,9 +177,8 @@ #:size 8 ; 8 bytes = 2 floats = 1 vec2 #:validator vec2? #:serializer - (let ((unwrap-vec2 (@@ (chickadee math vector) unwrap-vec2))) - (lambda (bv i v) - (bytevector-copy! (unwrap-vec2 v) 0 bv i 8))) + (lambda (bv i v) + (bytestruct-pack! <vec2> ((() v)) bv i)) #:setter gl-uniform2fv #:null (vec2 0.0 0.0)) @@ -187,9 +187,8 @@ #:size 12 ; 12 bytes = 3 floats = 1 vec3 #:validator vec3? #:serializer - (let ((unwrap-vec3 (@@ (chickadee math vector) unwrap-vec3))) - (lambda (bv i v) - (bytevector-copy! (unwrap-vec3 v) 0 bv i 12))) + (lambda (bv i v) + (bytestruct-pack! <vec3> ((() v)) bv i)) #:setter gl-uniform3fv #:null (vec3 0.0 0.0 0.0)) diff --git a/chickadee/math/vector.scm b/chickadee/math/vector.scm index 9222ed5..1d09e34 100644 --- a/chickadee/math/vector.scm +++ b/chickadee/math/vector.scm @@ -19,13 +19,14 @@ #:use-module (srfi srfi-9) #:use-module (srfi srfi-9 gnu) #:use-module (system foreign) + #:use-module (chickadee data bytestruct) #:use-module (chickadee math) - #:export (vec2 + #:export (<vec2> + vec2 vec2/polar vec2? vec2= vec3= - vec2->pointer vec2-copy vec2-copy! vec2-x @@ -44,6 +45,8 @@ vec2* vec2+ vec2- + + <vec3> vec3 vec3? vec3->pointer @@ -74,22 +77,18 @@ ;; 2D Vectors ;; -(define-record-type <vec2> - (wrap-vec2 bv pointer) +(define-byterecord-type <vec2> + (vec2 x y) vec2? - (bv unwrap-vec2) - (pointer vec2-pointer set-vec2-pointer!)) - -(define (vec2->pointer v) - "Return a foreign pointer to V." - ;; Create foreign pointer lazily. - (or (vec2-pointer v) - (let ((pointer (bytevector->pointer (unwrap-vec2 v)))) - (set-vec2-pointer! v pointer) - pointer))) + (x f32 vec2-x set-vec2-x!) + (y f32 vec2-y set-vec2-y!) + #:printer (lambda (v port) + (format port "#<vec2 ~a ~a>" + (bytestruct-ref <vec2> (x) v) + (bytestruct-ref <vec2> (y) v)))) (define (make-null-vec2) - (wrap-vec2 (make-f32vector 2) #f)) + (vec2 0.0 0.0)) (define-inlinable (vec2= a b) (and (= (vec2-x a) (vec2-x b)) @@ -98,47 +97,16 @@ (define-syntax-rule (with-new-vec2 name body ...) (let ((name (make-null-vec2))) body ... name)) -(define-inlinable (vec2-ref v i) - (f32vector-ref (unwrap-vec2 v) i)) - -(define-inlinable (vec2-set! v i x) - (f32vector-set! (unwrap-vec2 v) i x)) - -(define-inlinable (vec2 x y) - "Return a new vec2 with coordinates (X, Y)." - (with-new-vec2 v - (vec2-set! v 0 x) - (vec2-set! v 1 y))) - (define-inlinable (vec2/polar origin r theta) "Return a new vec2 containing the Cartesian representation of the polar coordinate (R, THETA) with an arbitrary ORIGIN point." (vec2 (+ (vec2-x origin) (* r (cos theta))) (+ (vec2-y origin) (* r (sin theta))))) -(define-inlinable (vec2-x v) - "Return the x coordinate of the vec2 V." - (vec2-ref v 0)) - -(define-inlinable (vec2-y v) - "Return the y coordinate of the vec2 V." - (vec2-ref v 1)) - -(define-inlinable (set-vec2-x! v x) - (vec2-set! v 0 x)) - -(define-inlinable (set-vec2-y! v y) - (vec2-set! v 1 y)) - (define-inlinable (set-vec2! v x y) (set-vec2-x! v x) (set-vec2-y! v y)) -(define (display-vec2 v port) - (format port "#<vec2 ~f, ~f>" (vec2-x v) (vec2-y v))) - -(set-record-type-printer! <vec2> display-vec2) - (define (vec2-copy! source-vec2 target-vec2) "Copy SOURCE-VEC2 to TARGET-VEC2." (set-vec2-x! target-vec2 (vec2-x source-vec2)) @@ -228,22 +196,20 @@ polar coordinate (R, THETA) with an arbitrary ORIGIN point." ;; 3D Vectors ;;; -(define-record-type <vec3> - (wrap-vec3 bv pointer) +(define-byterecord-type <vec3> + (vec3 x y z) vec3? - (bv unwrap-vec3) - (pointer vec3-pointer set-vec3-pointer!)) - -(define (vec3->pointer v) - "Return a foreign pointer to V." - ;; Create foreign pointer lazily. - (or (vec3-pointer v) - (let ((pointer (bytevector->pointer (unwrap-vec3 v)))) - (set-vec3-pointer! v pointer) - pointer))) + (x f32 vec3-x set-vec3-x!) + (y f32 vec3-y set-vec3-y!) + (z f32 vec3-z set-vec3-z!) + #:printer (lambda (v port) + (format port "#<vec3 ~a ~a ~a>" + (bytestruct-ref <vec3> (x) v) + (bytestruct-ref <vec3> (y) v) + (bytestruct-ref <vec3> (z) v)))) (define (make-null-vec3) - (wrap-vec3 (make-f32vector 3) #f)) + (vec3 0.0 0.0 0.0)) (define-inlinable (vec3= a b) (and (= (vec3-x a) (vec3-x b)) @@ -253,49 +219,17 @@ polar coordinate (R, THETA) with an arbitrary ORIGIN point." (define-syntax-rule (with-new-vec3 name body ...) (let ((name (make-null-vec3))) body ... name)) -(define-inlinable (vec3-ref v i) - (f32vector-ref (unwrap-vec3 v) i)) - -(define-inlinable (vec3-set! v i x) - (f32vector-set! (unwrap-vec3 v) i x)) - (define-inlinable (vec3 x y z) (with-new-vec3 v - (vec3-set! v 0 x) - (vec3-set! v 1 y) - (vec3-set! v 2 z))) - -(define-inlinable (vec3-x v) - "Return the x coordinate of the vec3 V." - (vec3-ref v 0)) - -(define-inlinable (vec3-y v) - "Return the y coordinate of the vec3 V." - (vec3-ref v 1)) - -(define-inlinable (vec3-z v) - "Return the z coordinate of the vec3 V." - (vec3-ref v 2)) - -(define-inlinable (set-vec3-x! v x) - (vec3-set! v 0 x)) - -(define-inlinable (set-vec3-y! v y) - (vec3-set! v 1 y)) - -(define-inlinable (set-vec3-z! v z) - (vec3-set! v 2 z)) + (set-vec3-x! v x) + (set-vec3-y! v y) + (set-vec3-z! v z))) (define-inlinable (set-vec3! v x y z) (set-vec3-x! v x) (set-vec3-y! v y) (set-vec3-z! v z)) -(define (display-vec3 v port) - (format port "#<vec3 ~f, ~f, ~f>" (vec3-x v) (vec3-y v) (vec3-z v))) - -(set-record-type-printer! <vec3> display-vec3) - (define (vec3-copy! source-vec3 target-vec3) "Copy SOURCE-VEC3 to TARGET-VEC3." (set-vec3! target-vec3 |