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