]> git.tdb.fi Git - libs/gl.git/blob - source/texturecube.cpp
Add some utility functions for textures
[libs/gl.git] / source / texturecube.cpp
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>
5 #include "bindable.h"
6 #include "error.h"
7 #include "pixelstore.h"
8 #include "texturecube.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 Vector3 TextureCube::directions[6] =
16 {
17         Vector3(1, 0, 0),
18         Vector3(-1, 0, 0),
19         Vector3(0, 1, 0),
20         Vector3(0, -1, 0),
21         Vector3(0, 0, 1),
22         Vector3(0, 0, -1)
23 };
24
25 TextureCube::TextureCube():
26         Texture(GL_TEXTURE_CUBE_MAP),
27         ifmt(RGB),
28         size(0),
29         allocated(0)
30 {
31         static Require _req(ARB_texture_cube_map);
32 }
33
34 void TextureCube::storage(PixelFormat fmt, unsigned sz)
35 {
36         if(size>0)
37                 throw invalid_operation("TextureCube::storage");
38         if(sz==0)
39                 throw invalid_argument("TextureCube::storage");
40
41         if(MSP_sized_internal_formats)
42                 fmt = get_sized_pixelformat(fmt);
43         require_pixelformat(fmt);
44
45         ifmt = fmt;
46         size = sz;
47 }
48
49 void TextureCube::allocate(unsigned level)
50 {
51         if(allocated&(1<<level))
52                 return;
53
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);
58 }
59
60 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
61 {
62         if(size==0)
63                 throw invalid_operation("TextureCube::image");
64
65         unsigned s = get_level_size(level);
66         if(s==0)
67                 throw out_of_range("TextureCube::image");
68
69         BindRestore _bind(this);
70         glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
71
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)
75         {
76                 // TODO Only do this once all faces are created
77                 auto_generate_mipmap();
78                 allocated |= (1<<get_n_levels())-1;
79         }
80 }
81
82 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
83 {
84         if(size==0)
85                 throw invalid_operation("TextureCube::sub_image");
86
87         allocate(level);
88
89         BindRestore _bind(this);
90         glTexSubImage2D(face, level, x, y, wd, ht, fmt, type, data);
91 }
92
93 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
94 {
95         unsigned w = img.get_width();
96         unsigned h = img.get_height();
97         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
98         if(size==0)
99         {
100                 if(w!=h)
101                         throw incompatible_data("TextureCube::image");
102
103                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
104         }
105         else if(w!=size || h!=size)
106                 throw incompatible_data("TextureCube::image");
107
108         PixelStore pstore = PixelStore::from_image(img);
109         BindRestore _bind_ps(pstore);
110
111         image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
112 }
113
114 void TextureCube::image(const Graphics::Image &img, bool srgb)
115 {
116         unsigned w = img.get_width();
117         unsigned h = img.get_height();
118
119         if(h!=w*6)
120                 throw incompatible_data("TextureCube::image");
121         h /= 6;
122
123         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
124         if(size==0)
125                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
126         else if(w!=size || h!=size)
127                 throw incompatible_data("TextureCube::image");
128
129         PixelStore pstore = PixelStore::from_image(img);
130         BindRestore _bind_ps(pstore);
131
132         const char *cdata = reinterpret_cast<const char *>(img.get_data());
133         unsigned face_size = img.get_stride()*size;
134         for(unsigned i=0; i<6; ++i)
135                 image(enumerate_faces(i), 0, fmt, UNSIGNED_BYTE, cdata+i*face_size);
136 }
137
138 unsigned TextureCube::get_n_levels() const
139 {
140         unsigned n = 0;
141         for(unsigned s=size; s; s>>=1, ++n) ;
142         return n;
143 }
144
145 unsigned TextureCube::get_level_size(unsigned level) const
146 {
147         return size>>level;
148 }
149
150 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
151 {
152         switch(i)
153         {
154         case 0: return POSITIVE_X;
155         case 1: return NEGATIVE_X;
156         case 2: return POSITIVE_Y;
157         case 3: return NEGATIVE_Y;
158         case 4: return POSITIVE_Z;
159         case 5: return NEGATIVE_Z;
160         default: throw out_of_range("TextureCube::enumerate_faces");
161         }
162 }
163
164 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
165 {
166         switch(face)
167         {
168         case POSITIVE_X: return directions[0];
169         case NEGATIVE_X: return directions[1];
170         case POSITIVE_Y: return directions[2];
171         case NEGATIVE_Y: return directions[3];
172         case POSITIVE_Z: return directions[4];
173         case NEGATIVE_Z: return directions[5];
174         default: throw invalid_argument("TextureCube::get_face_direction");
175         }
176 }
177
178 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
179 {
180         switch(face)
181         {
182         case POSITIVE_X: return directions[5];
183         case NEGATIVE_X: return directions[4];
184         case POSITIVE_Y: return directions[0];
185         case NEGATIVE_Y: return directions[0];
186         case POSITIVE_Z: return directions[0];
187         case NEGATIVE_Z: return directions[1];
188         default: throw invalid_argument("TextureCube::get_s_direction");
189         }
190 }
191
192 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
193 {
194         switch(face)
195         {
196         case POSITIVE_X: return directions[3];
197         case NEGATIVE_X: return directions[3];
198         case POSITIVE_Y: return directions[4];
199         case NEGATIVE_Y: return directions[5];
200         case POSITIVE_Z: return directions[3];
201         case NEGATIVE_Z: return directions[3];
202         default: throw invalid_argument("TextureCube::get_t_direction");
203         }
204 }
205
206 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
207 {
208         float s = (u+0.5f)*2.0f/size-1.0f;
209         float t = (v+0.5f)*2.0f/size-1.0f;
210         const Vector3 &fv = get_face_direction(face);
211         const Vector3 &sv = get_s_direction(face);
212         const Vector3 &tv = get_t_direction(face);
213         return fv+s*sv+t*tv;
214 }
215
216 UInt64 TextureCube::get_data_size() const
217 {
218         return id ? size*size*6*get_pixel_size(ifmt) : 0;
219 }
220
221
222 TextureCube::Loader::Loader(TextureCube &t):
223         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
224 {
225         init();
226 }
227
228 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
229         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
230 {
231         init();
232 }
233
234 void TextureCube::Loader::init()
235 {
236         add("external_image", &Loader::external_image);
237         add("image_data", &Loader::image_data);
238         add("raw_data", &Loader::raw_data);
239         add("storage", &Loader::storage);
240 }
241
242 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
243 {
244         Graphics::Image img;
245         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
246         img.load_io(*io);
247
248         obj.image(face, img, srgb);
249 }
250
251 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
252 {
253         Graphics::Image img;
254         IO::Memory mem(data.data(), data.size());
255         img.load_io(mem);
256
257         obj.image(face, img, srgb);
258 }
259
260 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
261 {
262         obj.image(face, 0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
263 }
264
265 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
266 {
267         if(srgb)
268                 fmt = get_srgb_pixelformat(fmt);
269         obj.storage(fmt, s);
270 }
271
272
273 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
274 {
275         const string &str = conv.get();
276         if(str=="POSITIVE_X")
277                 face = POSITIVE_X;
278         else if(str=="NEGATIVE_X")
279                 face = NEGATIVE_X;
280         else if(str=="POSITIVE_Y")
281                 face = POSITIVE_Y;
282         else if(str=="NEGATIVE_Y")
283                 face = NEGATIVE_Y;
284         else if(str=="POSITIVE_Z")
285                 face = POSITIVE_Z;
286         else if(str=="NEGATIVE_Z")
287                 face = NEGATIVE_Z;
288         else
289                 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));
290 }
291
292 } // namespace GL
293 } // namespace Msp