datamap object passed around while loading
[laserbrain_demo] / src / texture.cc
index 5bf291d..3aebec3 100644 (file)
@@ -1,8 +1,11 @@
 #include <math.h>
+#include <assert.h>
 #include "texture.h"
+#include "datamap.h"
 #include "image.h"
 #include "opengl.h"
 #include "imago2.h"
+#include "logger.h"
 
 static int glifmt_from_ifmt(unsigned int ifmt);
 static int glfmt_from_ifmt(unsigned int ifmt);
@@ -35,6 +38,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 +73,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 +108,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 +140,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;
@@ -143,7 +150,7 @@ void Texture::bind(int tex_unit) const
 void Texture::create(int xsz, int ysz, TextureType textype, unsigned int ifmt)
 {
        if(textype == TEX_CUBE && xsz != ysz) {
-               fprintf(stderr, "trying to create cubemap with different width and height (%dx%d)\n", xsz, ysz);
+               error_log("trying to create cubemap with different width and height (%dx%d)\n", xsz, ysz);
                return;
        }
 
@@ -153,6 +160,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,6 +199,7 @@ void Texture::create_default(TextureType type)
                                pixels[1] = 64;
                                pixels[2] = chess ? 32 : 255;
                                pixels[3] = 255;
+                               pixels += 4;
                        }
                }
        }
@@ -230,6 +239,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 +276,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);
@@ -311,12 +322,12 @@ bool Texture::load(const char *fname)
 {
        Image img;
        if(!img.load(fname)) {
-               fprintf(stderr, "failed to load 2D texture: %s\n", fname);
+               error_log("failed to load 2D texture: %s\n", fname);
                return false;
        }
        set_image(img);
 
-       printf("loaded 2D texture: %s\n", fname);
+       info_log("loaded 2D texture: %s\n", fname);
        return true;
 }
 
@@ -457,16 +468,26 @@ TextureSet::TextureSet()
 {
 }
 
-Texture *TextureSet::get_texture(const char *name, TextureType type) const
+Texture *TextureSet::get_texture(const char *name, TextureType type, const DataMap *dmap) const
 {
-       typename std::map<std::string, Texture*>::const_iterator iter = data.find(name);
+       char *fname;
+       int nsize = dmap ? dmap->path_size(name) : 0;
+       if(nsize) {
+               fname = (char*)alloca(nsize);
+               dmap->lookup(name, fname, nsize);
+       } else {
+               fname = (char*)name;
+       }
+
+       std::map<std::string, Texture*>::const_iterator iter = data.find(fname);
        if(iter != data.end()) {
                return iter->second;
        }
 
        Texture *res = create();
+       data[fname] = res;
        res->create_default(type);
-       resman_lookup(rman, name, res);
+       resman_lookup(rman, fname, res);
        return res;
 }