]> git.tdb.fi Git - libs/gl.git/blob - source/texturecube.cpp
Use explicit mipmap generation if necessary
[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         for(unsigned i=0; i<6; ++i)
51                 image(enumerate_faces(i), level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
52 }
53
54 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
55 {
56         if(size==0)
57                 throw invalid_operation("TextureCube::image");
58
59         unsigned s = get_level_size(level);
60         if(s==0)
61                 throw out_of_range("TextureCube::image");
62
63         BindRestore _bind(this);
64         glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
65
66         // XXX Allocation should be tracked per-face, but we'll run out of bits
67         allocated |= 1<<level;
68         if(gen_mipmap && level==0)
69         {
70                 // TODO Only do this once all faces are created
71                 auto_generate_mipmap();
72                 for(; s; s>>=1, ++level) ;
73                 allocated |= (1<<level)-1;
74         }
75 }
76
77 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
78 {
79         unsigned w = img.get_width();
80         unsigned h = img.get_height();
81         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
82         if(size==0)
83         {
84                 if(w!=h)
85                         throw incompatible_data("TextureCube::image");
86
87                 PixelFormat f = storage_pixelformat_from_graphics(img.get_format());
88                 if(srgb)
89                         f = get_srgb_pixelformat(f);
90                 storage(f, 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 unsigned TextureCube::get_level_size(unsigned level)
102 {
103         return size>>level;
104 }
105
106 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
107 {
108         switch(i)
109         {
110         case 0: return POSITIVE_X;
111         case 1: return NEGATIVE_X;
112         case 2: return POSITIVE_Y;
113         case 3: return NEGATIVE_Y;
114         case 4: return POSITIVE_Z;
115         case 5: return NEGATIVE_Z;
116         default: throw out_of_range("TextureCube::enumerate_faces");
117         }
118 }
119
120 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
121 {
122         switch(face)
123         {
124         case POSITIVE_X: return directions[0];
125         case NEGATIVE_X: return directions[1];
126         case POSITIVE_Y: return directions[2];
127         case NEGATIVE_Y: return directions[3];
128         case POSITIVE_Z: return directions[4];
129         case NEGATIVE_Z: return directions[5];
130         default: throw invalid_argument("TextureCube::get_face_direction");
131         }
132 }
133
134 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
135 {
136         switch(face)
137         {
138         case POSITIVE_X: return directions[5];
139         case NEGATIVE_X: return directions[4];
140         case POSITIVE_Y: return directions[0];
141         case NEGATIVE_Y: return directions[0];
142         case POSITIVE_Z: return directions[0];
143         case NEGATIVE_Z: return directions[1];
144         default: throw invalid_argument("TextureCube::get_s_direction");
145         }
146 }
147
148 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
149 {
150         switch(face)
151         {
152         case POSITIVE_X: return directions[3];
153         case NEGATIVE_X: return directions[3];
154         case POSITIVE_Y: return directions[4];
155         case NEGATIVE_Y: return directions[5];
156         case POSITIVE_Z: return directions[3];
157         case NEGATIVE_Z: return directions[3];
158         default: throw invalid_argument("TextureCube::get_t_direction");
159         }
160 }
161
162 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
163 {
164         float s = (u+0.5f)*2.0f/size-1.0f;
165         float t = (v+0.5f)*2.0f/size-1.0f;
166         const Vector3 &fv = get_face_direction(face);
167         const Vector3 &sv = get_s_direction(face);
168         const Vector3 &tv = get_t_direction(face);
169         return fv+s*sv+t*tv;
170 }
171
172 UInt64 TextureCube::get_data_size() const
173 {
174         return id ? size*size*6*get_pixel_size(ifmt) : 0;
175 }
176
177
178 TextureCube::Loader::Loader(TextureCube &t):
179         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
180 {
181         init();
182 }
183
184 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
185         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
186 {
187         init();
188 }
189
190 void TextureCube::Loader::init()
191 {
192         add("image_data", &Loader::image_data);
193         add("raw_data", &Loader::raw_data);
194         add("storage", &Loader::storage);
195 }
196
197 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
198 {
199         Graphics::Image img;
200         IO::Memory mem(data.data(), data.size());
201         img.load_io(mem);
202
203         obj.image(face, img, srgb);
204 }
205
206 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
207 {
208         obj.image(face, 0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
209 }
210
211 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
212 {
213         if(srgb)
214                 fmt = get_srgb_pixelformat(fmt);
215         obj.storage(fmt, s);
216 }
217
218
219 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
220 {
221         const string &str = conv.get();
222         if(str=="POSITIVE_X")
223                 face = POSITIVE_X;
224         else if(str=="NEGATIVE_X")
225                 face = NEGATIVE_X;
226         else if(str=="POSITIVE_Y")
227                 face = POSITIVE_Y;
228         else if(str=="NEGATIVE_Y")
229                 face = NEGATIVE_Y;
230         else if(str=="POSITIVE_Z")
231                 face = POSITIVE_Z;
232         else if(str=="NEGATIVE_Z")
233                 face = NEGATIVE_Z;
234         else
235                 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));
236 }
237
238 } // namespace GL
239 } // namespace Msp