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");
61 set_internal_format(fmt);
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, ifmt, size, size);
82 allocated |= (1<<levels)-1;
86 PixelFormat base_fmt = get_base_pixelformat(ifmt);
87 DataType type = get_alloc_type(base_fmt);
88 for(unsigned i=0; i<6; ++i)
89 image(enumerate_faces(i), level, base_fmt, type, 0);
93 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
96 throw invalid_operation("TextureCube::image");
98 unsigned s = get_level_size(level);
100 throw out_of_range("TextureCube::image");
102 if(ARB_texture_storage)
103 return sub_image(face, level, 0, 0, s, s, fmt, type, data);
105 BindRestore _bind(this);
109 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
112 glTexImage2D(face, level, ifmt, s, s, 0, get_upload_format(fmt), type, data);
116 allocated |= 1<<get_face_index(face);
117 if((allocated&63)==63)
123 allocated |= (64<<levels)-1;
127 else if(!(allocated&(64<<level)))
129 for(unsigned i=0; i<6; ++i)
130 if(enumerate_faces(i)!=face)
131 glTexImage2D(enumerate_faces(i), level, ifmt, s, s, 0, get_upload_format(fmt), type, 0);
133 allocated |= 64<<level;
137 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
140 throw invalid_operation("TextureCube::sub_image");
142 BindRestore _bind(this);
145 glTexSubImage2D(face, level, x, y, wd, ht, get_upload_format(fmt), type, data);
147 if(auto_gen_mipmap && level==0)
151 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
153 unsigned w = img.get_width();
154 unsigned h = img.get_height();
155 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
159 throw incompatible_data("TextureCube::image");
161 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
163 else if(w!=size || h!=size)
164 throw incompatible_data("TextureCube::image");
166 PixelStore pstore = PixelStore::from_image(img);
167 BindRestore _bind_ps(pstore);
169 image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
172 void TextureCube::image(const Graphics::Image &img, unsigned lv, bool srgb)
174 unsigned w = img.get_width();
175 unsigned h = img.get_height();
178 throw incompatible_data("TextureCube::image");
181 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
183 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, lv);
184 else if(w!=size || h!=size)
185 throw incompatible_data("TextureCube::image");
187 PixelStore pstore = PixelStore::from_image(img);
188 BindRestore _bind_ps(pstore);
190 const char *cdata = reinterpret_cast<const char *>(img.get_data());
191 unsigned face_size = img.get_stride()*size;
192 for(unsigned i=0; i<6; ++i)
193 image(enumerate_faces(i), 0, fmt, UNSIGNED_BYTE, cdata+i*face_size);
196 unsigned TextureCube::get_n_levels() const
199 for(unsigned s=size; s; s>>=1, ++n) ;
203 unsigned TextureCube::get_level_size(unsigned level) const
208 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
211 throw out_of_range("TextureCube::enumerate_faces");
212 return face_order[i];
215 unsigned TextureCube::get_face_index(TextureCubeFace face)
219 case POSITIVE_X: return 0;
220 case NEGATIVE_X: return 1;
221 case POSITIVE_Y: return 2;
222 case NEGATIVE_Y: return 3;
223 case POSITIVE_Z: return 4;
224 case NEGATIVE_Z: return 5;
225 default: throw invalid_argument("TextureCube::get_face_index");
229 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
231 return directions[get_face_index(face)];
234 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
236 return directions[orientations[get_face_index(face)*2]];
239 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
241 return directions[orientations[get_face_index(face)*2+1]];
244 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
246 float s = (u+0.5f)*2.0f/size-1.0f;
247 float t = (v+0.5f)*2.0f/size-1.0f;
248 const Vector3 &fv = get_face_direction(face);
249 const Vector3 &sv = get_s_direction(face);
250 const Vector3 &tv = get_t_direction(face);
254 UInt64 TextureCube::get_data_size() const
256 return id ? size*size*6*get_pixel_size(ifmt) : 0;
260 TextureCube::Loader::Loader(TextureCube &t):
261 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
266 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
267 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
272 void TextureCube::Loader::init()
274 add("external_image", &Loader::external_image);
275 add("image_data", &Loader::image_data);
276 add("raw_data", &Loader::raw_data);
277 add("storage", &Loader::storage);
278 add("storage", &Loader::storage_levels);
281 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
284 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
287 obj.image(face, img, srgb);
290 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
293 IO::Memory mem(data.data(), data.size());
296 obj.image(face, img, srgb);
299 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
301 obj.image(face, 0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
304 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
309 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
311 obj.storage(fmt, s, l);
315 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
317 const string &str = conv.get();
318 if(str=="POSITIVE_X")
320 else if(str=="NEGATIVE_X")
322 else if(str=="POSITIVE_Y")
324 else if(str=="NEGATIVE_Y")
326 else if(str=="POSITIVE_Z")
328 else if(str=="NEGATIVE_Z")
331 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));