1 #include <msp/io/memory.h>
4 #include "pixelstore.h"
12 Texture2D::Texture2D():
13 Texture(GL_TEXTURE_2D),
19 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
22 throw invalid_operation("Texture2D::storage");
24 throw invalid_argument("Texture2D::storage");
25 require_pixelformat(fmt);
32 void Texture2D::allocate(unsigned level)
34 if(allocated&(1<<level))
37 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
40 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
42 if(width==0 || height==0)
43 throw invalid_operation("Texture2D::image");
47 get_level_size(level, w, h);
49 BindRestore _bind(this);
50 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
52 allocated |= 1<<level;
53 if(gen_mipmap && level==0)
55 for(; (w || h); w>>=1, h>>=1, ++level) ;
56 allocated |= (1<<level)-1;
60 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
62 if(width==0 || height==0)
63 throw invalid_operation("Texture2D::sub_image");
67 BindRestore _bind(this);
68 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
71 void Texture2D::load_image(const string &fn)
79 void Texture2D::image(const Graphics::Image &img)
81 unsigned w = img.get_width();
82 unsigned h = img.get_height();
83 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
85 storage(storage_pixelformat_from_graphics(img.get_format()), w, h);
86 else if(w!=width || h!=height)
87 throw incompatible_data("Texture2D::image");
89 PixelStore pstore = PixelStore::from_image(img);
90 BindRestore _bind_ps(pstore);
92 image(0, fmt, UNSIGNED_BYTE, img.get_data());
95 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
107 Texture2D::Loader::Loader(Texture2D &t):
108 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
110 add("image_data", &Loader::image_data);
111 add("raw_data", &Loader::raw_data);
112 add("storage", &Loader::storage);
113 add("storage", &Loader::storage_b);
116 void Texture2D::Loader::image_data(const string &data)
119 IO::Memory mem(data.data(), data.size());
125 void Texture2D::Loader::raw_data(const string &data)
127 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
130 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
132 obj.storage(fmt, w, h);
135 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)