10 Texture2D::Texture2D():
11 Texture(GL_TEXTURE_2D),
17 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
20 throw invalid_operation("Texture2D::storage");
22 throw invalid_argument("Texture2D::storage");
29 void Texture2D::allocate(unsigned level)
31 if(allocated&(1<<level))
34 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
37 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
39 if(width==0 || height==0)
40 throw invalid_operation("Texture2D::image");
44 get_level_size(level, w, h);
46 Bind _bind(this, true);
47 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
49 allocated |= 1<<level;
50 if(gen_mipmap && level==0)
52 for(; (w || h); w>>=1, h>>=1, ++level) ;
53 allocated |= (1<<level)-1;
57 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
59 if(width==0 || height==0)
60 throw invalid_operation("Texture2D::sub_image");
64 Bind _bind(this, true);
65 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
68 void Texture2D::load_image(const string &fn)
76 void Texture2D::image(const Graphics::Image &img)
78 unsigned w = img.get_width();
79 unsigned h = img.get_height();
80 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
83 else if(w!=width || h!=height)
84 throw incompatible_data("Texture2D::image");
86 image(0, fmt, UNSIGNED_BYTE, img.get_data());
89 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
101 Texture2D::Loader::Loader(Texture2D &t):
104 add("image_data", &Loader::image_data);
105 add("raw_data", &Loader::raw_data);
106 add("storage", &Loader::storage);
107 add("storage", &Loader::storage_b);
110 void Texture2D::Loader::image_data(const string &data)
113 img.load_memory(data.data(), data.size());
115 static_cast<Texture2D &>(obj).image(img);
118 void Texture2D::Loader::raw_data(const string &data)
120 Texture2D &t2d = static_cast<Texture2D &>(obj);
121 t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data());
124 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
126 static_cast<Texture2D &>(obj).storage(fmt, w, h);
129 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)