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>
8 #include "pixelstore.h"
9 #include "texturecube.h"
16 TextureCubeFace TextureCube::face_order[6] =
26 Vector3 TextureCube::directions[6] =
36 unsigned TextureCube::orientations[12] =
46 TextureCube::TextureCube():
47 Texture(GL_TEXTURE_CUBE_MAP),
51 static Require _req(ARB_texture_cube_map);
54 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
57 throw invalid_operation("TextureCube::storage");
59 throw invalid_argument("TextureCube::storage");
63 levels = get_n_levels();
65 levels = min(levels, lv);
68 void TextureCube::allocate(unsigned level)
71 throw invalid_operation("TextureCube::allocate");
73 throw invalid_argument("TextureCube::allocate");
74 if(allocated&(64<<level))
77 if(ARB_texture_storage)
79 BindRestore _bind(this);
80 glTexStorage2D(target, levels, storage_fmt, size, size);
82 allocated |= (1<<levels)-1;
86 for(unsigned i=0; i<6; ++i)
87 image(enumerate_faces(i), level, 0);
91 void TextureCube::image(TextureCubeFace face, unsigned level, const void *data)
94 throw invalid_operation("TextureCube::image");
96 unsigned s = get_level_size(level);
98 throw out_of_range("TextureCube::image");
100 if(ARB_texture_storage)
101 return sub_image(face, level, 0, 0, s, s, data);
103 BindRestore _bind(this);
107 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
111 PixelComponents comp = get_components(storage_fmt);
112 DataType type = get_component_type(storage_fmt);
113 glTexImage2D(face, level, storage_fmt, s, s, 0, comp, type, data);
117 allocated |= 1<<get_face_index(face);
118 if((allocated&63)==63)
124 allocated |= (64<<levels)-1;
128 else if(!(allocated&(64<<level)))
130 for(unsigned i=0; i<6; ++i)
131 if(enumerate_faces(i)!=face)
132 glTexImage2D(enumerate_faces(i), level, storage_fmt, s, s, 0, comp, type, 0);
134 allocated |= 64<<level;
138 void TextureCube::image(TextureCubeFace face, unsigned level, PixelComponents comp, DataType type, const void *data)
140 if(comp!=get_components(format) || type!=get_component_type(format))
141 throw incompatible_data("TextureCube::image");
142 image(face, level, data);
145 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
148 throw invalid_operation("TextureCube::sub_image");
150 BindRestore _bind(this);
153 PixelComponents comp = get_components(storage_fmt);
154 DataType type = get_component_type(storage_fmt);
155 glTexSubImage2D(face, level, x, y, wd, ht, comp, type, data);
157 if(auto_gen_mipmap && level==0)
161 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
163 if(comp!=get_components(format) || type!=get_component_type(format))
164 throw incompatible_data("TextureCube::subimage");
165 sub_image(face, level, x, y, wd, ht, data);
168 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
170 unsigned w = img.get_width();
171 unsigned h = img.get_height();
172 PixelFormat fmt = pixelformat_from_image(img);
176 throw incompatible_data("TextureCube::image");
178 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), srgb), w);
180 else if(w!=size || h!=size)
181 throw incompatible_data("TextureCube::image");
183 PixelStore pstore = PixelStore::from_image(img);
184 BindRestore _bind_ps(pstore);
186 image(face, 0, img.get_data());
189 void TextureCube::image(const Graphics::Image &img, unsigned lv, bool srgb)
191 unsigned w = img.get_width();
192 unsigned h = img.get_height();
195 throw incompatible_data("TextureCube::image");
198 PixelFormat fmt = pixelformat_from_image(img);
200 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), srgb), w, lv);
201 else if(w!=size || h!=size)
202 throw incompatible_data("TextureCube::image");
204 PixelStore pstore = PixelStore::from_image(img);
205 BindRestore _bind_ps(pstore);
207 const char *cdata = reinterpret_cast<const char *>(img.get_data());
208 unsigned face_size = img.get_stride()*size;
209 for(unsigned i=0; i<6; ++i)
210 image(enumerate_faces(i), 0, cdata+i*face_size);
213 unsigned TextureCube::get_n_levels() const
216 for(unsigned s=size; s; s>>=1, ++n) ;
220 unsigned TextureCube::get_level_size(unsigned level) const
225 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
228 throw out_of_range("TextureCube::enumerate_faces");
229 return face_order[i];
232 unsigned TextureCube::get_face_index(TextureCubeFace face)
236 case POSITIVE_X: return 0;
237 case NEGATIVE_X: return 1;
238 case POSITIVE_Y: return 2;
239 case NEGATIVE_Y: return 3;
240 case POSITIVE_Z: return 4;
241 case NEGATIVE_Z: return 5;
242 default: throw invalid_argument("TextureCube::get_face_index");
246 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
248 return directions[get_face_index(face)];
251 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
253 return directions[orientations[get_face_index(face)*2]];
256 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
258 return directions[orientations[get_face_index(face)*2+1]];
261 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
263 float s = (u+0.5f)*2.0f/size-1.0f;
264 float t = (v+0.5f)*2.0f/size-1.0f;
265 const Vector3 &fv = get_face_direction(face);
266 const Vector3 &sv = get_s_direction(face);
267 const Vector3 &tv = get_t_direction(face);
271 UInt64 TextureCube::get_data_size() const
273 return id ? size*size*6*get_pixel_size(storage_fmt) : 0;
277 TextureCube::Loader::Loader(TextureCube &t):
278 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
283 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
284 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
289 void TextureCube::Loader::init()
291 add("external_image", &Loader::external_image);
292 add("image_data", &Loader::image_data);
293 add("raw_data", &Loader::raw_data);
294 add("storage", &Loader::storage);
295 add("storage", &Loader::storage_levels);
298 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
301 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
304 obj.image(face, img, srgb);
307 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
310 IO::Memory mem(data.data(), data.size());
313 obj.image(face, img, srgb);
316 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
318 obj.image(face, 0, data.data());
321 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
326 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
328 obj.storage(fmt, s, l);
332 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
334 const string &str = conv.get();
335 if(str=="POSITIVE_X")
337 else if(str=="NEGATIVE_X")
339 else if(str=="POSITIVE_Y")
341 else if(str=="NEGATIVE_Y")
343 else if(str=="POSITIVE_Z")
345 else if(str=="NEGATIVE_Z")
348 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));