;; returns the scenes top level node as XTMModel*
(bind-func ai_build_node
(lambda (scene:aiScene* node:aiNode* _node:XTMModel* parent:XTMModel* dir:i8*)
(let ((i:i32 0)
(aiparent:aiNode* (tref node 2))
(aipn:aiString* (if (null? aiparent) null (tref-ptr aiparent 0)))
(pnamelgth (if (null? aipn) 0 (tref aipn 0)))
(pname:i8* (if (null? aipn) null (halloc (+ 1 pnamelgth))))
(ain:aiString* (tref-ptr node 0))
(namelgth (tref ain 0))
(name:i8* (halloc (+ 1 namelgth)))
(numchildren:i32 (tref node 3))
(children:aiNode** (tref node 4))
(_children:XTMModel* (halloc (+ 1 numchildren)))
(nummeshes:i32 (tref node 5))
(meshes:aiMesh** (tref scene 3))
(_meshes:XTMMesh* (halloc (+ 1 nummeshes)))
(transmat:float* (halloc 16)))
(memset name 0 (+ namelgth 1))
(memcpy name (cast (tref-ptr ain 1) i8*) namelgth)
(if (null? aiparent)
(println "Node:" (Str name) "Parent:" (Str "") "Children:" numchildren "ptr:" (cast node i8*))
(begin
(memset pname 0 (+ pnamelgth 1))
(memcpy pname (cast (tref-ptr aipn 1) i8*) pnamelgth)
(println "Node:" (Str name) "Parent:" (Str pname) "Children:" numchildren "ptr:" (cast node i8*))))
;;(printf "Build node:%s:%p children(%d) meshes(%d)\n" name _node numchildren nummeshes)
(tset! _node 0 (Str name))
(ai_matrix4_to_float (tref-ptr node 1) transmat)
(tset! _node 1 transmat)
(tset! _node 2 parent)
(tset! _node 3 numchildren)
(dotimes (i numchildren)
(ai_build_node scene (pref children i) (pref-ptr _children i) _node dir))
(tset! _node 4 _children)
(tset! _node 5 nummeshes)
(dotimes (i nummeshes)
(ai_build_mesh scene (pref meshes (pref (tref node 6) i)) (pref-ptr _meshes i) dir))
(tset! _node 6 _meshes)
void)))