fixed regression in the texture resource management
[laserbrain_demo] / src / sceneload.cc
index d173538..6e959b2 100644 (file)
 #include "scene.h"
 #include "objmesh.h"
 #include "datamap.h"
+#include "logger.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);
 static Mesh *load_mesh(Scene *scn, const aiScene *aiscn, unsigned int flags, const aiMesh *aimesh);
-static void print_nodes(SceneNode *node, int lvl = 0);
 /*static const char *mprop_semantic(int x);
 static int count_textures(const aiMaterial *aimat);*/
 static int assimp_textype(aiTextureType type);
@@ -51,11 +51,11 @@ bool Scene::load(const char *fname, unsigned int flags)
                ppflags |= aiProcess_FlipUVs;
        }
 
-       printf("Loading scene file: %s\n", fname);
+       info_log("Loading scene file: %s\n", fname);
 
        const aiScene *aiscn = aiImportFile(fname, ppflags);
        if(!aiscn) {
-               fprintf(stderr, "failed to load scene file: %s\n", fname);
+               error_log("failed to load scene file: %s\n", fname);
                return false;
        }
 
@@ -68,11 +68,6 @@ bool Scene::load(const char *fname, unsigned int flags)
                root_pos = root_matrix.get_translation();
                root_rot = root_matrix.get_rotation();
                root_scaling = root_matrix.get_scaling();
-
-               printf("assimp root node: %s\n", aiscn->mRootNode->mName.data);
-               printf("  pos: %f %f %f\n", root_pos.x, root_pos.y, root_pos.z);
-               printf("  rot: %f %+f %+f %+f\n", root_rot.w, root_rot.x, root_rot.y, root_rot.z);
-               printf("  scaling: %f %f %f\n", root_scaling.x, root_scaling.y, root_scaling.z);
        }
 
        // load all meshes
@@ -89,13 +84,14 @@ bool Scene::load(const char *fname, unsigned int flags)
                        break;
 
                default:
-                       fprintf(stderr, "unsupported primitive type: %u\n", aimesh->mPrimitiveTypes);
+                       error_log("unsupported primitive type: %u\n", aimesh->mPrimitiveTypes);
                        break;
                }
        }
 
        if(!nodes) {
                nodes = new SceneNode;
+               nodes->scene = this;
                nodes->set_name("root");
                nodes->set_position(root_pos);
                nodes->set_rotation(root_rot);
@@ -114,9 +110,8 @@ bool Scene::load(const char *fname, unsigned int flags)
        mesh_by_aimesh.clear();
 
        aiReleaseImport(aiscn);
-       printf("loaded scene file: %s, %d meshes\n", fname, (int)meshes.size());
+       info_log("loaded scene file: %s, %d meshes\n", fname, (int)meshes.size());
        nodes->update(0);
-       print_nodes(nodes);
        return true;
 }
 
@@ -131,7 +126,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());
+       //info_log("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]);
@@ -162,29 +157,20 @@ static bool load_material(Scene *scn, Material *mat, const aiMaterial *aimat)
                                continue;
                        }
 
-                       char *fname;
-                       int nsize = datamap_path_size(aipath.data);
-                       if(nsize) {
-                               fname = new char[nsize];
-                               datamap_lookup(aipath.data, fname, nsize);
-                       } else {
-                               fname = new char[strlen(aipath.data) + 1];
-                               char *dptr = fname;
-                               char *sptr = aipath.data;
-                               do {
-                                       *dptr++ = *sptr == '\\' ? '/' : *sptr;
-                               } while(*sptr++);
-                       }
+                       char *fname = (char*)alloca(strlen(aipath.data) + 1);
+                       char *dptr = fname;
+                       char *sptr = aipath.data;
+                       do {
+                               *dptr++ = *sptr == '\\' ? '/' : *sptr;
+                       } while(*sptr++);
 
                        int textype = assimp_textype(aitype);
-                       printf("loading %s texture: %s\n", assimp_textypestr(aitype), fname);
+                       info_log("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);
 
-                       printf("   DBG(%p)\n", (void*)tex);
-
                        if(textype != MTL_TEX_UNKNOWN && !mat->stdtex[textype]) {
                                mat->stdtex[textype] = tex;
                        }
@@ -217,6 +203,7 @@ static SceneNode *load_node(Scene *scn, const aiScene *aiscn, unsigned int flags
                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]);
@@ -241,6 +228,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;
@@ -287,39 +275,9 @@ 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;
        }
-
-       AABox bbox = mesh->get_aabbox();
-       printf("mesh bounding box: %f %f %f -> %f %f %f\n", bbox.min.x, bbox.min.y, bbox.min.z,
-                       bbox.max.x, bbox.max.y, bbox.max.z);
-
        return mesh;
 }
 
-static void print_nodes(SceneNode *node, int lvl)
-{
-       Vec3 pos = node->get_node_position();
-       Quat rot = node->get_node_rotation();
-       Vec3 scale = node->get_node_scaling();
-
-       const char *type = node->get_num_objects() > 0 ? "mesh node" : "null node";
-
-       for(int i=0; i<lvl; i++) {
-               fputs("  ", stdout);
-       }
-       printf("%s[%s] p(%g %g %g) rq(%g %+gi %+gj %+gk) s(%g %g %g)\n", type, node->get_name(),
-                       pos.x, pos.y, pos.z, rot.w, rot.x, rot.y, rot.z, scale.x, scale.y, scale.z);
-
-       if(node->get_num_objects()) {
-               Mat4 xform = node->get_matrix();
-               xform.print(stdout);
-       }
-
-       int nchld = node->get_num_children();
-       for(int i=0; i<nchld; i++) {
-               print_nodes(node->get_child(i), lvl + 1);
-       }
-}
-
 static int assimp_textype(aiTextureType type)
 {
        switch(type) {