]> git.tdb.fi Git - libs/gl.git/blob - source/texture3d.cpp
Implement loading functionality for texture classes that were missing it
[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 "pixelstore.h"
7 #include "texture3d.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 Texture3D::Texture3D():
15         Texture(GL_TEXTURE_3D),
16         ifmt(RGB),
17         width(0),
18         height(0),
19         depth(0),
20         allocated(0)
21 {
22         static Require _req(EXT_texture3D);
23 }
24
25 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
26 {
27         if(width>0)
28                 throw invalid_operation("Texture3D::storage");
29         if(wd==0 || ht==0 || dp==0)
30                 throw invalid_argument("Texture3D::storage");
31         require_pixelformat(fmt);
32
33         width = wd;
34         height = ht;
35         depth = dp;
36         ifmt = fmt;
37 }
38
39 void Texture3D::allocate(unsigned level)
40 {
41         if(allocated&(1<<level))
42                 return;
43
44         PixelFormat base_fmt = get_base_pixelformat(ifmt);
45         image(level, base_fmt, get_alloc_type(base_fmt), 0);
46 }
47
48 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
49 {
50         if(width==0 || height==0 || depth==0)
51                 throw invalid_operation("Texture3D::image");
52
53         unsigned w = width;
54         unsigned h = height;
55         unsigned d = depth;
56         get_level_size(level, w, h, d);
57
58         BindRestore _bind(this);
59         glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
60
61         allocated |= 1<<level;
62         if(gen_mipmap && level==0)
63         {
64                 auto_generate_mipmap();
65                 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
66                 allocated |= (1<<level)-1;
67         }
68 }
69
70 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)
71 {
72         if(width==0 || height==0 || depth==0)
73                 throw invalid_operation("Texture3D::image");
74
75         allocate(level);
76
77         BindRestore _bind(this);
78         glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
79 }
80
81 void Texture3D::load_image(const string &fn, int dp)
82 {
83         Graphics::Image img;
84         img.load_file(fn);
85
86         unsigned w = img.get_width();
87         unsigned h = img.get_height();
88         unsigned d = 1;
89
90         if(dp==-1)
91         {
92                 if(h%w)
93                         throw incompatible_data("Texture3D::load_image");
94                 d = h/w;
95                 h = w;
96         }
97         else if(dp==-2)
98         {
99                 for(d=h; d*d>h; d>>=2) ;
100                 for(; d*d<h; ++d) ;
101                 if(d*d!=h)
102                         throw incompatible_data("Texture3D::load_image");
103                 h = d;
104         }
105         else if(dp>0)
106         {
107                 d = dp;
108                 if(h%d)
109                         throw incompatible_data("Texture3D::load_image");
110                 h /= d;
111         }
112         else
113                 throw invalid_argument("Texture3D::load_image");
114
115         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
116         if(width==0)
117                 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
118         else if(w!=width || h!=height || d!=depth)
119                 throw incompatible_data("Texture3D::load_image");
120
121         PixelStore pstore = PixelStore::from_image(img);
122         BindRestore _bind_ps(pstore);
123
124         image(0, fmt, UNSIGNED_BYTE, img.get_data());
125 }
126
127 void Texture3D::image(const Graphics::Image &img, bool srgb)
128 {
129         unsigned w = img.get_width();
130         unsigned h = img.get_height();
131         unsigned d = 1;
132
133         if(depth)
134         {
135                 if(h%depth)
136                         throw incompatible_data("Texture3D::load_image");
137                 h /= depth;
138                 d = depth;
139         }
140         else
141         {
142                 if(h%w)
143                         throw incompatible_data("Texture3D::load_image");
144                 d = h/w;
145                 h = w;
146         }
147
148         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
149         if(width==0)
150                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
151         else if(w!=width || h!=height || d!=depth)
152                 throw incompatible_data("Texture3D::load_image");
153
154         PixelStore pstore = PixelStore::from_image(img);
155         BindRestore _bind_ps(pstore);
156
157         image(0, fmt, UNSIGNED_BYTE, img.get_data());
158 }
159
160 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
161 {
162         w >>= level;
163         h >>= level;
164         d >>= level;
165
166         if(!w && (h || d))
167                 w = 1;
168         if(!h && (w || d))
169                 h = 1;
170         if(!d && (w || h))
171                 d = 1;
172 }
173
174 UInt64 Texture3D::get_data_size() const
175 {
176         return id ? width*height*depth*get_pixel_size(ifmt) : 0;
177 }
178
179
180 Texture3D::Loader::Loader(Texture3D &t):
181         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
182 {
183         init();
184 }
185
186 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
187         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
188 {
189         init();
190 }
191
192 void Texture3D::Loader::init()
193 {
194         add("raw_data", &Loader::raw_data);
195         add("storage", &Loader::storage);
196 }
197
198 void Texture3D::Loader::raw_data(const string &data)
199 {
200         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
201 }
202
203 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
204 {
205         if(srgb)
206                 fmt = get_srgb_pixelformat(fmt);
207         obj.storage(fmt, w, h, d);
208 }
209
210 } // namespace GL
211 } // namespace Msp