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 #:re-export (ai-material-key
32 ai-process-convert-to-left-handed
33 ai-process-preset-target-realtime-fast
34 ai-process-preset-target-realtime-quality
35 ai-process-preset-target-realtime-max-quality))
40 (define-conversion-type parse-aiScene -> ai-scene
41 (flags (field 'mFlags))
42 (root-node (wrap (field 'mRootNode) wrap-ai-node))
43 (meshes (wrap (array (field 'mNumMeshes) (field 'mMeshes)) wrap-ai-mesh))
44 (materials (wrap (array (field 'mNumMaterials) (field 'mMaterials)) wrap-ai-material))
45 (animations (array (field 'mNumAnimations) (field 'mAnimations)))
46 (textures (array (field 'mNumTextures) (field 'mTextures)))
47 (lights (array (field 'mNumLights) (field 'mLights)))
48 (cameras (array (field 'mNumCameras) (field 'mCameras))))
64 (define-conversion-type parse-aiNode -> ai-node
65 (name (sized-string (field 'mName)))
66 (transformation (wrap (parse-aiMatrix4x4 (field 'mTransformation) #:reverse #t) wrap-ai-matrix4x4))
67 (parent (wrap (field 'mParent) wrap-ai-node))
68 (children (wrap (array (field 'mNumChildren) (field 'mChildren)) wrap-ai-node))
69 (meshes (array (field 'mNumMeshes) (field 'mMeshes))))
74 ai-node-transformation
82 (define-conversion-type parse-aiMesh -> ai-mesh
83 (name (sized-string (field 'mName)))
84 (primitive-types (field 'mPrimitiveTypes))
86 (array (field 'mNumVertices) (field 'mVertices) #:element-proc get-element-address)
89 (array (field 'mNumFaces) (field 'mFaces) #:element-size 8 #:element-proc get-element-address)
92 (array (field 'mNumVertices) (field 'mNormals) #:element-size 12 #:element-proc get-element-address)
95 (array (field 'mNumVertices) (field 'mTangents) #:element-size 12 #:element-proc get-element-address)
98 (array (field 'mNumVertices) (field 'mBitangents) #:element-size 12 #:element-proc get-element-address)
103 (array (field 'mNumVertices) c #:element-size 16 #:element-proc get-element-address)
109 (array (field 'mNumVertices) tc #:element-size 12 #:element-proc get-element-address)
111 (field 'mTextureCoords)))
112 (num-uv-components (field 'mNumUVComponents))
113 (bones (wrap (array (field 'mNumBones) (field 'mBones)) wrap-ai-bone))
114 (material-index (field 'mMaterialIndex)))
119 ai-mesh-primitive-types
126 ai-mesh-texture-coords
127 ai-mesh-num-uv-components
129 ai-mesh-material-index)
134 (define-conversion-type parse-aiMaterial -> ai-material
135 (properties (array (field 'mNumProperties) (field 'mProperties)))
136 (num-allocated (field 'mNumAllocated)))
140 ai-material-properties
141 ai-material-num-allocated)
144 (define-public (ai-get-material-color mat color-type)
145 (let ((pmat (unwrap-ai-material mat))
146 (pkey (string->pointer (car color-type)))
147 (type (cadr color-type))
148 (index (caddr color-type))
149 (pout (parse-aiColor4D (make-list 4 0) #:reverse #t)))
150 (let ((res (aiGetMaterialColor pmat pkey type index pout)))
153 (wrap-ai-color4d pout)))))
155 (define-public (ai-get-material-float-array mat color-type max)
156 (let ((pmat (unwrap-ai-material mat))
157 (pkey (string->pointer (car color-type)))
158 (type (cadr color-type))
159 (index (caddr color-type))
160 (pout (make-c-struct (make-list max float) '(0)))
161 (pmax (make-c-struct (list unsigned-int) (list max))))
162 (let ((res (aiGetMaterialFloatArray pmat pkey type index pout pmax)))
165 (parse-c-struct pout (make-list max float))))))
167 (define-public (ai-get-material-integer-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 (make-c-struct (make-list max int) '(0)))
173 (pmax (make-c-struct (list unsigned-int) (list max))))
174 (let ((res (aiGetMaterialIntegerArray pmat pkey type index pout pmax)))
177 (parse-c-struct pout (make-list max int))))))
182 (define-conversion-type parse-aiFace -> ai-face
183 (indices (array (field 'mNumIndices) (field 'mIndices))))
192 (define-conversion-type parse-aiVector2D -> ai-vector2d
201 (define-conversion-type parse-aiVector3D -> ai-vector3d
215 (define-conversion-type parse-aiMatrix3x3 -> ai-matrix3x3
226 (export ai-matrix3x3?
227 ai-matrix3x3-contents
238 (define-conversion-type parse-aiMatrix4x4 -> ai-matrix4x4
256 (export ai-matrix4x4?
257 ai-matrix4x4-contents
278 (define-conversion-type parse-aiColor4D -> ai-color4d
294 (define-conversion-type parse-aiBone -> ai-bone
295 (name (sized-string (field 'mName)))
297 (array (field 'mNumWeights) (field 'mWeights) #:element-size 8 #:element-proc get-element-address)
298 wrap-ai-vertex-weight))
299 (offset-matrix (wrap (parse-aiMatrix4x4 (field 'mOffsetMatrix) #:reverse #t) wrap-ai-matrix4x4)))
305 ai-bone-offset-matrix)
310 (define-conversion-type parse-aiVertexWeight -> ai-vertex-weight
311 (vertex-id (field 'mVertexId))
312 (weight (field 'mWeight)))
314 (export ai-vertex-weight?
315 ai-vertex-weight-contents
316 ai-vertex-weight-vertex-id
317 ai-vertex-weight-weight)
322 (define-public (ai-import-file filename flags)
324 (aiImportFile (string->pointer filename)
327 (define-public (ai-transform-vec-by-matrix4 vec mat)
328 (let ((cvec (parse-aiVector3D (map cdr (ai-vector3d-contents vec)) #:reverse #t))
329 (cmat (parse-aiMatrix4x4 (map cdr (ai-matrix4x4-contents mat)) #:reverse #t)))
330 (aiTransformVecByMatrix4 cvec cmat)
331 (wrap-ai-vector3d cvec)))
333 (define-public (ai-multiply-matrix3 m1 m2)
334 (let ((cm1 (parse-aiMatrix3x3 (map cdr (ai-matrix3x3-contents m1)) #:reverse #t))
335 (cm2 (parse-aiMatrix3x3 (map cdr (ai-matrix3x3-contents m2)) #:reverse #t)))
336 (aiMultiplyMatrix3 cm1 cm2)
337 (wrap-ai-matrix3x3 cm1)))
339 (define-public (ai-multiply-matrix4 m1 m2)
340 (let ((cm1 (parse-aiMatrix4x4 (map cdr (ai-matrix4x4-contents m1)) #:reverse #t))
341 (cm2 (parse-aiMatrix4x4 (map cdr (ai-matrix4x4-contents m2)) #:reverse #t)))
342 (aiMultiplyMatrix4 cm1 cm2)
343 (wrap-ai-matrix4x4 cm1)))
345 (define-public (ai-identity-matrix3)
346 (let ((cmat (parse-aiMatrix3x3 (make-list 9 0) #:reverse #t)))
347 (aiIdentityMatrix3 cmat)
348 (wrap-ai-matrix3x3 cmat)))
350 (define-public (ai-identity-matrix4)
351 (let ((cmat (parse-aiMatrix4x4 (make-list 16 0) #:reverse #t)))
352 (aiIdentityMatrix4 cmat)
353 (wrap-ai-matrix4x4 cmat)))
355 (define-public (ai-transpose-matrix3 mat)
356 (let ((cmat (parse-aiMatrix3x3 (map cdr (ai-matrix3x3-contents mat)) #:reverse #t)))
357 (aiTransposeMatrix3 cmat)
358 (wrap-ai-matrix3x3 cmat)))
360 (define-public (ai-transpose-matrix4 mat)
361 (let ((cmat (parse-aiMatrix4x4 (map cdr (ai-matrix4x4-contents mat)) #:reverse #t)))
362 (aiTransposeMatrix4 cmat)
363 (wrap-ai-matrix4x4 cmat)))