10 Texture2D::Texture2D():
11 Texture(GL_TEXTURE_2D),
17 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
20 throw InvalidState("Texture storage may only be specified once");
22 throw InvalidParameterValue("Invalid texture dimensions");
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)
43 get_level_size(level, w, h);
45 Bind _bind(this, true);
46 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
48 allocated |= 1<<level;
49 if(gen_mipmap && level==0)
51 for(; (w || h); w>>=1, h>>=1, ++level) ;
52 allocated |= (1<<level)-1;
56 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
61 Bind _bind(this, true);
62 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
65 void Texture2D::load_image(const string &fn)
73 void Texture2D::image(const Graphics::Image &img)
75 unsigned w = img.get_width();
76 unsigned h = img.get_height();
77 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
80 else if(w!=width || h!=height)
81 throw IncompatibleData("Image does not match texture storage");
83 image(0, fmt, UNSIGNED_BYTE, img.get_data());
86 void Texture2D::require_storage()
88 if(width==0 || height==0)
89 throw InvalidState("Texture storage has not been specified");
92 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
104 Texture2D::Loader::Loader(Texture2D &t):
107 add("image_data", &Loader::image_data);
108 add("raw_data", &Loader::raw_data);
109 add("storage", &Loader::storage);
110 add("storage", &Loader::storage_b);
113 void Texture2D::Loader::image_data(const string &data)
116 img.load_memory(data.data(), data.size());
118 static_cast<Texture2D &>(obj).image(img);
121 void Texture2D::Loader::raw_data(const string &data)
123 Texture2D &t2d = static_cast<Texture2D &>(obj);
124 t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data());
127 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
129 static_cast<Texture2D &>(obj).storage(fmt, w, h);
132 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)