]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture.cpp
Remove deprecated functions from Renderer
[libs/gl.git] / source / core / texture.cpp
1 #include <msp/datafile/rawdata.h>
2 #include <msp/io/memory.h>
3 #include "error.h"
4 #include "resourcemanager.h"
5 #include "texture.h"
6 #include "texture1d.h"
7 #include "texture2d.h"
8 #include "texture2darray.h"
9 #include "texture3d.h"
10 #include "texturecube.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 Texture::Texture(unsigned t):
18         TextureBackend(t),
19         format(NO_PIXELFORMAT),
20         storage_fmt(format),
21         swizzle(NO_SWIZZLE),
22         use_srgb_format(false),
23         auto_gen_mipmap(false)
24 { }
25
26 void Texture::set_format(PixelFormat fmt)
27 {
28         PixelComponents comp = get_components(fmt);
29         ComponentSwizzle swiz = get_required_swizzle(comp);
30         PixelComponents st_comp = unswizzle_components(comp, swiz);
31
32         PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
33         require_pixelformat(st_fmt);
34         if(swiz!=NO_SWIZZLE)
35                 require_swizzle();
36
37         format = fmt;
38         storage_fmt = st_fmt;
39         swizzle = swiz;
40 }
41
42 void Texture::load_image(const string &fn, unsigned lv)
43 {
44         Graphics::Image img;
45         img.load_file(fn);
46
47         image(img, lv);
48 }
49
50 Texture::GenericLoader::TypeRegistry &Texture::get_texture_registry()
51 {
52         static GenericLoader::TypeRegistry registry;
53         static bool initialized = false;
54         if(!initialized)
55         {
56                 initialized = true;
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");
62         }
63         return registry;
64 }
65
66
67 Texture::Loader::Loader(Texture &t, Collection *c):
68         CollectionObjectLoader<Texture>(t, c),
69         levels(0)
70 {
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);
78 }
79
80 void Texture::Loader::finish()
81 {
82         if(obj.auto_gen_mipmap)
83                 obj.generate_mipmap();
84 }
85
86 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
87 {
88         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
89         if(!io)
90                 throw IO::file_not_found(fn);
91         img.load_io(*io);
92 }
93
94 void Texture::Loader::external_data(const string &fn)
95 {
96         if(obj.manager)
97                 obj.manager->set_resource_location(obj, get_collection(), fn);
98         else
99         {
100                 DataFile::RawData rd;
101                 rd.open_file(get_collection(), fn);
102                 rd.load();
103                 obj.image(0, rd.get_data());
104         }
105 }
106
107 void Texture::Loader::external_image(bool srgb, const string &fn)
108 {
109         obj.use_srgb_format = srgb;
110         if(obj.manager)
111                 obj.manager->set_resource_location(obj, get_collection(), fn);
112         else
113         {
114                 Graphics::Image img;
115                 load_external_image(img, fn);
116                 obj.image(img, levels);
117         }
118 }
119
120 void Texture::Loader::generate_mipmap(bool gm)
121 {
122         obj.auto_gen_mipmap = gm;
123 }
124
125 void Texture::Loader::image_data(const string &data)
126 {
127         if(obj.manager)
128                 obj.set_manager(0);
129
130         Graphics::Image img;
131         IO::Memory mem(data.data(), data.size());
132         img.load_io(mem);
133
134         obj.image(img, levels);
135 }
136
137 void Texture::Loader::mipmap_levels(unsigned l)
138 {
139         levels = l;
140 }
141
142 void Texture::Loader::raw_data(const string &data)
143 {
144         if(obj.manager)
145                 obj.set_manager(0);
146
147         obj.image(0, data.data());
148 }
149
150 } // namespace GL
151 } // namespace Msp