]> git.tdb.fi Git - libs/gl.git/blob - source/texture3d.cpp
Do not store generated files in the repository
[libs/gl.git] / source / texture3d.cpp
1 #include <cmath>
2 #include <msp/gl/extensions/ext_texture3d.h>
3 #include <msp/graphics/image.h>
4 #include "bindable.h"
5 #include "error.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         require_pixelformat(fmt);
30
31         width = wd;
32         height = ht;
33         depth = dp;
34         ifmt = fmt;
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         {
103                 d = dp;
104                 if(h%d)
105                         throw incompatible_data("Texture3D::load_image");
106                 h /= d;
107         }
108         else
109                 throw invalid_argument("Texture3D::load_image");
110
111         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
112         if(width==0)
113                 storage(fmt, w, h, d);
114         else if(w!=width || h!=height || d!=depth)
115                 throw incompatible_data("Texture3D::load_image");
116
117         image(0, fmt, UNSIGNED_INT, img.get_data());
118 }
119
120 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
121 {
122         w >>= level;
123         h >>= level;
124         d >>= level;
125
126         if(!w && (h || d))
127                 w = 1;
128         if(!h && (w || d))
129                 h = 1;
130         if(!d && (w || h))
131                 d = 1;
132 }
133
134 } // namespace GL
135 } // namespace Msp