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>
9 #include "texturecube.h"
16 const Vector3 TextureCube::directions[6] =
26 const unsigned TextureCube::orientations[12] =
36 TextureCube::TextureCube():
37 Texture(GL_TEXTURE_CUBE_MAP),
40 static Require _req(ARB_texture_cube_map);
41 if(ARB_seamless_cube_map)
43 static bool seamless_init = false;
46 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
52 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
56 if(fmt!=format || sz!=size || (lv && lv!=levels))
57 throw incompatible_data("TextureCube::storage");
61 throw invalid_argument("TextureCube::storage");
65 levels = get_n_levels();
67 levels = min(levels, lv);
69 GLenum gl_fmt = get_gl_pixelformat(storage_fmt);
70 if(ARB_texture_storage)
72 if(ARB_direct_state_access)
73 glTextureStorage2D(id, levels, gl_fmt, size, size);
77 glTexStorage2D(target, levels, gl_fmt, size, size);
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)
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);
91 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
97 void TextureCube::image(TextureCubeFace face, unsigned level, const void *data)
99 unsigned lsz = get_level_size(level);
100 return sub_image(face, level, 0, 0, lsz, lsz, data);
103 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
106 throw invalid_operation("TextureCube::sub_image");
108 throw out_of_range("TextureCube::sub_image");
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);
118 glTexSubImage2D(gl_face, level, x, y, wd, ht, comp, type, data);
122 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
124 unsigned w = img.get_width();
125 unsigned h = img.get_height();
127 throw incompatible_data("TextureCube::image");
129 storage(pixelformat_from_image(img, use_srgb_format), w);
130 image(face, 0, img.get_pixels());
133 void TextureCube::image(const Graphics::Image &img, unsigned lv)
135 unsigned w = img.get_width();
136 unsigned h = img.get_height();
139 throw incompatible_data("TextureCube::image");
143 storage(pixelformat_from_image(img, use_srgb_format), w, lv);
144 else if(w!=size || h!=size)
145 throw incompatible_data("TextureCube::image");
147 const char *pixels = reinterpret_cast<const char *>(img.get_pixels());
148 unsigned face_size = img.get_stride()*size;
149 for(unsigned i=0; i<6; ++i)
150 image(static_cast<TextureCubeFace>(i), 0, pixels+i*face_size);
153 unsigned TextureCube::get_n_levels() const
156 for(unsigned s=size; s; s>>=1, ++n) ;
160 unsigned TextureCube::get_level_size(unsigned level) const
165 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
167 return directions[face];
170 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
172 return directions[orientations[face*2]];
175 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
177 return directions[orientations[face*2+1]];
180 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
182 float s = (u+0.5f)*2.0f/size-1.0f;
183 float t = (v+0.5f)*2.0f/size-1.0f;
184 const Vector3 &fv = get_face_direction(face);
185 const Vector3 &sv = get_s_direction(face);
186 const Vector3 &tv = get_t_direction(face);
190 uint64_t TextureCube::get_data_size() const
192 return id ? size*size*6*get_pixel_size(storage_fmt) : 0;
196 TextureCube::Loader::Loader(TextureCube &t):
197 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
202 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
203 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
208 void TextureCube::Loader::init()
210 add("external_image", &Loader::external_image);
211 add("image_data", &Loader::image_data);
212 add("raw_data", &Loader::raw_data);
213 add("storage", &Loader::storage);
214 add("storage", &Loader::storage_levels);
217 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
220 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
223 obj.image(face, img);
226 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
229 IO::Memory mem(data.data(), data.size());
232 obj.image(face, img);
235 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
237 obj.image(face, 0, data.data());
240 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
245 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
247 obj.storage(fmt, s, l);
251 unsigned get_gl_cube_face(TextureCubeFace face)
255 case POSITIVE_X: return GL_TEXTURE_CUBE_MAP_POSITIVE_X;
256 case NEGATIVE_X: return GL_TEXTURE_CUBE_MAP_NEGATIVE_X;
257 case POSITIVE_Y: return GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
258 case NEGATIVE_Y: return GL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
259 case POSITIVE_Z: return GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
260 case NEGATIVE_Z: return GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
261 default: throw invalid_argument("get_gl_cube_face");
265 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
267 const string &str = conv.get();
268 if(str=="POSITIVE_X")
270 else if(str=="NEGATIVE_X")
272 else if(str=="POSITIVE_Y")
274 else if(str=="NEGATIVE_Y")
276 else if(str=="POSITIVE_Z")
278 else if(str=="NEGATIVE_Z")
281 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));