1 #include <msp/datafile/collection.h>
2 #include <msp/io/memory.h>
3 #include <msp/strings/format.h>
5 #include "texturecube.h"
12 const Vector3 TextureCube::directions[6] =
22 const unsigned TextureCube::orientations[12] =
32 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
36 if(fmt!=format || sz!=size || (lv && lv!=levels))
37 throw incompatible_data("TextureCube::storage");
41 throw invalid_argument("TextureCube::storage");
45 levels = get_n_levels();
47 levels = min(levels, lv);
52 void TextureCube::image(unsigned level, const void *data)
54 const char *pixels = static_cast<const char *>(data);
55 unsigned face_size = size*size*get_pixel_size(storage_fmt);
56 for(unsigned i=0; i<6; ++i)
57 image(static_cast<TextureCubeFace>(i), level, pixels+i*face_size);
60 void TextureCube::image(TextureCubeFace face, unsigned level, const void *data)
62 unsigned lsz = get_level_size(level);
63 return sub_image(face, level, 0, 0, lsz, lsz, data);
66 void TextureCube::sub_image(TextureCubeFace face, unsigned level, unsigned x, unsigned y, unsigned wd, unsigned ht, const void *data)
69 throw invalid_operation("TextureCube::sub_image");
70 if(level>=levels || x>size || x+wd>size || y>size || y+ht>size)
71 throw out_of_range("TextureCube::sub_image");
73 TextureCubeBackend::sub_image(face, level, x, y, wd, ht, data);
76 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
78 unsigned w = img.get_width();
79 unsigned h = img.get_height();
81 throw incompatible_data("TextureCube::image");
83 storage(pixelformat_from_image(img, use_srgb_format), w);
84 image(face, 0, img.get_pixels());
87 void TextureCube::image(const Graphics::Image &img, unsigned lv)
89 unsigned w = img.get_width();
90 unsigned h = img.get_height();
93 throw incompatible_data("TextureCube::image");
97 storage(pixelformat_from_image(img, use_srgb_format), w, lv);
98 else if(w!=size || h!=size)
99 throw incompatible_data("TextureCube::image");
101 image(0, img.get_pixels());
104 unsigned TextureCube::get_n_levels() const
107 for(unsigned s=size; s; s>>=1, ++n) ;
111 unsigned TextureCube::get_level_size(unsigned level) const
116 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
118 return directions[face];
121 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
123 return directions[orientations[face*2]];
126 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
128 return directions[orientations[face*2+1]];
131 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
133 float s = (u+0.5f)*2.0f/size-1.0f;
134 float t = (v+0.5f)*2.0f/size-1.0f;
135 const Vector3 &fv = get_face_direction(face);
136 const Vector3 &sv = get_s_direction(face);
137 const Vector3 &tv = get_t_direction(face);
142 TextureCube::Loader::Loader(TextureCube &t):
143 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
148 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
149 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
154 void TextureCube::Loader::init()
156 add("external_image", &Loader::external_image);
157 add("image_data", &Loader::image_data);
158 add("raw_data", &Loader::raw_data);
159 add("storage", &Loader::storage);
160 add("storage", &Loader::storage_levels);
163 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
166 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
169 obj.image(face, img);
172 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
175 IO::Memory mem(data.data(), data.size());
178 obj.image(face, img);
181 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
183 obj.image(face, 0, data.data());
186 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
191 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
193 obj.storage(fmt, s, l);
197 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
199 const string &str = conv.get();
200 if(str=="POSITIVE_X")
202 else if(str=="NEGATIVE_X")
204 else if(str=="POSITIVE_Y")
206 else if(str=="NEGATIVE_Y")
208 else if(str=="POSITIVE_Z")
210 else if(str=="NEGATIVE_Z")
213 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));