Skip to content

Instantly share code, notes, and snippets.

@shakdwipeea
Last active May 3, 2020 23:20
Show Gist options
  • Save shakdwipeea/822ba61e3435022c0e005988c1f3e1fd to your computer and use it in GitHub Desktop.
Save shakdwipeea/822ba61e3435022c0e005988c1f3e1fd to your computer and use it in GitHub Desktop.
#include <assimp/cimport.h> // Plain-C interface
#include <assimp/scene.h> // Output data structure
#include <assimp/postprocess.h> // Post processing flags
#include <stdio.h>
int main()
{
// Start the import on the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll t
// probably to request more postprocessing than we do in this example.
struct aiScene* scene = aiImportFile( "models/cube.obj",
aiProcess_FlipWindingOrder |
aiProcess_Triangulate |
aiProcess_PreTransformVertices );
// If the import failed, report it
if( !scene)
{
return 0;
}
struct aiMesh** mesh = scene->mMeshes;
int numVertices = (*mesh)->mNumVertices;
printf("%d \n", numVertices);
for(int i = 0; i < numVertices; i++) {
struct aiVector3D v = (*mesh)->mVertices[i];
printf("vertex %f %f %f \n", v.x, v.y, v.z);
}
// We're done. Release all resources associated with this import
aiReleaseImport( scene);
return 0;
}
;; assimp for chez
;; ref: https://www.cs.indiana.edu/~dyb/pubs/ftypes.pdf
(define-syntax (define-enum-ftype x)
(define (enum e*)
(let f ([e* e*] [n 0])
(if (null? e*)
'()
(syntax-case (car e*) ()
[(e v)
(cons #'(e v)
(f (cdr e*)
(+ (syntax->datum #'v) 1)))]
[e (identifier? #'e)
(cons #`(e #,n)
(f (cdr e*) (+ n 1)))]))))
(syntax-case x ()
[(_ name e* ...)
(with-syntax ([((e v) ...) (enum #'(e* ...))])
#'(begin
(define-ftype name int)
(define e v) ...))]))
;; Meshes
;; some constants
;; ---------------------------------------------------------------------------
;; Limits. These values are required to match the settings Assimp was
;; compiled against. Therefore, do not redefine them unless you build the
;; library from source using the same definitions.
;; ---------------------------------------------------------------------------
;; Maximum number of indices per face (polygon).
(define %max-face-indices% #x7fff)
;; Maximum number of indices per face (polygon).
(define %max-bone-weights% #x7fffffff)
;; Maximum number of vertices per mesh.
(define %max-vertices% #x7fffffff)
;; Maximum number of faces per mesh.
(define %max-faces% #x7fffffff)
;; Supported number of vertex color sets per mesh.
(define %max-number-of-color-sets% #x8)
;; Supported number of texture coord sets (UV(W) channels) per mesh
(define %max-number-of-texture-coords% #x8)
;; structs
;; change to float gives the correct o/p
(define-ftype ai-real float)
(define-ftype vector3 (struct (x ai-real) (y ai-real) (z ai-real)))
(define-ftype color4
(struct (r ai-real)
(g ai-real)
(b ai-real)
(a ai-real)))
;; A single face in a mesh, referring to multiple vertices.
;; If mNumIndices is 3, we call the face 'triangle', for mNumIndices > 3
;; it's called 'polygon' (hey, that's just a definition!).
(define-ftype face
(struct (num-indices unsigned)
(indices (* unsigned))))
(define-ftype aabb
(struct (min vector3)
(max vector3)))
;; struct representing a geometry or model with a single material
;; A mesh uses only a single material which is referenced by a material ID
(define-ftype mesh
(struct (primitive-types unsigned)
(num-vertices unsigned)
(num-faces unsigned)
(vertices (* vector3))
(normals (* vector3))
(tangents (* vector3))
(bit-tangents (* vector3))
(colors (array #x8 (* color4)))
(texture-coords (array #x8 (* vector3)))
(num-uv-component (array #x8 unsigned))
(faces (* face))
(num-bones unsigned)
(bones uptr)
(material-index unsigned)
(name uptr)
(num-anim-meshes unsigned)
(anim-meshes uptr)
(method unsigned)
(_aabb aabb)))
(define-ftype scene
(struct (flags unsigned)
(root-node uptr)
(num-meshes unsigned)
(meshes uptr)
(num-materials unsigned)
(materials uptr)
(num-animations unsigned)
(animations uptr)
(num-textures unsigned)
(textures uptr)
(num-lights unsigned)
(lights uptr)
(num-cameras unsigned)
(cameras uptr)
(metadata uptr)))
(define-enum-ftype 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))
;; load library
(define o (load-shared-object "libassimp.so.5.0.0"))
;; define native functions
(define import_file (foreign-procedure "aiImportFile" (string unsigned) (* scene)))
(define s (import_file "cube.obj"
(bitwise-ior flip-winding-order
triangulate
pretransform-vertices)))
(define m (make-ftype-pointer mesh (foreign-ref 'uptr (ftype-ref scene (meshes) s) 0)))
(for-each (lambda (i)
(let ((x (ftype-ref mesh (vertices i x) m))
(y (ftype-ref mesh (vertices i y) m))
(z (ftype-ref mesh (vertices i z) m)))
(display x) (display ",") (display y) (display ",") (display z)
(newline)))
(iota (ftype-ref mesh (num-vertices) m)))
36
vertex -5.000000 -5.000000 -5.000000
vertex -5.000000 5.000000 -5.000000
vertex 5.000000 5.000000 -5.000000
vertex 5.000000 5.000000 -5.000000
vertex 5.000000 -5.000000 -5.000000
vertex -5.000000 -5.000000 -5.000000
vertex -5.000000 -5.000000 5.000000
vertex 5.000000 -5.000000 5.000000
vertex 5.000000 5.000000 5.000000
vertex 5.000000 5.000000 5.000000
vertex -5.000000 5.000000 5.000000
vertex -5.000000 -5.000000 5.000000
vertex -5.000000 -5.000000 -5.000000
vertex 5.000000 -5.000000 -5.000000
vertex 5.000000 -5.000000 5.000000
vertex 5.000000 -5.000000 5.000000
vertex -5.000000 -5.000000 5.000000
vertex -5.000000 -5.000000 -5.000000
vertex 5.000000 -5.000000 -5.000000
vertex 5.000000 5.000000 -5.000000
vertex 5.000000 5.000000 5.000000
vertex 5.000000 5.000000 5.000000
vertex 5.000000 -5.000000 5.000000
vertex 5.000000 -5.000000 -5.000000
vertex 5.000000 5.000000 -5.000000
vertex -5.000000 5.000000 -5.000000
vertex -5.000000 5.000000 5.000000
vertex -5.000000 5.000000 5.000000
vertex 5.000000 5.000000 5.000000
vertex 5.000000 5.000000 -5.000000
vertex -5.000000 5.000000 -5.000000
vertex -5.000000 -5.000000 -5.000000
vertex -5.000000 -5.000000 5.000000
vertex -5.000000 -5.000000 5.000000
vertex -5.000000 5.000000 5.000000
vertex -5.000000 5.000000 -5.000000
// invalid o/p with double
-2048.0014696121216,-2048.0014696121216,-2048.0004930496216
2048.0004930496216,2048.0014696121216,-2048.0004930496216
-2048.0004930496216,-2048.0014696121216,-2048.0014696121216
-2048.0014696121216,2048.0004930496216,2048.0014696121216
2048.0004930496216,2048.0004930496216,2048.0004930496216
2048.0014696121216,-2048.0004930496216,2048.0014696121216
-2048.0014696121216,2048.0014696121216,-2048.0014696121216
-2048.0004930496216,2048.0004930496216,2048.0014696121216
-2048.0014696121216,-2048.0004930496216,-2048.0014696121216
-2048.0004930496216,2048.0014696121216,-2048.0004930496216
2048.0004930496216,2048.0004930496216,2048.0004930496216
-2048.0004930496216,2048.0004930496216,-2048.0014696121216
2048.0004930496216,-2048.0014696121216,-2048.0004930496216
2048.0014696121216,-2048.0004930496216,2048.0004930496216
2048.0004930496216,2048.0004930496216,-2048.0004930496216
2048.0014696121216,-2048.0014696121216,-2048.0014696121216
-2048.0014696121216,-2048.0004930496216,2048.0014696121216
2048.0014696121216,-2048.0004930496216,-2048.0004930496216
4.67004221933303e-310|47,6.37e-322|8,6.9170985155446e-310|47
4.6700422193038e-310|47,4.6700422181283e-310|47,4.8e-322|7
4.6700422193599e-310|47,4.67004221932197e-310|47,1.6e-322|6
1.6e-322|6,4.67004221933145e-310|47,1e-323|2
3.16e-322|7,1.6e-322|6,4.67004221933303e-310|47
0.0,6.3e-322|8,7.9e-322|8
4.67004221810933e-310|47,4.670042205666e-310|47,0.0
0.0,5.263544247e-315|30,0.0
0.0078125,0.0,0.0
5.263544247e-315|30,0.0,0.0078125
0.0,0.0,0.0
4.67004221921525e-310|47,4.67004221921545e-310|47,4.67004221921545e-310|47
2.6461938652294957e-260,6.37e-322|8,4.6700422192374e-310|47
4.670042205666e-310|47,0.0,0.0
0.0,0.0,0.0
0.0,0.0,0.0
0.0,0.0,+nan.0
2.646193865229492e-260,2.6461938652294957e-260,5.855e-321|11
# Max2Obj Version 4.0 Mar 10th, 2001
#
# object default to come ...
#
v -5.000000 -5.000000 -5.000000
v 5.000000 -5.000000 -5.000000
v -5.000000 5.000000 -5.000000
v 5.000000 5.000000 -5.000000
v -5.000000 -5.000000 5.000000
v 5.000000 -5.000000 5.000000
v -5.000000 5.000000 5.000000
v 5.000000 5.000000 5.000000
# 8 vertices
vt 0.000000 0.000000 0.000000
vt 1.000000 0.000000 0.000000
vt 0.000000 1.000000 0.000000
vt 1.000000 1.000000 0.000000
vt 0.000000 0.000000 0.000000
vt 1.000000 0.000000 0.000000
vt 0.000000 1.000000 0.000000
vt 1.000000 1.000000 0.000000
vt 0.000000 0.000000 0.000000
vt 1.000000 0.000000 0.000000
vt 0.000000 1.000000 0.000000
vt 1.000000 1.000000 0.000000
# 12 texture vertices
vn 0.000000 0.000000 -2.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -2.000000
vn 0.000000 -0.000000 2.000000
vn 0.000000 -0.000000 1.000000
vn 0.000000 -0.000000 1.000000
vn 0.000000 -0.000000 2.000000
# 8 vertex normals
g default
s 2
f 1/10/1 3/12/3 4/11/4
f 4/11/4 2/9/2 1/10/1
s 4
f 5/9/5 6/10/6 8/12/8
f 8/12/8 7/11/7 5/9/5
s 8
f 1/5/1 2/6/2 6/8/6
f 6/8/6 5/7/5 1/5/1
s 16
f 2/1/2 4/2/4 8/4/8
f 8/4/8 6/3/6 2/1/2
s 32
f 4/5/4 3/6/3 7/8/7
f 7/8/7 8/7/8 4/5/4
s 64
f 3/1/3 1/2/1 5/4/5
f 5/4/5 7/3/7 3/1/3
# 12 faces
g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment