6 #include <gmath/gmath.h>
7 #include <assimp/cimport.h>
8 #include <assimp/postprocess.h>
9 #include <assimp/scene.h>
10 #include <assimp/mesh.h>
11 #include <assimp/material.h>
12 #include <assimp/anim.h>
13 #include <assimp/vector3.h>
14 #include <assimp/matrix4x4.h>
15 #include <assimp/quaternion.h>
21 #include "metascene.h"
23 static bool load_material(Scene *scn, Material *mat, const aiMaterial *aimat);
24 static SceneNode *load_node(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiNode *ainode);
25 static Mesh *load_mesh(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiMesh *aimesh);
26 /*static const char *mprop_semantic(int x);
27 static int count_textures(const aiMaterial *aimat);*/
28 static int assimp_textype(aiTextureType type);
29 //static const char *assimp_textypestr(aiTextureType type);
31 static Mat4 assimp_matrix(const aiMatrix4x4 &aim);
33 /*static Vec3 assimp_vector(const aiVector3D &v);
34 static Quat assimp_quat(const aiQuaternion &q);
35 static long assimp_time(const aiAnimation *anim, double aitime);
36 static void print_hierarchy(const aiNode *node);
42 std::map<std::string, SceneNode*> node_by_name;
43 std::map<aiMesh*, Mesh*> mesh_by_aimesh;
46 #define LD_STAGE_MASK 0xf000
48 bool Scene::load(const char *fname, unsigned int flags)
50 if((flags & LD_STAGE_MASK) == 0) {
51 // not passing either of the stage specifiers, means do the whole job
52 flags |= LD_STAGE_MASK;
55 // first perform I/O and all operations not requiring access to an OpenGL context
56 if(flags & SCNLOAD_STAGE_IO) {
57 unsigned int ppflags = aiProcess_CalcTangentSpace |
58 aiProcess_GenNormals |
59 aiProcess_JoinIdenticalVertices |
60 aiProcess_Triangulate |
61 aiProcess_SortByPType |
62 aiProcess_GenUVCoords |
63 //aiProcess_PreTransformVertices |
64 aiProcess_TransformUVCoords;
66 if(flags & SCNLOAD_FLIPTEX) {
67 ppflags |= aiProcess_FlipUVs;
70 info_log("Loading scene file: %s\n", fname);
72 const aiScene *aiscn = aiImportFile(fname, ppflags);
74 error_log("failed to load scene file: %s\n", fname);
78 // assimp adds its own root node, which might have transformations
79 Vec3 root_pos, root_scaling(1.0, 1.0, 1.0);
82 if(aiscn->mRootNode) {
83 Mat4 root_matrix = assimp_matrix(aiscn->mRootNode->mTransformation);
84 root_pos = root_matrix.get_translation();
85 root_rot = root_matrix.get_rotation();
86 root_scaling = root_matrix.get_scaling();
90 nodes = new SceneNode;
92 nodes->set_name("root");
93 nodes->set_position(root_pos);
94 nodes->set_rotation(root_rot);
95 nodes->set_scaling(root_scaling);
98 LoaderData *ldata = new LoaderData;
100 ldata->fname = std::string(fname);
101 loader_data = (void*)ldata;
104 /* then, assuming we have successfully loaded everything, proceed to construct
105 * all the engine objects, which require access to the OpenGL context
107 if(flags & SCNLOAD_STAGE_GL) {
109 error_log("second stage scene loader failed to find valid I/O data\n");
113 LoaderData *ldata = (LoaderData*)loader_data;
114 const aiScene *aiscn = ldata->aiscn;
115 fname = ldata->fname.c_str();
118 for(unsigned int i=0; i<aiscn->mNumMeshes; i++) {
119 aiMesh *aimesh = aiscn->mMeshes[i];
122 switch(aimesh->mPrimitiveTypes) {
123 case aiPrimitiveType_TRIANGLE:
124 if((mesh = load_mesh(this, aiscn, flags, aimesh))) {
125 ldata->mesh_by_aimesh[aimesh] = mesh;
126 meshes.push_back(mesh);
131 error_log("unsupported primitive type: %u\n", aimesh->mPrimitiveTypes);
136 // load all the nodes recursively
137 for(unsigned int i=0; i<aiscn->mRootNode->mNumChildren; i++) {
138 SceneNode *node = load_node(this, aiscn, flags, aiscn->mRootNode->mChildren[i]);
140 nodes->add_child(node);
144 info_log("loaded scene file: %s, %d meshes\n", fname, (int)meshes.size());
146 aiReleaseImport(aiscn);
154 static bool load_material(Scene *scn, Material *mat, const aiMaterial *aimat)
158 float shin, shin_str;
160 if(aiGetMaterialString(aimat, AI_MATKEY_NAME, &name) == 0) {
161 mat->name = name.data;
163 mat->name = "unknown";
165 //info_log("load_material: %s\n", mat->name.c_str());
167 if(aiGetMaterialColor(aimat, AI_MATKEY_COLOR_DIFFUSE, &aicol) == 0) {
168 mat->diffuse = Vec3(aicol[0], aicol[1], aicol[2]);
170 if(aiGetMaterialColor(aimat, AI_MATKEY_COLOR_SPECULAR, &aicol) == 0) {
171 mat->specular = Vec3(aicol[0], aicol[1], aicol[2]);
174 unsigned int count = 1;
175 if(aiGetMaterialFloatArray(aimat, AI_MATKEY_SHININESS_STRENGTH, &shin_str, &count) != 0) {
178 if(aiGetMaterialFloatArray(aimat, AI_MATKEY_SHININESS, &shin, &count) == 0) {
179 // XXX can't remember how I came up with this...
180 mat->shininess = shin * shin_str * 0.0001 * 128.0;
185 const int num_tex_types = aiTextureType_UNKNOWN + 1;
186 for(int i=0; i<num_tex_types; i++) {
187 aiTextureType aitype = (aiTextureType)i;
188 int count = aiGetMaterialTextureCount(aimat, aitype);
190 for(int j=0; j<count; j++) {
192 if(aiGetMaterialTexture(aimat, aitype, j, &aipath) != 0) {
196 char *fname = (char*)alloca(strlen(aipath.data) + 1);
198 char *sptr = aipath.data;
200 *dptr++ = *sptr == '\\' ? '/' : *sptr;
203 if(!fname || !*fname) continue;
205 int textype = assimp_textype(aitype);
207 Texture *tex = texman.get_texture(fname, TEX_2D, &scn->datamap);
209 mat->textures.push_back(tex);
211 if(textype != MTL_TEX_UNKNOWN && !mat->stdtex[textype]) {
212 mat->stdtex[textype] = tex;
220 static SceneNode *load_node(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiNode *ainode)
222 LoaderData *ldata = (LoaderData*)scn->loader_data;
224 SceneNode *node = new SceneNode;
225 node->set_name(ainode->mName.data);
228 Mat4 matrix = assimp_matrix(ainode->mTransformation);
229 Vec3 pos = matrix.get_translation();
230 Quat rot = matrix.get_rotation();
231 Vec3 scale = matrix.get_scaling();
233 node->set_position(pos);
234 node->set_rotation(rot);
235 node->set_scaling(scale);
236 node->dbg_xform = matrix;
239 for(unsigned int i=0; i<ainode->mNumMeshes; i++) {
240 aiMesh *aimesh = aiscn->mMeshes[ainode->mMeshes[i]];
242 Mesh *mesh = ldata->mesh_by_aimesh[aimesh];
244 ObjMesh *obj = new ObjMesh;
245 obj->set_name(mesh->get_name());
247 // also grab the material of this mesh
248 load_material(scn, &obj->mtl, aiscn->mMaterials[aimesh->mMaterialIndex]);
250 node->add_object(obj);
251 scn->objects.push_back(obj);
255 /* recurse to all children */
256 for(unsigned int i=0; i<ainode->mNumChildren; i++) {
257 SceneNode *child = load_node(scn, aiscn, flags, ainode->mChildren[i]);
259 node->add_child(child);
263 ldata->node_by_name[node->get_name()] = node;
267 static Mesh *load_mesh(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiMesh *aimesh)
269 Mesh *mesh = new Mesh;
270 mesh->set_name(aimesh->mName.data);
272 int num_verts = aimesh->mNumVertices;
273 int num_faces = aimesh->mNumFaces;
275 mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, (float*)aimesh->mVertices);
277 if(aimesh->mNormals) {
278 mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, (float*)aimesh->mNormals);
280 if(aimesh->mTangents) {
281 mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, (float*)aimesh->mTangents);
283 if(aimesh->mTextureCoords[0]) {
284 mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 3, num_verts, (float*)aimesh->mTextureCoords[0]);
286 if(aimesh->mTextureCoords[1]) {
287 mesh->set_attrib_data(MESH_ATTR_TEXCOORD2, 3, num_verts, (float*)aimesh->mTextureCoords[1]);
290 if(flags & SCNLOAD_FLIPYZ) {
291 Vec3 *vptr = (Vec3*)mesh->get_attrib_data(MESH_ATTR_VERTEX);
292 for(int i=0; i<num_verts; i++) {
297 Vec3 *nptr = (Vec3*)mesh->get_attrib_data(MESH_ATTR_NORMAL);
298 for(int i=0; i<num_verts; i++) {
303 Vec3 *tptr = (Vec3*)mesh->get_attrib_data(MESH_ATTR_TANGENT);
304 for(int i=0; i<num_verts; i++) {
310 unsigned int *iptr = mesh->set_index_data(num_faces * 3);
311 for(int i=0; i<num_faces; i++) {
312 iptr[0] = aimesh->mFaces[i].mIndices[0];
313 iptr[1] = aimesh->mFaces[i].mIndices[flags & SCNLOAD_FLIPYZ ? 2 : 1];
314 iptr[2] = aimesh->mFaces[i].mIndices[flags & SCNLOAD_FLIPYZ ? 1 : 2];
320 static int assimp_textype(aiTextureType type)
323 case aiTextureType_DIFFUSE:
324 return MTL_TEX_DIFFUSE;
325 case aiTextureType_SPECULAR:
326 return MTL_TEX_SPECULAR;
327 case aiTextureType_NORMALS:
328 return MTL_TEX_NORMALMAP;
329 case aiTextureType_LIGHTMAP:
330 case aiTextureType_EMISSIVE:
331 return MTL_TEX_LIGHTMAP;
332 case aiTextureType_REFLECTION:
333 return MTL_TEX_ENVMAP;
337 return MTL_TEX_UNKNOWN;
340 /*static const char *assimp_textypestr(aiTextureType type)
343 case aiTextureType_DIFFUSE:
345 case aiTextureType_SPECULAR:
347 case aiTextureType_NORMALS:
349 case aiTextureType_LIGHTMAP:
350 case aiTextureType_EMISSIVE:
352 case aiTextureType_REFLECTION:
360 static Mat4 assimp_matrix(const aiMatrix4x4 &aim)
363 memcpy(m[0], &aim, 16 * sizeof(float));
371 : DataSet<Scene*>(create_scene, load_scene, done_scene, free_scene)
375 Scene *SceneSet::create_scene()
380 bool SceneSet::load_scene(Scene *scn, const char *fname)
383 return scn->load(fname, SCNLOAD_FLIPTEX | SCNLOAD_STAGE_IO);
386 bool SceneSet::done_scene(Scene *scn)
388 bool res = scn->load(0, SCNLOAD_STAGE_GL);
390 scn->metascn->scene_loaded(scn);
395 void SceneSet::free_scene(Scene *scn)