X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=laserbrain_demo;a=blobdiff_plain;f=src%2Fsceneload.cc;h=4c0d3241f096ac4c4ca0389678ec35419f03a5df;hp=6e959b22e4870b8dd2945edd0d237aa073123c7d;hb=e12a327cc0b4f1e59f4a66a80b170ec41ce97be6;hpb=d4d7f73284783d2a50d71014789d196bef7d0e0e diff --git a/src/sceneload.cc b/src/sceneload.cc index 6e959b2..4c0d324 100644 --- a/src/sceneload.cc +++ b/src/sceneload.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -12,10 +13,12 @@ #include #include #include +#include "app.h" #include "scene.h" #include "objmesh.h" #include "datamap.h" #include "logger.h" +#include "metascene.h" static bool load_material(Scene *scn, Material *mat, const aiMaterial *aimat); static SceneNode *load_node(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiNode *ainode); @@ -23,7 +26,7 @@ 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 const char *assimp_textypestr(aiTextureType type); static Mat4 assimp_matrix(const aiMatrix4x4 &aim); @@ -33,85 +36,123 @@ static long assimp_time(const aiAnimation *anim, double aitime); static void print_hierarchy(const aiNode *node); */ -static std::map node_by_name; -static std::map mesh_by_aimesh; +struct LoaderData { + const aiScene *aiscn; + std::string fname; + std::map node_by_name; + std::map mesh_by_aimesh; +}; + +#define LD_STAGE_MASK 0xf000 bool Scene::load(const char *fname, unsigned int flags) { - unsigned int ppflags = aiProcess_CalcTangentSpace | - aiProcess_GenNormals | - aiProcess_JoinIdenticalVertices | - aiProcess_Triangulate | - aiProcess_SortByPType | - aiProcess_GenUVCoords | - //aiProcess_PreTransformVertices | - aiProcess_TransformUVCoords; - - if(flags & SCNLOAD_FLIPTEX) { - ppflags |= aiProcess_FlipUVs; + if((flags & LD_STAGE_MASK) == 0) { + // not passing either of the stage specifiers, means do the whole job + flags |= LD_STAGE_MASK; } - info_log("Loading scene file: %s\n", fname); + // first perform I/O and all operations not requiring access to an OpenGL context + if(flags & SCNLOAD_STAGE_IO) { + unsigned int ppflags = aiProcess_CalcTangentSpace | + aiProcess_GenNormals | + aiProcess_JoinIdenticalVertices | + aiProcess_Triangulate | + aiProcess_SortByPType | + aiProcess_GenUVCoords | + //aiProcess_PreTransformVertices | + aiProcess_TransformUVCoords; + + if(flags & SCNLOAD_FLIPTEX) { + ppflags |= aiProcess_FlipUVs; + } - const aiScene *aiscn = aiImportFile(fname, ppflags); - if(!aiscn) { - error_log("failed to load scene file: %s\n", fname); - return false; - } + info_log("Loading scene file: %s\n", fname); + if(this->name.empty()) { + this->name = std::string(fname); + } - // assimp adds its own root node, which might have transformations - Vec3 root_pos, root_scaling(1.0, 1.0, 1.0); - Quat root_rot; + const aiScene *aiscn = aiImportFile(fname, ppflags); + if(!aiscn) { + error_log("failed to load scene file: %s\n", fname); + return false; + } - if(aiscn->mRootNode) { - Mat4 root_matrix = assimp_matrix(aiscn->mRootNode->mTransformation); - root_pos = root_matrix.get_translation(); - root_rot = root_matrix.get_rotation(); - root_scaling = root_matrix.get_scaling(); + LoaderData *ldata = new LoaderData; + ldata->aiscn = aiscn; + ldata->fname = std::string(fname); + loader_data = (void*)ldata; } - // load all meshes - for(unsigned int i=0; imNumMeshes; i++) { - aiMesh *aimesh = aiscn->mMeshes[i]; - Mesh *mesh; + /* then, assuming we have successfully loaded everything, proceed to construct + * all the engine objects, which require access to the OpenGL context + */ + if(flags & SCNLOAD_STAGE_GL) { + if(!loader_data) { + error_log("second stage scene loader failed to find valid I/O data\n"); + return false; + } - switch(aimesh->mPrimitiveTypes) { - case aiPrimitiveType_TRIANGLE: - if((mesh = load_mesh(this, aiscn, flags, aimesh))) { - mesh_by_aimesh[aimesh] = mesh; - meshes.push_back(mesh); - } - break; + LoaderData *ldata = (LoaderData*)loader_data; + const aiScene *aiscn = ldata->aiscn; + fname = ldata->fname.c_str(); + + clear(); // clear any previous data (TODO: add flag for not clearing) - default: - error_log("unsupported primitive type: %u\n", aimesh->mPrimitiveTypes); - break; + // 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(); + root_scaling = root_matrix.get_scaling(); } - } - 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); - } + 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) { - nodes->add_child(node); + // load all meshes + for(unsigned int i=0; imNumMeshes; i++) { + aiMesh *aimesh = aiscn->mMeshes[i]; + Mesh *mesh; + + switch(aimesh->mPrimitiveTypes) { + case aiPrimitiveType_TRIANGLE: + if((mesh = load_mesh(this, aiscn, flags, aimesh))) { + ldata->mesh_by_aimesh[aimesh] = mesh; + meshes.push_back(mesh); + } + break; + + default: + error_log("unsupported primitive type: %u\n", aimesh->mPrimitiveTypes); + break; + } + } + + // 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) { + nodes->add_child(node); + } } - } - node_by_name.clear(); - mesh_by_aimesh.clear(); + info_log("loaded scene file: %s, %d meshes\n", fname, (int)meshes.size()); - aiReleaseImport(aiscn); - info_log("loaded scene file: %s, %d meshes\n", fname, (int)meshes.size()); - nodes->update(0); + aiReleaseImport(aiscn); + delete ldata; + loader_data = 0; + nodes->update(0); + } return true; } @@ -164,10 +205,11 @@ static bool load_material(Scene *scn, Material *mat, const aiMaterial *aimat) *dptr++ = *sptr == '\\' ? '/' : *sptr; } while(*sptr++); + if(!fname || !*fname) continue; + int textype = assimp_textype(aitype); - info_log("loading %s texture: %s\n", assimp_textypestr(aitype), fname); - Texture *tex = scn->texset->get_texture(fname, TEX_2D); + Texture *tex = texman.get_texture(fname, TEX_2D, &scn->datamap); assert(tex); mat->textures.push_back(tex); @@ -182,6 +224,8 @@ static bool load_material(Scene *scn, Material *mat, const aiMaterial *aimat) static SceneNode *load_node(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiNode *ainode) { + LoaderData *ldata = (LoaderData*)scn->loader_data; + SceneNode *node = new SceneNode; node->set_name(ainode->mName.data); @@ -200,7 +244,7 @@ static SceneNode *load_node(Scene *scn, const aiScene *aiscn, unsigned int flags for(unsigned int i=0; imNumMeshes; i++) { aiMesh *aimesh = aiscn->mMeshes[ainode->mMeshes[i]]; - Mesh *mesh = mesh_by_aimesh[aimesh]; + Mesh *mesh = ldata->mesh_by_aimesh[aimesh]; if(mesh) { ObjMesh *obj = new ObjMesh; obj->set_name(mesh->get_name()); @@ -221,7 +265,7 @@ static SceneNode *load_node(Scene *scn, const aiScene *aiscn, unsigned int flags } } - node_by_name[node->get_name()] = node; + ldata->node_by_name[node->get_name()] = node; return node; } @@ -298,7 +342,7 @@ static int assimp_textype(aiTextureType type) return MTL_TEX_UNKNOWN; } -static const char *assimp_textypestr(aiTextureType type) +/*static const char *assimp_textypestr(aiTextureType type) { switch(type) { case aiTextureType_DIFFUSE: @@ -316,7 +360,7 @@ static const char *assimp_textypestr(aiTextureType type) break; } return "unknown"; -} +}*/ static Mat4 assimp_matrix(const aiMatrix4x4 &aim) { @@ -324,3 +368,35 @@ static Mat4 assimp_matrix(const aiMatrix4x4 &aim) memcpy(m[0], &aim, 16 * sizeof(float)); return transpose(m); } + + +// --- SceneSet --- + +SceneSet::SceneSet() + : DataSet(create_scene, load_scene, done_scene, free_scene) +{ +} + +Scene *SceneSet::create_scene() +{ + return new Scene; +} + +bool SceneSet::load_scene(Scene *scn, const char *fname) +{ + return scn->load(fname, SCNLOAD_FLIPTEX | SCNLOAD_STAGE_IO); +} + +bool SceneSet::done_scene(Scene *scn) +{ + bool res = scn->load(0, SCNLOAD_STAGE_GL); + if(scn->metascn) { + scn->metascn->scene_loaded(scn); + } + return res; +} + +void SceneSet::free_scene(Scene *scn) +{ + delete scn; +}