1 ;;; guile-assimp, foreign interface to libassimp
2 ;;; Copyright (C) 2014 by Javier Sancho Fernandez <jsf at jsancho dot org>
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;; GNU General Public License for more details.
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 (define-module (assimp assimp)
19 #:use-module (assimp low-level)
20 #:use-module (assimp low-level cimport)
21 #:use-module (assimp low-level color)
22 #:use-module (assimp low-level material)
23 #:use-module (assimp low-level matrix)
24 #:use-module (assimp low-level mesh)
25 #:use-module (assimp low-level postprocess)
26 #:use-module (assimp low-level scene)
27 #:use-module (assimp low-level types)
28 #:use-module (assimp low-level vector)
29 #:use-module (system foreign)
30 #:export (ai-import-file
32 ai-attach-predefined-log-stream
33 ai-transform-vec-by-matrix4
40 #:re-export (ai-material-key
42 ai-process-convert-to-left-handed
43 ai-process-preset-target-realtime-fast
44 ai-process-preset-target-realtime-quality
45 ai-process-preset-target-realtime-max-quality
47 (aiDetachAllLogStreams . ai-detach-all-log-streams)))
52 (define-conversion-type parse-aiScene -> ai-scene
53 (flags (field 'mFlags))
54 (root-node (wrap (field 'mRootNode) wrap-ai-node))
55 (meshes (wrap (array (field 'mNumMeshes) (field 'mMeshes)) wrap-ai-mesh))
56 (materials (wrap (array (field 'mNumMaterials) (field 'mMaterials)) wrap-ai-material))
57 (animations (array (field 'mNumAnimations) (field 'mAnimations)))
58 (textures (array (field 'mNumTextures) (field 'mTextures)))
59 (lights (array (field 'mNumLights) (field 'mLights)))
60 (cameras (array (field 'mNumCameras) (field 'mCameras))))
76 (define-conversion-type parse-aiNode -> ai-node
77 (name (sized-string (field 'mName)))
78 (transformation (wrap (parse-aiMatrix4x4 (field 'mTransformation) #:reverse #t) wrap-ai-matrix4x4))
79 (parent (wrap (field 'mParent) wrap-ai-node))
80 (children (wrap (array (field 'mNumChildren) (field 'mChildren)) wrap-ai-node))
81 (meshes (array (field 'mNumMeshes) (field 'mMeshes))))
86 ai-node-transformation
94 (define-conversion-type parse-aiMesh -> ai-mesh
95 (name (sized-string (field 'mName)))
96 (primitive-types (field 'mPrimitiveTypes))
98 (array (field 'mNumVertices) (field 'mVertices) #:element-size 12 #:element-proc get-element-address)
101 (array (field 'mNumFaces) (field 'mFaces) #:element-size 8 #:element-proc get-element-address)
104 (array (field 'mNumVertices) (field 'mNormals) #:element-size 12 #:element-proc get-element-address)
107 (array (field 'mNumVertices) (field 'mTangents) #:element-size 12 #:element-proc get-element-address)
110 (array (field 'mNumVertices) (field 'mBitangents) #:element-size 12 #:element-proc get-element-address)
115 (array (field 'mNumVertices) c #:element-size 16 #:element-proc get-element-address)
121 (array (field 'mNumVertices) tc #:element-size 12 #:element-proc get-element-address)
123 (field 'mTextureCoords)))
124 (num-uv-components (field 'mNumUVComponents))
125 (bones (wrap (array (field 'mNumBones) (field 'mBones)) wrap-ai-bone))
126 (material-index (field 'mMaterialIndex)))
131 ai-mesh-primitive-types
138 ai-mesh-texture-coords
139 ai-mesh-num-uv-components
141 ai-mesh-material-index)
146 (define-conversion-type parse-aiMaterial -> ai-material
147 (properties (array (field 'mNumProperties) (field 'mProperties)))
148 (num-allocated (field 'mNumAllocated)))
152 ai-material-properties
153 ai-material-num-allocated)
156 (define-public (ai-get-material-color mat color-type)
157 (let ((pmat (unwrap-ai-material mat))
158 (pkey (string->pointer (car color-type)))
159 (type (cadr color-type))
160 (index (caddr color-type))
161 (pout (parse-aiColor4D (make-list 4 0) #:reverse #t)))
162 (let ((res (aiGetMaterialColor pmat pkey type index pout)))
165 (wrap-ai-color4d pout)))))
167 (define-public (ai-get-material-float-array mat color-type max)
168 (let ((pmat (unwrap-ai-material mat))
169 (pkey (string->pointer (car color-type)))
170 (type (cadr color-type))
171 (index (caddr color-type))
172 (pout (bytevector->pointer (list->f32vector (make-list max 0))))
173 (pmax (bytevector->pointer (list->u32vector (list max)))))
174 (let ((res (aiGetMaterialFloatArray pmat pkey type index pout pmax)))
177 (f32vector->list (pointer->bytevector pout max 0 'f32))))))
179 (define-public (ai-get-material-integer-array mat color-type max)
180 (let ((pmat (unwrap-ai-material mat))
181 (pkey (string->pointer (car color-type)))
182 (type (cadr color-type))
183 (index (caddr color-type))
184 (pout (bytevector->pointer (list->s32vector (make-list max 0))))
185 (pmax (bytevector->pointer (list->u32vector (list max)))))
186 (let ((res (aiGetMaterialIntegerArray pmat pkey type index pout pmax)))
189 (s32vector->list (pointer->bytevector pout max 0 's32))))))
194 (define-conversion-type parse-aiFace -> ai-face
195 (indices (array (field 'mNumIndices) (field 'mIndices))))
204 (define-conversion-type parse-aiVector2D -> ai-vector2d
213 (define-conversion-type parse-aiVector3D -> ai-vector3d
227 (define-conversion-type parse-aiMatrix3x3 -> ai-matrix3x3
238 (export ai-matrix3x3?
239 ai-matrix3x3-contents
250 (define-conversion-type parse-aiMatrix4x4 -> ai-matrix4x4
268 (export ai-matrix4x4?
269 ai-matrix4x4-contents
290 (define-conversion-type parse-aiColor4D -> ai-color4d
306 (define-conversion-type parse-aiBone -> ai-bone
307 (name (sized-string (field 'mName)))
309 (array (field 'mNumWeights) (field 'mWeights) #:element-size 8 #:element-proc get-element-address)
310 wrap-ai-vertex-weight))
311 (offset-matrix (wrap (parse-aiMatrix4x4 (field 'mOffsetMatrix) #:reverse #t) wrap-ai-matrix4x4)))
317 ai-bone-offset-matrix)
322 (define-conversion-type parse-aiVertexWeight -> ai-vertex-weight
323 (vertex-id (field 'mVertexId))
324 (weight (field 'mWeight)))
326 (export ai-vertex-weight?
327 ai-vertex-weight-contents
328 ai-vertex-weight-vertex-id
329 ai-vertex-weight-weight)
334 (define (ai-import-file filename flags)
336 (aiImportFile (string->pointer filename)
339 (define (ai-release-import scene)
340 (aiReleaseImport (unwrap-ai-scene scene)))
342 (define* (ai-attach-predefined-log-stream type #:optional file)
344 (aiGetPredefinedLogStream
347 (string->pointer file)
350 (define (ai-transform-vec-by-matrix4 vec mat)
351 (let ((cvec (parse-aiVector3D (map cdr (ai-vector3d-contents vec)) #:reverse #t))
352 (cmat (parse-aiMatrix4x4 (map cdr (ai-matrix4x4-contents mat)) #:reverse #t)))
353 (aiTransformVecByMatrix4 cvec cmat)
354 (wrap-ai-vector3d cvec)))
356 (define (ai-multiply-matrix3 m1 m2)
357 (let ((cm1 (parse-aiMatrix3x3 (map cdr (ai-matrix3x3-contents m1)) #:reverse #t))
358 (cm2 (parse-aiMatrix3x3 (map cdr (ai-matrix3x3-contents m2)) #:reverse #t)))
359 (aiMultiplyMatrix3 cm1 cm2)
360 (wrap-ai-matrix3x3 cm1)))
362 (define (ai-multiply-matrix4 m1 m2)
363 (let ((cm1 (parse-aiMatrix4x4 (map cdr (ai-matrix4x4-contents m1)) #:reverse #t))
364 (cm2 (parse-aiMatrix4x4 (map cdr (ai-matrix4x4-contents m2)) #:reverse #t)))
365 (aiMultiplyMatrix4 cm1 cm2)
366 (wrap-ai-matrix4x4 cm1)))
368 (define (ai-identity-matrix3)
369 (let ((cmat (parse-aiMatrix3x3 (make-list 9 0) #:reverse #t)))
370 (aiIdentityMatrix3 cmat)
371 (wrap-ai-matrix3x3 cmat)))
373 (define (ai-identity-matrix4)
374 (let ((cmat (parse-aiMatrix4x4 (make-list 16 0) #:reverse #t)))
375 (aiIdentityMatrix4 cmat)
376 (wrap-ai-matrix4x4 cmat)))
378 (define (ai-transpose-matrix3 mat)
379 (let ((cmat (parse-aiMatrix3x3 (map cdr (ai-matrix3x3-contents mat)) #:reverse #t)))
380 (aiTransposeMatrix3 cmat)
381 (wrap-ai-matrix3x3 cmat)))
383 (define (ai-transpose-matrix4 mat)
384 (let ((cmat (parse-aiMatrix4x4 (map cdr (ai-matrix4x4-contents mat)) #:reverse #t)))
385 (aiTransposeMatrix4 cmat)
386 (wrap-ai-matrix4x4 cmat)))