1 #include <msp/io/memory.h>
4 #include "pixelstore.h"
13 Texture2D::Texture2D():
14 Texture(GL_TEXTURE_2D),
20 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
23 throw invalid_operation("Texture2D::storage");
25 throw invalid_argument("Texture2D::storage");
26 require_pixelformat(fmt);
33 void Texture2D::allocate(unsigned level)
35 if(allocated&(1<<level))
38 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
41 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
43 if(width==0 || height==0)
44 throw invalid_operation("Texture2D::image");
48 get_level_size(level, w, h);
50 BindRestore _bind(this);
51 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
53 allocated |= 1<<level;
54 if(gen_mipmap && level==0)
56 for(; (w || h); w>>=1, h>>=1, ++level) ;
57 allocated |= (1<<level)-1;
61 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
63 if(width==0 || height==0)
64 throw invalid_operation("Texture2D::sub_image");
68 BindRestore _bind(this);
69 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
72 void Texture2D::load_image(const string &fn, bool srgb)
80 void Texture2D::image(const Graphics::Image &img, bool srgb)
82 unsigned w = img.get_width();
83 unsigned h = img.get_height();
84 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
87 PixelFormat f = storage_pixelformat_from_graphics(img.get_format());
89 f = get_srgb_pixelformat(f);
92 else if(w!=width || h!=height)
93 throw incompatible_data("Texture2D::image");
95 PixelStore pstore = PixelStore::from_image(img);
96 BindRestore _bind_ps(pstore);
98 image(0, fmt, UNSIGNED_BYTE, img.get_data());
101 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
113 Texture2D::Loader::Loader(Texture2D &t):
114 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
119 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
120 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
125 void Texture2D::Loader::init()
127 add("image_data", &Loader::image_data);
128 add("raw_data", &Loader::raw_data);
129 add("storage", &Loader::storage);
130 add("storage", &Loader::storage_b);
133 void Texture2D::Loader::image_data(const string &data)
136 IO::Memory mem(data.data(), data.size());
139 obj.image(img, srgb);
142 void Texture2D::Loader::raw_data(const string &data)
144 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
147 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
150 fmt = get_srgb_pixelformat(fmt);
151 obj.storage(fmt, w, h);
154 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)