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