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