]> git.tdb.fi Git - libs/gl.git/blob - source/texture1d.cpp
Add some utility functions for textures
[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
27         if(MSP_sized_internal_formats)
28                 fmt = get_sized_pixelformat(fmt);
29         require_pixelformat(fmt);
30
31         ifmt = fmt;
32         width = wd;
33 }
34
35 void Texture1D::allocate(unsigned level)
36 {
37         if(allocated&(1<<level))
38                 return;
39
40         PixelFormat base_fmt = get_base_pixelformat(ifmt);
41         image(level, base_fmt, get_alloc_type(base_fmt), 0);
42 }
43
44 void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
45 {
46         if(width==0)
47                 throw invalid_operation("Texture1D::image");
48
49         unsigned w = get_level_size(level);
50
51         BindRestore _bind(this);
52         glTexImage1D(target, level, ifmt, w, 0, fmt, type, data);
53
54         allocated |= 1<<level;
55         if(gen_mipmap && level==0)
56         {
57                 auto_generate_mipmap();
58                 allocated |= (1<<get_n_levels())-1;
59         }
60 }
61
62 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelFormat fmt, DataType type, const void *data)
63 {
64         if(width==0)
65                 throw invalid_operation("Texture3D::image");
66
67         allocate(level);
68
69         BindRestore _bind(this);
70         glTexSubImage1D(target, level, x, wd, fmt, type, data);
71 }
72
73 void Texture1D::image(const Graphics::Image &img, bool srgb)
74 {
75         if(img.get_height()!=1)
76                 throw incompatible_data("Texture1D::image");
77
78         unsigned w = img.get_width();
79         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
80         if(width==0)
81                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
82         else if(w!=width)
83                 throw incompatible_data("Texture1D::image");
84
85         image(0, fmt, UNSIGNED_BYTE, img.get_data());
86 }
87
88 unsigned Texture1D::get_n_levels() const
89 {
90         unsigned n = 0;
91         for(unsigned s=width; s; s>>=1, ++n) ;
92         return n;
93 }
94
95 unsigned Texture1D::get_level_size(unsigned level) const
96 {
97         return width>>level;
98 }
99
100 UInt64 Texture1D::get_data_size() const
101 {
102         return id ? width*get_pixel_size(ifmt) : 0;
103 }
104
105
106 Texture1D::Loader::Loader(Texture1D &t):
107         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
108 {
109         init();
110 }
111
112 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
113         DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
114 {
115         init();
116 }
117
118 void Texture1D::Loader::init()
119 {
120         add("raw_data", &Loader::raw_data);
121         add("storage", &Loader::storage);
122 }
123
124 void Texture1D::Loader::raw_data(const string &data)
125 {
126         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
127 }
128
129 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
130 {
131         if(srgb)
132                 fmt = get_srgb_pixelformat(fmt);
133         obj.storage(fmt, w);
134 }
135
136 } // namespace GL
137 } // namespace Msp