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 PixelComponents st_comp = comp;
30 FormatSwizzle swiz = NO_SWIZZLE;
35 swiz = R_TO_LUMINANCE;
39 swiz = RG_TO_LUMINANCE_ALPHA;
52 PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
53 require_pixelformat(st_fmt);
62 void Texture::load_image(const string &fn, unsigned lv)
70 Texture::GenericLoader::TypeRegistry &Texture::get_texture_registry()
72 static GenericLoader::TypeRegistry registry;
73 static bool initialized = false;
77 registry.register_type<Texture1D>("1d");
78 registry.register_type<Texture2D>("2d");
79 registry.register_type<Texture3D>("3d");
80 registry.register_type<Texture2DArray>("2d_array");
81 registry.register_type<TextureCube>("cube");
87 Texture::Loader::Loader(Texture &t, Collection *c):
88 CollectionObjectLoader<Texture>(t, c),
91 add("external_data", &Loader::external_data);
92 add("external_image", &Loader::external_image, false);
93 add("external_image_srgb", &Loader::external_image, true);
94 add("generate_mipmap", &Loader::generate_mipmap);
95 add("image_data", &Loader::image_data);
96 add("mipmap_levels", &Loader::mipmap_levels);
97 add("raw_data", &Loader::raw_data);
100 void Texture::Loader::finish()
102 if(obj.auto_gen_mipmap)
103 obj.generate_mipmap();
106 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
108 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
110 throw IO::file_not_found(fn);
114 void Texture::Loader::external_data(const string &fn)
117 obj.manager->set_resource_location(obj, get_collection(), fn);
120 DataFile::RawData rd;
121 rd.open_file(get_collection(), fn);
123 obj.image(0, rd.get_data());
127 void Texture::Loader::external_image(bool srgb, const string &fn)
129 obj.use_srgb_format = srgb;
131 obj.manager->set_resource_location(obj, get_collection(), fn);
135 load_external_image(img, fn);
136 obj.image(img, levels);
140 void Texture::Loader::generate_mipmap(bool gm)
142 obj.auto_gen_mipmap = gm;
145 void Texture::Loader::image_data(const string &data)
151 IO::Memory mem(data.data(), data.size());
154 obj.image(img, levels);
157 void Texture::Loader::mipmap_levels(unsigned l)
162 void Texture::Loader::raw_data(const string &data)
167 obj.image(0, data.data());