fixed bugs in the resource manager and removed the hardcoded textures
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 8 Nov 2016 12:42:20 +0000 (14:42 +0200)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 8 Nov 2016 12:42:20 +0000 (14:42 +0200)
src/app.cc
src/datamap.cc
src/dataset.inl
src/sceneload.cc
src/texture.cc

index 3426c99..6cb808c 100644 (file)
@@ -7,6 +7,7 @@
 #include "mesh.h"
 #include "meshgen.h"
 #include "scene.h"
+#include "datamap.h"
 
 static void draw_scene();
 
@@ -43,6 +44,11 @@ bool app_init()
        float ambient[] = {0.0, 0.0, 0.0, 0.0};
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
 
+       datamap_set_path("data");
+       if(!datamap_load_map("data.map")) {
+               fprintf(stderr, "failed to load datafile mappings\n");
+       }
+
        unsigned int sflags = SCNLOAD_FLIPTEX;
        scn = new Scene(&texman);
        if(!(scn->load("data/testscene/patoma.fbx", sflags)) ||
@@ -52,37 +58,28 @@ bool app_init()
        }
 
        // hardcoded texture assignment hack
-       Texture *tex_girogiromarmaro = texman.get("data/testscene/girogiromarmarodiffuse.jpg");
-       Texture *tex_kafemarble = texman.get("data/testscene/kafemarblediffuse.jpg");
-       Texture *tex_kentrikokafemarble = texman.get("data/testscene/kentrikokafemarblediffuse.jpg");
-       Texture *tex_paliomarmaro = texman.get("data/testscene/paliomarmarodiffuse.jpg");
-       Texture *tex_steelgreygranite = texman.get("data/testscene/steel-grey-granitediffuse.jpg");
-       Texture *tex_whitemarble = texman.get("data/testscene/whitemarblediffuse.jpg");
-       Texture *tex_kolones_lightmap = texman.get("data/testscene/kolones_lighmap.jpg");
-       Texture *tex_patoma_lightmap = texman.get("data/testscene/patomacorona_lightmap.jpg");
+       Texture *tex_kolones_lightmap = texman.get_texture("data/testscene/kolones_lighmap.jpg", TEX_2D);
+       Texture *tex_patoma_lightmap = texman.get_texture("data/testscene/patomacorona_lightmap.jpg", TEX_2D);
 
+       /*
        for(int i=0; i<(int)scn->objects.size(); i++) {
                Object *obj = scn->objects[i];
                if(obj->mtl.name == "WiteMarble") {
-                       obj->mtl.add_texture(tex_whitemarble, MTL_TEX_DIFFUSE);
                        obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
                } else if(obj->mtl.name == "BrownMarble") {
-                       obj->mtl.add_texture(tex_kafemarble, MTL_TEX_DIFFUSE);
                        obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
                } else if(obj->mtl.name == "GiroGiroMarmaro") {
-                       obj->mtl.add_texture(tex_girogiromarmaro, MTL_TEX_DIFFUSE);
                        obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
                } else if(obj->mtl.name == "KentrikoKafeMarmaro") {
-                       obj->mtl.add_texture(tex_kentrikokafemarble, MTL_TEX_DIFFUSE);
                        obj->mtl.add_texture(tex_patoma_lightmap, MTL_TEX_LIGHTMAP);
+
                } else if(obj->mtl.name == "SkouroGrizoMarmaro") {
-                       obj->mtl.add_texture(tex_steelgreygranite, MTL_TEX_DIFFUSE);
                        obj->mtl.add_texture(tex_kolones_lightmap, MTL_TEX_LIGHTMAP);
                } else if(obj->mtl.name == "PalioMarmaro") {
-                       obj->mtl.add_texture(tex_paliomarmaro, MTL_TEX_DIFFUSE);
                        obj->mtl.add_texture(tex_kolones_lightmap, MTL_TEX_LIGHTMAP);
                }
        }
+       */
 
        if(!(sdr = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) {
                fprintf(stderr, "failed to load test shaders\n");
@@ -106,7 +103,7 @@ static void update(float dt)
 
        scn->update(dt);
 
-       float walk_speed = 10.0 * dt;
+       float walk_speed = 2000.0 * dt;
        Vec3 dir;
 
        if(keystate[(int)'w']) {
@@ -199,7 +196,7 @@ void app_reshape(int x, int y)
 
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
-       gluPerspective(50.0, (float)x / (float)y, 0.5, 1000.0);
+       gluPerspective(50.0, (float)x / (float)y, 1.0, 10000.0);
 }
 
 void app_keyboard(int key, bool pressed)
index 626c47f..19aaf44 100644 (file)
@@ -4,7 +4,7 @@
 #include <vector>
 #include <map>
 #include <string>
-#include <regex>
+//#include <regex>
 #include "datamap.h"
 
 #ifdef WIN32
 
 static char *clean_line(char *s);
 
-static std::vector<std::pair<std::regex, std::string>> dmap;
+//static std::vector<std::pair<std::regex, std::string>> dmap;
+static std::vector<std::pair<std::string, std::string>> dmap;
 static std::map<std::string, std::string> cache;
 static std::string root;
 
 void datamap_reset()
 {
-       root.clear();
        dmap.clear();
        cache.clear();
 }
@@ -64,8 +64,10 @@ bool datamap_load_map(const char *fname)
                }
                *colon = 0;
 
-               std::pair<std::regex, std::string> pair;
-               pair.first = std::regex(line);
+               //std::pair<std::regex, std::string> pair;
+               //pair.first = std::regex(line);
+               std::pair<std::string, std::string> pair;
+               pair.first = std::string(line);
 
                char *value = clean_line(colon + 1);
                if(!value || !*value) {
@@ -88,8 +90,10 @@ err:
 
 void datamap_map(const char *re, const char *path)
 {
-       std::pair<std::regex, std::string> mapping;
-       mapping.first = std::regex(re);
+       //std::pair<std::regex, std::string> mapping;
+       //mapping.first = std::regex(re);
+       std::pair<std::string, std::string> mapping;
+       mapping.first = std::string(re);
        mapping.second = std::string(path);
        dmap.push_back(std::move(mapping));
 }
@@ -108,15 +112,17 @@ int datamap_lookup(const char *in, char *buf, int bsz)
                res = it->second;
        } else {
                // try matching with the available mappings
+               res = std::string(in);
+
                int num = dmap.size();
                for(int i=0; i<num; i++) {
-                       if(std::regex_match(in, dmap[i].first)) {
+                       //if(std::regex_search(in, dmap[i].first)) {
+                       if(strstr(in, dmap[i].first.c_str())) {
                                res = root.empty() ? dmap[i].second : root + "/" + dmap[i].second;
                                cache[in] = res;        // add it to the cache
                                break;
                        }
                }
-               return 0;
        }
 
        // copy result in buf, truncating if necessary and return the size of the
index 5f0fbf9..1df20d5 100644 (file)
@@ -45,6 +45,7 @@ T DataSet<T>::get(const char *name) const
        }
 
        T res = create();
+       data[name] = res;
        resman_lookup(rman, name, res);
        return res;
 }
