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>
10 #include "pixelstore.h"
11 #include "texturecube.h"
18 TextureCubeFace TextureCube::face_order[6] =
28 Vector3 TextureCube::directions[6] =
38 unsigned TextureCube::orientations[12] =
48 TextureCube::TextureCube():
49 Texture(GL_TEXTURE_CUBE_MAP),
53 static Require _req(ARB_texture_cube_map);
54 if(ARB_seamless_cube_map)
55 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
58 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
62 if(fmt!=format || sz!=size || (lv && lv!=levels))
63 throw incompatible_data("TextureCube::storage");
67 throw invalid_argument("TextureCube::storage");
71 levels = get_n_levels();
73 levels = min(levels, lv);
76 void TextureCube::allocate(unsigned level)
79 throw invalid_operation("TextureCube::allocate");
81 throw invalid_argument("TextureCube::allocate");
82 if(allocated&(64<<level))
85 if(ARB_texture_storage)
87 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
88 if(ARB_direct_state_access)
89 glTextureStorage2D(id, levels, storage_fmt, size, size);
91 glTexStorage2D(target, levels, storage_fmt, size, size);
93 allocated |= (64<<levels)-1;
97 for(unsigned i=0; i<6; ++i)
98 image(enumerate_faces(i), level, 0);
102 void TextureCube::image(TextureCubeFace face, unsigned level, const void *data)
105 throw invalid_operation("TextureCube::image");
107 unsigned s = get_level_size(level);
109 throw out_of_range("TextureCube::image");
111 if(ARB_texture_storage)
112 return sub_image(face, level, 0, 0, s, s, data);
114 BindRestore _bind(this);
118 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
122 PixelComponents comp = get_components(storage_fmt);
123 GLenum type = get_gl_type(get_component_type(storage_fmt));
124 glTexImage2D(face, level, storage_fmt, s, s, 0, comp, type, data);
128 allocated |= 1<<get_face_index(face);
129 if((allocated&63)==63)
135 allocated |= (64<<levels)-1;
139 else if(!(allocated&(64<<level)))
141 for(unsigned i=0; i<6; ++i)
142 if(enumerate_faces(i)!=face)
143 glTexImage2D(enumerate_faces(i), level, storage_fmt, s, s, 0, comp, type, 0);
145 allocated |= 64<<level;
149 void TextureCube::image(TextureCubeFace face, unsigned level, PixelComponents comp, DataType type, const void *data)
151 if(comp!=get_components(format) || type!=get_component_type(format))
152 throw incompatible_data("TextureCube::image");
153 image(face, level, data);
156 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
159 throw invalid_operation("TextureCube::sub_image");
161 Conditional<BindRestore> _bind(!ARB_direct_state_acess, this);
164 PixelComponents comp = get_components(storage_fmt);
165 GLenum type = get_gl_type(get_component_type(storage_fmt));
166 if(ARB_direct_state_access)
167 glTextureSubImage3D(id, level, x, y, get_face_index(face), wd, ht, 1, comp, type, data);
169 glTexSubImage2D(face, level, x, y, wd, ht, comp, type, data);
171 if(auto_gen_mipmap && level==0)
175 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
177 if(comp!=get_components(format) || type!=get_component_type(format))
178 throw incompatible_data("TextureCube::subimage");
179 sub_image(face, level, x, y, wd, ht, data);
182 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
184 unsigned w = img.get_width();
185 unsigned h = img.get_height();
187 throw incompatible_data("TextureCube::image");
189 PixelFormat fmt = pixelformat_from_image(img);
190 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w);
192 PixelStore pstore = PixelStore::from_image(img);
193 BindRestore _bind_ps(pstore);
195 image(face, 0, img.get_pixels());
198 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool)
203 void TextureCube::image(const Graphics::Image &img, unsigned lv)
205 unsigned w = img.get_width();
206 unsigned h = img.get_height();
209 throw incompatible_data("TextureCube::image");
212 PixelFormat fmt = pixelformat_from_image(img);
214 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
215 else if(w!=size || h!=size)
216 throw incompatible_data("TextureCube::image");
218 PixelStore pstore = PixelStore::from_image(img);
219 BindRestore _bind_ps(pstore);
221 const char *pixels = reinterpret_cast<const char *>(img.get_pixels());
222 unsigned face_size = img.get_stride()*size;
223 for(unsigned i=0; i<6; ++i)
224 image(enumerate_faces(i), 0, pixels+i*face_size);
227 unsigned TextureCube::get_n_levels() const
230 for(unsigned s=size; s; s>>=1, ++n) ;
234 unsigned TextureCube::get_level_size(unsigned level) const
239 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
242 throw out_of_range("TextureCube::enumerate_faces");
243 return face_order[i];
246 unsigned TextureCube::get_face_index(TextureCubeFace face)
250 case POSITIVE_X: return 0;
251 case NEGATIVE_X: return 1;
252 case POSITIVE_Y: return 2;
253 case NEGATIVE_Y: return 3;
254 case POSITIVE_Z: return 4;
255 case NEGATIVE_Z: return 5;
256 default: throw invalid_argument("TextureCube::get_face_index");
260 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
262 return directions[get_face_index(face)];
265 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
267 return directions[orientations[get_face_index(face)*2]];
270 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
272 return directions[orientations[get_face_index(face)*2+1]];
275 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
277 float s = (u+0.5f)*2.0f/size-1.0f;
278 float t = (v+0.5f)*2.0f/size-1.0f;
279 const Vector3 &fv = get_face_direction(face);
280 const Vector3 &sv = get_s_direction(face);
281 const Vector3 &tv = get_t_direction(face);
285 UInt64 TextureCube::get_data_size() const
287 return id ? size*size*6*get_pixel_size(storage_fmt) : 0;
291 TextureCube::Loader::Loader(TextureCube &t):
292 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
297 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
298 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
303 void TextureCube::Loader::init()
305 add("external_image", &Loader::external_image);
306 add("image_data", &Loader::image_data);
307 add("raw_data", &Loader::raw_data);
308 add("storage", &Loader::storage);
309 add("storage", &Loader::storage_levels);
312 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
315 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
318 obj.image(face, img);
321 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
324 IO::Memory mem(data.data(), data.size());
327 obj.image(face, img);
330 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
332 obj.image(face, 0, data.data());
335 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
340 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
342 obj.storage(fmt, s, l);
346 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
348 const string &str = conv.get();
349 if(str=="POSITIVE_X")
351 else if(str=="NEGATIVE_X")
353 else if(str=="POSITIVE_Y")
355 else if(str=="NEGATIVE_Y")
357 else if(str=="POSITIVE_Z")
359 else if(str=="NEGATIVE_Z")
362 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));