]> git.tdb.fi Git - libs/gl.git/blob - source/texturecube.cpp
Appease the compiler with some extra copy-ctor's and operator='s
[libs/gl.git] / source / texturecube.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/gl/extensions/arb_texture_cube_map.h>
3 #include <msp/gl/extensions/arb_texture_storage.h>
4 #include <msp/io/memory.h>
5 #include <msp/strings/format.h>
6 #include "bindable.h"
7 #include "error.h"
8 #include "pixelstore.h"
9 #include "texturecube.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 TextureCubeFace TextureCube::face_order[6] =
17 {
18         POSITIVE_X,
19         NEGATIVE_X,
20         POSITIVE_Y,
21         NEGATIVE_Y,
22         POSITIVE_Z,
23         NEGATIVE_Z
24 };
25
26 Vector3 TextureCube::directions[6] =
27 {
28         Vector3(1, 0, 0),
29         Vector3(-1, 0, 0),
30         Vector3(0, 1, 0),
31         Vector3(0, -1, 0),
32         Vector3(0, 0, 1),
33         Vector3(0, 0, -1)
34 };
35
36 unsigned TextureCube::orientations[12] =
37 {
38         5, 3,
39         4, 3,
40         0, 4,
41         0, 5,
42         0, 3,
43         1, 3
44 };
45
46 TextureCube::TextureCube():
47         Texture(GL_TEXTURE_CUBE_MAP),
48         size(0),
49         allocated(0)
50 {
51         static Require _req(ARB_texture_cube_map);
52 }
53
54 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
55 {
56         if(size>0)
57                 throw invalid_operation("TextureCube::storage");
58         if(sz==0)
59                 throw invalid_argument("TextureCube::storage");
60
61         set_internal_format(fmt);
62         size = sz;
63         levels = get_n_levels();
64         if(lv>0)
65                 levels = min(levels, lv);
66 }
67
68 void TextureCube::allocate(unsigned level)
69 {
70         if(size==0)
71                 throw invalid_operation("TextureCube::allocate");
72         if(level>=levels)
73                 throw invalid_argument("TextureCube::allocate");
74         if(allocated&(1<<level))
75                 return;
76
77         if(ARB_texture_storage)
78         {
79                 BindRestore _bind(this);
80                 glTexStorage2D(target, levels, ifmt, size, size);
81                 allocated |= (1<<levels)-1;
82         }
83         else
84         {
85                 PixelFormat base_fmt = get_base_pixelformat(ifmt);
86                 DataType type = get_alloc_type(base_fmt);
87                 for(unsigned i=0; i<6; ++i)
88                         image(enumerate_faces(i), level, base_fmt, type, 0);
89         }
90 }
91
92 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
93 {
94         if(size==0)
95                 throw invalid_operation("TextureCube::image");
96
97         unsigned s = get_level_size(level);
98         if(s==0)
99                 throw out_of_range("TextureCube::image");
100
101         if(ARB_texture_storage)
102                 return sub_image(face, level, 0, 0, s, s, fmt, type, data);
103
104         BindRestore _bind(this);
105         glTexImage2D(face, level, ifmt, s, s, 0, get_upload_format(fmt), type, data);
106
107         // XXX Allocation should be tracked per-face, but we'll run out of bits
108         allocated |= 1<<level;
109         if(auto_gen_mipmap==1 && level==0)
110         {
111                 // TODO Only do this once all faces are created
112                 generate_mipmap();
113                 allocated |= (1<<get_n_levels())-1;
114         }
115 }
116
117 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
118 {
119         if(size==0)
120                 throw invalid_operation("TextureCube::sub_image");
121
122         BindRestore _bind(this);
123         allocate(level);
124
125         glTexSubImage2D(face, level, x, y, wd, ht, get_upload_format(fmt), type, data);
126
127         if(auto_gen_mipmap==1 && level==0)
128                 generate_mipmap();
129 }
130
131 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
132 {
133         unsigned w = img.get_width();
134         unsigned h = img.get_height();
135         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
136         if(size==0)
137         {
138                 if(w!=h)
139                         throw incompatible_data("TextureCube::image");
140
141                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
142         }
143         else if(w!=size || h!=size)
144                 throw incompatible_data("TextureCube::image");
145
146         PixelStore pstore = PixelStore::from_image(img);
147         BindRestore _bind_ps(pstore);
148
149         image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
150 }
151
152 void TextureCube::image(const Graphics::Image &img, bool srgb)
153 {
154         unsigned w = img.get_width();
155         unsigned h = img.get_height();
156
157         if(h!=w*6)
158                 throw incompatible_data("TextureCube::image");
159         h /= 6;
160
161         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
162         if(size==0)
163         {
164                 unsigned l = (is_mipmapped(min_filter) ? mipmap_levels ? mipmap_levels : 0 : 1);
165                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, l);
166         }
167         else if(w!=size || h!=size)
168                 throw incompatible_data("TextureCube::image");
169
170         PixelStore pstore = PixelStore::from_image(img);
171         BindRestore _bind_ps(pstore);
172
173         const char *cdata = reinterpret_cast<const char *>(img.get_data());
174         unsigned face_size = img.get_stride()*size;
175         for(unsigned i=0; i<6; ++i)
176                 image(enumerate_faces(i), 0, fmt, UNSIGNED_BYTE, cdata+i*face_size);
177 }
178
179 unsigned TextureCube::get_n_levels() const
180 {
181         unsigned n = 0;
182         for(unsigned s=size; s; s>>=1, ++n) ;
183         return n;
184 }
185
186 unsigned TextureCube::get_level_size(unsigned level) const
187 {
188         return size>>level;
189 }
190
191 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
192 {
193         if(i>=6)
194                 throw out_of_range("TextureCube::enumerate_faces");
195         return face_order[i];
196 }
197
198 unsigned TextureCube::get_face_index(TextureCubeFace face)
199 {
200         switch(face)
201         {
202         case POSITIVE_X: return 0;
203         case NEGATIVE_X: return 1;
204         case POSITIVE_Y: return 2;
205         case NEGATIVE_Y: return 3;
206         case POSITIVE_Z: return 4;
207         case NEGATIVE_Z: return 5;
208         default: throw invalid_argument("TextureCube::get_face_index");
209         }
210 }
211
212 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
213 {
214         return directions[get_face_index(face)];
215 }
216
217 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
218 {
219         return directions[orientations[get_face_index(face)*2]];
220 }
221
222 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
223 {
224         return directions[orientations[get_face_index(face)*2+1]];
225 }
226
227 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
228 {
229         float s = (u+0.5f)*2.0f/size-1.0f;
230         float t = (v+0.5f)*2.0f/size-1.0f;
231         const Vector3 &fv = get_face_direction(face);
232         const Vector3 &sv = get_s_direction(face);
233         const Vector3 &tv = get_t_direction(face);
234         return fv+s*sv+t*tv;
235 }
236
237 UInt64 TextureCube::get_data_size() const
238 {
239         return id ? size*size*6*get_pixel_size(ifmt) : 0;
240 }
241
242
243 TextureCube::Loader::Loader(TextureCube &t):
244         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
245 {
246         init();
247 }
248
249 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
250         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
251 {
252         init();
253 }
254
255 void TextureCube::Loader::init()
256 {
257         add("external_image", &Loader::external_image);
258         add("image_data", &Loader::image_data);
259         add("raw_data", &Loader::raw_data);
260         add("storage", &Loader::storage);
261 }
262
263 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
264 {
265         Graphics::Image img;
266         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
267         img.load_io(*io);
268
269         obj.image(face, img, srgb);
270 }
271
272 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
273 {
274         Graphics::Image img;
275         IO::Memory mem(data.data(), data.size());
276         img.load_io(mem);
277
278         obj.image(face, img, srgb);
279 }
280
281 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
282 {
283         obj.image(face, 0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
284 }
285
286 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
287 {
288         obj.storage(fmt, s);
289 }
290
291
292 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
293 {
294         const string &str = conv.get();
295         if(str=="POSITIVE_X")
296                 face = POSITIVE_X;
297         else if(str=="NEGATIVE_X")
298                 face = NEGATIVE_X;
299         else if(str=="POSITIVE_Y")
300                 face = POSITIVE_Y;
301         else if(str=="NEGATIVE_Y")
302                 face = NEGATIVE_Y;
303         else if(str=="POSITIVE_Z")
304                 face = POSITIVE_Z;
305         else if(str=="NEGATIVE_Z")
306                 face = NEGATIVE_Z;
307         else
308                 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));
309 }
310
311 } // namespace GL
312 } // namespace Msp