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