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 for(unsigned i=0; i<6; ++i)
51 image(enumerate_faces(i), level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
54 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
57 throw invalid_operation("TextureCube::image");
59 unsigned s = get_level_size(level);
61 throw out_of_range("TextureCube::image");
63 BindRestore _bind(this);
64 glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
66 // XXX Allocation should be tracked per-face, but we'll run out of bits
67 allocated |= 1<<level;
68 if(gen_mipmap && level==0)
70 for(; s; s>>=1, ++level) ;
71 allocated |= (1<<level)-1;
75 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
77 unsigned w = img.get_width();
78 unsigned h = img.get_height();
79 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
83 throw incompatible_data("TextureCube::image");
85 PixelFormat f = storage_pixelformat_from_graphics(img.get_format());
87 f = get_srgb_pixelformat(f);
90 else if(w!=size || h!=size)
91 throw incompatible_data("TextureCube::image");
93 PixelStore pstore = PixelStore::from_image(img);
94 BindRestore _bind_ps(pstore);
96 image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
99 unsigned TextureCube::get_level_size(unsigned level)
104 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
108 case 0: return POSITIVE_X;
109 case 1: return NEGATIVE_X;
110 case 2: return POSITIVE_Y;
111 case 3: return NEGATIVE_Y;
112 case 4: return POSITIVE_Z;
113 case 5: return NEGATIVE_Z;
114 default: throw out_of_range("TextureCube::enumerate_faces");
118 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
122 case POSITIVE_X: return directions[0];
123 case NEGATIVE_X: return directions[1];
124 case POSITIVE_Y: return directions[2];
125 case NEGATIVE_Y: return directions[3];
126 case POSITIVE_Z: return directions[4];
127 case NEGATIVE_Z: return directions[5];
128 default: throw invalid_argument("TextureCube::get_face_direction");
132 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
136 case POSITIVE_X: return directions[5];
137 case NEGATIVE_X: return directions[4];
138 case POSITIVE_Y: return directions[0];
139 case NEGATIVE_Y: return directions[0];
140 case POSITIVE_Z: return directions[0];
141 case NEGATIVE_Z: return directions[1];
142 default: throw invalid_argument("TextureCube::get_s_direction");
146 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
150 case POSITIVE_X: return directions[3];
151 case NEGATIVE_X: return directions[3];
152 case POSITIVE_Y: return directions[4];
153 case NEGATIVE_Y: return directions[5];
154 case POSITIVE_Z: return directions[3];
155 case NEGATIVE_Z: return directions[3];
156 default: throw invalid_argument("TextureCube::get_t_direction");
160 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
162 float s = (u+0.5f)*2.0f/size-1.0f;
163 float t = (v+0.5f)*2.0f/size-1.0f;
164 const Vector3 &fv = get_face_direction(face);
165 const Vector3 &sv = get_s_direction(face);
166 const Vector3 &tv = get_t_direction(face);
170 UInt64 TextureCube::get_data_size() const
172 return id ? size*size*6*get_pixel_size(ifmt) : 0;
176 TextureCube::Loader::Loader(TextureCube &t):
177 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
182 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
183 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
188 void TextureCube::Loader::init()
190 add("image_data", &Loader::image_data);
191 add("raw_data", &Loader::raw_data);
192 add("storage", &Loader::storage);
195 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
198 IO::Memory mem(data.data(), data.size());
201 obj.image(face, img, srgb);
204 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
206 obj.image(face, 0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
209 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
212 fmt = get_srgb_pixelformat(fmt);
217 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
219 const string &str = conv.get();
220 if(str=="POSITIVE_X")
222 else if(str=="NEGATIVE_X")
224 else if(str=="POSITIVE_Y")
226 else if(str=="NEGATIVE_Y")
228 else if(str=="POSITIVE_Z")
230 else if(str=="NEGATIVE_Z")
233 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));