]> git.tdb.fi Git - libs/gl.git/blob - source/texturecube.cpp
Add a datafile statement to use an external image for a texture
[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         require_pixelformat(fmt);
41
42         ifmt = fmt;
43         size = sz;
44 }
45
46 void TextureCube::allocate(unsigned level)
47 {
48         if(allocated&(1<<level))
49                 return;
50
51         PixelFormat base_fmt = get_base_pixelformat(ifmt);
52         DataType type = get_alloc_type(base_fmt);
53         for(unsigned i=0; i<6; ++i)
54                 image(enumerate_faces(i), level, base_fmt, type, 0);
55 }
56
57 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
58 {
59         if(size==0)
60                 throw invalid_operation("TextureCube::image");
61
62         unsigned s = get_level_size(level);
63         if(s==0)
64                 throw out_of_range("TextureCube::image");
65
66         BindRestore _bind(this);
67         glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
68
69         // XXX Allocation should be tracked per-face, but we'll run out of bits
70         allocated |= 1<<level;
71         if(gen_mipmap && level==0)
72         {
73                 // TODO Only do this once all faces are created
74                 auto_generate_mipmap();
75                 for(; s; s>>=1, ++level) ;
76                 allocated |= (1<<level)-1;
77         }
78 }
79
80 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
81 {
82         unsigned w = img.get_width();
83         unsigned h = img.get_height();
84         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
85         if(size==0)
86         {
87                 if(w!=h)
88                         throw incompatible_data("TextureCube::image");
89
90                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
91         }
92         else if(w!=size || h!=size)
93                 throw incompatible_data("TextureCube::image");
94
95         PixelStore pstore = PixelStore::from_image(img);
96         BindRestore _bind_ps(pstore);
97
98         image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
99 }
100
101 void TextureCube::image(const Graphics::Image &img, bool srgb)
102 {
103         unsigned w = img.get_width();
104         unsigned h = img.get_height();
105
106         if(h!=w*6)
107                 throw incompatible_data("TextureCube::image");
108         h /= 6;
109
110         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
111         if(size==0)
112                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
113         else if(w!=size || h!=size)
114                 throw incompatible_data("TextureCube::image");
115
116         PixelStore pstore = PixelStore::from_image(img);
117         BindRestore _bind_ps(pstore);
118
119         const char *cdata = reinterpret_cast<const char *>(img.get_data());
120         unsigned face_size = img.get_stride()*size;
121         for(unsigned i=0; i<6; ++i)
122                 image(enumerate_faces(i), 0, fmt, UNSIGNED_BYTE, cdata+i*face_size);
123 }
124
125 unsigned TextureCube::get_level_size(unsigned level)
126 {
127         return size>>level;
128 }
129
130 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
131 {
132         switch(i)
133         {
134         case 0: return POSITIVE_X;
135         case 1: return NEGATIVE_X;
136         case 2: return POSITIVE_Y;
137         case 3: return NEGATIVE_Y;
138         case 4: return POSITIVE_Z;
139         case 5: return NEGATIVE_Z;
140         default: throw out_of_range("TextureCube::enumerate_faces");
141         }
142 }
143
144 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
145 {
146         switch(face)
147         {
148         case POSITIVE_X: return directions[0];
149         case NEGATIVE_X: return directions[1];
150         case POSITIVE_Y: return directions[2];
151         case NEGATIVE_Y: return directions[3];
152         case POSITIVE_Z: return directions[4];
153         case NEGATIVE_Z: return directions[5];
154         default: throw invalid_argument("TextureCube::get_face_direction");
155         }
156 }
157
158 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
159 {
160         switch(face)
161         {
162         case POSITIVE_X: return directions[5];
163         case NEGATIVE_X: return directions[4];
164         case POSITIVE_Y: return directions[0];
165         case NEGATIVE_Y: return directions[0];
166         case POSITIVE_Z: return directions[0];
167         case NEGATIVE_Z: return directions[1];
168         default: throw invalid_argument("TextureCube::get_s_direction");
169         }
170 }
171
172 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
173 {
174         switch(face)
175         {
176         case POSITIVE_X: return directions[3];
177         case NEGATIVE_X: return directions[3];
178         case POSITIVE_Y: return directions[4];
179         case NEGATIVE_Y: return directions[5];
180         case POSITIVE_Z: return directions[3];
181         case NEGATIVE_Z: return directions[3];
182         default: throw invalid_argument("TextureCube::get_t_direction");
183         }
184 }
185
186 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
187 {
188         float s = (u+0.5f)*2.0f/size-1.0f;
189         float t = (v+0.5f)*2.0f/size-1.0f;
190         const Vector3 &fv = get_face_direction(face);
191         const Vector3 &sv = get_s_direction(face);
192         const Vector3 &tv = get_t_direction(face);
193         return fv+s*sv+t*tv;
194 }
195
196 UInt64 TextureCube::get_data_size() const
197 {
198         return id ? size*size*6*get_pixel_size(ifmt) : 0;
199 }
200
201
202 TextureCube::Loader::Loader(TextureCube &t):
203         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
204 {
205         init();
206 }
207
208 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
209         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
210 {
211         init();
212 }
213
214 void TextureCube::Loader::init()
215 {
216         add("external_image", &Loader::external_image);
217         add("image_data", &Loader::image_data);
218         add("raw_data", &Loader::raw_data);
219         add("storage", &Loader::storage);
220 }
221
222 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
223 {
224         Graphics::Image img;
225         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
226         img.load_io(*io);
227
228         obj.image(face, img, srgb);
229 }
230
231 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
232 {
233         Graphics::Image img;
234         IO::Memory mem(data.data(), data.size());
235         img.load_io(mem);
236
237         obj.image(face, img, srgb);
238 }
239
240 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
241 {
242         obj.image(face, 0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
243 }
244
245 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
246 {
247         if(srgb)
248                 fmt = get_srgb_pixelformat(fmt);
249         obj.storage(fmt, s);
250 }
251
252
253 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
254 {
255         const string &str = conv.get();
256         if(str=="POSITIVE_X")
257                 face = POSITIVE_X;
258         else if(str=="NEGATIVE_X")
259                 face = NEGATIVE_X;
260         else if(str=="POSITIVE_Y")
261                 face = POSITIVE_Y;
262         else if(str=="NEGATIVE_Y")
263                 face = NEGATIVE_Y;
264         else if(str=="POSITIVE_Z")
265                 face = POSITIVE_Z;
266         else if(str=="NEGATIVE_Z")
267                 face = NEGATIVE_Z;
268         else
269                 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));
270 }
271
272 } // namespace GL
273 } // namespace Msp