2 #include <msp/gbase/image.h>
7 #include "version_1_2.h"
14 Texture3D::Texture3D():
15 Texture(GL_TEXTURE_3D),
21 static RequireVersion _ver(1, 2);
24 void Texture3D::storage(PixelFormat f, unsigned w, unsigned h, unsigned d)
27 throw InvalidState("Textures storage may only be specified once");
28 if(w==0 || h==0 || d==0)
29 throw InvalidParameterValue("Invalid texture dimensions");
37 void Texture3D::allocate(unsigned level)
39 if(allocated&(1<<level))
42 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
45 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
52 get_level_size(level, w, h, d);
54 Bind _bind(this, true);
55 glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
57 allocated |= 1<<level;
58 if(gen_mipmap && level==0)
60 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
61 allocated |= (1<<level)-1;
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)
70 Bind _bind(this, true);
71 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
74 void Texture3D::load_image(const string &fn, int dp)
79 unsigned w = img.get_width();
80 unsigned h = img.get_height();
86 throw IncompatibleData("Image height is not divisible by its width");
92 for(d=h; d*d>h; d>>=2) ;
95 throw IncompatibleData("Could not find a square root of texture height");
101 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
103 storage(fmt, w, h, d);
104 else if(w!=width || h!=height || d!=depth)
105 throw IncompatibleData("Image does not match texture storage");
107 image(0, fmt, UNSIGNED_INT, img.get_data());
110 void Texture3D::require_storage()
112 if(width==0 || height==0 || depth==0)
113 throw InvalidState("Texture storage has not been specified");
116 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)