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