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