1 #include <msp/datafile/rawdata.h>
2 #include <msp/io/memory.h>
4 #include "resourcemanager.h"
8 #include "texture2darray.h"
10 #include "texturecube.h"
17 Texture::Texture(unsigned t):
21 void Texture::set_format(PixelFormat fmt)
23 PixelComponents comp = get_components(fmt);
24 ComponentSwizzle swiz = get_required_swizzle(comp);
25 PixelComponents st_comp = unswizzle_components(comp, swiz);
27 PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
28 require_pixelformat(st_fmt);
37 unsigned Texture::count_levels(unsigned size)
40 for(; size; size>>=1, ++n) ;
44 void Texture::load_image(const string &fn, unsigned lv)
52 Texture::GenericLoader::TypeRegistry &Texture::get_texture_registry()
54 static GenericLoader::TypeRegistry registry;
55 static bool initialized = false;
59 registry.register_type<Texture1D>("1d");
60 registry.register_type<Texture2D>("2d");
61 registry.register_type<Texture3D>("3d");
62 registry.register_type<Texture2DArray>("2d_array");
63 registry.register_type<TextureCube>("cube");
69 Texture::Loader::Loader(Texture &t, Collection *c):
70 CollectionObjectLoader<Texture>(t, c),
73 add("external_data", &Loader::external_data);
74 add("external_image", &Loader::external_image, false);
75 add("external_image_srgb", &Loader::external_image, true);
76 add("generate_mipmap", &Loader::generate_mipmap);
77 add("image_data", &Loader::image_data);
78 add("mipmap_levels", &Loader::mipmap_levels);
79 add("raw_data", &Loader::raw_data);
82 void Texture::Loader::finish()
84 if(obj.auto_gen_mipmap)
85 obj.generate_mipmap();
88 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
90 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
92 throw IO::file_not_found(fn);
96 void Texture::Loader::external_data(const string &fn)
99 obj.manager->set_resource_location(obj, get_collection(), fn);
102 DataFile::RawData rd;
103 rd.open_file(get_collection(), fn);
105 obj.image(0, rd.get_data());
109 void Texture::Loader::external_image(bool srgb, const string &fn)
111 obj.use_srgb_format = srgb;
113 obj.manager->set_resource_location(obj, get_collection(), fn);
117 load_external_image(img, fn);
118 obj.image(img, levels);
122 void Texture::Loader::generate_mipmap(bool gm)
124 obj.auto_gen_mipmap = gm;
127 void Texture::Loader::image_data(const string &data)
133 IO::Memory mem(data.data(), data.size());
136 obj.image(img, levels);
139 void Texture::Loader::mipmap_levels(unsigned l)
144 void Texture::Loader::raw_data(const string &data)
149 obj.image(0, data.data());