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