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):
19 format(NO_PIXELFORMAT),
22 use_srgb_format(false),
23 auto_gen_mipmap(false)
26 void Texture::set_format(PixelFormat fmt)
28 PixelComponents comp = get_components(fmt);
29 ComponentSwizzle swiz = get_required_swizzle(comp);
30 PixelComponents st_comp = unswizzle_components(comp, swiz);
32 PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
33 require_pixelformat(st_fmt);
42 void Texture::load_image(const string &fn, unsigned lv)
50 Texture::GenericLoader::TypeRegistry &Texture::get_texture_registry()
52 static GenericLoader::TypeRegistry registry;
53 static bool initialized = false;
57 registry.register_type<Texture1D>("1d");
58 registry.register_type<Texture2D>("2d");
59 registry.register_type<Texture3D>("3d");
60 registry.register_type<Texture2DArray>("2d_array");
61 registry.register_type<TextureCube>("cube");
67 Texture::Loader::Loader(Texture &t, Collection *c):
68 CollectionObjectLoader<Texture>(t, c),
71 add("external_data", &Loader::external_data);
72 add("external_image", &Loader::external_image, false);
73 add("external_image_srgb", &Loader::external_image, true);
74 add("generate_mipmap", &Loader::generate_mipmap);
75 add("image_data", &Loader::image_data);
76 add("mipmap_levels", &Loader::mipmap_levels);
77 add("raw_data", &Loader::raw_data);
80 void Texture::Loader::finish()
82 if(obj.auto_gen_mipmap)
83 obj.generate_mipmap();
86 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
88 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
90 throw IO::file_not_found(fn);
94 void Texture::Loader::external_data(const string &fn)
97 obj.manager->set_resource_location(obj, get_collection(), fn);
100 DataFile::RawData rd;
101 rd.open_file(get_collection(), fn);
103 obj.image(0, rd.get_data());
107 void Texture::Loader::external_image(bool srgb, const string &fn)
109 obj.use_srgb_format = srgb;
111 obj.manager->set_resource_location(obj, get_collection(), fn);
115 load_external_image(img, fn);
116 obj.image(img, levels);
120 void Texture::Loader::generate_mipmap(bool gm)
122 obj.auto_gen_mipmap = gm;
125 void Texture::Loader::image_data(const string &data)
131 IO::Memory mem(data.data(), data.size());
134 obj.image(img, levels);
137 void Texture::Loader::mipmap_levels(unsigned l)
142 void Texture::Loader::raw_data(const string &data)
147 obj.image(0, data.data());