index 1ae167b..d173538 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <assert.h>
 #include <vector>
 #include <map>
 #include <gmath/gmath.h>
@@ -22,6 +23,7 @@ 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);
+static const char *assimp_textypestr(aiTextureType type);
 
 static Mat4 assimp_matrix(const aiMatrix4x4 &aim);
 
@@ -42,12 +44,15 @@ bool Scene::load(const char *fname, unsigned int flags)
                aiProcess_Triangulate |
                aiProcess_SortByPType |
                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);
@@ -126,7 +131,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]);
@@ -171,17 +176,15 @@ 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);
+                       printf("   DBG(%p)\n", (void*)tex);
+
                        if(textype != MTL_TEX_UNKNOWN && !mat->stdtex[textype]) {
                                mat->stdtex[textype] = tex;
                        }
@@ -285,6 +288,10 @@ static Mesh *load_mesh(Scene *scn, const aiScene *aiscn, unsigned int flags, con
                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;
 }
 
@@ -323,6 +330,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;
@@ -332,6 +340,26 @@ 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;
index 45f9094..dddc0d1 100644 (file)
@@ -1,4 +1,5 @@
 #include <math.h>
+#include <assert.h>
 #include "texture.h"
 #include "image.h"
 #include "opengl.h"
@@ -35,6 +36,7 @@ void bind_texture(Texture *tex, int tunit)
        } else {
                glActiveTexture(GL_TEXTURE0 + tunit);
                glBindTexture(cur_target[tunit], 0);
+               assert(glGetError() == GL_NO_ERROR);
                glActiveTexture(GL_TEXTURE0);
        }
 }
@@ -69,6 +71,7 @@ void Texture::set_wrapping(unsigned int wrap)
        }
 
        glBindTexture(target, id);
+       assert(glGetError() == GL_NO_ERROR);
        glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap);
        glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap);
        glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap);
@@ -103,6 +106,7 @@ void Texture::set_filtering(unsigned int filt)
 void Texture::set_filtering(unsigned int min_filt, unsigned int mag_filt)
 {
        glBindTexture(target, id);
+       assert(glGetError() == GL_NO_ERROR);
        glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filt);
        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filt);
 }
@@ -134,6 +138,7 @@ void Texture::bind(int tex_unit) const
 {
        glActiveTexture(GL_TEXTURE0 + tex_unit);
        glBindTexture(target, id);
+       assert(glGetError() == GL_NO_ERROR);
        glActiveTexture(GL_TEXTURE0);
 
        cur_target[tex_unit] = target;
@@ -153,6 +158,7 @@ void Texture::create(int xsz, int ysz, TextureType textype, unsigned int ifmt)
        target = type_to_target(textype);
 
        glBindTexture(target, id);
+       assert(glGetError() == GL_NO_ERROR);
        glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
@@ -191,8 +197,10 @@ void Texture::create_default(TextureType type)
                                pixels[1] = 64;
                                pixels[2] = chess ? 32 : 255;
                                pixels[3] = 255;
+                               pixels += 4;
                        }
                }
+               default_img->save("/tmp/foo.png");
        }
 
        switch(type) {
@@ -230,6 +238,7 @@ void Texture::set_image_2d(const Image &img)
 
        target = GL_TEXTURE_2D;
        glBindTexture(target, id);
+       assert(glGetError() == GL_NO_ERROR);
        glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
@@ -266,6 +275,7 @@ bool Texture::set_image_cube(const Image &img, int idx)
 
        target = GL_TEXTURE_CUBE_MAP;
        glBindTexture(target, id);
+       assert(glGetError() == GL_NO_ERROR);
        glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
@@ -465,6 +475,7 @@ Texture *TextureSet::get_texture(const char *name, TextureType type) const
        }
 
        Texture *res = create();
+       data[name] = res;
        res->create_default(type);
        resman_lookup(rman, name, res);
        return res;