X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fsceneload.cc;h=6fb17d452d442e68b45bab2b808f1e19ec08a18b;hb=dbcb9345c23c5c027d808915962843e7db2d14aa;hp=865a4c8e4a628f216f3ac65bb9fc53568dac8929;hpb=8137121400748ee8afb1608253aae15323c5e3a2;p=laserbrain_demo diff --git a/src/sceneload.cc b/src/sceneload.cc index 865a4c8..6fb17d4 100644 --- a/src/sceneload.cc +++ b/src/sceneload.cc @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -21,10 +22,12 @@ static Mesh *load_mesh(Scene *scn, const aiScene *aiscn, unsigned int flags, con /*static const char *mprop_semantic(int x); static int count_textures(const aiMaterial *aimat);*/ static int assimp_textype(aiTextureType type); +static const char *assimp_textypestr(aiTextureType type); + +static Mat4 assimp_matrix(const aiMatrix4x4 &aim); /*static Vec3 assimp_vector(const aiVector3D &v); static Quat assimp_quat(const aiQuaternion &q); -static Mat4 assimp_matrix(const aiMatrix4x4 &aim); static long assimp_time(const aiAnimation *anim, double aitime); static void print_hierarchy(const aiNode *node); */ @@ -39,28 +42,32 @@ bool Scene::load(const char *fname, unsigned int flags) aiProcess_JoinIdenticalVertices | aiProcess_Triangulate | aiProcess_SortByPType | - aiProcess_TransformUVCoords | aiProcess_PreTransformVertices; + aiProcess_GenUVCoords | + //aiProcess_PreTransformVertices | + aiProcess_TransformUVCoords; if(flags & SCNLOAD_FLIPTEX) { ppflags |= aiProcess_FlipUVs; } + printf("Loading scene file: %s\n", fname); + const aiScene *aiscn = aiImportFile(fname, ppflags); if(!aiscn) { fprintf(stderr, "failed to load scene file: %s\n", fname); return false; } - /* + // assimp adds its own root node, which might have transformations Vec3 root_pos, root_scaling(1.0, 1.0, 1.0); Quat root_rot; if(aiscn->mRootNode) { Mat4 root_matrix = assimp_matrix(aiscn->mRootNode->mTransformation); root_pos = root_matrix.get_translation(); - root_rot = root_matrix.get_rotation_quat(); + root_rot = root_matrix.get_rotation(); root_scaling = root_matrix.get_scaling(); - }*/ + } // load all meshes for(unsigned int i=0; imNumMeshes; i++) { @@ -81,32 +88,29 @@ bool Scene::load(const char *fname, unsigned int flags) } } - SceneNode *root = new SceneNode; + if(!nodes) { + nodes = new SceneNode; + nodes->scene = this; + nodes->set_name("root"); + nodes->set_position(root_pos); + nodes->set_rotation(root_rot); + nodes->set_scaling(root_scaling); + } // load all the nodes recursively for(unsigned int i=0; imRootNode->mNumChildren; i++) { SceneNode *node = load_node(this, aiscn, flags, aiscn->mRootNode->mChildren[i]); if(node) { - root->add_child(node); + nodes->add_child(node); } } - int nnodes = root->get_num_children(); - if(nnodes <= 0) { - delete root; - } else if(nnodes == 1) { - nodes = root->get_child(0); - root->remove_child(nodes); - delete root; - } else { - nodes = root; - } - node_by_name.clear(); mesh_by_aimesh.clear(); aiReleaseImport(aiscn); printf("loaded scene file: %s, %d meshes\n", fname, (int)meshes.size()); + nodes->update(0); return true; } @@ -121,7 +125,7 @@ static bool load_material(Scene *scn, Material *mat, const aiMaterial *aimat) } else { mat->name = "unknown"; } - printf("load_material: %s\n", mat->name.c_str()); + //printf("load_material: %s\n", mat->name.c_str()); if(aiGetMaterialColor(aimat, AI_MATKEY_COLOR_DIFFUSE, &aicol) == 0) { mat->diffuse = Vec3(aicol[0], aicol[1], aicol[2]); @@ -166,44 +170,19 @@ static bool load_material(Scene *scn, Material *mat, const aiMaterial *aimat) } while(*sptr++); } - Texture *tex = scn->texset->get(fname); - if(!tex) { - fprintf(stderr, "failed to load texture: %s\n", fname); - delete [] fname; - continue; - } - delete [] fname; + int textype = assimp_textype(aitype); + printf("loading %s texture: %s\n", assimp_textypestr(aitype), fname); + Texture *tex = scn->texset->get_texture(fname, TEX_2D); + assert(tex); mat->textures.push_back(tex); - int textype = assimp_textype(aitype); if(textype != MTL_TEX_UNKNOWN && !mat->stdtex[textype]) { mat->stdtex[textype] = tex; } } } - /* - for(size_t i=0; itex[textypes[i].type] = texset.get(fname); - } - } - } - */ - return true; } @@ -212,12 +191,25 @@ static SceneNode *load_node(Scene *scn, const aiScene *aiscn, unsigned int flags SceneNode *node = new SceneNode; node->set_name(ainode->mName.data); + // transformation + Mat4 matrix = assimp_matrix(ainode->mTransformation); + Vec3 pos = matrix.get_translation(); + Quat rot = matrix.get_rotation(); + Vec3 scale = matrix.get_scaling(); + + node->set_position(pos); + node->set_rotation(rot); + node->set_scaling(scale); + node->dbg_xform = matrix; + + // meshes for(unsigned int i=0; imNumMeshes; i++) { - aiMesh *aimesh = aiscn->mMeshes[ainode->mMeshes[0]]; + aiMesh *aimesh = aiscn->mMeshes[ainode->mMeshes[i]]; Mesh *mesh = mesh_by_aimesh[aimesh]; if(mesh) { ObjMesh *obj = new ObjMesh; + obj->set_name(mesh->get_name()); obj->mesh = mesh; // also grab the material of this mesh load_material(scn, &obj->mtl, aiscn->mMaterials[aimesh->mMaterialIndex]); @@ -242,6 +234,7 @@ static SceneNode *load_node(Scene *scn, const aiScene *aiscn, unsigned int flags static Mesh *load_mesh(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiMesh *aimesh) { Mesh *mesh = new Mesh; + mesh->set_name(aimesh->mName.data); int num_verts = aimesh->mNumVertices; int num_faces = aimesh->mNumFaces; @@ -288,7 +281,6 @@ static Mesh *load_mesh(Scene *scn, const aiScene *aiscn, unsigned int flags, con iptr[2] = aimesh->mFaces[i].mIndices[flags & SCNLOAD_FLIPYZ ? 1 : 2]; iptr += 3; } - return mesh; } @@ -302,6 +294,7 @@ static int assimp_textype(aiTextureType type) case aiTextureType_NORMALS: return MTL_TEX_NORMALMAP; case aiTextureType_LIGHTMAP: + case aiTextureType_EMISSIVE: return MTL_TEX_LIGHTMAP; case aiTextureType_REFLECTION: return MTL_TEX_ENVMAP; @@ -310,3 +303,30 @@ static int assimp_textype(aiTextureType type) } return MTL_TEX_UNKNOWN; } + +static const char *assimp_textypestr(aiTextureType type) +{ + switch(type) { + case aiTextureType_DIFFUSE: + return "diffuse"; + case aiTextureType_SPECULAR: + return "specular"; + case aiTextureType_NORMALS: + return "normalmap"; + case aiTextureType_LIGHTMAP: + case aiTextureType_EMISSIVE: + return "lightmap"; + case aiTextureType_REFLECTION: + return "envmap"; + default: + break; + } + return "unknown"; +} + +static Mat4 assimp_matrix(const aiMatrix4x4 &aim) +{ + Mat4 m; + memcpy(m[0], &aim, 16 * sizeof(float)); + return transpose(m); +}