]> git.tdb.fi Git - libs/gl.git/blob - source/texturecube.cpp
Make cube map textures loadable from datafiles
[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 "texturecube.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 Vector3 TextureCube::directions[6] =
14 {
15         Vector3(1, 0, 0),
16         Vector3(-1, 0, 0),
17         Vector3(0, 1, 0),
18         Vector3(0, -1, 0),
19         Vector3(0, 0, 1),
20         Vector3(0, 0, -1)
21 };
22
23 TextureCube::TextureCube():
24         Texture(GL_TEXTURE_CUBE_MAP),
25         size(0),
26         allocated(0)
27 {
28         static Require _req(ARB_texture_cube_map);
29 }
30
31 void TextureCube::storage(PixelFormat fmt, unsigned sz)
32 {
33         if(size>0)
34                 throw invalid_operation("TextureCube::storage");
35         if(sz==0)
36                 throw invalid_argument("TextureCube::storage");
37         require_pixelformat(fmt);
38
39         ifmt = fmt;
40         size = sz;
41 }
42
43 void TextureCube::allocate(unsigned level)
44 {
45         if(allocated&(1<<level))
46                 return;
47
48         for(unsigned i=0; i<6; ++i)
49                 image(enumerate_faces(i), level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
50 }
51
52 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
53 {
54         if(size==0)
55                 throw invalid_operation("TextureCube::image");
56
57         unsigned s = get_level_size(level);
58         if(s==0)
59                 throw out_of_range("TextureCube::image");
60
61         Bind _bind(this, true);
62         glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
63
64         // XXX Allocation should be tracked per-face, but we'll run out of bits
65         allocated |= 1<<level;
66         if(gen_mipmap && level==0)
67         {
68                 for(; s; s>>=1, ++level) ;
69                 allocated |= (1<<level)-1;
70         }
71 }
72
73 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
74 {
75         unsigned w = img.get_width();
76         unsigned h = img.get_height();
77         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
78         if(size==0)
79         {
80                 if(w==h)
81                         storage(fmt, w);
82                 else
83                         throw incompatible_data("TextureCube::image");
84         }
85         else if(w!=size || h!=size)
86                 throw incompatible_data("TextureCube::image");
87
88         image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
89 }
90
91 unsigned TextureCube::get_level_size(unsigned level)
92 {
93         return size>>level;
94 }
95
96 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
97 {
98         switch(i)
99         {
100         case 0: return POSITIVE_X;
101         case 1: return NEGATIVE_X;
102         case 2: return POSITIVE_Y;
103         case 3: return NEGATIVE_Y;
104         case 4: return POSITIVE_Z;
105         case 5: return NEGATIVE_Z;
106         default: throw out_of_range("TextureCube::enumerate_faces");
107         }
108 }
109
110 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
111 {
112         switch(face)
113         {
114         case POSITIVE_X: return directions[0];
115         case NEGATIVE_X: return directions[1];
116         case POSITIVE_Y: return directions[2];
117         case NEGATIVE_Y: return directions[3];
118         case POSITIVE_Z: return directions[4];
119         case NEGATIVE_Z: return directions[5];
120         default: throw invalid_argument("TextureCube::get_face_direction");
121         }
122 }
123
124 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
125 {
126         switch(face)
127         {
128         case POSITIVE_X: return directions[5];
129         case NEGATIVE_X: return directions[4];
130         case POSITIVE_Y: return directions[0];
131         case NEGATIVE_Y: return directions[0];
132         case POSITIVE_Z: return directions[0];
133         case NEGATIVE_Z: return directions[1];
134         default: throw invalid_argument("TextureCube::get_s_direction");
135         }
136 }
137
138 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
139 {
140         switch(face)
141         {
142         case POSITIVE_X: return directions[3];
143         case NEGATIVE_X: return directions[3];
144         case POSITIVE_Y: return directions[4];
145         case NEGATIVE_Y: return directions[5];
146         case POSITIVE_Z: return directions[3];
147         case NEGATIVE_Z: return directions[3];
148         default: throw invalid_argument("TextureCube::get_t_direction");
149         }
150 }
151
152 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
153 {
154         float s = (u+0.5f)*2.0f/size-1.0f;
155         float t = (v+0.5f)*2.0f/size-1.0f;
156         const Vector3 &fv = get_face_direction(face);
157         const Vector3 &sv = get_s_direction(face);
158         const Vector3 &tv = get_t_direction(face);
159         return fv+s*sv+t*tv;
160 }
161
162
163 TextureCube::Loader::Loader(TextureCube &t):
164         DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
165 {
166         add("image_data", &Loader::image_data);
167         add("raw_data", &Loader::raw_data);
168         add("storage", &Loader::storage);
169 }
170
171 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
172 {
173         Graphics::Image img;
174         IO::Memory mem(data.data(), data.size());
175         img.load_io(mem);
176
177         obj.image(face, img);
178 }
179
180 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
181 {
182         obj.image(face, 0, obj.ifmt, UNSIGNED_BYTE, data.data());
183 }
184
185 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
186 {
187         obj.storage(fmt, s);
188 }
189
190
191 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
192 {
193         const string &str = conv.get();
194         if(str=="POSITIVE_X")
195                 face = POSITIVE_X;
196         else if(str=="NEGATIVE_X")
197                 face = NEGATIVE_X;
198         else if(str=="POSITIVE_Y")
199                 face = POSITIVE_Y;
200         else if(str=="NEGATIVE_Y")
201                 face = NEGATIVE_Y;
202         else if(str=="POSITIVE_Z")
203                 face = POSITIVE_Z;
204         else if(str=="NEGATIVE_Z")
205                 face = NEGATIVE_Z;
206         else
207                 throw lexical_error(format("conversion of '%s' to TextureCubeFace", str));
208 }
209
210 } // namespace GL
211 } // namespace Msp