]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Rework exceptions
[libs/gl.git] / source / texture2d.cpp
1 #include "bindable.h"
2 #include "error.h"
3 #include "texture2d.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Texture2D::Texture2D():
11         Texture(GL_TEXTURE_2D),
12         width(0),
13         height(0),
14         allocated(0)
15 { }
16
17 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
18 {
19         if(width>0)
20                 throw invalid_operation("Texture2D::storage");
21         if(wd==0 || ht==0)
22                 throw invalid_argument("Texture2D::storage");
23
24         ifmt = fmt;
25         width = wd;
26         height = ht;
27 }
28
29 void Texture2D::allocate(unsigned level)
30 {
31         if(allocated&(1<<level))
32                 return;
33
34         image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
35 }
36
37 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
38 {
39         if(width==0 || height==0)
40                 throw invalid_operation("Texture2D::image");
41
42         unsigned w = width;
43         unsigned h = height;
44         get_level_size(level, w, h);
45
46         Bind _bind(this, true);
47         glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
48
49         allocated |= 1<<level;
50         if(gen_mipmap && level==0)
51         {
52                 for(; (w || h); w>>=1, h>>=1, ++level) ;
53                 allocated |= (1<<level)-1;
54         }
55 }
56
57 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
58 {
59         if(width==0 || height==0)
60                 throw invalid_operation("Texture2D::sub_image");
61
62         allocate(level);
63
64         Bind _bind(this, true);
65         glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
66 }
67
68 void Texture2D::load_image(const string &fn)
69 {
70         Graphics::Image img;
71         img.load_file(fn);
72
73         image(img);
74 }
75
76 void Texture2D::image(const Graphics::Image &img)
77 {
78         unsigned w = img.get_width();
79         unsigned h = img.get_height();
80         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
81         if(width==0)
82                 storage(fmt, w, h);
83         else if(w!=width || h!=height)
84                 throw incompatible_data("Texture2D::image");
85
86         image(0, fmt, UNSIGNED_BYTE, img.get_data());
87 }
88
89 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
90 {
91         w >>= level;
92         h >>= level;
93
94         if(!w && h)
95                 w = 1;
96         else if(!h && w)
97                 h = 1;
98 }
99
100
101 Texture2D::Loader::Loader(Texture2D &t):
102         Texture::Loader(t)
103 {
104         add("image_data", &Loader::image_data);
105         add("raw_data", &Loader::raw_data);
106         add("storage", &Loader::storage);
107         add("storage", &Loader::storage_b);
108 }
109
110 void Texture2D::Loader::image_data(const string &data)
111 {
112         Graphics::Image img;
113         img.load_memory(data.data(), data.size());
114
115         static_cast<Texture2D &>(obj).image(img);
116 }
117
118 void Texture2D::Loader::raw_data(const string &data)
119 {
120         Texture2D &t2d = static_cast<Texture2D &>(obj);
121         t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data());
122 }
123
124 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
125 {
126         static_cast<Texture2D &>(obj).storage(fmt, w, h);
127 }
128
129 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)
130 {
131         storage(fmt, w, h);
132 }
133
134 } // namespace GL
135 } // namespace Msp