2 #include <msp/graphics/image.h>
5 #include "ext_texture3d.h"
13 Texture3D::Texture3D():
14 Texture(GL_TEXTURE_3D),
20 static Require _req(EXT_texture3D);
23 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
26 throw invalid_operation("Texture3D::storage");
27 if(wd==0 || ht==0 || dp==0)
28 throw invalid_argument("Texture3D::storage");
36 void Texture3D::allocate(unsigned level)
38 if(allocated&(1<<level))
41 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
44 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
46 if(width==0 || height==0 || depth==0)
47 throw invalid_operation("Texture3D::image");
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)
67 if(width==0 || height==0 || depth==0)
68 throw invalid_operation("Texture3D::image");
72 Bind _bind(this, true);
73 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
76 void Texture3D::load_image(const string &fn, int dp)
81 unsigned w = img.get_width();
82 unsigned h = img.get_height();
88 throw incompatible_data("Texture3D::load_image");
94 for(d=h; d*d>h; d>>=2) ;
97 throw incompatible_data("Texture3D::load_image");
104 throw incompatible_data("Texture3D::load_image");
108 throw invalid_argument("Texture3D::load_image");
110 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
112 storage(fmt, w, h, d);
113 else if(w!=width || h!=height || d!=depth)
114 throw incompatible_data("Texture3D::load_image");
116 image(0, fmt, UNSIGNED_INT, img.get_data());
119 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)