1 #include <msp/gl/extensions/arb_texture_cube_map.h>
2 #include <msp/io/memory.h>
3 #include <msp/strings/format.h>
6 #include "pixelstore.h"
7 #include "texturecube.h"
14 Vector3 TextureCube::directions[6] =
24 TextureCube::TextureCube():
25 Texture(GL_TEXTURE_CUBE_MAP),
30 static Require _req(ARB_texture_cube_map);
33 void TextureCube::storage(PixelFormat fmt, unsigned sz)
36 throw invalid_operation("TextureCube::storage");
38 throw invalid_argument("TextureCube::storage");
39 require_pixelformat(fmt);
45 void TextureCube::allocate(unsigned level)
47 if(allocated&(1<<level))
50 PixelFormat base_fmt = get_base_pixelformat(ifmt);
51 DataType type = get_alloc_type(base_fmt);
52 for(unsigned i=0; i<6; ++i)
53 image(enumerate_faces(i), level, base_fmt, type, 0);
56 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
59 throw invalid_operation("TextureCube::image");
61 unsigned s = get_level_size(level);
63 throw out_of_range("TextureCube::image");
65 BindRestore _bind(this);
66 glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
68 // XXX Allocation should be tracked per-face, but we'll run out of bits
69 allocated |= 1<<level;
70 if(gen_mipmap && level==0)
72 // TODO Only do this once all faces are created
73 auto_generate_mipmap();
74 for(; s; s>>=1, ++level) ;
75 allocated |= (1<<level)-1;
79 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
81 unsigned w = img.get_width();
82 unsigned h = img.get_height();
83 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
87 throw incompatible_data("TextureCube::image");
89 PixelFormat f = storage_pixelformat_from_graphics(img.get_format());
91 f = get_srgb_pixelformat(f);
94 else if(w!=size || h!=size)
95 throw incompatible_data("TextureCube::image");
97 PixelStore pstore = PixelStore::from_image(img);
98 BindRestore _bind_ps(pstore);
100 image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
103 unsigned TextureCube::get_level_size(unsigned level)
108 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
112 case 0: return POSITIVE_X;
113 case 1: return NEGATIVE_X;
114 case 2: return POSITIVE_Y;
115 case 3: return NEGATIVE_Y;
116 case 4: return POSITIVE_Z;
117 case 5: return NEGATIVE_Z;
118 default: throw out_of_range("TextureCube::enumerate_faces");
122 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
126 case POSITIVE_X: return directions[0];
127 case NEGATIVE_X: return directions[1];
128 case POSITIVE_Y: return directions[2];
129 case NEGATIVE_Y: return directions[3];
130 case POSITIVE_Z: return directions[4];
131 case NEGATIVE_Z: return directions[5];
132 default: throw invalid_argument("TextureCube::get_face_direction");
136 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
140 case POSITIVE_X: return directions[5];
141 case NEGATIVE_X: return directions[4];
142 case POSITIVE_Y: return directions[0];
143 case NEGATIVE_Y: return directions[0];
144 case POSITIVE_Z: return directions[0];
145 case NEGATIVE_Z: return directions[1];
146 default: throw invalid_argument("TextureCube::get_s_direction");
150 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
154 case POSITIVE_X: return directions[3];
155 case NEGATIVE_X: return directions[3];
156 case POSITIVE_Y: return directions[4];
157 case NEGATIVE_Y: return directions[5];
158 case POSITIVE_Z: return directions[3];
159 case NEGATIVE_Z: return directions[3];
160 default: throw invalid_argument("TextureCube::get_t_direction");
164 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
166 float s = (u+0.5f)*2.0f/size-1.0f;
167 float t = (v+0.5f)*2.0f/size-1.0f;
168 const Vector3 &fv = get_face_direction(face);
169 const Vector3 &sv = get_s_direction(face);
170 const Vector3 &tv = get_t_direction(face);
174 UInt64 TextureCube::get_data_size() const
176 return id ? size*size*6*get_pixel_size(ifmt) : 0;
180 TextureCube::Loader::Loader(TextureCube &t):
181 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
186 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
187 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
192 void TextureCube::Loader::init()
194 add("image_data", &Loader::image_data);
195 add("raw_data", &Loader::raw_data);
196 add("storage", &Loader::storage);
199 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
202 IO::Memory mem(data.data(), data.size());
205 obj.image(face, img, srgb);
208 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
210 obj.image(face, 0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
213 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
216 fmt = get_srgb_pixelformat(fmt);
221 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
223 const string &str = conv.get();
224 if(str=="POSITIVE_X")
226 else if(str=="NEGATIVE_X")
228 else if(str=="POSITIVE_Y")
230 else if(str=="NEGATIVE_Y")
232 else if(str=="POSITIVE_Z")
234 else if(str=="NEGATIVE_Z")
237 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));