]> git.tdb.fi Git - libs/gl.git/blob - source/core/texturecube.cpp
ea29499732646897dd9e81c7080c8cc3bc9ff013
[libs/gl.git] / source / core / texturecube.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/io/memory.h>
3 #include <msp/strings/format.h>
4 #include "error.h"
5 #include "texturecube.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 const Vector3 TextureCube::directions[6] =
13 {
14         Vector3(1, 0, 0),
15         Vector3(-1, 0, 0),
16         Vector3(0, 1, 0),
17         Vector3(0, -1, 0),
18         Vector3(0, 0, 1),
19         Vector3(0, 0, -1)
20 };
21
22 const unsigned TextureCube::orientations[12] =
23 {
24         5, 3,
25         4, 3,
26         0, 4,
27         0, 5,
28         0, 3,
29         1, 3
30 };
31
32 TextureCube::TextureCube():
33         size(0)
34 { }
35
36 void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
37 {
38         if(size>0)
39         {
40                 if(fmt!=format || sz!=size || (lv && lv!=levels))
41                         throw incompatible_data("TextureCube::storage");
42                 return;
43         }
44         if(sz==0)
45                 throw invalid_argument("TextureCube::storage");
46
47         set_format(fmt);
48         size = sz;
49         levels = get_n_levels();
50         if(lv>0)
51                 levels = min(levels, lv);
52
53         allocate();
54 }
55
56 void TextureCube::image(TextureCubeFace face, unsigned level, const void *data)
57 {
58         unsigned lsz = get_level_size(level);
59         return sub_image(face, level, 0, 0, lsz, lsz, data);
60 }
61
62 void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
63 {
64         if(size==0)
65                 throw invalid_operation("TextureCube::sub_image");
66         if(level>=levels)
67                 throw out_of_range("TextureCube::sub_image");
68
69         TextureCubeBackend::sub_image(face, level, x, y, wd, ht, data);
70 }
71
72 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
73 {
74         unsigned w = img.get_width();
75         unsigned h = img.get_height();
76         if(w!=h)
77                 throw incompatible_data("TextureCube::image");
78
79         storage(pixelformat_from_image(img, use_srgb_format), w);
80         image(face, 0, img.get_pixels());
81 }
82
83 void TextureCube::image(const Graphics::Image &img, unsigned lv)
84 {
85         unsigned w = img.get_width();
86         unsigned h = img.get_height();
87
88         if(h!=w*6)
89                 throw incompatible_data("TextureCube::image");
90         h /= 6;
91
92         if(size==0)
93                 storage(pixelformat_from_image(img, use_srgb_format), w, lv);
94         else if(w!=size || h!=size)
95                 throw incompatible_data("TextureCube::image");
96
97         const char *pixels = reinterpret_cast<const char *>(img.get_pixels());
98         unsigned face_size = img.get_stride()*size;
99         for(unsigned i=0; i<6; ++i)
100                 image(static_cast<TextureCubeFace>(i), 0, pixels+i*face_size);
101 }
102
103 unsigned TextureCube::get_n_levels() const
104 {
105         unsigned n = 0;
106         for(unsigned s=size; s; s>>=1, ++n) ;
107         return n;
108 }
109
110 unsigned TextureCube::get_level_size(unsigned level) const
111 {
112         return size>>level;
113 }
114
115 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
116 {
117         return directions[face];
118 }
119
120 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
121 {
122         return directions[orientations[face*2]];
123 }
124
125 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
126 {
127         return directions[orientations[face*2+1]];
128 }
129
130 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
131 {
132         float s = (u+0.5f)*2.0f/size-1.0f;
133         float t = (v+0.5f)*2.0f/size-1.0f;
134         const Vector3 &fv = get_face_direction(face);
135         const Vector3 &sv = get_s_direction(face);
136         const Vector3 &tv = get_t_direction(face);
137         return fv+s*sv+t*tv;
138 }
139
140 uint64_t TextureCube::get_data_size() const
141 {
142         return id ? size*size*6*get_pixel_size(storage_fmt) : 0;
143 }
144
145
146 TextureCube::Loader::Loader(TextureCube &t):
147         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
148 {
149         init();
150 }
151
152 TextureCube::Loader::Loader(TextureCube &t, Collection &c):
153         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
154 {
155         init();
156 }
157
158 void TextureCube::Loader::init()
159 {
160         add("external_image", &Loader::external_image);
161         add("image_data", &Loader::image_data);
162         add("raw_data", &Loader::raw_data);
163         add("storage", &Loader::storage);
164         add("storage", &Loader::storage_levels);
165 }
166
167 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
168 {
169         Graphics::Image img;
170         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
171         img.load_io(*io);
172
173         obj.image(face, img);
174 }
175
176 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
177 {
178         Graphics::Image img;
179         IO::Memory mem(data.data(), data.size());
180         img.load_io(mem);
181
182         obj.image(face, img);
183 }
184
185 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
186 {
187         obj.image(face, 0, data.data());
188 }
189
190 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
191 {
192         obj.storage(fmt, s);
193 }
194
195 void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
196 {
197         obj.storage(fmt, s, l);
198 }
199
200
201 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
202 {
203         const string &str = conv.get();
204         if(str=="POSITIVE_X")
205                 face = POSITIVE_X;
206         else if(str=="NEGATIVE_X")
207                 face = NEGATIVE_X;
208         else if(str=="POSITIVE_Y")
209                 face = POSITIVE_Y;
210         else if(str=="NEGATIVE_Y")
211                 face = NEGATIVE_Y;
212         else if(str=="POSITIVE_Z")
213                 face = POSITIVE_Z;
214         else if(str=="NEGATIVE_Z")
215                 face = NEGATIVE_Z;
216         else
217                 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));
218 }
219
220 } // namespace GL
221 } // namespace Msp