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