From: Javier Sancho Date: Fri, 23 May 2014 07:12:21 +0000 (+0200) Subject: Improving ways for accessing data structs X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=279b472d96c8bbe3edb18f1bd3998bb39b65324b;p=guile-assimp.git Improving ways for accessing data structs --- diff --git a/src/assimp.scm b/src/assimp.scm index 4ecbcf4..e4d8f8e 100644 --- a/src/assimp.scm +++ b/src/assimp.scm @@ -26,24 +26,117 @@ (dynamic-func "aiImportFile" libassimp) (list '* unsigned-int))) -(define-wrapped-pointer-type scene - scene? - wrap-scene unwrap-scene - (lambda (s p) - (format p "#" - (pointer-address (unwrap-scene s))))) + +;;; Type Generation + +(define-syntax define-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)))) + (syntax-case x () + ((_ name (field field-proc) ...) + (with-syntax ((type? (mk-symbol #'name "?")) + (wrap-type (mk-symbol "wrap-" #'name)) + (unwrap-type (mk-symbol "unwrap-" #'name)) + (output-string (mk-string "#<" #'name " ~x>")) + (type-contents (mk-symbol #'name "-contents"))) + #'(begin + (define-wrapped-pointer-type name + type? + wrap-type unwrap-type + (lambda (x p) + (format p output-string + (pointer-address (unwrap-type x))))) + (define (type-contents wrapped) + (let ((unwrapped (unwrap-type wrapped))) + (cond ((= (pointer-address unwrapped) 0) + '()) + (else + (filter + (lambda (f) + (not (null? (cdr f)))) + (list (cons 'field (field-proc unwrapped)) + ...)))))))))))) + + +(define (bv-uint-ref pointer index) + (bytevector-uint-ref + (pointer->bytevector pointer 4 index) + 0 + (native-endianness) + 4)) + +(define* (get-pointer-of-pointers-procedure num-index root-index #:optional wrap-proc) + (lambda (pointer) + (let* ((num (bv-uint-ref pointer num-index)) + (rootp (make-pointer (bv-uint-ref pointer root-index)))) + (let loop ((i 0)) + (cond ((= i num) + '()) + (else + (let* ((p (make-pointer (bv-uint-ref rootp (* 4 i)))) + (wp (if wrap-proc (wrap-proc p) p))) + (cons wp (loop (+ i 1)))))))))) + +;;; Scenes + +(define-type scene + (flags (lambda (p) (bv-uint-ref p 0))) + (root-node (lambda (p) (wrap-node (make-pointer (bv-uint-ref p 4))))) + (meshes (get-pointer-of-pointers-procedure 8 12 wrap-mesh)) + (materials (get-pointer-of-pointers-procedure 16 20)) + (animations (get-pointer-of-pointers-procedure 24 28)) + (textures (get-pointer-of-pointers-procedure 32 36)) + (lights (get-pointer-of-pointers-procedure 40 44)) + (cameras (get-pointer-of-pointers-procedure 48 52))) (define (load-scene filename flags) (wrap-scene (aiImportFile (string->pointer filename) flags))) -(bytevector-uint-ref (pointer->bytevector numptob (sizeof long)) - 0 (native-endianness) - (sizeof long)) +(export load-scene + unwrap-scene + scene? + scene-contents) + + +;;; Nodes + +(define-type node + (name (lambda (p) (bv-uint-ref p 0))) ;check, it's a struct + (transformation (lambda (p) (bv-uint-ref p 1028))) ;check, it's a struct + (parent (lambda (p) (bv-uint-ref p 1092))) + (children (get-pointer-of-pointers-procedure 1096 1100)) + (meshes (get-pointer-of-pointers-procedure 1104 1108))) + +(export node? + node-contents) + + +;;; Meshes -(define (scene-contents scene) - (bytevector-uint-ref (pointer->bytevector (unwrap-scene scene) 16) - 0 (native-endiannes) 4)) +(define-type mesh + (num-primitive-types (lambda (p) (bv-uint-ref p 0))) + (vertices (get-pointer-of-pointers-procedure 4 12)) + (faces (get-pointer-of-pointers-procedure 8 124)) + (normals (lambda (p) (bv-uint-ref p 16))) + (tangents (lambda (p) (bv-uint-ref p 20))) + (bitangents (lambda (p) (bv-uint-ref p 24))) + (colors (lambda (p) (bv-uint-ref p 28))) ;AI_MAX_NUMBER_OF_COLOR_SETS + (texture-coords (lambda (p) (bv-uint-ref p 60))) ;AI_MAX_NUMBER_OF_TEXTURECOORDS + (num-uv-components (lambda (p) (bv-uint-ref p 92))) ;AI_MAX_NUMBER_OF_TEXTURECOORDS + (bones (get-pointer-of-pointers-procedure 128 132)) + (material-index (lambda (p) (bv-uint-ref p 136)))) -(export load-scene) +(export mesh? + mesh-contents) diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..d93d72e --- /dev/null +++ b/src/test.c @@ -0,0 +1,18 @@ +#include +#include + +#include + +#include +#include +#include + +int main(int argc, char **argv) +{ + const struct aiScene* scene = NULL; + const struct aiNode* node = NULL; + + scene = aiImportFile("/home/jsancho/Imágenes/fleurOptonl.blend", aiProcessPreset_TargetRealtime_MaxQuality); + node = scene->mRootNode; + printf("Node:\n parent: %d\n children: %d\n meshes: %d\n", node->mParent, node->mNumChildren, node->mNumMeshes); +}