]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture3d.cpp
Use mesh as the task object when exporting it
[libs/gl.git] / source / core / texture3d.cpp
1 #include <cmath>
2 #include "error.h"
3 #include "texture3d.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Texture3D::Texture3D(unsigned t):
11         Texture3DBackend(t)
12 { }
13
14 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv)
15 {
16         if(width>0)
17         {
18                 if(fmt!=format || wd!=width || ht!=height || dp!=depth || (lv && lv!=n_levels))
19                         throw incompatible_data("Texture3D::storage");
20                 return;
21         }
22         if(wd==0 || ht==0 || dp==0)
23                 throw invalid_argument("Texture3D::storage");
24
25         set_format(fmt);
26         width = wd;
27         height = ht;
28         depth = dp;
29         unsigned size = max(width, height);
30         if(!is_array())
31                 size = max(size, depth);
32         n_levels = count_levels(size);
33         if(lv>0)
34                 n_levels = min(n_levels, lv);
35
36         allocate();
37 }
38
39 void Texture3D::image(unsigned level, const void *data)
40 {
41         LinAl::Vector<unsigned, 3> size = get_level_size(level);
42         return sub_image(level, 0, 0, 0, size.x, size.y, size.z, data);
43 }
44
45 void Texture3D::sub_image(unsigned level, unsigned x, unsigned y, unsigned z, unsigned wd, unsigned ht, unsigned dp, const void *data)
46 {
47         if(width==0 || height==0 || depth==0)
48                 throw invalid_operation("Texture3D::sub_image");
49         if(level>=n_levels || x>width || x+wd>width || y>height || y+ht>height || z>depth || z+dp>depth)
50                 throw out_of_range("Texture3D::sub_image");
51
52         Texture3DBackend::sub_image(level, x, y, z, wd, ht, dp, data);
53 }
54
55 void Texture3D::image(const Graphics::Image &img, unsigned lv)
56 {
57         unsigned w = img.get_width();
58         unsigned h = img.get_height();
59
60         if(h%w)
61                 throw incompatible_data("Texture3D::load_image");
62         unsigned d = h/w;
63         h = w;
64
65         storage(pixelformat_from_image(img, use_srgb_format), w, h, d, lv);
66         image(0, img.get_pixels());
67 }
68
69 LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
70 {
71         unsigned w = width>>level;
72         unsigned h = height>>level;
73         unsigned d = depth;
74         if(!is_array())
75                 d >>= level;
76
77         if(!w && (h || d))
78                 w = 1;
79         if(!h && (w || d))
80                 h = 1;
81         if(!d && (w || h))
82                 d = 1;
83
84         return LinAl::Vector<unsigned, 3>(w, h, d);
85 }
86
87
88 Texture3D::Loader::Loader(Texture3D &t):
89         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
90 {
91         init();
92 }
93
94 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
95         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
96 {
97         init();
98 }
99
100 void Texture3D::Loader::init()
101 {
102         add("storage", &Loader::storage);
103         add("storage", &Loader::storage_levels);
104 }
105
106 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
107 {
108         obj.storage(fmt, w, h, d);
109 }
110
111 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
112 {
113         obj.storage(fmt, w, h, d, l);
114 }
115
116 } // namespace GL
117 } // namespace Msp