]> git.tdb.fi Git - libs/gl.git/blob - source/texturecube.cpp
Add support for cube map textures
[libs/gl.git] / source / texturecube.cpp
1 #include "bindable.h"
2 #include "error.h"
3 #include "extension.h"
4 #include "texturecube.h"
5
6 using namespace std;
7
8 namespace {
9
10 // An array to facilitate looping through the cube faces
11 Msp::GL::TextureCubeFace faces[6] =
12 {
13         Msp::GL::POSITIVE_X,
14         Msp::GL::NEGATIVE_X,
15         Msp::GL::POSITIVE_Y,
16         Msp::GL::NEGATIVE_Y,
17         Msp::GL::POSITIVE_Z,
18         Msp::GL::NEGATIVE_Z
19 };
20
21 }
22
23
24 namespace Msp {
25 namespace GL {
26
27 TextureCube::TextureCube():
28         Texture(GL_TEXTURE_CUBE_MAP),
29         size(0),
30         allocated(0)
31 {
32         static RequireVersion _ver(1, 3);
33 }
34
35 void TextureCube::storage(PixelFormat fmt, unsigned sz)
36 {
37         if(size>0)
38                 throw invalid_operation("TextureCube::storage");
39         if(sz==0)
40                 throw invalid_argument("TextureCube::storage");
41
42         ifmt = fmt;
43         size = sz;
44 }
45
46 void TextureCube::allocate(unsigned level)
47 {
48         if(allocated&(1<<level))
49                 return;
50
51         for(unsigned i=0; i<6; ++i)
52                 image(faces[i], level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
53 }
54
55 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
56 {
57         if(size==0)
58                 throw invalid_operation("TextureCube::image");
59
60         unsigned s = get_level_size(level);
61         if(s==0)
62                 throw invalid_argument("TextureCube::image");
63
64         Bind _bind(this, true);
65         glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
66
67         // XXX Allocation should be tracked per-face, but we'll run out of bits
68         allocated |= 1<<level;
69         if(gen_mipmap && level==0)
70         {
71                 for(; s; s>>=1, ++level) ;
72                 allocated |= (1<<level)-1;
73         }
74 }
75
76 unsigned TextureCube::get_level_size(unsigned level)
77 {
78         return size>>level;
79 }
80
81 } // namespace GL
82 } // namespace Msp