1 #include <msp/io/memory.h>
11 Texture2D::Texture2D():
12 Texture(GL_TEXTURE_2D),
18 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
21 throw invalid_operation("Texture2D::storage");
23 throw invalid_argument("Texture2D::storage");
24 require_pixelformat(fmt);
31 void Texture2D::allocate(unsigned level)
33 if(allocated&(1<<level))
36 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
39 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
41 if(width==0 || height==0)
42 throw invalid_operation("Texture2D::image");
46 get_level_size(level, w, h);
48 Bind _bind(this, true);
49 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
51 allocated |= 1<<level;
52 if(gen_mipmap && level==0)
54 for(; (w || h); w>>=1, h>>=1, ++level) ;
55 allocated |= (1<<level)-1;
59 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
61 if(width==0 || height==0)
62 throw invalid_operation("Texture2D::sub_image");
66 Bind _bind(this, true);
67 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
70 void Texture2D::load_image(const string &fn)
78 void Texture2D::image(const Graphics::Image &img)
80 unsigned w = img.get_width();
81 unsigned h = img.get_height();
82 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
85 else if(w!=width || h!=height)
86 throw incompatible_data("Texture2D::image");
88 image(0, fmt, UNSIGNED_BYTE, img.get_data());
91 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
103 Texture2D::Loader::Loader(Texture2D &t):
104 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
106 add("image_data", &Loader::image_data);
107 add("raw_data", &Loader::raw_data);
108 add("storage", &Loader::storage);
109 add("storage", &Loader::storage_b);
112 void Texture2D::Loader::image_data(const string &data)
115 IO::Memory mem(data.data(), data.size());
121 void Texture2D::Loader::raw_data(const string &data)
123 obj.image(0, obj.ifmt, UNSIGNED_BYTE, data.data());
126 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
128 obj.storage(fmt, w, h);
131 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)