1 #include <msp/datafile/collection.h>
2 #include <msp/gl/extensions/arb_texture_cube_map.h>
3 #include <msp/io/memory.h>
4 #include <msp/strings/format.h>
7 #include "pixelstore.h"
8 #include "texturecube.h"
15 Vector3 TextureCube::directions[6] =
25 TextureCube::TextureCube():
26 Texture(GL_TEXTURE_CUBE_MAP),
31 static Require _req(ARB_texture_cube_map);
34 void TextureCube::storage(PixelFormat fmt, unsigned sz)
37 throw invalid_operation("TextureCube::storage");
39 throw invalid_argument("TextureCube::storage");
41 if(MSP_sized_internal_formats)
42 fmt = get_sized_pixelformat(fmt);
43 require_pixelformat(fmt);
49 void TextureCube::allocate(unsigned level)
51 if(allocated&(1<<level))
54 PixelFormat base_fmt = get_base_pixelformat(ifmt);
55 DataType type = get_alloc_type(base_fmt);
56 for(unsigned i=0; i<6; ++i)
57 image(enumerate_faces(i), level, base_fmt, type, 0);
60 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
63 throw invalid_operation("TextureCube::image");
65 unsigned s = get_level_size(level);
67 throw out_of_range("TextureCube::image");
69 BindRestore _bind(this);
70 glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
72 // XXX Allocation should be tracked per-face, but we'll run out of bits
73 allocated |= 1<<level;
74 if(gen_mipmap && level==0)
76 // TODO Only do this once all faces are created
77 auto_generate_mipmap();
78 for(; s; s>>=1, ++level) ;
79 allocated |= (1<<level)-1;
83 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
85 unsigned w = img.get_width();
86 unsigned h = img.get_height();
87 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
91 throw incompatible_data("TextureCube::image");
93 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
95 else if(w!=size || h!=size)
96 throw incompatible_data("TextureCube::image");
98 PixelStore pstore = PixelStore::from_image(img);
99 BindRestore _bind_ps(pstore);
101 image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
104 void TextureCube::image(const Graphics::Image &img, bool srgb)
106 unsigned w = img.get_width();
107 unsigned h = img.get_height();
110 throw incompatible_data("TextureCube::image");
113 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
115 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
116 else if(w!=size || h!=size)
117 throw incompatible_data("TextureCube::image");
119 PixelStore pstore = PixelStore::from_image(img);
120 BindRestore _bind_ps(pstore);
122 const char *cdata = reinterpret_cast<const char *>(img.get_data());
123 unsigned face_size = img.get_stride()*size;
124 for(unsigned i=0; i<6; ++i)
125 image(enumerate_faces(i), 0, fmt, UNSIGNED_BYTE, cdata+i*face_size);
128 unsigned TextureCube::get_level_size(unsigned level)
133 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
137 case 0: return POSITIVE_X;
138 case 1: return NEGATIVE_X;
139 case 2: return POSITIVE_Y;
140 case 3: return NEGATIVE_Y;
141 case 4: return POSITIVE_Z;
142 case 5: return NEGATIVE_Z;
143 default: throw out_of_range("TextureCube::enumerate_faces");
147 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
151 case POSITIVE_X: return directions[0];
152 case NEGATIVE_X: return directions[1];
153 case POSITIVE_Y: return directions[2];
154 case NEGATIVE_Y: return directions[3];
155 case POSITIVE_Z: return directions[4];
156 case NEGATIVE_Z: return directions[5];
157 default: throw invalid_argument("TextureCube::get_face_direction");
161 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
165 case POSITIVE_X: return directions[5];
166 case NEGATIVE_X: return directions[4];
167 case POSITIVE_Y: return directions[0];
168 case NEGATIVE_Y: return directions[0];
169 case POSITIVE_Z: return directions[0];
170 case NEGATIVE_Z: return directions[1];
171 default: throw invalid_argument("TextureCube::get_s_direction");
175 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
179 case POSITIVE_X: return directions[3];
180 case NEGATIVE_X: return directions[3];
181 case POSITIVE_Y: return directions[4];
182 case NEGATIVE_Y: return directions[5];
183 case POSITIVE_Z: return directions[3];
184 case NEGATIVE_Z: return directions[3];
185 default: throw invalid_argument("TextureCube::get_t_direction");
189 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
191 float s = (u+0.5f)*2.0f/size-1.0f;
192 float t = (v+0.5f)*2.0f/size-1.0f;
193 const Vector3 &fv = get_face_direction(face);
194 const Vector3 &sv = get_s_direction(face);
195 const Vector3 &tv = get_t_direction(face);
199 UInt64 TextureCube::get_data_size() const
201 return id ? size*size*6*get_pixel_size(ifmt) : 0;
205 TextureCube::Loader::Loader(TextureCube &t):
206 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
211 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
212 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
217 void TextureCube::Loader::init()
219 add("external_image", &Loader::external_image);
220 add("image_data", &Loader::image_data);
221 add("raw_data", &Loader::raw_data);
222 add("storage", &Loader::storage);
225 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
228 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
231 obj.image(face, img, srgb);
234 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
237 IO::Memory mem(data.data(), data.size());
240 obj.image(face, img, srgb);
243 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
245 obj.image(face, 0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
248 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
251 fmt = get_srgb_pixelformat(fmt);
256 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
258 const string &str = conv.get();
259 if(str=="POSITIVE_X")
261 else if(str=="NEGATIVE_X")
263 else if(str=="POSITIVE_Y")
265 else if(str=="NEGATIVE_Y")
267 else if(str=="POSITIVE_Z")
269 else if(str=="NEGATIVE_Z")
272 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));