]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture.cpp
Add keywords for light and material to Resources::Loader
[libs/gl.git] / source / core / texture.cpp
1 #include <msp/io/memory.h>
2 #include "error.h"
3 #include "resourcemanager.h"
4 #include "texture.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 Texture::Texture(unsigned t):
12         TextureBackend(t),
13         format(NO_PIXELFORMAT),
14         storage_fmt(format),
15         swizzle(NO_SWIZZLE),
16         use_srgb_format(false),
17         auto_gen_mipmap(false)
18 { }
19
20 void Texture::set_format(PixelFormat fmt)
21 {
22         PixelComponents comp = get_components(fmt);
23         PixelComponents st_comp = comp;
24         FormatSwizzle swiz = NO_SWIZZLE;
25         switch(comp)
26         {
27         case LUMINANCE:
28                 st_comp = RED;
29                 swiz = R_TO_LUMINANCE;
30                 break;
31         case LUMINANCE_ALPHA:
32                 st_comp = RG;
33                 swiz = RG_TO_LUMINANCE_ALPHA;
34                 break;
35         case BGR:
36                 st_comp = RGB;
37                 swiz = RGB_TO_BGR;
38                 break;
39         case BGRA:
40                 st_comp = RGBA;
41                 swiz = RGB_TO_BGR;
42                 break;
43         default:;
44         }
45
46         PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
47         require_pixelformat(st_fmt);
48         if(swiz!=NO_SWIZZLE)
49                 require_swizzle();
50
51         format = fmt;
52         storage_fmt = st_fmt;
53         swizzle = swiz;
54 }
55
56 void Texture::load_image(const string &fn, unsigned lv)
57 {
58         Graphics::Image img;
59         img.load_file(fn);
60
61         image(img, lv);
62 }
63
64
65 Texture::Loader::Loader(Texture &t, Collection *c):
66         CollectionObjectLoader<Texture>(t, c),
67         levels(0)
68 {
69         add("external_image", &Loader::external_image);
70         add("external_image_srgb", &Loader::external_image_srgb);
71         add("generate_mipmap", &Loader::generate_mipmap);
72         add("image_data", &Loader::image_data);
73         add("mipmap_levels", &Loader::mipmap_levels);
74 }
75
76 void Texture::Loader::finish()
77 {
78         if(obj.auto_gen_mipmap)
79                 obj.generate_mipmap();
80 }
81
82 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
83 {
84         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
85         if(!io)
86                 throw IO::file_not_found(fn);
87         img.load_io(*io);
88 }
89
90 void Texture::Loader::external_image(const string &fn)
91 {
92         obj.use_srgb_format = false;
93         external_image_common(fn);
94 }
95
96 void Texture::Loader::external_image_srgb(const string &fn)
97 {
98         obj.use_srgb_format = true;
99         external_image_common(fn);
100 }
101
102 void Texture::Loader::external_image_common(const string &fn)
103 {
104         if(obj.manager)
105                 obj.manager->set_resource_location(obj, get_collection(), fn);
106         else
107         {
108                 Graphics::Image img;
109                 load_external_image(img, fn);
110                 obj.image(img, levels);
111         }
112 }
113
114 void Texture::Loader::generate_mipmap(bool gm)
115 {
116         obj.auto_gen_mipmap = gm;
117 }
118
119 void Texture::Loader::image_data(const string &data)
120 {
121         if(obj.manager)
122                 obj.set_manager(0);
123
124         Graphics::Image img;
125         IO::Memory mem(data.data(), data.size());
126         img.load_io(mem);
127
128         obj.image(img, levels);
129 }
130
131 void Texture::Loader::mipmap_levels(unsigned l)
132 {
133         levels = l;
134 }
135
136 } // namespace GL
137 } // namespace Msp