]> git.tdb.fi Git - libs/gl.git/blob - source/core/texturecube.cpp
Mark constant data as const
[libs/gl.git] / source / core / texturecube.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_seamless_cube_map.h>
4 #include <msp/gl/extensions/arb_texture_cube_map.h>
5 #include <msp/gl/extensions/arb_texture_storage.h>
6 #include <msp/io/memory.h>
7 #include <msp/strings/format.h>
8 #include "error.h"
9 #include "texturecube.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 const 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 const 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 const 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 {
50         static Require _req(ARB_texture_cube_map);
51         if(ARB_seamless_cube_map)
52         {
53                 static bool seamless_init = false;
54                 if(!seamless_init)
55                 {
56                         glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
57                         seamless_init = true;
58                 }
59         }
60 }
61
62 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
63 {
64         if(size>0)
65         {
66                 if(fmt!=format || sz!=size || (lv && lv!=levels))
67                         throw incompatible_data("TextureCube::storage");
68                 return;
69         }
70         if(sz==0)
71                 throw invalid_argument("TextureCube::storage");
72
73         set_format(fmt);
74         size = sz;
75         levels = get_n_levels();
76         if(lv>0)
77                 levels = min(levels, lv);
78
79         GLenum gl_fmt = get_gl_pixelformat(storage_fmt);
80         if(ARB_texture_storage)
81         {
82                 if(ARB_direct_state_access)
83                         glTextureStorage2D(id, levels, gl_fmt, size, size);
84                 else
85                 {
86                         bind_scratch();
87                         glTexStorage2D(target, levels, gl_fmt, size, size);
88                 }
89         }
90         else
91         {
92                 bind_scratch();
93                 GLenum comp = get_gl_components(get_components(storage_fmt));
94                 GLenum type = get_gl_type(get_component_type(storage_fmt));
95                 for(unsigned i=0; i<levels; ++i)
96                 {
97                         unsigned lv_size = get_level_size(i);
98                         for(unsigned j=0; j<6; ++j)
99                                 glTexImage2D(enumerate_faces(j), i, gl_fmt, lv_size, lv_size, 0, comp, type, 0);
100                 }
101                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
102         }
103
104         apply_swizzle();
105 }
106
107 void TextureCube::image(TextureCubeFace face, unsigned level, const void *data)
108 {
109         unsigned lsz = get_level_size(level);
110         return sub_image(face, level, 0, 0, lsz, lsz, data);
111 }
112
113 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
114 {
115         if(size==0)
116                 throw invalid_operation("TextureCube::sub_image");
117         if(level>=levels)
118                 throw out_of_range("TextureCube::sub_image");
119
120         GLenum comp = get_gl_components(get_components(storage_fmt));
121         GLenum type = get_gl_type(get_component_type(storage_fmt));
122         if(ARB_direct_state_access)
123                 glTextureSubImage3D(id, level, x, y, get_face_index(face), wd, ht, 1, comp, type, data);
124         else
125         {
126                 bind_scratch();
127                 glTexSubImage2D(face, level, x, y, wd, ht, comp, type, data);
128         }
129 }
130
131 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
132 {
133         unsigned w = img.get_width();
134         unsigned h = img.get_height();
135         if(w!=h)
136                 throw incompatible_data("TextureCube::image");
137
138         PixelFormat fmt = pixelformat_from_image(img);
139         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w);
140
141         image(face, 0, img.get_pixels());
142 }
143
144 void TextureCube::image(const Graphics::Image &img, unsigned lv)
145 {
146         unsigned w = img.get_width();
147         unsigned h = img.get_height();
148
149         if(h!=w*6)
150                 throw incompatible_data("TextureCube::image");
151         h /= 6;
152
153         PixelFormat fmt = pixelformat_from_image(img);
154         if(size==0)
155                 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
156         else if(w!=size || h!=size)
157                 throw incompatible_data("TextureCube::image");
158
159         const char *pixels = reinterpret_cast<const char *>(img.get_pixels());
160         unsigned face_size = img.get_stride()*size;
161         for(unsigned i=0; i<6; ++i)
162                 image(enumerate_faces(i), 0, pixels+i*face_size);
163 }
164
165 unsigned TextureCube::get_n_levels() const
166 {
167         unsigned n = 0;
168         for(unsigned s=size; s; s>>=1, ++n) ;
169         return n;
170 }
171
172 unsigned TextureCube::get_level_size(unsigned level) const
173 {
174         return size>>level;
175 }
176
177 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
178 {
179         if(i>=6)
180                 throw out_of_range("TextureCube::enumerate_faces");
181         return face_order[i];
182 }
183
184 unsigned TextureCube::get_face_index(TextureCubeFace face)
185 {
186         switch(face)
187         {
188         case POSITIVE_X: return 0;
189         case NEGATIVE_X: return 1;
190         case POSITIVE_Y: return 2;
191         case NEGATIVE_Y: return 3;
192         case POSITIVE_Z: return 4;
193         case NEGATIVE_Z: return 5;
194         default: throw invalid_argument("TextureCube::get_face_index");
195         }
196 }
197
198 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
199 {
200         return directions[get_face_index(face)];
201 }
202
203 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
204 {
205         return directions[orientations[get_face_index(face)*2]];
206 }
207
208 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
209 {
210         return directions[orientations[get_face_index(face)*2+1]];
211 }
212
213 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
214 {
215         float s = (u+0.5f)*2.0f/size-1.0f;
216         float t = (v+0.5f)*2.0f/size-1.0f;
217         const Vector3 &fv = get_face_direction(face);
218         const Vector3 &sv = get_s_direction(face);
219         const Vector3 &tv = get_t_direction(face);
220         return fv+s*sv+t*tv;
221 }
222
223 uint64_t TextureCube::get_data_size() const
224 {
225         return id ? size*size*6*get_pixel_size(storage_fmt) : 0;
226 }
227
228
229 TextureCube::Loader::Loader(TextureCube &t):
230         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
231 {
232         init();
233 }
234
235 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
236         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
237 {
238         init();
239 }
240
241 void TextureCube::Loader::init()
242 {
243         add("external_image", &Loader::external_image);
244         add("image_data", &Loader::image_data);
245         add("raw_data", &Loader::raw_data);
246         add("storage", &Loader::storage);
247         add("storage", &Loader::storage_levels);
248 }
249
250 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
251 {
252         Graphics::Image img;
253         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
254         img.load_io(*io);
255
256         obj.image(face, img);
257 }
258
259 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
260 {
261         Graphics::Image img;
262         IO::Memory mem(data.data(), data.size());
263         img.load_io(mem);
264
265         obj.image(face, img);
266 }
267
268 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
269 {
270         obj.image(face, 0, data.data());
271 }
272
273 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
274 {
275         obj.storage(fmt, s);
276 }
277
278 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
279 {
280         obj.storage(fmt, s, l);
281 }
282
283
284 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
285 {
286         const string &str = conv.get();
287         if(str=="POSITIVE_X")
288                 face = POSITIVE_X;
289         else if(str=="NEGATIVE_X")
290                 face = NEGATIVE_X;
291         else if(str=="POSITIVE_Y")
292                 face = POSITIVE_Y;
293         else if(str=="NEGATIVE_Y")
294                 face = NEGATIVE_Y;
295         else if(str=="POSITIVE_Z")
296                 face = POSITIVE_Z;
297         else if(str=="NEGATIVE_Z")
298                 face = NEGATIVE_Z;
299         else
300                 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));
301 }
302
303 } // namespace GL
304 } // namespace Msp