]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Drop Id tags and copyright notices from files
[libs/gl.git] / source / texture2d.cpp
1 #include "bindable.h"
2 #include "except.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 InvalidState("Texture storage may only be specified once");
21         if(wd==0 || ht==0)
22                 throw InvalidParameterValue("Invalid texture dimensions");
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         require_storage();
40
41         unsigned w = width;
42         unsigned h = height;
43         get_level_size(level, w, h);
44
45         Bind _bind(this, true);
46         glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
47
48         allocated |= 1<<level;
49         if(gen_mipmap && level==0)
50         {
51                 for(; (w || h); w>>=1, h>>=1, ++level) ;
52                 allocated |= (1<<level)-1;
53         }
54 }
55
56 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
57 {
58         require_storage();
59         allocate(level);
60
61         Bind _bind(this, true);
62         glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
63 }
64
65 void Texture2D::load_image(const string &fn)
66 {
67         Graphics::Image img;
68         img.load_file(fn);
69
70         image(img);
71 }
72
73 void Texture2D::image(const Graphics::Image &img)
74 {
75         unsigned w = img.get_width();
76         unsigned h = img.get_height();
77         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
78         if(width==0)
79                 storage(fmt, w, h);
80         else if(w!=width || h!=height)
81                 throw IncompatibleData("Image does not match texture storage");
82
83         image(0, fmt, UNSIGNED_BYTE, img.get_data());
84 }
85
86 void Texture2D::require_storage()
87 {
88         if(width==0 || height==0)
89                 throw InvalidState("Texture storage has not been specified");
90 }
91
92 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
93 {
94         w >>= level;
95         h >>= level;
96
97         if(!w && h)
98                 w = 1;
99         else if(!h && w)
100                 h = 1;
101 }
102
103
104 Texture2D::Loader::Loader(Texture2D &t):
105         Texture::Loader(t)
106 {
107         add("image_data", &Loader::image_data);
108         add("raw_data", &Loader::raw_data);
109         add("storage", &Loader::storage);
110         add("storage", &Loader::storage_b);
111 }
112
113 void Texture2D::Loader::image_data(const string &data)
114 {
115         Graphics::Image img;
116         img.load_memory(data.data(), data.size());
117
118         static_cast<Texture2D &>(obj).image(img);
119 }
120
121 void Texture2D::Loader::raw_data(const string &data)
122 {
123         Texture2D &t2d = static_cast<Texture2D &>(obj);
124         t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data());
125 }
126
127 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
128 {
129         static_cast<Texture2D &>(obj).storage(fmt, w, h);
130 }
131
132 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)
133 {
134         storage(fmt, w, h);
135 }
136
137 } // namespace GL
138 } // namespace Msp