1 #include <msp/gl/extensions/arb_texture_cube_map.h>
2 #include <msp/io/memory.h>
3 #include <msp/strings/format.h>
6 #include "texturecube.h"
13 Vector3 TextureCube::directions[6] =
23 TextureCube::TextureCube():
24 Texture(GL_TEXTURE_CUBE_MAP),
28 static Require _req(ARB_texture_cube_map);
31 void TextureCube::storage(PixelFormat fmt, unsigned sz)
34 throw invalid_operation("TextureCube::storage");
36 throw invalid_argument("TextureCube::storage");
37 require_pixelformat(fmt);
43 void TextureCube::allocate(unsigned level)
45 if(allocated&(1<<level))
48 for(unsigned i=0; i<6; ++i)
49 image(enumerate_faces(i), level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
52 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
55 throw invalid_operation("TextureCube::image");
57 unsigned s = get_level_size(level);
59 throw out_of_range("TextureCube::image");
61 Bind _bind(this, true);
62 glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
64 // XXX Allocation should be tracked per-face, but we'll run out of bits
65 allocated |= 1<<level;
66 if(gen_mipmap && level==0)
68 for(; s; s>>=1, ++level) ;
69 allocated |= (1<<level)-1;
73 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
75 unsigned w = img.get_width();
76 unsigned h = img.get_height();
77 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
83 throw incompatible_data("TextureCube::image");
85 else if(w!=size || h!=size)
86 throw incompatible_data("TextureCube::image");
88 image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
91 unsigned TextureCube::get_level_size(unsigned level)
96 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
100 case 0: return POSITIVE_X;
101 case 1: return NEGATIVE_X;
102 case 2: return POSITIVE_Y;
103 case 3: return NEGATIVE_Y;
104 case 4: return POSITIVE_Z;
105 case 5: return NEGATIVE_Z;
106 default: throw out_of_range("TextureCube::enumerate_faces");
110 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
114 case POSITIVE_X: return directions[0];
115 case NEGATIVE_X: return directions[1];
116 case POSITIVE_Y: return directions[2];
117 case NEGATIVE_Y: return directions[3];
118 case POSITIVE_Z: return directions[4];
119 case NEGATIVE_Z: return directions[5];
120 default: throw invalid_argument("TextureCube::get_face_direction");
124 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
128 case POSITIVE_X: return directions[5];
129 case NEGATIVE_X: return directions[4];
130 case POSITIVE_Y: return directions[0];
131 case NEGATIVE_Y: return directions[0];
132 case POSITIVE_Z: return directions[0];
133 case NEGATIVE_Z: return directions[1];
134 default: throw invalid_argument("TextureCube::get_s_direction");
138 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
142 case POSITIVE_X: return directions[3];
143 case NEGATIVE_X: return directions[3];
144 case POSITIVE_Y: return directions[4];
145 case NEGATIVE_Y: return directions[5];
146 case POSITIVE_Z: return directions[3];
147 case NEGATIVE_Z: return directions[3];
148 default: throw invalid_argument("TextureCube::get_t_direction");
152 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
154 float s = (u+0.5f)*2.0f/size-1.0f;
155 float t = (v+0.5f)*2.0f/size-1.0f;
156 const Vector3 &fv = get_face_direction(face);
157 const Vector3 &sv = get_s_direction(face);
158 const Vector3 &tv = get_t_direction(face);
163 TextureCube::Loader::Loader(TextureCube &t):
164 DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
166 add("image_data", &Loader::image_data);
167 add("raw_data", &Loader::raw_data);
168 add("storage", &Loader::storage);
171 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
174 IO::Memory mem(data.data(), data.size());
177 obj.image(face, img);
180 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
182 obj.image(face, 0, obj.ifmt, UNSIGNED_BYTE, data.data());
185 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
191 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
193 const string &str = conv.get();
194 if(str=="POSITIVE_X")
196 else if(str=="NEGATIVE_X")
198 else if(str=="POSITIVE_Y")
200 else if(str=="NEGATIVE_Y")
202 else if(str=="POSITIVE_Z")
204 else if(str=="NEGATIVE_Z")
207 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));