From: Javier Sancho Date: Fri, 25 Jul 2014 12:52:51 +0000 (+0200) Subject: A lot of functionality added X-Git-Url: https://git.jsancho.org/?p=guile-assimp.git;a=commitdiff_plain;h=fe037abb54b99784fa30c9f8ffa1c594482b97bf A lot of functionality added * src/assimp.scm: New foreign functions mapped: ai-get-material-color ai-get-material-float-array ai-get-material-integer-array ai-multiply-matrix3 ai-identity-matrix3 ai-identity-matrix4 ai-transpose-matrix3 ai-transpose-matrix4 * src/low-level.scm: New tools for C library support: define-assimp-function define-enumeration define-bitfield * src/low-level/cimport.scm: New foreign functions defined: aiMultiplyMatrix3 aiMultiplyMatrix4 aiIdentityMatrix3 aiIdentityMatrix4 aiTransposeMatrix3 aiTransposeMatrix4 * src/low-level/material.scm: Parsers: parse-aiMaterial, parse-aiMaterialProperty Enumerations: ai-material-key Functions: aiGetMaterialColor, aiGetMaterialFloatArray, aiGetMaterialIntegerArray * src/low-level/postprocess.scm: Flags used when importing assets. --- diff --git a/src/assimp.scm b/src/assimp.scm index 917a685..dad5634 100644 --- a/src/assimp.scm +++ b/src/assimp.scm @@ -22,10 +22,17 @@ #:use-module (assimp low-level material) #:use-module (assimp low-level matrix) #:use-module (assimp low-level mesh) + #:use-module (assimp low-level postprocess) #:use-module (assimp low-level scene) #:use-module (assimp low-level types) #:use-module (assimp low-level vector) - #:use-module (system foreign)) + #:use-module (system foreign) + #:re-export (ai-material-key + ai-process-steps + ai-process-convert-to-left-handed + ai-process-preset-target-realtime-fast + ai-process-preset-target-realtime-quality + ai-process-preset-target-realtime-max-quality)) ;;; Scenes @@ -134,6 +141,42 @@ ai-material-num-allocated) +(define-public (ai-get-material-color mat color-type) + (let ((pmat (unwrap-ai-material mat)) + (pkey (string->pointer (car color-type))) + (type (cadr color-type)) + (index (caddr color-type)) + (pout (parse-aiColor4D (make-list 4 0) #:reverse #t))) + (let ((res (aiGetMaterialColor pmat pkey type index pout))) + (if (< res 0) + res + (wrap-ai-color4d pout))))) + +(define-public (ai-get-material-float-array mat color-type max) + (let ((pmat (unwrap-ai-material mat)) + (pkey (string->pointer (car color-type))) + (type (cadr color-type)) + (index (caddr color-type)) + (pout (make-c-struct (make-list max float) '(0))) + (pmax (make-c-struct (list unsigned-int) (list max)))) + (let ((res (aiGetMaterialFloatArray pmat pkey type index pout pmax))) + (if (< res 0) + res + (parse-c-struct pout (make-list max float)))))) + +(define-public (ai-get-material-integer-array mat color-type max) + (let ((pmat (unwrap-ai-material mat)) + (pkey (string->pointer (car color-type))) + (type (cadr color-type)) + (index (caddr color-type)) + (pout (make-c-struct (make-list max int) '(0))) + (pmax (make-c-struct (list unsigned-int) (list max)))) + (let ((res (aiGetMaterialIntegerArray pmat pkey type index pout pmax))) + (if (< res 0) + res + (parse-c-struct pout (make-list max int)))))) + + ;;; Faces (define-conversion-type parse-aiFace -> ai-face @@ -287,8 +330,34 @@ (aiTransformVecByMatrix4 cvec cmat) (wrap-ai-vector3d cvec))) +(define-public (ai-multiply-matrix3 m1 m2) + (let ((cm1 (parse-aiMatrix3x3 (map cdr (ai-matrix3x3-contents m1)) #:reverse #t)) + (cm2 (parse-aiMatrix3x3 (map cdr (ai-matrix3x3-contents m2)) #:reverse #t))) + (aiMultiplyMatrix3 cm1 cm2) + (wrap-ai-matrix3x3 cm1))) + (define-public (ai-multiply-matrix4 m1 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) (wrap-ai-matrix4x4 cm1))) + +(define-public (ai-identity-matrix3) + (let ((cmat (parse-aiMatrix3x3 (make-list 9 0) #:reverse #t))) + (aiIdentityMatrix3 cmat) + (wrap-ai-matrix3x3 cmat))) + +(define-public (ai-identity-matrix4) + (let ((cmat (parse-aiMatrix4x4 (make-list 16 0) #:reverse #t))) + (aiIdentityMatrix4 cmat) + (wrap-ai-matrix4x4 cmat))) + +(define-public (ai-transpose-matrix3 mat) + (let ((cmat (parse-aiMatrix3x3 (map cdr (ai-matrix3x3-contents mat)) #:reverse #t))) + (aiTransposeMatrix3 cmat) + (wrap-ai-matrix3x3 cmat))) + +(define-public (ai-transpose-matrix4 mat) + (let ((cmat (parse-aiMatrix4x4 (map cdr (ai-matrix4x4-contents mat)) #:reverse #t))) + (aiTransposeMatrix4 cmat) + (wrap-ai-matrix4x4 cmat))) diff --git a/src/low-level.scm b/src/low-level.scm index fd7af9d..148b26d 100644 --- a/src/low-level.scm +++ b/src/low-level.scm @@ -21,6 +21,8 @@ #:use-module (system foreign)) +;;; Generic Functions + (define (mk-string . args) (string-concatenate (map (lambda (a) @@ -177,4 +179,52 @@ (dynamic-func name-string foreign-lib) (list arg-type ...)))))))) -(export-syntax define-foreign-function) + +(define libassimp (dynamic-link "libassimp")) + +(define-syntax define-assimp-function + (syntax-rules (->) + ((_ (name arg-type ...) -> return-type) + (define-foreign-function ((libassimp name) arg-type ...) -> return-type)))) + + +(export-syntax define-foreign-function + define-assimp-function) + + +;;; Enumerators and bitfields (copìed from figl code, thanks to Andy Wingo + +(define-syntax-rule (define-enumeration enumerator (name value) ...) + (define-syntax enumerator + (lambda (x) + (syntax-case x () + ((_) + #''(name ...)) + ((_ enum) (number? (syntax->datum #'enum)) + #'enum) + ((_ enum) + #'(or (assq-ref `((name . ,(syntax->datum value)) ...) + (syntax->datum #'enum)) + (syntax-violation 'enumerator "invalid enumerated value" + #'enum))))))) + +(define-syntax-rule (define-bitfield bitfield (name value) ...) + (define-syntax bitfield + (lambda (x) + (syntax-case x () + ((_) + #''(name ...)) + ((_ bit (... ...)) + #`(logior + #,@(map + (lambda (bit) + (let ((datum (syntax->datum bit))) + (if (number? datum) + datum + (or (assq-ref '((name . value) ...) datum) + (syntax-violation 'bitfield "invalid bitfield value" + bit))))) + #'(bit (... ...))))))))) + +(export-syntax define-enumeration + define-bitfield) diff --git a/src/low-level/cimport.scm b/src/low-level/cimport.scm index b0d71e7..023dc7a 100644 --- a/src/low-level/cimport.scm +++ b/src/low-level/cimport.scm @@ -20,15 +20,18 @@ #:use-module (system foreign) #:export (aiImportFile aiTransformVecByMatrix4 - aiMultiplyMatrix4)) - -(define libassimp (dynamic-link "libassimp")) - -(define-syntax define-assimp-function - (syntax-rules (->) - ((_ (name arg-type ...) -> return-type) - (define-foreign-function ((libassimp name) arg-type ...) -> return-type)))) + aiMultiplyMatrix3 + aiMultiplyMatrix4 + aiIdentityMatrix3 + aiIdentityMatrix4 + aiTransposeMatrix3 + aiTransposeMatrix4)) (define-assimp-function (aiImportFile '* unsigned-int) -> '*) (define-assimp-function (aiTransformVecByMatrix4 '* '*) -> void) +(define-assimp-function (aiMultiplyMatrix3 '* '*) -> void) (define-assimp-function (aiMultiplyMatrix4 '* '*) -> void) +(define-assimp-function (aiIdentityMatrix3 '*) -> void) +(define-assimp-function (aiIdentityMatrix4 '*) -> void) +(define-assimp-function (aiTransposeMatrix3 '*) -> void) +(define-assimp-function (aiTransposeMatrix4 '*) -> void) diff --git a/src/low-level/material.scm b/src/low-level/material.scm index a67a762..2b3ade1 100644 --- a/src/low-level/material.scm +++ b/src/low-level/material.scm @@ -17,12 +17,52 @@ (define-module (assimp low-level material) #:use-module (assimp low-level) - #:use-module (system foreign)) + #:use-module (assimp low-level types) + #:use-module (system foreign) + #:export (parse-aiMaterial + parse-aiMaterialProperty + ai-material-key + aiGetMaterialColor + aiGetMaterialFloatArray + aiGetMaterialIntegerArray)) +(define-struct-parser parse-aiMaterialProperty + (mKey aiString-type) + (mSemantic unsigned-int) + (mIndex unsigned-int) + (mDataLength unsigned-int) + (mType unsigned-int) + (mData '*)) + (define-struct-parser parse-aiMaterial (mProperties '*) (mNumProperties unsigned-int) (mNumAllocated unsigned-int)) -(export parse-aiMaterial) + +(define-enumeration + ai-material-key + (name '("?mat.name" 0 0)) + (twosided '("$mat.twosided" 0 0)) + (shading-model '("$mat.shadingm" 0 0)) + (enable-wireframe '("$mat.wireframe" 0 0)) + (blend-func '("$mat.blend" 0 0)) + (opacity '("$mat.opacity" 0 0)) + (bumpscaling '("$mat.bumpscaling" 0 0)) + (shininess '("$mat.shininess" 0 0)) + (reflectivity '("$mat.reflectivity" 0 0)) + (shininess-strength '("$mat.shinpercent" 0 0)) + (refracti '("$mat.refracti" 0 0)) + (color-diffuse '("$clr.diffuse" 0 0)) + (color-ambient '("$clr.ambient" 0 0)) + (color-specular '("$clr.specular" 0 0)) + (color-emissive '("$clr.emissive" 0 0)) + (color-transparent '("$clr.transparent" 0 0)) + (color-reflective '("$clr.reflective" 0 0)) + (global-background-image '("?bg.global" 0 0))) + + +(define-assimp-function (aiGetMaterialColor '* '* unsigned-int unsigned-int '*) -> int) +(define-assimp-function (aiGetMaterialFloatArray '* '* unsigned-int unsigned-int '* '*) -> int) +(define-assimp-function (aiGetMaterialIntegerArray '* '* unsigned-int unsigned-int '* '*) -> int) diff --git a/src/low-level/postprocess.scm b/src/low-level/postprocess.scm new file mode 100644 index 0000000..20ca550 --- /dev/null +++ b/src/low-level/postprocess.scm @@ -0,0 +1,92 @@ +;;; 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 postprocess) + #:use-module (assimp low-level) + #:export (ai-process-steps + ai-process-convert-to-left-handed + ai-process-preset-target-realtime-fast + ai-process-preset-target-realtime-quality + ai-process-preset-target-realtime-max-quality)) + + +(define-bitfield + ai-process-steps + (calc-tangent-space #x1) + (join-identical-vertices #x2) + (make-left-handed #x4) + (triangulate #x8) + (remove-component #x10) + (gen-normals #x20) + (gen-smooth-normals #x40) + (split-large-meshes #x80) + (pretransform-vertices #x100) + (limit-bone-weights #x200) + (validate-data-structure #x400) + (improve-cache-locality #x800) + (remove-redundant-materials #x1000) + (fix-infacing-normals #x2000) + (sort-by-ptype #x8000) + (find-degenerates #x10000) + (find-invalid-data #x20000) + (gen-UV-coords #x40000) + (transform-UV-coords #x80000) + (find-instances #x100000) + (optimize-meshes #x200000) + (optimize-graph #x400000) + (flip-UVs #x800000) + (flip-winding-order #x1000000) + (split-by-bone-count #x2000000) + (debone #x4000000)) + +(define ai-process-convert-to-left-handed + (ai-process-steps + make-left-handed + flip-UVs + flip-winding-order)) + +(define ai-process-preset-target-realtime-fast + (ai-process-steps + calc-tangent-space + gen-normals + join-identical-vertices + triangulate + gen-UV-coords + sort-by-ptype)) + +(define ai-process-preset-target-realtime-quality + (ai-process-steps + calc-tangent-space + gen-smooth-normals + join-identical-vertices + improve-cache-locality + limit-bone-weights + remove-redundant-materials + split-large-meshes + triangulate + gen-UV-coords + sort-by-ptype + find-degenerates + find-invalid-data)) + +(define ai-process-preset-target-realtime-max-quality + (+ ai-process-preset-target-realtime-quality + (ai-process-steps + find-instances + validate-data-structure + optimize-meshes + debone)))