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