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");
23 require_pixelformat(fmt);
30 void Texture2D::allocate(unsigned level)
32 if(allocated&(1<<level))
35 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
38 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
40 if(width==0 || height==0)
41 throw invalid_operation("Texture2D::image");
45 get_level_size(level, w, h);
47 Bind _bind(this, true);
48 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
50 allocated |= 1<<level;
51 if(gen_mipmap && level==0)
53 for(; (w || h); w>>=1, h>>=1, ++level) ;
54 allocated |= (1<<level)-1;
58 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
60 if(width==0 || height==0)
61 throw invalid_operation("Texture2D::sub_image");
65 Bind _bind(this, true);
66 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
69 void Texture2D::load_image(const string &fn)
77 void Texture2D::image(const Graphics::Image &img)
79 unsigned w = img.get_width();
80 unsigned h = img.get_height();
81 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
84 else if(w!=width || h!=height)
85 throw incompatible_data("Texture2D::image");
87 image(0, fmt, UNSIGNED_BYTE, img.get_data());
90 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
102 Texture2D::Loader::Loader(Texture2D &t):
103 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
105 add("image_data", &Loader::image_data);
106 add("raw_data", &Loader::raw_data);
107 add("storage", &Loader::storage);
108 add("storage", &Loader::storage_b);
111 void Texture2D::Loader::image_data(const string &data)
114 img.load_memory(data.data(), data.size());
119 void Texture2D::Loader::raw_data(const string &data)
121 obj.image(0, obj.ifmt, UNSIGNED_BYTE, data.data());
124 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
126 obj.storage(fmt, w, h);
129 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)