]> git.tdb.fi Git - libs/gl.git/blob - source/texture3d.cpp
Rework exceptions
[libs/gl.git] / source / texture3d.cpp
1 #include <cmath>
2 #include <msp/graphics/image.h>
3 #include "bindable.h"
4 #include "error.h"
5 #include "extension.h"
6 #include "texture3d.h"
7 #include "version_1_2.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 Texture3D::Texture3D():
15         Texture(GL_TEXTURE_3D),
16         width(0),
17         height(0),
18         depth(0),
19         allocated(0)
20 {
21         static RequireVersion _ver(1, 2);
22 }
23
24 void Texture3D::storage(PixelFormat f, unsigned w, unsigned h, unsigned d)
25 {
26         if(width>0)
27                 throw invalid_operation("Texture3D::storage");
28         if(w==0 || h==0 || d==0)
29                 throw invalid_argument("Texture3D::storage");
30
31         width = w;
32         height = h;
33         depth = d;
34         ifmt = f;
35 }
36
37 void Texture3D::allocate(unsigned level)
38 {
39         if(allocated&(1<<level))
40                 return;
41
42         image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
43 }
44
45 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
46 {
47         if(width==0 || height==0 || depth==0)
48                 throw invalid_operation("Texture3D::image");
49
50         unsigned w = width;
51         unsigned h = height;
52         unsigned d = depth;
53         get_level_size(level, w, h, d);
54
55         Bind _bind(this, true);
56         glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
57
58         allocated |= 1<<level;
59         if(gen_mipmap && level==0)
60         {
61                 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
62                 allocated |= (1<<level)-1;
63         }
64 }
65
66 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, PixelFormat fmt, DataType type, const void *data)
67 {
68         if(width==0 || height==0 || depth==0)
69                 throw invalid_operation("Texture3D::image");
70
71         allocate(level);
72
73         Bind _bind(this, true);
74         glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
75 }
76
77 void Texture3D::load_image(const string &fn, int dp)
78 {
79         Graphics::Image img;
80         img.load_file(fn);
81
82         unsigned w = img.get_width();
83         unsigned h = img.get_height();
84         unsigned d = 1;
85
86         if(dp==-1)
87         {
88                 if(h%w)
89                         throw incompatible_data("Texture3D::load_image");
90                 d = h/w;
91                 h = w;
92         }
93         else if(dp==-2)
94         {
95                 for(d=h; d*d>h; d>>=2) ;
96                 for(; d*d<h; ++d) ;
97                 if(d*d!=h)
98                         throw incompatible_data("Texture3D::load_image");
99                 h = d;
100         }
101         else if(dp>0)
102                 d = dp;
103
104         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
105         if(width==0)
106                 storage(fmt, w, h, d);
107         else if(w!=width || h!=height || d!=depth)
108                 throw incompatible_data("Texture3D::load_image");
109
110         image(0, fmt, UNSIGNED_INT, img.get_data());
111 }
112
113 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
114 {
115         w >>= level;
116         h >>= level;
117         d >>= level;
118
119         if(!w && (h || d))
120                 w = 1;
121         if(!h && (w || d))
122                 h = 1;
123         if(!d && (w || h))
124                 d = 1;
125 }
126
127 } // namespace GL
128 } // namespace Msp