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