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