]> git.tdb.fi Git - libs/gl.git/blob - source/texture3d.cpp
Remove support for texture borders
[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         image(0, ifmt, UNSIGNED_BYTE, 0);
44 }
45
46 void Texture3D::allocate(unsigned level)
47 {
48         if(allocated&(1<<level))
49                 return;
50
51         image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
52 }
53
54 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
55 {
56         require_storage();
57
58         unsigned w = width;
59         unsigned h = height;
60         unsigned d = depth;
61         get_level_size(level, w, h, d);
62
63         Bind _bind(this, true);
64         glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
65
66         allocated |= 1<<level;
67         if(gen_mipmap && level==0)
68         {
69                 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
70                 allocated |= (1<<level)-1;
71         }
72 }
73
74 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)
75 {
76         require_storage();
77         allocate(level);
78
79         Bind _bind(this, true);
80         glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
81 }
82
83 void Texture3D::load_image(const string &fn, int dp)
84 {
85         Graphics::Image img;
86         img.load_file(fn);
87
88         unsigned w = img.get_width();
89         unsigned h = img.get_height();
90         unsigned d = 1;
91
92         if(dp==-1)
93         {
94                 if(h%w)
95                         throw IncompatibleData("Image height is not divisible by its width");
96                 d = h/w;
97                 h = w;
98         }
99         else if(dp==-2)
100         {
101                 for(d=h; d*d>h; d>>=2) ;
102                 for(; d*d<h; ++d) ;
103                 if(d*d!=h)
104                         throw IncompatibleData("Could not find a square root of texture height");
105                 h = d;
106         }
107         else if(dp>0)
108                 d = dp;
109
110         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
111         if(width==0)
112                 storage(fmt, w, h, d);
113         else if(w!=width || h!=height || d!=depth)
114                 throw IncompatibleData("Image does not match texture storage");
115
116         image(0, fmt, UNSIGNED_INT, img.get_data());
117 }
118
119 void Texture3D::require_storage()
120 {
121         if(width==0 || height==0 || depth==0)
122                 throw InvalidState("Texture storage has not been specified");
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 } // namespace GL
140 } // namespace Msp