(bind-func ai_build_mesh
(lambda (scene:aiScene* mesh:aiMesh* xmesh:XTMMesh* dir:i8*)
;; (printf "\tBuild Mesh\n")
(let ((facemode:i32* (salloc))
(vbo:VBO* null)
(vao:VAO* (VAO))
(color:aiColor4D* (salloc))
(val:float* (salloc))
(diffuse:float* (halloc 4))
(ambient:float* (halloc 4))
(specular:float* (halloc 4))
(emissive:float* (halloc 4))
(material_index (tref mesh 13))
(material (pref (tref scene 5) material_index))
;; (texid 0)
(diffuse_tex:Texture* null)
(normal_tex:Texture* null)
(specular_tex:Texture* null)
(height_tex:Texture* null)
(texfile:aiString* (salloc))
(name:aiString* (tref-ptr mesh 14)))
(set! vbo (ai_build_mesh_vbo vao mesh xmesh facemode))
(tset! xmesh 0 vbo)
(set! diffuse_tex (load_assim_material_texture scene material aiTextureType_DIFFUSE dir))
(set! specular_tex (load_assim_material_texture scene material aiTextureType_SPECULAR dir))
(set! height_tex (load_assim_material_texture scene material aiTextureType_HEIGHT dir))
(set! normal_tex (load_assim_material_texture scene material aiTextureType_NORMALS dir))
(tset! xmesh 1 diffuse_tex)
(tset! xmesh 15 diffuse_tex)
(tset! xmesh 16 specular_tex)
(tset! xmesh 17 height_tex)
(tset! xmesh 18 normal_tex)
(if (= (aiGetMaterialColor material "$clr.diffuse" 0 0 color) 0)
(ai_color4_to_float4 color diffuse))
(tset! xmesh 2 diffuse)
(if (= (aiGetMaterialColor material "$clr.ambient" 0 0 color) 0)
(ai_color4_to_float4 color ambient))
(tset! xmesh 3 ambient)
(if (= (aiGetMaterialColor material "$clr.specular" 0 0 color) 0)
(ai_color4_to_float4 color specular))
(tset! xmesh 4 specular)
(if (= (aiGetMaterialColor material "$clr.emissive" 0 0 color) 0)
(ai_color4_to_float4 color emissive))
(tset! xmesh 5 emissive)
(if (= (aiGetMaterialFloat material "$mat.shininess" 0 0 val) 0)
(begin (tset! xmesh 6 (pref val 0)) 1)
(begin (tset! xmesh 6 0.0) 1))
(if (= (aiGetMaterialFloat material "$mat.opacity" 0 0 val) 0)
(begin (tset! xmesh 7 (pref val 0)) 1)
(begin (tset! xmesh 7 0.0) 1))
(tset! xmesh 8 (pref facemode 0))
(tset! xmesh 9 (elements vbo))
(tset! xmesh 10 vao)
(tset! xmesh 11 (Str (cast (tref-ptr name 1) i8*)))
void)))