]> git.tdb.fi Git - libs/gl.git/blob - source/core/texturecube.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / texturecube.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/io/memory.h>
3 #include <msp/strings/format.h>
4 #include "error.h"
5 #include "texturecube.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 const Vector3 TextureCube::directions[6] =
13 {
14         Vector3(1, 0, 0),
15         Vector3(-1, 0, 0),
16         Vector3(0, 1, 0),
17         Vector3(0, -1, 0),
18         Vector3(0, 0, 1),
19         Vector3(0, 0, -1)
20 };
21
22 const unsigned TextureCube::orientations[12] =
23 {
24         5, 3,
25         4, 3,
26         0, 4,
27         0, 5,
28         0, 3,
29         1, 3
30 };
31
32 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
33 {
34         if(size>0)
35         {
36                 if(fmt!=format || sz!=size || (lv && lv!=n_levels))
37                         throw incompatible_data("TextureCube::storage");
38                 return;
39         }
40         if(sz==0)
41                 throw invalid_argument("TextureCube::storage");
42
43         set_format(fmt);
44         size = sz;
45         n_levels = count_levels(size);
46         if(lv>0)
47                 n_levels = min(n_levels, lv);
48
49         allocate();
50 }
51
52 void TextureCube::image(unsigned level, const void *data)
53 {
54         const char *pixels = static_cast<const char *>(data);
55         unsigned face_size = size*size*get_pixel_size(storage_fmt);
56         for(unsigned i=0; i<6; ++i)
57                 image(static_cast<TextureCubeFace>(i), level, pixels+i*face_size);
58 }
59
60 void TextureCube::image(TextureCubeFace face, unsigned level, const void *data)
61 {
62         unsigned lsz = get_level_size(level);
63         return sub_image(face, level, 0, 0, lsz, lsz, data);
64 }
65
66 void TextureCube::sub_image(TextureCubeFace face, unsigned level, unsigned x, unsigned y, unsigned wd, unsigned ht, const void *data)
67 {
68         if(size==0)
69                 throw invalid_operation("TextureCube::sub_image");
70         if(level>=n_levels || x>size || x+wd>size || y>size || y+ht>size)
71                 throw out_of_range("TextureCube::sub_image");
72
73         TextureCubeBackend::sub_image(face, level, x, y, wd, ht, data);
74 }
75
76 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
77 {
78         unsigned w = img.get_width();
79         unsigned h = img.get_height();
80         if(w!=h)
81                 throw incompatible_data("TextureCube::image");
82
83         storage(pixelformat_from_image(img, use_srgb_format), w);
84         image(face, 0, img.get_pixels());
85 }
86
87 void TextureCube::image(const Graphics::Image &img, unsigned lv)
88 {
89         unsigned w = img.get_width();
90         unsigned h = img.get_height();
91
92         if(h!=w*6)
93                 throw incompatible_data("TextureCube::image");
94         h /= 6;
95
96         if(size==0)
97                 storage(pixelformat_from_image(img, use_srgb_format), w, lv);
98         else if(w!=size || h!=size)
99                 throw incompatible_data("TextureCube::image");
100
101         image(0, img.get_pixels());
102 }
103
104 unsigned TextureCube::get_level_size(unsigned level) const
105 {
106         return size>>level;
107 }
108
109 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
110 {
111         return directions[face];
112 }
113
114 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
115 {
116         return directions[orientations[face*2]];
117 }
118
119 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
120 {
121         return directions[orientations[face*2+1]];
122 }
123
124 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
125 {
126         float s = (u+0.5f)*2.0f/size-1.0f;
127         float t = (v+0.5f)*2.0f/size-1.0f;
128         const Vector3 &fv = get_face_direction(face);
129         const Vector3 &sv = get_s_direction(face);
130         const Vector3 &tv = get_t_direction(face);
131         return fv+s*sv+t*tv;
132 }
133
134
135 TextureCube::Loader::Loader(TextureCube &t):
136         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
137 {
138         init();
139 }
140
141 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
142         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
143 {
144         init();
145 }
146
147 void TextureCube::Loader::init()
148 {
149         add("external_image", &Loader::external_image);
150         add("image_data", &Loader::image_data);
151         add("raw_data", &Loader::raw_data);
152         add("storage", &Loader::storage);
153         add("storage", &Loader::storage_levels);
154 }
155
156 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
157 {
158         Graphics::Image img;
159         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
160         img.load_io(*io);
161
162         obj.image(face, img);
163 }
164
165 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
166 {
167         Graphics::Image img;
168         IO::Memory mem(data.data(), data.size());
169         img.load_io(mem);
170
171         obj.image(face, img);
172 }
173
174 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
175 {
176         obj.image(face, 0, data.data());
177 }
178
179 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
180 {
181         obj.storage(fmt, s);
182 }
183
184 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
185 {
186         obj.storage(fmt, s, l);
187 }
188
189
190 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
191 {
192         const string &str = conv.get();
193         if(str=="POSITIVE_X")
194                 face = POSITIVE_X;
195         else if(str=="NEGATIVE_X")
196                 face = NEGATIVE_X;
197         else if(str=="POSITIVE_Y")
198                 face = POSITIVE_Y;
199         else if(str=="NEGATIVE_Y")
200                 face = NEGATIVE_Y;
201         else if(str=="POSITIVE_Z")
202                 face = POSITIVE_Z;
203         else if(str=="NEGATIVE_Z")
204                 face = NEGATIVE_Z;
205         else
206                 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));
207 }
208
209 } // namespace GL
210 } // namespace Msp