4 #include <gmath/gmath.h>
5 #include <assimp/cimport.h>
6 #include <assimp/postprocess.h>
7 #include <assimp/scene.h>
8 #include <assimp/mesh.h>
9 #include <assimp/material.h>
10 #include <assimp/anim.h>
11 #include <assimp/vector3.h>
12 #include <assimp/matrix4x4.h>
13 #include <assimp/quaternion.h>
18 static bool load_material(Scene *scn, Material *mat, const aiMaterial *aimat);
19 static SceneNode *load_node(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiNode *ainode);
20 static Mesh *load_mesh(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiMesh *aimesh);
21 static void print_nodes(SceneNode *node, int lvl = 0);
22 /*static const char *mprop_semantic(int x);
23 static int count_textures(const aiMaterial *aimat);*/
24 static int assimp_textype(aiTextureType type);
26 static Mat4 assimp_matrix(const aiMatrix4x4 &aim);
28 /*static Vec3 assimp_vector(const aiVector3D &v);
29 static Quat assimp_quat(const aiQuaternion &q);
30 static long assimp_time(const aiAnimation *anim, double aitime);
31 static void print_hierarchy(const aiNode *node);
34 static std::map<std::string, SceneNode*> node_by_name;
35 static std::map<aiMesh*, Mesh*> mesh_by_aimesh;
37 bool Scene::load(const char *fname, unsigned int flags)
39 unsigned int ppflags = aiProcess_CalcTangentSpace |
40 aiProcess_GenNormals |
41 aiProcess_JoinIdenticalVertices |
42 aiProcess_Triangulate |
43 aiProcess_SortByPType |
44 aiProcess_GenUVCoords |
45 aiProcess_TransformUVCoords;
47 if(flags & SCNLOAD_FLIPTEX) {
48 ppflags |= aiProcess_FlipUVs;
51 const aiScene *aiscn = aiImportFile(fname, ppflags);
53 fprintf(stderr, "failed to load scene file: %s\n", fname);
57 // assimp adds its own root node, which might have transformations
58 Vec3 root_pos, root_scaling(1.0, 1.0, 1.0);
61 if(aiscn->mRootNode) {
62 Mat4 root_matrix = assimp_matrix(aiscn->mRootNode->mTransformation);
63 root_pos = root_matrix.get_translation();
64 root_rot = root_matrix.get_rotation();
65 root_scaling = root_matrix.get_scaling();
67 printf("assimp root node: %s\n", aiscn->mRootNode->mName.data);
68 printf(" pos: %f %f %f\n", root_pos.x, root_pos.y, root_pos.z);
69 printf(" rot: %f %+f %+f %+f\n", root_rot.w, root_rot.x, root_rot.y, root_rot.z);
70 printf(" scaling: %f %f %f\n", root_scaling.x, root_scaling.y, root_scaling.z);
74 for(unsigned int i=0; i<aiscn->mNumMeshes; i++) {
75 aiMesh *aimesh = aiscn->mMeshes[i];
78 switch(aimesh->mPrimitiveTypes) {
79 case aiPrimitiveType_TRIANGLE:
80 if((mesh = load_mesh(this, aiscn, flags, aimesh))) {
81 mesh_by_aimesh[aimesh] = mesh;
82 meshes.push_back(mesh);
87 fprintf(stderr, "unsupported primitive type: %u\n", aimesh->mPrimitiveTypes);
93 nodes = new SceneNode;
94 nodes->set_name("root");
95 nodes->set_position(root_pos);
96 nodes->set_rotation(root_rot);
97 nodes->set_scaling(root_scaling);
100 // load all the nodes recursively
101 for(unsigned int i=0; i<aiscn->mRootNode->mNumChildren; i++) {
102 SceneNode *node = load_node(this, aiscn, flags, aiscn->mRootNode->mChildren[i]);
104 nodes->add_child(node);
108 node_by_name.clear();
109 mesh_by_aimesh.clear();
111 aiReleaseImport(aiscn);
112 printf("loaded scene file: %s, %d meshes\n", fname, (int)meshes.size());
118 static bool load_material(Scene *scn, Material *mat, const aiMaterial *aimat)
122 float shin, shin_str;
124 if(aiGetMaterialString(aimat, AI_MATKEY_NAME, &name) == 0) {
125 mat->name = name.data;
127 mat->name = "unknown";
129 //printf("load_material: %s\n", mat->name.c_str());
131 if(aiGetMaterialColor(aimat, AI_MATKEY_COLOR_DIFFUSE, &aicol) == 0) {
132 mat->diffuse = Vec3(aicol[0], aicol[1], aicol[2]);
134 if(aiGetMaterialColor(aimat, AI_MATKEY_COLOR_SPECULAR, &aicol) == 0) {
135 mat->specular = Vec3(aicol[0], aicol[1], aicol[2]);
138 unsigned int count = 1;
139 if(aiGetMaterialFloatArray(aimat, AI_MATKEY_SHININESS_STRENGTH, &shin_str, &count) != 0) {
142 if(aiGetMaterialFloatArray(aimat, AI_MATKEY_SHININESS, &shin, &count) == 0) {
143 // XXX can't remember how I came up with this...
144 mat->shininess = shin * shin_str * 0.0001 * 128.0;
149 const int num_tex_types = aiTextureType_UNKNOWN + 1;
150 for(int i=0; i<num_tex_types; i++) {
151 aiTextureType aitype = (aiTextureType)i;
152 int count = aiGetMaterialTextureCount(aimat, aitype);
154 for(int j=0; j<count; j++) {
156 if(aiGetMaterialTexture(aimat, aitype, j, &aipath) != 0) {
161 int nsize = datamap_path_size(aipath.data);
163 fname = new char[nsize];
164 datamap_lookup(aipath.data, fname, nsize);
166 fname = new char[strlen(aipath.data) + 1];
168 char *sptr = aipath.data;
170 *dptr++ = *sptr == '\\' ? '/' : *sptr;
174 Texture *tex = scn->texset->get(fname);
176 fprintf(stderr, "failed to load texture: %s\n", fname);
182 mat->textures.push_back(tex);
184 int textype = assimp_textype(aitype);
185 if(textype != MTL_TEX_UNKNOWN && !mat->stdtex[textype]) {
186 mat->stdtex[textype] = tex;
194 static SceneNode *load_node(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiNode *ainode)
196 SceneNode *node = new SceneNode;
197 node->set_name(ainode->mName.data);
200 Mat4 matrix = assimp_matrix(ainode->mTransformation);
201 Vec3 pos = matrix.get_translation();
202 Quat rot = matrix.get_rotation();
203 Vec3 scale = matrix.get_scaling();
205 node->set_position(pos);
206 node->set_rotation(rot);
207 node->set_scaling(scale);
208 node->dbg_xform = matrix;
211 for(unsigned int i=0; i<ainode->mNumMeshes; i++) {
212 aiMesh *aimesh = aiscn->mMeshes[ainode->mMeshes[i]];
214 Mesh *mesh = mesh_by_aimesh[aimesh];
216 ObjMesh *obj = new ObjMesh;
218 // also grab the material of this mesh
219 load_material(scn, &obj->mtl, aiscn->mMaterials[aimesh->mMaterialIndex]);
221 node->add_object(obj);
222 scn->objects.push_back(obj);
226 /* recurse to all children */
227 for(unsigned int i=0; i<ainode->mNumChildren; i++) {
228 SceneNode *child = load_node(scn, aiscn, flags, ainode->mChildren[i]);
230 node->add_child(child);
234 node_by_name[node->get_name()] = node;
238 static Mesh *load_mesh(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiMesh *aimesh)
240 Mesh *mesh = new Mesh;
242 int num_verts = aimesh->mNumVertices;
243 int num_faces = aimesh->mNumFaces;
245 mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, (float*)aimesh->mVertices);
247 if(aimesh->mNormals) {
248 mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, (float*)aimesh->mNormals);
250 if(aimesh->mTangents) {
251 mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, (float*)aimesh->mTangents);
253 if(aimesh->mTextureCoords[0]) {
254 mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 3, num_verts, (float*)aimesh->mTextureCoords[0]);
256 if(aimesh->mTextureCoords[1]) {
257 mesh->set_attrib_data(MESH_ATTR_TEXCOORD2, 3, num_verts, (float*)aimesh->mTextureCoords[1]);
260 if(flags & SCNLOAD_FLIPYZ) {
261 Vec3 *vptr = (Vec3*)mesh->get_attrib_data(MESH_ATTR_VERTEX);
262 for(int i=0; i<num_verts; i++) {
267 Vec3 *nptr = (Vec3*)mesh->get_attrib_data(MESH_ATTR_NORMAL);
268 for(int i=0; i<num_verts; i++) {
273 Vec3 *tptr = (Vec3*)mesh->get_attrib_data(MESH_ATTR_TANGENT);
274 for(int i=0; i<num_verts; i++) {
280 unsigned int *iptr = mesh->set_index_data(num_faces * 3);
281 for(int i=0; i<num_faces; i++) {
282 iptr[0] = aimesh->mFaces[i].mIndices[0];
283 iptr[1] = aimesh->mFaces[i].mIndices[flags & SCNLOAD_FLIPYZ ? 2 : 1];
284 iptr[2] = aimesh->mFaces[i].mIndices[flags & SCNLOAD_FLIPYZ ? 1 : 2];
291 static void print_nodes(SceneNode *node, int lvl)
293 Vec3 pos = node->get_node_position();
294 Quat rot = node->get_node_rotation();
295 Vec3 scale = node->get_node_scaling();
297 const char *type = node->get_num_objects() > 0 ? "mesh node" : "null node";
299 for(int i=0; i<lvl; i++) {
302 printf("%s[%s] p(%g %g %g) rq(%g %+gi %+gj %+gk) s(%g %g %g)\n", type, node->get_name(),
303 pos.x, pos.y, pos.z, rot.w, rot.x, rot.y, rot.z, scale.x, scale.y, scale.z);
305 if(node->get_num_objects()) {
306 Mat4 xform = node->get_matrix();
310 int nchld = node->get_num_children();
311 for(int i=0; i<nchld; i++) {
312 print_nodes(node->get_child(i), lvl + 1);
316 static int assimp_textype(aiTextureType type)
319 case aiTextureType_DIFFUSE:
320 return MTL_TEX_DIFFUSE;
321 case aiTextureType_SPECULAR:
322 return MTL_TEX_SPECULAR;
323 case aiTextureType_NORMALS:
324 return MTL_TEX_NORMALMAP;
325 case aiTextureType_LIGHTMAP:
326 return MTL_TEX_LIGHTMAP;
327 case aiTextureType_REFLECTION:
328 return MTL_TEX_ENVMAP;
332 return MTL_TEX_UNKNOWN;
335 static Mat4 assimp_matrix(const aiMatrix4x4 &aim)
338 memcpy(m[0], &aim, 16 * sizeof(float));