]> git.tdb.fi Git - libs/gl.git/blob - source/texture1d.cpp
Implement sub_image for Texture1D
[libs/gl.git] / source / texture1d.cpp
1 #include <msp/gl/extensions/msp_texture1d.h>
2 #include "bindable.h"
3 #include "error.h"
4 #include "texture1d.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 Texture1D::Texture1D():
12         Texture(GL_TEXTURE_1D),
13         ifmt(RGB),
14         width(0),
15         allocated(0)
16 {
17         static Require _req(MSP_texture1D);
18 }
19
20 void Texture1D::storage(PixelFormat fmt, unsigned wd)
21 {
22         if(width>0)
23                 throw invalid_operation("Texture1D::storage");
24         if(wd==0)
25                 throw invalid_argument("Texture1D::storage");
26         require_pixelformat(fmt);
27
28         ifmt = fmt;
29         width = wd;
30 }
31
32 void Texture1D::allocate(unsigned level)
33 {
34         if(allocated&(1<<level))
35                 return;
36
37         PixelFormat base_fmt = get_base_pixelformat(ifmt);
38         image(level, base_fmt, get_alloc_type(base_fmt), 0);
39 }
40
41 void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
42 {
43         if(width==0)
44                 throw invalid_operation("Texture1D::image");
45
46         unsigned w = get_level_size(level);
47
48         BindRestore _bind(this);
49         glTexImage1D(target, level, ifmt, w, 0, fmt, type, data);
50
51         allocated |= 1<<level;
52         if(gen_mipmap && level==0)
53         {
54                 auto_generate_mipmap();
55                 for(; w; w>>=1, ++level) ;
56                 allocated |= (1<<level)-1;
57         }
58 }
59
60 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelFormat fmt, DataType type, const void *data)
61 {
62         if(width==0)
63                 throw invalid_operation("Texture3D::image");
64
65         allocate(level);
66
67         BindRestore _bind(this);
68         glTexSubImage1D(target, level, x, wd, fmt, type, data);
69 }
70
71 void Texture1D::image(const Graphics::Image &img, bool srgb)
72 {
73         if(img.get_height()!=1)
74                 throw incompatible_data("Texture1D::image");
75
76         unsigned w = img.get_width();
77         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
78         if(width==0)
79                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
80         else if(w!=width)
81                 throw incompatible_data("Texture1D::image");
82
83         image(0, fmt, UNSIGNED_BYTE, img.get_data());
84 }
85
86 unsigned Texture1D::get_level_size(unsigned level)
87 {
88         return width>>level;
89 }
90
91 UInt64 Texture1D::get_data_size() const
92 {
93         return id ? width*get_pixel_size(ifmt) : 0;
94 }
95
96
97 Texture1D::Loader::Loader(Texture1D &t):
98         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
99 {
100         init();
101 }
102
103 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
104         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
105 {
106         init();
107 }
108
109 void Texture1D::Loader::init()
110 {
111         add("raw_data", &Loader::raw_data);
112         add("storage", &Loader::storage);
113 }
114
115 void Texture1D::Loader::raw_data(const string &data)
116 {
117         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
118 }
119
120 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
121 {
122         if(srgb)
123                 fmt = get_srgb_pixelformat(fmt);
124         obj.storage(fmt, w);
125 }
126
127 } // namespace GL
128 } // namespace Msp