]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture3d.cpp
Use default member initializers for simple types
[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!=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         levels = get_n_levels();
30         if(lv>0)
31                 levels = min(levels, lv);
32
33         allocate();
34 }
35
36 void Texture3D::image(unsigned level, const void *data)
37 {
38         LinAl::Vector<unsigned, 3> size = get_level_size(level);
39         return sub_image(level, 0, 0, 0, size.x, size.y, size.z, data);
40 }
41
42 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
43 {
44         if(width==0 || height==0 || depth==0)
45                 throw invalid_operation("Texture3D::sub_image");
46         if(level>=levels)
47                 throw out_of_range("Texture3D::sub_image");
48
49         Texture3DBackend::sub_image(level, x, y, z, wd, ht, dp, data);
50 }
51
52 void Texture3D::image(const Graphics::Image &img, unsigned lv)
53 {
54         unsigned w = img.get_width();
55         unsigned h = img.get_height();
56
57         if(h%w)
58                 throw incompatible_data("Texture3D::load_image");
59         unsigned d = h/w;
60         h = w;
61
62         storage(pixelformat_from_image(img, use_srgb_format), w, h, d, lv);
63         image(0, img.get_pixels());
64 }
65
66 unsigned Texture3D::get_n_levels() const
67 {
68         unsigned s = max(width, height);
69         if(!is_array())
70                 s = max(s, depth);
71         unsigned n = 0;
72         for(; s; s>>=1, ++n) ;
73         return n;
74 }
75
76 LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
77 {
78         unsigned w = width>>level;
79         unsigned h = height>>level;
80         unsigned d = depth;
81         if(!is_array())
82                 d >>= level;
83
84         if(!w && (h || d))
85                 w = 1;
86         if(!h && (w || d))
87                 h = 1;
88         if(!d && (w || h))
89                 d = 1;
90
91         return LinAl::Vector<unsigned, 3>(w, h, d);
92 }
93
94 uint64_t Texture3D::get_data_size() const
95 {
96         return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
97 }
98
99
100 Texture3D::Loader::Loader(Texture3D &t):
101         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
102 {
103         init();
104 }
105
106 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
107         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
108 {
109         init();
110 }
111
112 void Texture3D::Loader::init()
113 {
114         add("raw_data", &Loader::raw_data);
115         add("storage", &Loader::storage);
116         add("storage", &Loader::storage_levels);
117 }
118
119 void Texture3D::Loader::raw_data(const string &data)
120 {
121         obj.image(0, data.data());
122 }
123
124 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
125 {
126         obj.storage(fmt, w, h, d);
127 }
128
129 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
130 {
131         obj.storage(fmt, w, h, d, l);
132 }
133
134 } // namespace GL
135 } // namespace Msp