From d53c6fdc3383148b2e73599fb955693aa6f597a5 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Thu, 17 Jul 2014 13:13:26 +0200 Subject: [PATCH] New struct types ai-matrix3x3 and ai-matrix4x4 * src/assimp.scm: New conversion types for matrixes; before matrixes were list of numbers. * src/low-level.scm: Parsers can reverse conversion from scheme to C. * src/low-level/matrix.scm: New struct types from C matrixes. --- src/assimp.scm | 74 +++++++++++++++++++++++++++++++++++++--- src/low-level.scm | 40 ++++++++++++++-------- src/low-level/matrix.scm | 53 ++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 20 deletions(-) create mode 100644 src/low-level/matrix.scm diff --git a/src/assimp.scm b/src/assimp.scm index 5c8ccd2..9954e82 100644 --- a/src/assimp.scm +++ b/src/assimp.scm @@ -20,6 +20,7 @@ #:use-module (assimp low-level cimport) #:use-module (assimp low-level color) #:use-module (assimp low-level material) + #:use-module (assimp low-level matrix) #:use-module (assimp low-level mesh) #:use-module (assimp low-level scene) #:use-module (assimp low-level types) @@ -55,7 +56,7 @@ (define-conversion-type parse-aiNode -> ai-node (name (sized-string (field 'mName))) - (transformation (field 'mTransformation)) + (transformation (wrap (parse-aiMatrix4x4 (field 'mTransformation) #:reverse #t) wrap-ai-matrix4x4)) (parent (wrap (field 'mParent) wrap-ai-node)) (children (wrap (array (field 'mNumChildren) (field 'mChildren)) wrap-ai-node)) (meshes (array (field 'mNumMeshes) (field 'mMeshes)))) @@ -166,6 +167,69 @@ ai-vector3d-z) +;;; Matrixes + +(define-conversion-type parse-aiMatrix3x3 -> ai-matrix3x3 + (a1 (field 'a1)) + (a2 (field 'a2)) + (a3 (field 'a3)) + (b1 (field 'b1)) + (b2 (field 'b2)) + (b3 (field 'b3)) + (c1 (field 'c1)) + (c2 (field 'c2)) + (c3 (field 'c3))) + +(export ai-matrix3x3? + ai-matrix3x3-contents + ai-matrix3x3-a1 + ai-matrix3x3-a2 + ai-matrix3x3-a3 + ai-matrix3x3-b1 + ai-matrix3x3-b2 + ai-matrix3x3-b3 + ai-matrix3x3-c1 + ai-matrix3x3-c2 + ai-matrix3x3-c3) + +(define-conversion-type parse-aiMatrix4x4 -> ai-matrix4x4 + (a1 (field 'a1)) + (a2 (field 'a2)) + (a3 (field 'a3)) + (a4 (field 'a4)) + (b1 (field 'b1)) + (b2 (field 'b2)) + (b3 (field 'b3)) + (b4 (field 'b4)) + (c1 (field 'c1)) + (c2 (field 'c2)) + (c3 (field 'c3)) + (c4 (field 'c4)) + (d1 (field 'd1)) + (d2 (field 'd2)) + (d3 (field 'd3)) + (d4 (field 'd4))) + +(export ai-matrix4x4? + ai-matrix4x4-contents + ai-matrix4x4-a1 + ai-matrix4x4-a2 + ai-matrix4x4-a3 + ai-matrix4x4-a4 + ai-matrix4x4-b1 + ai-matrix4x4-b2 + ai-matrix4x4-b3 + ai-matrix4x4-b4 + ai-matrix4x4-c1 + ai-matrix4x4-c2 + ai-matrix4x4-c3 + ai-matrix4x4-c4 + ai-matrix4x4-d1 + ai-matrix4x4-d2 + ai-matrix4x4-d3 + ai-matrix4x4-d4) + + ;;; Colors (define-conversion-type parse-aiColor4D -> ai-color4d @@ -189,7 +253,7 @@ (weights (wrap (array (field 'mNumWeights) (field 'mWeights) #:element-size 8 #:element-proc get-element-address) wrap-ai-vertex-weight)) - (offset-matrix (field 'mOffsetMatrix))) + (offset-matrix (wrap (parse-aiMatrix4x4 (field 'mOffsetMatrix) #:reverse #t) wrap-ai-matrix4x4))) (export ai-bone? ai-bone-contents @@ -218,7 +282,7 @@ flags))) (define-public (ai-multiply-matrix4 m1 m2) - (let ((cm1 (make-c-struct aiMatrix4x4-type m1)) - (cm2 (make-c-struct aiMatrix4x4-type m2))) + (let ((cm1 (parse-aiMatrix4x4 (map cdr (ai-matrix4x4-contents m1)) #:reverse #t)) + (cm2 (parse-aiMatrix4x4 (map cdr (ai-matrix4x4-contents m2)) #:reverse #t))) (aiMultiplyMatrix4 cm1 cm2) - (parse-c-struct cm1 aiMatrix4x4-type))) + (wrap-ai-matrix4x4 cm1))) diff --git a/src/low-level.scm b/src/low-level.scm index d604789..fd7af9d 100644 --- a/src/low-level.scm +++ b/src/low-level.scm @@ -21,6 +21,21 @@ #:use-module (system foreign)) +(define (mk-string . args) + (string-concatenate + (map (lambda (a) + (if (string? a) + a + (symbol->string (syntax->datum a)))) + args))) + +(define (lambda-mk-symbol x) + (lambda args + (datum->syntax x + (string->symbol + (apply mk-string args))))) + + ;;; Parsers Definition (define-syntax define-struct-parser @@ -29,10 +44,15 @@ ((_ name (field type) ...) (with-syntax (((field-name ...) (map car #'((field type) ...))) ((field-type ...) (map cadr #'((field type) ...)))) - #'(define (name pointer) - (map cons - '(field-name ...) - (parse-c-struct pointer (list field-type ...))))))))) + #'(define* (name pointer-or-data #:key (reverse #f)) + (cond (reverse + (make-c-struct + (list field-type ...) + pointer-or-data)) + (else + (map cons + '(field-name ...) + (parse-c-struct pointer-or-data (list field-type ...))))))))))) (export-syntax define-struct-parser) @@ -41,17 +61,7 @@ (define-syntax define-conversion-type (lambda (x) - (define (mk-string . args) - (string-concatenate - (map (lambda (a) - (if (string? a) - a - (symbol->string (syntax->datum a)))) - args))) - (define (mk-symbol . args) - (datum->syntax x - (string->symbol - (apply mk-string args)))) + (define mk-symbol (lambda-mk-symbol x)) (syntax-case x (->) ((_ parser -> name (field-name field-proc) ...) (with-syntax ((type? (mk-symbol #'name "?")) diff --git a/src/low-level/matrix.scm b/src/low-level/matrix.scm new file mode 100644 index 0000000..416b4d0 --- /dev/null +++ b/src/low-level/matrix.scm @@ -0,0 +1,53 @@ +;;; guile-assimp, foreign interface to libassimp +;;; Copyright (C) 2014 by Javier Sancho Fernandez +;;; +;;; This program is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, or +;;; (at your option) any later version. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see . + + +(define-module (assimp low-level matrix) + #:use-module (assimp low-level) + #:use-module (system foreign)) + + +(define-struct-parser parse-aiMatrix3x3 + (a1 float) + (a2 float) + (a3 float) + (b1 float) + (b2 float) + (b3 float) + (c1 float) + (c2 float) + (c3 float)) + +(define-struct-parser parse-aiMatrix4x4 + (a1 float) + (a2 float) + (a3 float) + (a4 float) + (b1 float) + (b2 float) + (b3 float) + (b4 float) + (c1 float) + (c2 float) + (c3 float) + (c4 float) + (d1 float) + (d2 float) + (d3 float) + (d4 float)) + +(export parse-aiMatrix3x3 + parse-aiMatrix4x4) -- 2.39.2