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