]> git.tdb.fi Git - libs/gl.git/blob - source/texture3d.cpp
Drop Id tags and copyright notices from files
[libs/gl.git] / source / texture3d.cpp
1 #include <cmath>
2 #include <msp/gbase/image.h>
3 #include "bindable.h"
4 #include "except.h"
5 #include "extension.h"
6 #include "texture3d.h"
7 #include "version_1_2.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 RequireVersion _ver(1, 2);
22 }
23
24 void Texture3D::storage(PixelFormat f, unsigned w, unsigned h, unsigned d)
25 {
26         if(width>0)
27                 throw InvalidState("Textures storage may only be specified once");
28         if(w==0 || h==0 || d==0)
29                 throw InvalidParameterValue("Invalid texture dimensions");
30
31         width = w;
32         height = h;
33         depth = d;
34         ifmt = f;
35 }
36
37 void Texture3D::allocate(unsigned level)
38 {
39         if(allocated&(1<<level))
40                 return;
41
42         image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
43 }
44
45 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
46 {
47         require_storage();
48
49         unsigned w = width;
50         unsigned h = height;
51         unsigned d = depth;
52         get_level_size(level, w, h, d);
53
54         Bind _bind(this, true);
55         glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
56
57         allocated |= 1<<level;
58         if(gen_mipmap && level==0)
59         {
60                 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
61                 allocated |= (1<<level)-1;
62         }
63 }
64
65 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)
66 {
67         require_storage();
68         allocate(level);
69
70         Bind _bind(this, true);
71         glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
72 }
73
74 void Texture3D::load_image(const string &fn, int dp)
75 {
76         Graphics::Image img;
77         img.load_file(fn);
78
79         unsigned w = img.get_width();
80         unsigned h = img.get_height();
81         unsigned d = 1;
82
83         if(dp==-1)
84         {
85                 if(h%w)
86                         throw IncompatibleData("Image height is not divisible by its width");
87                 d = h/w;
88                 h = w;
89         }
90         else if(dp==-2)
91         {
92                 for(d=h; d*d>h; d>>=2) ;
93                 for(; d*d<h; ++d) ;
94                 if(d*d!=h)
95                         throw IncompatibleData("Could not find a square root of texture height");
96                 h = d;
97         }
98         else if(dp>0)
99                 d = dp;
100
101         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
102         if(width==0)
103                 storage(fmt, w, h, d);
104         else if(w!=width || h!=height || d!=depth)
105                 throw IncompatibleData("Image does not match texture storage");
106
107         image(0, fmt, UNSIGNED_INT, img.get_data());
108 }
109
110 void Texture3D::require_storage()
111 {
112         if(width==0 || height==0 || depth==0)
113                 throw InvalidState("Texture storage has not been specified");
114 }
115
116 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
117 {
118         w >>= level;
119         h >>= level;
120         d >>= level;
121
122         if(!w && (h || d))
123                 w = 1;
124         if(!h && (w || d))
125                 h = 1;
126         if(!d && (w || h))
127                 d = 1;
128 }
129
130 } // namespace GL
131 } // namespace Msp