]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture1d.cpp
Move all OpenGL-specific code to a separate directory
[libs/gl.git] / source / core / texture1d.cpp
1 #include "error.h"
2 #include "texture1d.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GL {
8
9 Texture1D::Texture1D():
10         width(0)
11 { }
12
13 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
14 {
15         if(width>0)
16         {
17                 if(fmt!=format || wd!=width || (lv && lv!=levels))
18                         throw incompatible_data("Texture1D::storage");
19                 return;
20         }
21         if(wd==0)
22                 throw invalid_argument("Texture1D::storage");
23
24         set_format(fmt);
25         width = wd;
26         levels = get_n_levels();
27         if(lv)
28                 levels = min(levels, lv);
29
30         allocate();
31 }
32
33 void Texture1D::image(unsigned level, const void *data)
34 {
35         return sub_image(level, 0, get_level_size(level), data);
36 }
37
38 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
39 {
40         if(width==0)
41                 throw invalid_operation("Texture1D::sub_image");
42         if(level>=levels)
43                 throw out_of_range("Texture1D::sub_image");
44
45         Texture1DBackend::sub_image(level, x, wd, data);
46 }
47
48 void Texture1D::image(const Graphics::Image &img, unsigned lv)
49 {
50         if(img.get_height()!=1)
51                 throw incompatible_data("Texture1D::image");
52
53         storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), lv);
54         image(0, img.get_pixels());
55 }
56
57 unsigned Texture1D::get_n_levels() const
58 {
59         unsigned n = 0;
60         for(unsigned s=width; s; s>>=1, ++n) ;
61         return n;
62 }
63
64 unsigned Texture1D::get_level_size(unsigned level) const
65 {
66         return width>>level;
67 }
68
69 uint64_t Texture1D::get_data_size() const
70 {
71         return id ? width*get_pixel_size(storage_fmt) : 0;
72 }
73
74
75 Texture1D::Loader::Loader(Texture1D &t):
76         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
77 {
78         init();
79 }
80
81 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
82         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
83 {
84         init();
85 }
86
87 void Texture1D::Loader::init()
88 {
89         add("raw_data", &Loader::raw_data);
90         add("storage", &Loader::storage);
91         add("storage", &Loader::storage_levels);
92 }
93
94 void Texture1D::Loader::raw_data(const string &data)
95 {
96         obj.image(0, data.data());
97 }
98
99 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
100 {
101         obj.storage(fmt, w);
102 }
103
104 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
105 {
106         obj.storage(fmt, w, l);
107 }
108
109 } // namespace GL
110 } // namespace Msp