From: Javier Sancho Date: Mon, 7 Jul 2014 14:58:42 +0000 (+0200) Subject: Rewrite definition types using new C parsers X-Git-Url: https://git.jsancho.org/?p=guile-assimp.git;a=commitdiff_plain;h=ebdf468033662662f62674baf9bfa915e6f6a262 Rewrite definition types using new C parsers * src/assimp.scm: Rewrite definition for meshes, materials and faces. * src/low-level/material.scm: Parser for aiMaterial struct. * src/low-level/mesh.scm: Parser for aiMesh and aiFace structs. --- diff --git a/src/assimp.scm b/src/assimp.scm index 874330f..03c9b33 100644 --- a/src/assimp.scm +++ b/src/assimp.scm @@ -16,6 +16,8 @@ (define-module (assimp assimp) + #:use-module (assimp low-level material) + #:use-module (assimp low-level mesh) #:use-module (assimp low-level scene) #:use-module (ice-9 iconv) #:use-module (rnrs bytevectors) @@ -174,16 +176,22 @@ (lambda (alist) (assoc-ref alist name))) -(define (array size-tag root-tag) +(define (get-element-address root-pointer offset) + (make-pointer (+ (pointer-address root-pointer) offset))) + +(define* (array size-tag root-tag #:key (element-size 4) (element-proc bv-uint-ref)) (lambda (alist) (let ((size (assoc-ref alist size-tag)) (root (assoc-ref alist root-tag))) - (let loop ((i 0)) - (cond ((= i size) - '()) - (else - (cons (bv-uint-ref root (* 4 i)) - (loop (+ i 1))))))))) + (cond ((= (pointer-address root) 0) + '()) + (else + (let loop ((i 0)) + (cond ((= i size) + '()) + (else + (cons (element-proc root (* element-size i)) + (loop (+ i 1))))))))))) (define (wrap proc wrap-proc) (define (make-wrap element) @@ -268,39 +276,54 @@ ;;; Meshes -(define AI_MAX_NUMBER_OF_COLOR_SETS 8) - -(define-type mesh - (num-primitive-types (lambda (p) (bv-uint-ref p 0))) - (vertices (get-pointer-of-pointers 4 12)) - (faces (get-structs-array 8 124 8 wrap-face)) - (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 128 132)) - (material-index (lambda (p) (bv-uint-ref p 136)))) +(define-conversion-type parse-aiMesh -> mesh + (name (sized-string 'mName)) + (primitive-types (field 'mPrimitiveTypes)) + (vertices (array 'mNumVertices 'mVertices #:element-proc get-element-address)) + (faces (wrap (array 'mNumFaces 'mFaces #:element-size 8 #:element-proc get-element-address) wrap-face)) + (normals (array 'mNumVertices 'mNormals #:element-size 12 #:element-proc get-element-address)) + (tangents (array 'mNumVertices 'mTangents #:element-size 12 #:element-proc get-element-address)) + (bitangents (array 'mNumVertices 'mBitangents #:element-size 12 #:element-proc get-element-address)) + (colors (field 'mColors)) + (texture-coords (field 'mTextureCoords)) + (num-uv-components (field 'mNumUVComponents)) + (bones (array 'mNumBones 'mBones)) + (material-index (field 'mMaterialIndex)) +) (export mesh? - mesh-contents) + mesh-contents + mesh-name + mesh-primitive-types + mesh-vertices + mesh-faces + mesh-normals + mesh-tangents + mesh-bitangents + mesh-colors + mesh-texture-coords + mesh-num-uv-components + mesh-bones + mesh-material-index) ;;; Materials -(define-type material - (properties (get-pointer-of-pointers 4 0)) - (allocated (lambda (p) (bv-uint-ref p 8)))) +(define-conversion-type parse-aiMaterial -> material + (properties (array 'mNumProperties 'mProperties)) + (num-allocated (field 'mNumAllocated))) (export material? - material-contents) + material-contents + material-properties + material-num-allocated) ;;; Faces -(define-type face - (indices (get-array 0 4 'u32))) +(define-conversion-type parse-aiFace -> face + (indices (array 'mNumIndices 'mIndices))) (export face? - face-contents) + face-contents + face-indices) diff --git a/src/low-level/material.scm b/src/low-level/material.scm new file mode 100644 index 0000000..a67a762 --- /dev/null +++ b/src/low-level/material.scm @@ -0,0 +1,28 @@ +;;; 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 material) + #:use-module (assimp low-level) + #:use-module (system foreign)) + + +(define-struct-parser parse-aiMaterial + (mProperties '*) + (mNumProperties unsigned-int) + (mNumAllocated unsigned-int)) + +(export parse-aiMaterial) diff --git a/src/low-level/mesh.scm b/src/low-level/mesh.scm new file mode 100644 index 0000000..e151045 --- /dev/null +++ b/src/low-level/mesh.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 mesh) + #:use-module (assimp low-level) + #:use-module (assimp low-level types) + #:use-module (system foreign)) + + +(define-struct-parser parse-aiFace + (mNumIndices unsigned-int) + (mIndices '*)) + +(export parse-aiFace) + + +(define AI_MAX_NUMBER_OF_COLOR_SETS #x8) +(define AI_MAX_NUMBER_OF_TEXTURECOORDS #x8) + +(define-struct-parser parse-aiMesh + (mPrimitiveTypes unsigned-int) + (mNumVertices unsigned-int) + (mNumFaces unsigned-int) + (mVertices '*) + (mNormals '*) + (mTangents '*) + (mBitangents '*) + (mColors (make-list AI_MAX_NUMBER_OF_COLOR_SETS '*)) + (mTextureCoords (make-list AI_MAX_NUMBER_OF_TEXTURECOORDS '*)) + (mNumUVComponents (make-list AI_MAX_NUMBER_OF_TEXTURECOORDS unsigned-int)) + (mFaces '*) + (mNumBones unsigned-int) + (mBones '*) + (mMaterialIndex unsigned-int) + (mName aiString-type) + (mNumAnimMeshes unsigned-int) + (mAnimMeshes '*)) + +(export parse-aiMesh)